@anthonyalayo/drizzle-kit 1.0.2 → 1.0.5
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/api.d.mts +2891 -0
- package/api.d.ts +2891 -0
- package/api.js +47534 -0
- package/api.mjs +47521 -0
- package/bin.cjs +80043 -0
- package/{src/index.ts → index-Z-1TKnbX.d.mts} +123 -141
- package/index-Z-1TKnbX.d.ts +308 -0
- package/index.d.mts +2 -0
- package/index.d.ts +2 -0
- package/index.js +32 -0
- package/index.mjs +7 -0
- package/package.json +2 -2
- package/utils.js +6056 -0
- package/utils.mjs +6032 -0
- package/.gitignore +0 -25
- package/build.dev.ts +0 -51
- package/build.ts +0 -112
- package/imports-checker/analyze.ts +0 -78
- package/imports-checker/checker.ts +0 -296
- package/imports-checker/grammar/grammar.ohm +0 -121
- package/imports-checker/grammar/grammar.ohm-bundle.d.ts +0 -164
- package/imports-checker/grammar/grammar.ohm-bundle.js +0 -753
- package/imports-checker/index.ts +0 -48
- package/patches/difflib@0.2.4.patch +0 -31
- package/src/@types/utils.ts +0 -58
- package/src/api.ts +0 -504
- package/src/cli/commands/_es5.ts +0 -2
- package/src/cli/commands/check.ts +0 -52
- package/src/cli/commands/drop.ts +0 -60
- package/src/cli/commands/introspect.ts +0 -772
- package/src/cli/commands/libSqlPushUtils.ts +0 -355
- package/src/cli/commands/migrate.ts +0 -1526
- package/src/cli/commands/mysqlIntrospect.ts +0 -53
- package/src/cli/commands/mysqlPushUtils.ts +0 -352
- package/src/cli/commands/mysqlUp.ts +0 -102
- package/src/cli/commands/pgIntrospect.ts +0 -55
- package/src/cli/commands/pgPushUtils.ts +0 -269
- package/src/cli/commands/pgUp.ts +0 -217
- package/src/cli/commands/push.ts +0 -637
- package/src/cli/commands/singlestoreIntrospect.ts +0 -53
- package/src/cli/commands/singlestorePushUtils.ts +0 -456
- package/src/cli/commands/singlestoreUp.ts +0 -1
- package/src/cli/commands/sqliteIntrospect.ts +0 -96
- package/src/cli/commands/sqlitePushUtils.ts +0 -322
- package/src/cli/commands/sqliteUp.ts +0 -52
- package/src/cli/commands/utils.ts +0 -858
- package/src/cli/connections.ts +0 -914
- package/src/cli/index.ts +0 -48
- package/src/cli/schema.ts +0 -794
- package/src/cli/selector-ui.ts +0 -38
- package/src/cli/utils.ts +0 -112
- package/src/cli/validations/cli.ts +0 -79
- package/src/cli/validations/common.ts +0 -193
- package/src/cli/validations/libsql.ts +0 -27
- package/src/cli/validations/mysql.ts +0 -61
- package/src/cli/validations/outputs.ts +0 -91
- package/src/cli/validations/postgres.ts +0 -83
- package/src/cli/validations/singlestore.ts +0 -61
- package/src/cli/validations/sqlite.ts +0 -104
- package/src/cli/validations/studio.ts +0 -24
- package/src/cli/views.ts +0 -658
- package/src/extensions/getTablesFilterByExtensions.ts +0 -16
- package/src/extensions/vector.ts +0 -10
- package/src/global.ts +0 -61
- package/src/introspect-mysql.ts +0 -1030
- package/src/introspect-pg.ts +0 -1435
- package/src/introspect-singlestore.ts +0 -913
- package/src/introspect-sqlite.ts +0 -533
- package/src/jsonDiffer.js +0 -882
- package/src/jsonStatements.ts +0 -3590
- package/src/loader.mjs +0 -57
- package/src/migrationPreparator.ts +0 -217
- package/src/schemaValidator.ts +0 -23
- package/src/serializer/index.ts +0 -142
- package/src/serializer/mysqlImports.ts +0 -38
- package/src/serializer/mysqlSchema.ts +0 -423
- package/src/serializer/mysqlSerializer.ts +0 -999
- package/src/serializer/pgImports.ts +0 -106
- package/src/serializer/pgSchema.ts +0 -937
- package/src/serializer/pgSerializer.ts +0 -2223
- package/src/serializer/singlestoreImports.ts +0 -38
- package/src/serializer/singlestoreSchema.ts +0 -257
- package/src/serializer/singlestoreSerializer.ts +0 -767
- package/src/serializer/sqliteImports.ts +0 -41
- package/src/serializer/sqliteSchema.ts +0 -352
- package/src/serializer/sqliteSerializer.ts +0 -931
- package/src/serializer/studio.ts +0 -694
- package/src/serializer/utils.ts +0 -45
- package/src/simulator.ts +0 -157
- package/src/snapshotsDiffer.ts +0 -4398
- package/src/sqlgenerator.ts +0 -4212
- package/src/statementCombiner.ts +0 -597
- package/src/utils/certs.ts +0 -44
- package/src/utils/words.ts +0 -1333
- package/src/utils.ts +0 -370
- package/tests/bin.test.ts +0 -58
- package/tests/cli/d1http.config.ts +0 -12
- package/tests/cli/drizzle.config.ts +0 -9
- package/tests/cli/durable-sqlite.config.ts +0 -7
- package/tests/cli/expo.config.ts +0 -7
- package/tests/cli/postgres.config.ts +0 -13
- package/tests/cli/postgres2.config.ts +0 -17
- package/tests/cli/schema.ts +0 -1
- package/tests/cli/turso.config.ts +0 -10
- package/tests/cli-export.test.ts +0 -79
- package/tests/cli-generate.test.ts +0 -259
- package/tests/cli-migrate.test.ts +0 -103
- package/tests/cli-push.test.ts +0 -126
- package/tests/common.ts +0 -15
- package/tests/indexes/common.ts +0 -21
- package/tests/indexes/pg.test.ts +0 -245
- package/tests/introspect/libsql.test.ts +0 -35
- package/tests/introspect/mysql.test.ts +0 -319
- package/tests/introspect/pg.test.ts +0 -894
- package/tests/introspect/singlestore.test.ts +0 -275
- package/tests/introspect/sqlite.test.ts +0 -122
- package/tests/libsql-checks.test.ts +0 -308
- package/tests/libsql-statements.test.ts +0 -989
- package/tests/libsql-views.test.ts +0 -218
- package/tests/migrate/libsq-schema.ts +0 -6
- package/tests/migrate/libsql-migrate.test.ts +0 -58
- package/tests/migrate/migrations/0000_little_blizzard.sql +0 -4
- package/tests/migrate/migrations/0001_nebulous_storm.sql +0 -10
- package/tests/migrate/migrations/meta/0000_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/0001_snapshot.json +0 -40
- package/tests/migrate/migrations/meta/_journal.json +0 -20
- package/tests/mysql-checks.test.ts +0 -291
- package/tests/mysql-generated.test.ts +0 -1290
- package/tests/mysql-schemas.test.ts +0 -156
- package/tests/mysql-views.test.ts +0 -553
- package/tests/mysql.test.ts +0 -863
- package/tests/pg-array.test.ts +0 -368
- package/tests/pg-checks.test.ts +0 -329
- package/tests/pg-columns.test.ts +0 -484
- package/tests/pg-domains.test.ts +0 -306
- package/tests/pg-enums.test.ts +0 -751
- package/tests/pg-generated.test.ts +0 -529
- package/tests/pg-identity.test.ts +0 -447
- package/tests/pg-schemas.test.ts +0 -105
- package/tests/pg-sequences.test.ts +0 -298
- package/tests/pg-tables.test.ts +0 -1021
- package/tests/pg-views.test.ts +0 -1929
- package/tests/push/common.ts +0 -58
- package/tests/push/libsql.test.ts +0 -1400
- package/tests/push/mysql-push.test.ts +0 -345
- package/tests/push/mysql.test.ts +0 -788
- package/tests/push/pg.test.ts +0 -4341
- package/tests/push/singlestore-push.test.ts +0 -894
- package/tests/push/singlestore.test.ts +0 -444
- package/tests/push/sqlite.test.ts +0 -1613
- package/tests/rls/pg-policy.test.ts +0 -1656
- package/tests/rls/pg-role.test.ts +0 -234
- package/tests/schemaDiffer.ts +0 -2852
- package/tests/singlestore-generated.test.ts +0 -1290
- package/tests/singlestore-schemas.test.ts +0 -155
- package/tests/singlestore.test.ts +0 -978
- package/tests/sqlite-checks.test.ts +0 -308
- package/tests/sqlite-columns.test.ts +0 -1049
- package/tests/sqlite-generated.test.ts +0 -1726
- package/tests/sqlite-tables.test.ts +0 -637
- package/tests/sqlite-views.test.ts +0 -218
- package/tests/statements-combiner/libsql-statements-combiner.test.ts +0 -1812
- package/tests/statements-combiner/singlestore-statements-combiner.test.ts +0 -882
- package/tests/statements-combiner/sqlite-statements-combiner.test.ts +0 -1211
- package/tests/test/sqlite.test.ts +0 -39
- package/tests/testsinglestore.ts +0 -29
- package/tests/validations.test.ts +0 -869
- package/tests/wrap-param.test.ts +0 -16
- package/tsconfig.build.json +0 -4
- package/tsconfig.cli-types.json +0 -9
- package/tsconfig.json +0 -28
- package/vitest.config.ts +0 -34
package/src/serializer/studio.ts
DELETED
|
@@ -1,694 +0,0 @@
|
|
|
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
|
-
};
|