Skip to content

Commit

Permalink
Merge branch 'beta' into issue-3703
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiSherman authored Jan 8, 2025
2 parents fc2c675 + 629b5aa commit 4a4629c
Show file tree
Hide file tree
Showing 30 changed files with 1,798 additions and 132 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ dist-dts
rollup.config-*.mjs
*.log
.DS_Store
drizzle-seed/src/test.ts
drizzle-seed/src/testMysql.ts
drizzle-seed/src/testSqlite.ts
drizzle-seed/src/schemaTest.ts
drizzle-seed/src/dev
1 change: 1 addition & 0 deletions changelogs/drizzle-kit/0.30.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix certificates generation utility for Drizzle Studio; [[BUG]: [drizzle-kit]: drizzle-kit dependency on drizzle-studio perms error](https://github.com/drizzle-team/drizzle-orm/issues/3729)
2 changes: 1 addition & 1 deletion changelogs/drizzle-orm/0.29.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ await migrate(db, {
});
```

### 🎉 SQLite Proxy bacth and Relational Queries support
### 🎉 SQLite Proxy batch and Relational Queries support

- You can now use `.query.findFirst` and `.query.findMany` syntax with sqlite proxy driver

Expand Down
40 changes: 40 additions & 0 deletions changelogs/drizzle-seed/0.3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# New features

## Drizzle Relations support

The `seed` function can now accept Drizzle Relations objects and treat them as foreign key constraints


```ts
// schema.ts
import { integer, serial, text, pgTable } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content').notNull(),
authorId: integer('author_id').notNull(),
});
export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, { fields: [posts.authorId], references: [users.id] }),
}));
```

```ts
// index.ts
import { seed } from "drizzle-seed";
import * as schema from './schema.ts'

async function main() {
const db = drizzle(process.env.DATABASE_URL!);
await seed(db, schema);
}

main();
```
2 changes: 1 addition & 1 deletion drizzle-kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-kit",
"version": "0.30.1",
"version": "0.30.2",
"homepage": "https://orm.drizzle.team",
"keywords": [
"drizzle",
Expand Down
11 changes: 11 additions & 0 deletions drizzle-kit/src/introspect-singlestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const singlestoreImportsList = new Set([
'tinyint',
'varbinary',
'varchar',
'vector',
'year',
'enum',
]);
Expand Down Expand Up @@ -789,6 +790,16 @@ const column = (
return out;
}

if (lowered.startsWith('vector')) {
const [dimensions, elementType] = lowered.substring('vector'.length + 1, lowered.length - 1).split(',');
let out = `${casing(name)}: vector(${
dbColumnName({ name, casing: rawCasing, withMode: true })
}{ dimensions: ${dimensions}, elementType: ${elementType} })`;

out += defaultValue ? `.default(${mapColumnDefault(defaultValue, isExpression)})` : '';
return out;
}

console.log('uknown', type);
return `// Warning: Can't parse ${type} from database\n\t// ${type}Type: ${type}("${name}")`;
};
Expand Down
2 changes: 1 addition & 1 deletion drizzle-kit/src/serializer/singlestoreSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export const generateSingleStoreSnapshot = (
if (typeof column.default === 'string') {
columnToSet.default = `'${column.default}'`;
} else {
if (sqlTypeLowered === 'json') {
if (sqlTypeLowered === 'json' || Array.isArray(column.default)) {
columnToSet.default = `'${JSON.stringify(column.default)}'`;
} else if (column.default instanceof Date) {
if (sqlTypeLowered === 'date') {
Expand Down
30 changes: 16 additions & 14 deletions drizzle-kit/src/utils/certs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@ import { access, readFile } from 'fs/promises';
import { join } from 'path';
import { $ } from 'zx';

const p = envPaths('drizzle-studio', {
suffix: '',
});

$.verbose = false;
$.cwd = p.data;
mkdirSync(p.data, { recursive: true });

export const certs = async () => {
const res = await $`mkcert --help`.nothrow();
$.verbose = false;

// ~/.local/share/drizzle-studio
const keyPath = join(p.data, 'localhost-key.pem');
const certPath = join(p.data, 'localhost.pem');
const res = await $`mkcert --help`.nothrow();

if (res.exitCode === 0) {
const p = envPaths('drizzle-studio', {
suffix: '',
});

$.cwd = p.data;

// create ~/.local/share/drizzle-studio
mkdirSync(p.data, { recursive: true });

const keyPath = join(p.data, 'localhost-key.pem');
const certPath = join(p.data, 'localhost.pem');

try {
// check if the files exist
await Promise.all([access(keyPath), access(certPath)]);
} catch (e) {
// if not create them
await $`mkcert localhost`.nothrow();
}
const [key, cert] = await Promise.all([
Expand All @@ -33,5 +37,3 @@ export const certs = async () => {
}
return null;
};

certs();
8 changes: 8 additions & 0 deletions drizzle-kit/tests/push/singlestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
tinyint,
varbinary,
varchar,
vector,
year,
} from 'drizzle-orm/singlestore-core';
import getPort from 'get-port';
Expand Down Expand Up @@ -249,6 +250,13 @@ const singlestoreSuite: DialectSuite = {
columnNotNull: binary('column_not_null', { length: 1 }).notNull(),
columnDefault: binary('column_default', { length: 12 }),
}),

allVectors: singlestoreTable('all_vectors', {
vectorSimple: vector('vector_simple', { dimensions: 1 }),
vectorElementType: vector('vector_element_type', { dimensions: 1, elementType: 'I8' }),
vectorNotNull: vector('vector_not_null', { dimensions: 1 }).notNull(),
vectorDefault: vector('vector_default', { dimensions: 1 }).default([1]),
}),
};

const { statements } = await diffTestSchemasPushSingleStore(
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/src/singlestore-core/columns/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { timestamp } from './timestamp.ts';
import { tinyint } from './tinyint.ts';
import { varbinary } from './varbinary.ts';
import { varchar } from './varchar.ts';
import { vector } from './vector.ts';
import { year } from './year.ts';

export function getSingleStoreColumnBuilders() {
Expand Down Expand Up @@ -51,6 +52,7 @@ export function getSingleStoreColumnBuilders() {
tinyint,
varbinary,
varchar,
vector,
year,
};
}
Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/singlestore-core/columns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export * from './timestamp.ts';
export * from './tinyint.ts';
export * from './varbinary.ts';
export * from './varchar.ts';
export * from './vector.ts';
export * from './year.ts';
83 changes: 83 additions & 0 deletions drizzle-orm/src/singlestore-core/columns/vector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnConfig } from '~/column-builder.ts';
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnySingleStoreTable } from '~/singlestore-core/table.ts';
import { SQL } from '~/sql/index.ts';
import { getColumnNameAndConfig } from '~/utils.ts';
import { SingleStoreColumn, SingleStoreColumnBuilder, SingleStoreGeneratedColumnConfig } from './common.ts';

export type SingleStoreVectorBuilderInitial<TName extends string> = SingleStoreVectorBuilder<{
name: TName;
dataType: 'array';
columnType: 'SingleStoreVector';
data: Array<number>;
driverParam: string;
enumValues: undefined;
}>;

export class SingleStoreVectorBuilder<T extends ColumnBuilderBaseConfig<'array', 'SingleStoreVector'>>
extends SingleStoreColumnBuilder<T, SingleStoreVectorConfig>
{
static override readonly [entityKind]: string = 'SingleStoreVectorBuilder';

constructor(name: T['name'], config: SingleStoreVectorConfig) {
super(name, 'array', 'SingleStoreVector');
this.config.dimensions = config.dimensions;
this.config.elementType = config.elementType;
}

/** @internal */
override build<TTableName extends string>(
table: AnySingleStoreTable<{ name: TTableName }>,
): SingleStoreVector<MakeColumnConfig<T, TTableName>> {
return new SingleStoreVector<MakeColumnConfig<T, TTableName>>(
table,
this.config as ColumnBuilderRuntimeConfig<any, any>,
);
}

/** @internal */
override generatedAlwaysAs(as: SQL<unknown> | (() => SQL) | T['data'], config?: SingleStoreGeneratedColumnConfig) {
throw new Error('not implemented');
}
}

export class SingleStoreVector<T extends ColumnBaseConfig<'array', 'SingleStoreVector'>>
extends SingleStoreColumn<T, SingleStoreVectorConfig>
{
static override readonly [entityKind]: string = 'SingleStoreVector';

dimensions: number = this.config.dimensions;
elementType: ElementType | undefined = this.config.elementType;

getSQLType(): string {
return `vector(${this.dimensions}, ${this.elementType || 'F32'})`;
}

override mapToDriverValue(value: Array<number>) {
return JSON.stringify(value);
}

override mapFromDriverValue(value: string): Array<number> {
return JSON.parse(value);
}
}

type ElementType = 'I8' | 'I16' | 'I32' | 'I64' | 'F32' | 'F64';

export interface SingleStoreVectorConfig {
dimensions: number;
elementType?: ElementType;
}

export function vector(
config: SingleStoreVectorConfig,
): SingleStoreVectorBuilderInitial<''>;
export function vector<TName extends string>(
name: TName,
config: SingleStoreVectorConfig,
): SingleStoreVectorBuilderInitial<TName>;
export function vector(a: string | SingleStoreVectorConfig, b?: SingleStoreVectorConfig) {
const { name, config } = getColumnNameAndConfig<SingleStoreVectorConfig>(a, b);
return new SingleStoreVectorBuilder(name, config);
}
9 changes: 9 additions & 0 deletions drizzle-orm/src/singlestore-core/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ export function substring(
chunks.push(sql`)`);
return sql.join(chunks);
}

// Vectors
export function dotProduct(column: SingleStoreColumn | SQL.Aliased, value: Array<number>): SQL {
return sql`${column} <*> ${JSON.stringify(value)}`;
}

export function euclideanDistance(column: SingleStoreColumn | SQL.Aliased, value: Array<number>): SQL {
return sql`${column} <-> ${JSON.stringify(value)}`;
}
5 changes: 5 additions & 0 deletions drizzle-orm/type-tests/singlestore/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
uniqueIndex,
varbinary,
varchar,
vector,
year,
} from '~/singlestore-core/index.ts';
import { singlestoreSchema } from '~/singlestore-core/schema.ts';
Expand Down Expand Up @@ -917,6 +918,8 @@ Expect<
varchar: varchar('varchar', { length: 1 }),
varchar2: varchar('varchar2', { length: 1, enum: ['a', 'b', 'c'] }),
varchardef: varchar('varchardef', { length: 1 }).default(''),
vector: vector('vector', { dimensions: 1 }),
vector2: vector('vector2', { dimensions: 1, elementType: 'I8' }),
year: year('year'),
yeardef: year('yeardef').default(0),
});
Expand Down Expand Up @@ -1015,6 +1018,8 @@ Expect<
varchar: varchar({ length: 1 }),
varchar2: varchar({ length: 1, enum: ['a', 'b', 'c'] }),
varchardef: varchar({ length: 1 }).default(''),
vector: vector({ dimensions: 1 }),
vector2: vector({ dimensions: 1, elementType: 'I8' }),
year: year(),
yeardef: year().default(0),
});
Expand Down
4 changes: 2 additions & 2 deletions drizzle-seed/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-seed",
"version": "0.2.1",
"version": "0.3.0",
"main": "index.js",
"type": "module",
"scripts": {
Expand All @@ -12,7 +12,7 @@
"generate-for-tests:mysql": "drizzle-kit generate --config=./src/tests/mysql/drizzle.config.ts",
"generate-for-tests:sqlite": "drizzle-kit generate --config=./src/tests/sqlite/drizzle.config.ts",
"generate": "drizzle-kit generate",
"start": "npx tsx ./src/test.ts",
"start": "npx tsx ./src/dev/test.ts",
"start:pg": "npx tsx ./src/tests/northwind/pgTest.ts",
"start:mysql": "npx tsx ./src/tests/northwind/mysqlTest.ts",
"start:sqlite": "npx tsx ./src/tests/northwind/sqliteTest.ts",
Expand Down
Loading

0 comments on commit 4a4629c

Please sign in to comment.