Skip to content

Commit

Permalink
singlestore support for bson column type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodriguespn committed Aug 19, 2024
1 parent 55dfb8e commit 25a56f9
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 81 deletions.
1 change: 1 addition & 0 deletions drizzle-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"@vercel/postgres": "^0.8.0",
"ava": "^5.1.0",
"better-sqlite3": "^9.4.3",
"bson": "^6.8.0",
"camelcase": "^7.0.1",
"chalk": "^5.2.0",
"commander": "^12.1.0",
Expand Down
30 changes: 12 additions & 18 deletions drizzle-kit/src/introspect-singlestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ const singlestoreImportsList = new Set([
'singlestoreEnum',
'bigint',
'binary',
// TODO: add new type Blob
'boolean',
'bson',
'char',
'date',
'datetime',
Expand All @@ -21,8 +23,6 @@ const singlestoreImportsList = new Set([
'float',
'int',
'json',
// TODO: add new type BSON
// TODO: add new type Blob
// TODO: add new type UUID
// TODO: add new type GUID
// TODO: add new type Vector
Expand Down Expand Up @@ -245,18 +245,6 @@ const mapColumnDefault = (defaultValue: any, isExpression?: boolean) => {
return defaultValue;
};

const mapColumnDefaultForJson = (defaultValue: any) => {
if (
typeof defaultValue === 'string'
&& defaultValue.startsWith("('")
&& defaultValue.endsWith("')")
) {
return defaultValue.substring(2, defaultValue.length - 2);
}

return defaultValue;
};

const column = (
type: string,
name: string,
Expand Down Expand Up @@ -490,19 +478,25 @@ const column = (
return out;
}

// in mysql json can't have default value. Will leave it in case smth ;)
// TODO: check if SingleStore has json can't have default value
if (lowered === 'json') {
let out = `${casing(name)}: json("${name}")`;

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

return out;
}

// TODO: add new type BSON
if (lowered === 'bson') {
let out = `${casing(name)}: bson("${name}")`;

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

return out;
}

// TODO: add new type Blob

Expand Down
6 changes: 6 additions & 0 deletions drizzle-kit/src/serializer/singlestoreSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { RowDataPacket } from 'mysql2/promise';
import { withStyle } from '../cli/validations/outputs';
import { IntrospectStage, IntrospectStatus } from '../cli/views';

import { BSON } from 'bson';
import type { DB } from '../utils';
import { sqlToStr } from '.';
import {
Expand Down Expand Up @@ -130,6 +131,11 @@ export const generateSingleStoreSnapshot = (
} else {
if (sqlTypeLowered === 'json') {
columnToSet.default = `'${JSON.stringify(column.default)}'`;
} else if (sqlTypeLowered === 'bson') {
if (column.default !== null) {
const hexaCode = `0x${Buffer.from(BSON.serialize(column.default)).toString('hex')}`;
columnToSet.default = `${hexaCode}`;
}
} else if (column.default instanceof Date) {
if (sqlTypeLowered === 'date') {
columnToSet.default = `'${column.default.toISOString().split('T')[0]}'`;
Expand Down
45 changes: 45 additions & 0 deletions drizzle-kit/tests/singlestore.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { BSON } from 'bson';
import { sql } from 'drizzle-orm';
import {
bson,
index,
json,
primaryKey,
Expand Down Expand Up @@ -495,6 +497,49 @@ test('add table #14', async () => {
});

// TODO: add bson type tests
test('add table with bson field', async () => {
const to = {
users: singlestoreTable('table', {
bson: bson('bson'),
}),
};

const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toBe('CREATE TABLE `table` (\n\t`bson` bson\n);\n');
});

test('add table with bson field with empty default value', async () => {
const to = {
users: singlestoreTable('table', {
bson: bson('bson').default({}),
}),
};

const hexaCode = `0x${Buffer.from(BSON.serialize({})).toString('hex')}`;

const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toBe(
`CREATE TABLE \`table\` (\n\t\`bson\` bson DEFAULT ${hexaCode}\n);\n`,
);
});

test('add table with bson field with object default value', async () => {
const to = {
users: singlestoreTable('table', {
bson: bson('bson').default({ key: 'value' }),
}),
};

const hexaCode = `0x${Buffer.from(BSON.serialize({ key: 'value' })).toString('hex')}`;

const { sqlStatements } = await diffTestSchemasSingleStore({}, to, []);
expect(sqlStatements.length).toBe(1);
expect(sqlStatements[0]).toBe(
`CREATE TABLE \`table\` (\n\t\`bson\` bson DEFAULT ${hexaCode}\n);\n`,
);
});

// TODO: add blob type tests

Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
"@vercel/postgres": "^0.8.0",
"@xata.io/client": "^0.29.3",
"better-sqlite3": "^8.4.0",
"bson": "^6.8.0",
"bun-types": "^0.6.6",
"cpy": "^10.1.0",
"expo-sqlite": "^13.2.0",
Expand Down
7 changes: 3 additions & 4 deletions drizzle-orm/src/singlestore-core/columns/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { ColumnBuilderBaseConfig, ColumnBuilderRuntimeConfig, MakeColumnCon
import type { ColumnBaseConfig } from '~/column.ts';
import { entityKind } from '~/entity.ts';
import type { AnySingleStoreTable } from '~/singlestore-core/table.ts';
import { sql } from '~/sql/sql.ts';
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts';

export type SingleStoreBsonBuilderInitial<TName extends string> = SingleStoreBsonBuilder<{
Expand Down Expand Up @@ -42,9 +41,9 @@ export class SingleStoreBson<T extends ColumnBaseConfig<'buffer', 'SingleStoreBs
return 'bson';
}

override mapToDriverValue(value: T['data']) {
const json = JSON.stringify(value);
return sql`${json}:>BSON`;
override mapToDriverValue(value: T['data']): string {
const jsonData = JSON.stringify(value);
return `${jsonData}:>BSON`;
}
}

Expand Down
1 change: 1 addition & 0 deletions drizzle-orm/src/singlestore-core/columns/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class SingleStoreJson<T extends ColumnBaseConfig<'json', 'SingleStoreJson
}

override mapToDriverValue(value: T['data']): string {
console.log('value', value);
return JSON.stringify(value);
}
}
Expand Down
4 changes: 2 additions & 2 deletions drizzle-orm/type-tests/singlestore/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { sql } from '~/sql/sql.ts';
import { db } from './db.ts';
import { users } from './tables.ts';

const mysqlInsertReturning = await db.insert(users).values({
const singlestoreInsertReturning = await db.insert(users).values({
// ^?
homeCity: 1,
class: 'A',
age1: 1,
enumCol: 'a',
}).$returningId();

Expect<Equal<{ id: number; serialNullable: number; serialNotNull: number }[], typeof mysqlInsertReturning>>;
Expect<Equal<{ id: number; serialNullable: number; serialNotNull: number }[], typeof singlestoreInsertReturning>>;

const insert = await db.insert(users).values({
homeCity: 1,
Expand Down
Loading

0 comments on commit 25a56f9

Please sign in to comment.