From 28876e6d53e0141f3bf6922c507d3021bbc51557 Mon Sep 17 00:00:00 2001 From: prodrigues Date: Fri, 20 Sep 2024 14:12:50 +0100 Subject: [PATCH] adding missing driver declaration in drizzle-orm --- drizzle-kit/src/cli/commands/singlestoreUp.ts | 1 + drizzle-kit/src/cli/commands/utils.ts | 6 ++--- drizzle-kit/src/cli/schema.ts | 27 +++++++++++-------- drizzle-orm/src/monodriver.ts | 20 +++++++++++++- drizzle-orm/src/monomigrator.ts | 9 ++++++- 5 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 drizzle-kit/src/cli/commands/singlestoreUp.ts diff --git a/drizzle-kit/src/cli/commands/singlestoreUp.ts b/drizzle-kit/src/cli/commands/singlestoreUp.ts new file mode 100644 index 000000000..dc5004ed0 --- /dev/null +++ b/drizzle-kit/src/cli/commands/singlestoreUp.ts @@ -0,0 +1 @@ +export const upSinglestoreHandler = (out: string) => {}; diff --git a/drizzle-kit/src/cli/commands/utils.ts b/drizzle-kit/src/cli/commands/utils.ts index d206fd235..25e80ed4d 100644 --- a/drizzle-kit/src/cli/commands/utils.ts +++ b/drizzle-kit/src/cli/commands/utils.ts @@ -334,7 +334,7 @@ export const preparePushConfig = async ( if (config.dialect === 'singlestore') { const parsed = singlestoreCredentials.safeParse(config); if (!parsed.success) { - printIssuesPg(config); + printIssuesSingleStore(config); process.exit(1); } @@ -508,7 +508,7 @@ export const preparePullConfig = async ( if (dialect === 'singlestore') { const parsed = singlestoreCredentials.safeParse(config); if (!parsed.success) { - printIssuesPg(config); + printIssuesSingleStore(config); process.exit(1); } @@ -617,7 +617,7 @@ export const prepareStudioConfig = async (options: Record) => { if (dialect === 'singlestore') { const parsed = singlestoreCredentials.safeParse(flattened); if (!parsed.success) { - printIssuesPg(flattened as Record); + printIssuesSingleStore(flattened as Record); process.exit(1); } const credentials = parsed.data; diff --git a/drizzle-kit/src/cli/schema.ts b/drizzle-kit/src/cli/schema.ts index 5316a9e90..6b7bcb560 100644 --- a/drizzle-kit/src/cli/schema.ts +++ b/drizzle-kit/src/cli/schema.ts @@ -1,11 +1,20 @@ +import { boolean, command, number, string } from '@drizzle-team/brocli'; import chalk from 'chalk'; -import { checkHandler } from './commands/check'; -import { assertOrmCoreVersion, assertPackages, assertStudioNodeVersion, ormVersionGt } from './utils'; +import 'dotenv/config'; +import { mkdirSync } from 'fs'; +import { renderWithTask } from 'hanji'; +import { dialects } from 'src/schemaValidator'; import '../@types/utils'; +import { assertUnreachable } from '../global'; +import { drizzleForLibSQL, drizzleForSingleStore, prepareSingleStoreSchema, type Setup } from '../serializer/studio'; import { assertV1OutFolder } from '../utils'; +import { certs } from '../utils/certs'; +import { checkHandler } from './commands/check'; import { dropMigration } from './commands/drop'; +import { prepareAndMigrateSingleStore } from './commands/migrate'; import { upMysqlHandler } from './commands/mysqlUp'; import { upPgHandler } from './commands/pgUp'; +import { upSinglestoreHandler } from './commands/singlestoreUp'; import { upSqliteHandler } from './commands/sqliteUp'; import { prepareCheckParams, @@ -16,17 +25,9 @@ import { preparePushConfig, prepareStudioConfig, } from './commands/utils'; +import { assertOrmCoreVersion, assertPackages, assertStudioNodeVersion, ormVersionGt } from './utils'; import { assertCollisions, drivers, prefixes } from './validations/common'; import { withStyle } from './validations/outputs'; -import 'dotenv/config'; -import { boolean, command, number, string } from '@drizzle-team/brocli'; -import { mkdirSync } from 'fs'; -import { renderWithTask } from 'hanji'; -import { dialects } from 'src/schemaValidator'; -import { assertUnreachable } from '../global'; -import { drizzleForLibSQL, drizzleForSingleStore, prepareSingleStoreSchema, type Setup } from '../serializer/studio'; -import { certs } from '../utils/certs'; -import { prepareAndMigrateSingleStore } from './commands/migrate'; import { grey, MigrateProgress } from './views'; const optionDialect = string('dialect') @@ -407,6 +408,10 @@ export const up = command({ upMysqlHandler(out); } + if (dialect === 'singlestore') { + upSinglestoreHandler(out); + } + if (dialect === 'sqlite' || dialect === 'turso') { upSqliteHandler(out); } diff --git a/drizzle-orm/src/monodriver.ts b/drizzle-orm/src/monodriver.ts index 0706f271a..03fcdc58f 100644 --- a/drizzle-orm/src/monodriver.ts +++ b/drizzle-orm/src/monodriver.ts @@ -30,6 +30,7 @@ import type { NeonDatabase } from './neon-serverless/index.ts'; import type { NodePgDatabase } from './node-postgres/index.ts'; import type { PlanetScaleDatabase } from './planetscale-serverless/index.ts'; import type { PostgresJsDatabase } from './postgres-js/index.ts'; +import type { SingleStore2Database, SingleStore2DrizzleConfig } from './singlestore/driver.ts'; import type { TiDBServerlessDatabase } from './tidb-serverless/index.ts'; import type { DrizzleConfig } from './utils.ts'; import type { VercelPgDatabase } from './vercel-postgres/index.ts'; @@ -98,7 +99,8 @@ type DatabaseClient = | 'libsql' | 'd1' | 'bun:sqlite' - | 'better-sqlite3'; + | 'better-sqlite3' + | 'singlestore'; type ClientDrizzleInstanceMap> = { 'node-postgres': NodePgDatabase; @@ -114,6 +116,7 @@ type ClientDrizzleInstanceMap> = { d1: DrizzleD1Database; 'bun:sqlite': BunSQLiteDatabase; 'better-sqlite3': DrizzleBetterSQLite3Database; + singlestore: SingleStore2Database; }; type ClientInstanceMap = { @@ -135,6 +138,7 @@ type ClientInstanceMap = { d1: D1Database; 'bun:sqlite': BunDatabase; 'better-sqlite3': BetterSQLite3Database; + singlestore: SingleStore2Database; }; type InitializerParams = { @@ -177,6 +181,10 @@ type InitializerParams = { 'better-sqlite3': { connection?: BetterSQLite3DatabaseConfig; }; + singlestore: { + // This Mysql2Config is from the node package 'mysql2' and not the one from Drizzle + connection: Mysql2Config; + }; }; type DetermineClient< @@ -379,6 +387,16 @@ export async function drizzle< const db = drizzle(sql, drizzleConfig) as any; db.$client = sql; + return db; + } + case 'singlestore': { + const { createPool } = await import('mysql2/promise').catch(() => importError('mysql2/promise')); + const instance = createPool(connection as Mysql2Config); + const { drizzle } = await import('./mysql2'); + + const db = drizzle(instance, drizzleConfig as SingleStore2DrizzleConfig) as any; + db.$client = instance; + return db; } } diff --git a/drizzle-orm/src/monomigrator.ts b/drizzle-orm/src/monomigrator.ts index bee18ad46..d8681ebf9 100644 --- a/drizzle-orm/src/monomigrator.ts +++ b/drizzle-orm/src/monomigrator.ts @@ -12,6 +12,7 @@ import type { NeonDatabase } from './neon-serverless/index.ts'; import type { NodePgDatabase } from './node-postgres/index.ts'; import type { PlanetScaleDatabase } from './planetscale-serverless/index.ts'; import type { PostgresJsDatabase } from './postgres-js/index.ts'; +import type { SingleStore2Database } from './singlestore/driver.ts'; import type { TiDBServerlessDatabase } from './tidb-serverless/index.ts'; import type { VercelPgDatabase } from './vercel-postgres/index.ts'; @@ -29,7 +30,8 @@ export async function migrate( | PlanetScaleDatabase | PostgresJsDatabase | VercelPgDatabase - | TiDBServerlessDatabase, + | TiDBServerlessDatabase + | SingleStore2Database, config: | string | MigrationConfig, @@ -100,5 +102,10 @@ export async function migrate( return migrate(db as VercelPgDatabase, config as string | MigrationConfig); } + case 'SingleStore2Database': { + const { migrate } = await import('./singlestore/migrator'); + + return migrate(db as SingleStore2Database, config as MigrationConfig); + } } }