@anthonyalayo/drizzle-kit 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +25 -0
- package/README.md +79 -0
- package/build.dev.ts +51 -0
- package/build.ts +112 -0
- package/imports-checker/analyze.ts +78 -0
- package/imports-checker/checker.ts +296 -0
- package/imports-checker/grammar/grammar.ohm +121 -0
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +164 -0
- package/imports-checker/grammar/grammar.ohm-bundle.js +753 -0
- package/imports-checker/index.ts +48 -0
- package/package.json +142 -0
- package/patches/difflib@0.2.4.patch +31 -0
- package/src/@types/utils.ts +58 -0
- package/src/api.ts +504 -0
- package/src/cli/commands/_es5.ts +2 -0
- package/src/cli/commands/check.ts +52 -0
- package/src/cli/commands/drop.ts +60 -0
- package/src/cli/commands/introspect.ts +772 -0
- package/src/cli/commands/libSqlPushUtils.ts +355 -0
- package/src/cli/commands/migrate.ts +1526 -0
- package/src/cli/commands/mysqlIntrospect.ts +53 -0
- package/src/cli/commands/mysqlPushUtils.ts +352 -0
- package/src/cli/commands/mysqlUp.ts +102 -0
- package/src/cli/commands/pgIntrospect.ts +55 -0
- package/src/cli/commands/pgPushUtils.ts +269 -0
- package/src/cli/commands/pgUp.ts +217 -0
- package/src/cli/commands/push.ts +637 -0
- package/src/cli/commands/singlestoreIntrospect.ts +53 -0
- package/src/cli/commands/singlestorePushUtils.ts +456 -0
- package/src/cli/commands/singlestoreUp.ts +1 -0
- package/src/cli/commands/sqliteIntrospect.ts +96 -0
- package/src/cli/commands/sqlitePushUtils.ts +322 -0
- package/src/cli/commands/sqliteUp.ts +52 -0
- package/src/cli/commands/utils.ts +858 -0
- package/src/cli/connections.ts +914 -0
- package/src/cli/index.ts +48 -0
- package/src/cli/schema.ts +794 -0
- package/src/cli/selector-ui.ts +38 -0
- package/src/cli/utils.ts +112 -0
- package/src/cli/validations/cli.ts +79 -0
- package/src/cli/validations/common.ts +193 -0
- package/src/cli/validations/libsql.ts +27 -0
- package/src/cli/validations/mysql.ts +61 -0
- package/src/cli/validations/outputs.ts +91 -0
- package/src/cli/validations/postgres.ts +83 -0
- package/src/cli/validations/singlestore.ts +61 -0
- package/src/cli/validations/sqlite.ts +104 -0
- package/src/cli/validations/studio.ts +24 -0
- package/src/cli/views.ts +658 -0
- package/src/extensions/getTablesFilterByExtensions.ts +16 -0
- package/src/extensions/vector.ts +10 -0
- package/src/global.ts +61 -0
- package/src/index.ts +326 -0
- package/src/introspect-mysql.ts +1030 -0
- package/src/introspect-pg.ts +1435 -0
- package/src/introspect-singlestore.ts +913 -0
- package/src/introspect-sqlite.ts +533 -0
- package/src/jsonDiffer.js +882 -0
- package/src/jsonStatements.ts +3590 -0
- package/src/loader.mjs +57 -0
- package/src/migrationPreparator.ts +217 -0
- package/src/schemaValidator.ts +23 -0
- package/src/serializer/index.ts +142 -0
- package/src/serializer/mysqlImports.ts +38 -0
- package/src/serializer/mysqlSchema.ts +423 -0
- package/src/serializer/mysqlSerializer.ts +999 -0
- package/src/serializer/pgImports.ts +106 -0
- package/src/serializer/pgSchema.ts +937 -0
- package/src/serializer/pgSerializer.ts +2223 -0
- package/src/serializer/singlestoreImports.ts +38 -0
- package/src/serializer/singlestoreSchema.ts +257 -0
- package/src/serializer/singlestoreSerializer.ts +767 -0
- package/src/serializer/sqliteImports.ts +41 -0
- package/src/serializer/sqliteSchema.ts +352 -0
- package/src/serializer/sqliteSerializer.ts +931 -0
- package/src/serializer/studio.ts +694 -0
- package/src/serializer/utils.ts +45 -0
- package/src/simulator.ts +157 -0
- package/src/snapshotsDiffer.ts +4398 -0
- package/src/sqlgenerator.ts +4212 -0
- package/src/statementCombiner.ts +597 -0
- package/src/utils/certs.ts +44 -0
- package/src/utils/words.ts +1333 -0
- package/src/utils.ts +370 -0
- package/tests/bin.test.ts +58 -0
- package/tests/cli/d1http.config.ts +12 -0
- package/tests/cli/drizzle.config.ts +9 -0
- package/tests/cli/durable-sqlite.config.ts +7 -0
- package/tests/cli/expo.config.ts +7 -0
- package/tests/cli/postgres.config.ts +13 -0
- package/tests/cli/postgres2.config.ts +17 -0
- package/tests/cli/schema.ts +1 -0
- package/tests/cli/turso.config.ts +10 -0
- package/tests/cli-export.test.ts +79 -0
- package/tests/cli-generate.test.ts +259 -0
- package/tests/cli-migrate.test.ts +103 -0
- package/tests/cli-push.test.ts +126 -0
- package/tests/common.ts +15 -0
- package/tests/indexes/common.ts +21 -0
- package/tests/indexes/pg.test.ts +245 -0
- package/tests/introspect/libsql.test.ts +35 -0
- package/tests/introspect/mysql.test.ts +319 -0
- package/tests/introspect/pg.test.ts +894 -0
- package/tests/introspect/singlestore.test.ts +275 -0
- package/tests/introspect/sqlite.test.ts +122 -0
- package/tests/libsql-checks.test.ts +308 -0
- package/tests/libsql-statements.test.ts +989 -0
- package/tests/libsql-views.test.ts +218 -0
- package/tests/migrate/libsq-schema.ts +6 -0
- package/tests/migrate/libsql-migrate.test.ts +58 -0
- package/tests/migrate/migrations/0000_little_blizzard.sql +4 -0
- package/tests/migrate/migrations/0001_nebulous_storm.sql +10 -0
- package/tests/migrate/migrations/meta/0000_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/0001_snapshot.json +40 -0
- package/tests/migrate/migrations/meta/_journal.json +20 -0
- package/tests/mysql-checks.test.ts +291 -0
- package/tests/mysql-generated.test.ts +1290 -0
- package/tests/mysql-schemas.test.ts +156 -0
- package/tests/mysql-views.test.ts +553 -0
- package/tests/mysql.test.ts +863 -0
- package/tests/pg-array.test.ts +368 -0
- package/tests/pg-checks.test.ts +329 -0
- package/tests/pg-columns.test.ts +484 -0
- package/tests/pg-domains.test.ts +306 -0
- package/tests/pg-enums.test.ts +751 -0
- package/tests/pg-generated.test.ts +529 -0
- package/tests/pg-identity.test.ts +447 -0
- package/tests/pg-schemas.test.ts +105 -0
- package/tests/pg-sequences.test.ts +298 -0
- package/tests/pg-tables.test.ts +1021 -0
- package/tests/pg-views.test.ts +1929 -0
- package/tests/push/common.ts +58 -0
- package/tests/push/libsql.test.ts +1400 -0
- package/tests/push/mysql-push.test.ts +345 -0
- package/tests/push/mysql.test.ts +788 -0
- package/tests/push/pg.test.ts +4341 -0
- package/tests/push/singlestore-push.test.ts +894 -0
- package/tests/push/singlestore.test.ts +444 -0
- package/tests/push/sqlite.test.ts +1613 -0
- package/tests/rls/pg-policy.test.ts +1656 -0
- package/tests/rls/pg-role.test.ts +234 -0
- package/tests/schemaDiffer.ts +2852 -0
- package/tests/singlestore-generated.test.ts +1290 -0
- package/tests/singlestore-schemas.test.ts +155 -0
- package/tests/singlestore.test.ts +978 -0
- package/tests/sqlite-checks.test.ts +308 -0
- package/tests/sqlite-columns.test.ts +1049 -0
- package/tests/sqlite-generated.test.ts +1726 -0
- package/tests/sqlite-tables.test.ts +637 -0
- package/tests/sqlite-views.test.ts +218 -0
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +1812 -0
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +882 -0
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +1211 -0
- package/tests/test/sqlite.test.ts +39 -0
- package/tests/testsinglestore.ts +29 -0
- package/tests/validations.test.ts +869 -0
- package/tests/wrap-param.test.ts +16 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.cli-types.json +9 -0
- package/tsconfig.json +28 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
import { serve } from '@hono/node-server';
|
|
2
|
+
import { zValidator } from '@hono/zod-validator';
|
|
3
|
+
import { createHash } from 'crypto';
|
|
4
|
+
import {
|
|
5
|
+
AnyColumn,
|
|
6
|
+
AnyTable,
|
|
7
|
+
createTableRelationsHelpers,
|
|
8
|
+
extractTablesRelationalConfig,
|
|
9
|
+
is,
|
|
10
|
+
Many,
|
|
11
|
+
normalizeRelation,
|
|
12
|
+
One,
|
|
13
|
+
Relations,
|
|
14
|
+
TablesRelationalConfig,
|
|
15
|
+
} from 'drizzle-orm';
|
|
16
|
+
import { AnyMySqlTable, getTableConfig as mysqlTableConfig, MySqlTable } from 'drizzle-orm/mysql-core';
|
|
17
|
+
import { AnyPgTable, getTableConfig as pgTableConfig, PgTable } from 'drizzle-orm/pg-core';
|
|
18
|
+
import {
|
|
19
|
+
AnySingleStoreTable,
|
|
20
|
+
getTableConfig as singlestoreTableConfig,
|
|
21
|
+
SingleStoreTable,
|
|
22
|
+
} from 'drizzle-orm/singlestore-core';
|
|
23
|
+
import { AnySQLiteTable, getTableConfig as sqliteTableConfig, SQLiteTable } from 'drizzle-orm/sqlite-core';
|
|
24
|
+
import fs from 'fs';
|
|
25
|
+
import { Hono } from 'hono';
|
|
26
|
+
import { compress } from 'hono/compress';
|
|
27
|
+
import { cors } from 'hono/cors';
|
|
28
|
+
import { createServer } from 'node:https';
|
|
29
|
+
import { LibSQLCredentials } from 'src/cli/validations/libsql';
|
|
30
|
+
import { assertUnreachable } from 'src/global';
|
|
31
|
+
import superjson from 'superjson';
|
|
32
|
+
import { z } from 'zod';
|
|
33
|
+
import { safeRegister } from '../cli/commands/utils';
|
|
34
|
+
import type { MysqlCredentials } from '../cli/validations/mysql';
|
|
35
|
+
import type { PostgresCredentials } from '../cli/validations/postgres';
|
|
36
|
+
import type { SingleStoreCredentials } from '../cli/validations/singlestore';
|
|
37
|
+
import type { SqliteCredentials } from '../cli/validations/sqlite';
|
|
38
|
+
import { prepareFilenames } from '.';
|
|
39
|
+
|
|
40
|
+
type CustomDefault = {
|
|
41
|
+
schema: string;
|
|
42
|
+
table: string;
|
|
43
|
+
column: string;
|
|
44
|
+
func: () => unknown;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
type SchemaFile = {
|
|
48
|
+
name: string;
|
|
49
|
+
content: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export type Setup = {
|
|
53
|
+
dbHash: string;
|
|
54
|
+
dialect: 'postgresql' | 'mysql' | 'sqlite' | 'singlestore';
|
|
55
|
+
driver?: 'aws-data-api' | 'd1-http' | 'turso' | 'pglite';
|
|
56
|
+
proxy: (params: ProxyParams) => Promise<any[] | any>;
|
|
57
|
+
customDefaults: CustomDefault[];
|
|
58
|
+
schema: Record<string, Record<string, AnyTable<any>>>;
|
|
59
|
+
relations: Record<string, Relations>;
|
|
60
|
+
schemaFiles?: SchemaFile[];
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type ProxyParams = {
|
|
64
|
+
sql: string;
|
|
65
|
+
params: any[];
|
|
66
|
+
typings?: any[];
|
|
67
|
+
mode: 'array' | 'object';
|
|
68
|
+
method: 'values' | 'get' | 'all' | 'run' | 'execute';
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const preparePgSchema = async (path: string | string[]) => {
|
|
72
|
+
const imports = prepareFilenames(path);
|
|
73
|
+
const pgSchema: Record<string, Record<string, AnyPgTable>> = {};
|
|
74
|
+
const relations: Record<string, Relations> = {};
|
|
75
|
+
|
|
76
|
+
// files content as string
|
|
77
|
+
const files = imports.map((it, index) => ({
|
|
78
|
+
// get the file name from the path
|
|
79
|
+
name: it.split('/').pop() || `schema${index}.ts`,
|
|
80
|
+
content: fs.readFileSync(it, 'utf-8'),
|
|
81
|
+
}));
|
|
82
|
+
|
|
83
|
+
const { unregister } = await safeRegister();
|
|
84
|
+
for (let i = 0; i < imports.length; i++) {
|
|
85
|
+
const it = imports[i];
|
|
86
|
+
|
|
87
|
+
const i0: Record<string, unknown> = require(`${it}`);
|
|
88
|
+
const i0values = Object.entries(i0);
|
|
89
|
+
|
|
90
|
+
i0values.forEach(([k, t]) => {
|
|
91
|
+
if (is(t, PgTable)) {
|
|
92
|
+
const schema = pgTableConfig(t).schema || 'public';
|
|
93
|
+
pgSchema[schema] = pgSchema[schema] || {};
|
|
94
|
+
pgSchema[schema][k] = t;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (is(t, Relations)) {
|
|
98
|
+
relations[k] = t;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
unregister();
|
|
103
|
+
|
|
104
|
+
return { schema: pgSchema, relations, files };
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export const prepareMySqlSchema = async (path: string | string[]) => {
|
|
108
|
+
const imports = prepareFilenames(path);
|
|
109
|
+
const mysqlSchema: Record<string, Record<string, AnyMySqlTable>> = {
|
|
110
|
+
public: {},
|
|
111
|
+
};
|
|
112
|
+
const relations: Record<string, Relations> = {};
|
|
113
|
+
|
|
114
|
+
// files content as string
|
|
115
|
+
const files = imports.map((it, index) => ({
|
|
116
|
+
// get the file name from the path
|
|
117
|
+
name: it.split('/').pop() || `schema${index}.ts`,
|
|
118
|
+
content: fs.readFileSync(it, 'utf-8'),
|
|
119
|
+
}));
|
|
120
|
+
|
|
121
|
+
const { unregister } = await safeRegister();
|
|
122
|
+
for (let i = 0; i < imports.length; i++) {
|
|
123
|
+
const it = imports[i];
|
|
124
|
+
|
|
125
|
+
const i0: Record<string, unknown> = require(`${it}`);
|
|
126
|
+
const i0values = Object.entries(i0);
|
|
127
|
+
|
|
128
|
+
i0values.forEach(([k, t]) => {
|
|
129
|
+
if (is(t, MySqlTable)) {
|
|
130
|
+
const schema = mysqlTableConfig(t).schema || 'public';
|
|
131
|
+
mysqlSchema[schema][k] = t;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (is(t, Relations)) {
|
|
135
|
+
relations[k] = t;
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
unregister();
|
|
140
|
+
|
|
141
|
+
return { schema: mysqlSchema, relations, files };
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export const prepareSQLiteSchema = async (path: string | string[]) => {
|
|
145
|
+
const imports = prepareFilenames(path);
|
|
146
|
+
const sqliteSchema: Record<string, Record<string, AnySQLiteTable>> = {
|
|
147
|
+
public: {},
|
|
148
|
+
};
|
|
149
|
+
const relations: Record<string, Relations> = {};
|
|
150
|
+
|
|
151
|
+
// files content as string
|
|
152
|
+
const files = imports.map((it, index) => ({
|
|
153
|
+
// get the file name from the path
|
|
154
|
+
name: it.split('/').pop() || `schema${index}.ts`,
|
|
155
|
+
content: fs.readFileSync(it, 'utf-8'),
|
|
156
|
+
}));
|
|
157
|
+
|
|
158
|
+
const { unregister } = await safeRegister();
|
|
159
|
+
for (let i = 0; i < imports.length; i++) {
|
|
160
|
+
const it = imports[i];
|
|
161
|
+
|
|
162
|
+
const i0: Record<string, unknown> = require(`${it}`);
|
|
163
|
+
const i0values = Object.entries(i0);
|
|
164
|
+
|
|
165
|
+
i0values.forEach(([k, t]) => {
|
|
166
|
+
if (is(t, SQLiteTable)) {
|
|
167
|
+
const schema = 'public'; // sqlite does not have schemas
|
|
168
|
+
sqliteSchema[schema][k] = t;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (is(t, Relations)) {
|
|
172
|
+
relations[k] = t;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
unregister();
|
|
177
|
+
|
|
178
|
+
return { schema: sqliteSchema, relations, files };
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export const prepareSingleStoreSchema = async (path: string | string[]) => {
|
|
182
|
+
const imports = prepareFilenames(path);
|
|
183
|
+
const singlestoreSchema: Record<string, Record<string, AnySingleStoreTable>> = {
|
|
184
|
+
public: {},
|
|
185
|
+
};
|
|
186
|
+
const relations: Record<string, Relations> = {};
|
|
187
|
+
|
|
188
|
+
// files content as string
|
|
189
|
+
const files = imports.map((it, index) => ({
|
|
190
|
+
// get the file name from the path
|
|
191
|
+
name: it.split('/').pop() || `schema${index}.ts`,
|
|
192
|
+
content: fs.readFileSync(it, 'utf-8'),
|
|
193
|
+
}));
|
|
194
|
+
|
|
195
|
+
const { unregister } = await safeRegister();
|
|
196
|
+
for (let i = 0; i < imports.length; i++) {
|
|
197
|
+
const it = imports[i];
|
|
198
|
+
|
|
199
|
+
const i0: Record<string, unknown> = require(`${it}`);
|
|
200
|
+
const i0values = Object.entries(i0);
|
|
201
|
+
|
|
202
|
+
i0values.forEach(([k, t]) => {
|
|
203
|
+
if (is(t, SingleStoreTable)) {
|
|
204
|
+
const schema = singlestoreTableConfig(t).schema || 'public';
|
|
205
|
+
singlestoreSchema[schema][k] = t;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (is(t, Relations)) {
|
|
209
|
+
relations[k] = t;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
unregister();
|
|
214
|
+
|
|
215
|
+
return { schema: singlestoreSchema, relations, files };
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const getCustomDefaults = <T extends AnyTable<{}>>(
|
|
219
|
+
schema: Record<string, Record<string, T>>,
|
|
220
|
+
): CustomDefault[] => {
|
|
221
|
+
const customDefaults: CustomDefault[] = [];
|
|
222
|
+
|
|
223
|
+
Object.entries(schema).map(([schema, tables]) => {
|
|
224
|
+
Object.entries(tables).map(([, table]) => {
|
|
225
|
+
let tableConfig: {
|
|
226
|
+
name: string;
|
|
227
|
+
columns: AnyColumn[];
|
|
228
|
+
};
|
|
229
|
+
if (is(table, PgTable)) {
|
|
230
|
+
tableConfig = pgTableConfig(table);
|
|
231
|
+
} else if (is(table, MySqlTable)) {
|
|
232
|
+
tableConfig = mysqlTableConfig(table);
|
|
233
|
+
} else if (is(table, SQLiteTable)) {
|
|
234
|
+
tableConfig = sqliteTableConfig(table);
|
|
235
|
+
} else {
|
|
236
|
+
tableConfig = singlestoreTableConfig(table);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
tableConfig.columns.map((column) => {
|
|
240
|
+
if (column.defaultFn) {
|
|
241
|
+
customDefaults.push({
|
|
242
|
+
schema,
|
|
243
|
+
table: tableConfig.name,
|
|
244
|
+
column: column.name,
|
|
245
|
+
func: column.defaultFn,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
return customDefaults;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
export const drizzleForPostgres = async (
|
|
256
|
+
credentials: PostgresCredentials,
|
|
257
|
+
pgSchema: Record<string, Record<string, AnyPgTable>>,
|
|
258
|
+
relations: Record<string, Relations>,
|
|
259
|
+
schemaFiles?: SchemaFile[],
|
|
260
|
+
): Promise<Setup> => {
|
|
261
|
+
const { preparePostgresDB } = await import('../cli/connections');
|
|
262
|
+
const db = await preparePostgresDB(credentials);
|
|
263
|
+
const customDefaults = getCustomDefaults(pgSchema);
|
|
264
|
+
|
|
265
|
+
let dbUrl: string;
|
|
266
|
+
|
|
267
|
+
if ('driver' in credentials) {
|
|
268
|
+
const { driver } = credentials;
|
|
269
|
+
if (driver === 'aws-data-api') {
|
|
270
|
+
dbUrl = `aws-data-api://${credentials.database}/${credentials.secretArn}/${credentials.resourceArn}`;
|
|
271
|
+
} else if (driver === 'pglite') {
|
|
272
|
+
dbUrl = credentials.url;
|
|
273
|
+
} else {
|
|
274
|
+
assertUnreachable(driver);
|
|
275
|
+
}
|
|
276
|
+
} else if ('url' in credentials) {
|
|
277
|
+
dbUrl = credentials.url;
|
|
278
|
+
} else {
|
|
279
|
+
dbUrl =
|
|
280
|
+
`postgresql://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}/${credentials.database}`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const dbHash = createHash('sha256').update(dbUrl).digest('hex');
|
|
284
|
+
|
|
285
|
+
return {
|
|
286
|
+
dbHash,
|
|
287
|
+
dialect: 'postgresql',
|
|
288
|
+
driver: 'driver' in credentials ? credentials.driver : undefined,
|
|
289
|
+
proxy: db.proxy,
|
|
290
|
+
customDefaults,
|
|
291
|
+
schema: pgSchema,
|
|
292
|
+
relations,
|
|
293
|
+
schemaFiles,
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
export const drizzleForMySQL = async (
|
|
298
|
+
credentials: MysqlCredentials,
|
|
299
|
+
mysqlSchema: Record<string, Record<string, AnyMySqlTable>>,
|
|
300
|
+
relations: Record<string, Relations>,
|
|
301
|
+
schemaFiles?: SchemaFile[],
|
|
302
|
+
): Promise<Setup> => {
|
|
303
|
+
const { connectToMySQL } = await import('../cli/connections');
|
|
304
|
+
const { proxy } = await connectToMySQL(credentials);
|
|
305
|
+
|
|
306
|
+
const customDefaults = getCustomDefaults(mysqlSchema);
|
|
307
|
+
|
|
308
|
+
let dbUrl: string;
|
|
309
|
+
|
|
310
|
+
if ('url' in credentials) {
|
|
311
|
+
dbUrl = credentials.url;
|
|
312
|
+
} else {
|
|
313
|
+
dbUrl =
|
|
314
|
+
`mysql://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}/${credentials.database}`;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const dbHash = createHash('sha256').update(dbUrl).digest('hex');
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
dbHash,
|
|
321
|
+
dialect: 'mysql',
|
|
322
|
+
proxy,
|
|
323
|
+
customDefaults,
|
|
324
|
+
schema: mysqlSchema,
|
|
325
|
+
relations,
|
|
326
|
+
schemaFiles,
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
export const drizzleForSQLite = async (
|
|
331
|
+
credentials: SqliteCredentials,
|
|
332
|
+
sqliteSchema: Record<string, Record<string, AnySQLiteTable>>,
|
|
333
|
+
relations: Record<string, Relations>,
|
|
334
|
+
schemaFiles?: SchemaFile[],
|
|
335
|
+
): Promise<Setup> => {
|
|
336
|
+
const { connectToSQLite } = await import('../cli/connections');
|
|
337
|
+
|
|
338
|
+
const sqliteDB = await connectToSQLite(credentials);
|
|
339
|
+
const customDefaults = getCustomDefaults(sqliteSchema);
|
|
340
|
+
|
|
341
|
+
let dbUrl: string;
|
|
342
|
+
|
|
343
|
+
if ('driver' in credentials) {
|
|
344
|
+
const { driver } = credentials;
|
|
345
|
+
if (driver === 'd1-http') {
|
|
346
|
+
dbUrl = `d1-http://${credentials.accountId}/${credentials.databaseId}/${credentials.token}`;
|
|
347
|
+
} else {
|
|
348
|
+
assertUnreachable(driver);
|
|
349
|
+
}
|
|
350
|
+
} else {
|
|
351
|
+
dbUrl = credentials.url;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const dbHash = createHash('sha256').update(dbUrl).digest('hex');
|
|
355
|
+
|
|
356
|
+
return {
|
|
357
|
+
dbHash,
|
|
358
|
+
dialect: 'sqlite',
|
|
359
|
+
driver: 'driver' in credentials ? credentials.driver : undefined,
|
|
360
|
+
proxy: sqliteDB.proxy,
|
|
361
|
+
customDefaults,
|
|
362
|
+
schema: sqliteSchema,
|
|
363
|
+
relations,
|
|
364
|
+
schemaFiles,
|
|
365
|
+
};
|
|
366
|
+
};
|
|
367
|
+
export const drizzleForLibSQL = async (
|
|
368
|
+
credentials: LibSQLCredentials,
|
|
369
|
+
sqliteSchema: Record<string, Record<string, AnySQLiteTable>>,
|
|
370
|
+
relations: Record<string, Relations>,
|
|
371
|
+
schemaFiles?: SchemaFile[],
|
|
372
|
+
): Promise<Setup> => {
|
|
373
|
+
const { connectToLibSQL } = await import('../cli/connections');
|
|
374
|
+
|
|
375
|
+
const sqliteDB = await connectToLibSQL(credentials);
|
|
376
|
+
const customDefaults = getCustomDefaults(sqliteSchema);
|
|
377
|
+
|
|
378
|
+
let dbUrl: string = `turso://${credentials.url}/${credentials.authToken}`;
|
|
379
|
+
|
|
380
|
+
const dbHash = createHash('sha256').update(dbUrl).digest('hex');
|
|
381
|
+
|
|
382
|
+
return {
|
|
383
|
+
dbHash,
|
|
384
|
+
dialect: 'sqlite',
|
|
385
|
+
driver: undefined,
|
|
386
|
+
proxy: sqliteDB.proxy,
|
|
387
|
+
customDefaults,
|
|
388
|
+
schema: sqliteSchema,
|
|
389
|
+
relations,
|
|
390
|
+
schemaFiles,
|
|
391
|
+
};
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
export const drizzleForSingleStore = async (
|
|
395
|
+
credentials: SingleStoreCredentials,
|
|
396
|
+
singlestoreSchema: Record<string, Record<string, AnySingleStoreTable>>,
|
|
397
|
+
relations: Record<string, Relations>,
|
|
398
|
+
schemaFiles?: SchemaFile[],
|
|
399
|
+
): Promise<Setup> => {
|
|
400
|
+
const { connectToSingleStore } = await import('../cli/connections');
|
|
401
|
+
const { proxy } = await connectToSingleStore(credentials);
|
|
402
|
+
|
|
403
|
+
const customDefaults = getCustomDefaults(singlestoreSchema);
|
|
404
|
+
|
|
405
|
+
let dbUrl: string;
|
|
406
|
+
|
|
407
|
+
if ('url' in credentials) {
|
|
408
|
+
dbUrl = credentials.url;
|
|
409
|
+
} else {
|
|
410
|
+
dbUrl =
|
|
411
|
+
`singlestore://${credentials.user}:${credentials.password}@${credentials.host}:${credentials.port}/${credentials.database}`;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const dbHash = createHash('sha256').update(dbUrl).digest('hex');
|
|
415
|
+
|
|
416
|
+
return {
|
|
417
|
+
dbHash,
|
|
418
|
+
dialect: 'singlestore',
|
|
419
|
+
proxy,
|
|
420
|
+
customDefaults,
|
|
421
|
+
schema: singlestoreSchema,
|
|
422
|
+
relations,
|
|
423
|
+
schemaFiles,
|
|
424
|
+
};
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
export const extractRelations = (tablesConfig: {
|
|
428
|
+
tables: TablesRelationalConfig;
|
|
429
|
+
tableNamesMap: Record<string, string>;
|
|
430
|
+
}) => {
|
|
431
|
+
const relations = Object.values(tablesConfig.tables)
|
|
432
|
+
.map((it) =>
|
|
433
|
+
Object.entries(it.relations).map(([name, relation]) => {
|
|
434
|
+
const normalized = normalizeRelation(
|
|
435
|
+
tablesConfig.tables,
|
|
436
|
+
tablesConfig.tableNamesMap,
|
|
437
|
+
relation,
|
|
438
|
+
);
|
|
439
|
+
const rel = relation;
|
|
440
|
+
const refTableName = rel.referencedTableName;
|
|
441
|
+
const refTable = rel.referencedTable;
|
|
442
|
+
const fields = normalized.fields.map((it) => it.name).flat();
|
|
443
|
+
const refColumns = normalized.references.map((it) => it.name).flat();
|
|
444
|
+
|
|
445
|
+
let refSchema: string | undefined;
|
|
446
|
+
if (is(refTable, PgTable)) {
|
|
447
|
+
refSchema = pgTableConfig(refTable).schema;
|
|
448
|
+
} else if (is(refTable, MySqlTable)) {
|
|
449
|
+
refSchema = mysqlTableConfig(refTable).schema;
|
|
450
|
+
} else if (is(refTable, SQLiteTable)) {
|
|
451
|
+
refSchema = undefined;
|
|
452
|
+
} else if (is(refTable, SingleStoreTable)) {
|
|
453
|
+
refSchema = singlestoreTableConfig(refTable).schema;
|
|
454
|
+
} else {
|
|
455
|
+
throw new Error('unsupported dialect');
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
let type: 'one' | 'many';
|
|
459
|
+
if (is(rel, One)) {
|
|
460
|
+
type = 'one';
|
|
461
|
+
} else if (is(rel, Many)) {
|
|
462
|
+
type = 'many';
|
|
463
|
+
} else {
|
|
464
|
+
throw new Error('unsupported relation type');
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
return {
|
|
468
|
+
name,
|
|
469
|
+
type,
|
|
470
|
+
table: it.dbName,
|
|
471
|
+
schema: it.schema || 'public',
|
|
472
|
+
columns: fields,
|
|
473
|
+
refTable: refTableName,
|
|
474
|
+
refSchema: refSchema || 'public',
|
|
475
|
+
refColumns: refColumns,
|
|
476
|
+
};
|
|
477
|
+
})
|
|
478
|
+
)
|
|
479
|
+
.flat();
|
|
480
|
+
return relations;
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
const init = z.object({
|
|
484
|
+
type: z.literal('init'),
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
const proxySchema = z.object({
|
|
488
|
+
type: z.literal('proxy'),
|
|
489
|
+
data: z.object({
|
|
490
|
+
sql: z.string(),
|
|
491
|
+
params: z.array(z.any()).optional(),
|
|
492
|
+
typings: z.string().array().optional(),
|
|
493
|
+
mode: z.enum(['array', 'object']).default('object'),
|
|
494
|
+
method: z.union([
|
|
495
|
+
z.literal('values'),
|
|
496
|
+
z.literal('get'),
|
|
497
|
+
z.literal('all'),
|
|
498
|
+
z.literal('run'),
|
|
499
|
+
z.literal('execute'),
|
|
500
|
+
]),
|
|
501
|
+
}),
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
const defaultsSchema = z.object({
|
|
505
|
+
type: z.literal('defaults'),
|
|
506
|
+
data: z
|
|
507
|
+
.array(
|
|
508
|
+
z.object({
|
|
509
|
+
schema: z.string(),
|
|
510
|
+
table: z.string(),
|
|
511
|
+
column: z.string(),
|
|
512
|
+
}),
|
|
513
|
+
)
|
|
514
|
+
.min(1),
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
const schema = z.union([init, proxySchema, defaultsSchema]);
|
|
518
|
+
|
|
519
|
+
superjson.registerCustom<Buffer, number[]>(
|
|
520
|
+
{
|
|
521
|
+
isApplicable: (v): v is Buffer => v instanceof Buffer,
|
|
522
|
+
serialize: (v) => [...v],
|
|
523
|
+
deserialize: (v) => Buffer.from(v),
|
|
524
|
+
},
|
|
525
|
+
'buffer',
|
|
526
|
+
);
|
|
527
|
+
|
|
528
|
+
const jsonStringify = (data: any) => {
|
|
529
|
+
return JSON.stringify(data, (_key, value) => {
|
|
530
|
+
if (typeof value === 'bigint') {
|
|
531
|
+
return value.toString();
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Convert Buffer and ArrayBuffer to base64
|
|
535
|
+
if (
|
|
536
|
+
(value
|
|
537
|
+
&& typeof value === 'object'
|
|
538
|
+
&& 'type' in value
|
|
539
|
+
&& 'data' in value
|
|
540
|
+
&& value.type === 'Buffer')
|
|
541
|
+
|| value instanceof ArrayBuffer
|
|
542
|
+
|| value instanceof Buffer
|
|
543
|
+
) {
|
|
544
|
+
return Buffer.from(value).toString('base64');
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return value;
|
|
548
|
+
});
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
export type Server = {
|
|
552
|
+
start: (params: {
|
|
553
|
+
host: string;
|
|
554
|
+
port: number;
|
|
555
|
+
key?: string;
|
|
556
|
+
cert?: string;
|
|
557
|
+
cb: (err: Error | null, address: string) => void;
|
|
558
|
+
}) => void;
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
export const prepareServer = async (
|
|
562
|
+
{
|
|
563
|
+
dialect,
|
|
564
|
+
driver,
|
|
565
|
+
proxy,
|
|
566
|
+
customDefaults,
|
|
567
|
+
schema: drizzleSchema,
|
|
568
|
+
relations,
|
|
569
|
+
dbHash,
|
|
570
|
+
schemaFiles,
|
|
571
|
+
}: Setup,
|
|
572
|
+
app?: Hono,
|
|
573
|
+
): Promise<Server> => {
|
|
574
|
+
app = app !== undefined ? app : new Hono();
|
|
575
|
+
|
|
576
|
+
app.use(compress());
|
|
577
|
+
app.use(async (ctx, next) => {
|
|
578
|
+
await next();
|
|
579
|
+
// * https://wicg.github.io/private-network-access/#headers
|
|
580
|
+
// * https://github.com/drizzle-team/drizzle-orm/issues/1857#issuecomment-2395724232
|
|
581
|
+
ctx.header('Access-Control-Allow-Private-Network', 'true');
|
|
582
|
+
});
|
|
583
|
+
app.use(cors());
|
|
584
|
+
app.onError((err, ctx) => {
|
|
585
|
+
console.error(err);
|
|
586
|
+
return ctx.json({
|
|
587
|
+
status: 'error',
|
|
588
|
+
error: err.message,
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
|
|
592
|
+
const relationalSchema: Record<string, unknown> = {
|
|
593
|
+
...Object.fromEntries(
|
|
594
|
+
Object.entries(drizzleSchema)
|
|
595
|
+
.map(([schemaName, schema]) => {
|
|
596
|
+
// have unique keys across schemas
|
|
597
|
+
const mappedTableEntries = Object.entries(schema).map(
|
|
598
|
+
([tableName, table]) => {
|
|
599
|
+
return [`__${schemaName}__.${tableName}`, table];
|
|
600
|
+
},
|
|
601
|
+
);
|
|
602
|
+
|
|
603
|
+
return mappedTableEntries;
|
|
604
|
+
})
|
|
605
|
+
.flat(),
|
|
606
|
+
),
|
|
607
|
+
...relations,
|
|
608
|
+
};
|
|
609
|
+
|
|
610
|
+
const relationsConfig = extractTablesRelationalConfig(
|
|
611
|
+
relationalSchema,
|
|
612
|
+
createTableRelationsHelpers,
|
|
613
|
+
);
|
|
614
|
+
|
|
615
|
+
app.post('/', zValidator('json', schema), async (c) => {
|
|
616
|
+
const body = c.req.valid('json');
|
|
617
|
+
const { type } = body;
|
|
618
|
+
|
|
619
|
+
if (type === 'init') {
|
|
620
|
+
const preparedDefaults = customDefaults.map((d) => ({
|
|
621
|
+
schema: d.schema,
|
|
622
|
+
table: d.table,
|
|
623
|
+
column: d.column,
|
|
624
|
+
}));
|
|
625
|
+
|
|
626
|
+
return c.json({
|
|
627
|
+
version: '6',
|
|
628
|
+
dialect,
|
|
629
|
+
driver,
|
|
630
|
+
schemaFiles,
|
|
631
|
+
customDefaults: preparedDefaults,
|
|
632
|
+
relations: extractRelations(relationsConfig),
|
|
633
|
+
dbHash,
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (type === 'proxy') {
|
|
638
|
+
const result = await proxy({
|
|
639
|
+
...body.data,
|
|
640
|
+
params: body.data.params || [],
|
|
641
|
+
});
|
|
642
|
+
return c.json(JSON.parse(jsonStringify(result)));
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (type === 'defaults') {
|
|
646
|
+
const columns = body.data;
|
|
647
|
+
|
|
648
|
+
const result = columns.map((column) => {
|
|
649
|
+
const found = customDefaults.find((d) => {
|
|
650
|
+
return (
|
|
651
|
+
d.schema === column.schema
|
|
652
|
+
&& d.table === column.table
|
|
653
|
+
&& d.column === column.column
|
|
654
|
+
);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
if (!found) {
|
|
658
|
+
throw new Error(
|
|
659
|
+
`Custom default not found for ${column.schema}.${column.table}.${column.column}`,
|
|
660
|
+
);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
const value = found.func();
|
|
664
|
+
|
|
665
|
+
return {
|
|
666
|
+
...column,
|
|
667
|
+
value,
|
|
668
|
+
};
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
return c.json(JSON.parse(jsonStringify(result)));
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
throw new Error(`Unknown type: ${type}`);
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
return {
|
|
678
|
+
start: (params: Parameters<Server['start']>[0]) => {
|
|
679
|
+
serve(
|
|
680
|
+
{
|
|
681
|
+
fetch: app!.fetch,
|
|
682
|
+
createServer: params.key ? createServer : undefined,
|
|
683
|
+
hostname: params.host,
|
|
684
|
+
port: params.port,
|
|
685
|
+
serverOptions: {
|
|
686
|
+
key: params.key,
|
|
687
|
+
cert: params.cert,
|
|
688
|
+
},
|
|
689
|
+
},
|
|
690
|
+
() => params.cb(null, `${params.host}:${params.port}`),
|
|
691
|
+
);
|
|
692
|
+
},
|
|
693
|
+
};
|
|
694
|
+
};
|