-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.ts
428 lines (372 loc) · 14.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import { common, exports, functions, imports, object, variables } from '@sveltejs/cli-core/js';
import { defineAddon, defineAddonOptions, dedent, type OptionValues } from '@sveltejs/cli-core';
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
import { getNodeTypesVersion } from '../common.ts';
const PORTS = {
mysql: '3306',
postgresql: '5432',
sqlite: ''
} as const;
const options = defineAddonOptions({
database: {
question: 'Which database would you like to use?',
type: 'select',
default: 'sqlite',
options: [
{ value: 'postgresql', label: 'PostgreSQL' },
{ value: 'mysql', label: 'MySQL' },
{ value: 'sqlite', label: 'SQLite' }
]
},
postgresql: {
question: 'Which PostgreSQL client would you like to use?',
type: 'select',
group: 'client',
default: 'postgres.js',
options: [
{ value: 'postgres.js', label: 'Postgres.JS', hint: 'recommended for most users' },
{ value: 'neon', label: 'Neon', hint: 'popular hosted platform' }
],
condition: ({ database }) => database === 'postgresql'
},
mysql: {
question: 'Which MySQL client would you like to use?',
type: 'select',
group: 'client',
default: 'mysql2',
options: [
{ value: 'mysql2', hint: 'recommended for most users' },
{ value: 'planetscale', label: 'PlanetScale', hint: 'popular hosted platform' }
],
condition: ({ database }) => database === 'mysql'
},
sqlite: {
question: 'Which SQLite client would you like to use?',
type: 'select',
group: 'client',
default: 'libsql',
options: [
{ value: 'better-sqlite3', hint: 'for traditional Node environments' },
{ value: 'libsql', label: 'libSQL', hint: 'for serverless environments' },
{ value: 'turso', label: 'Turso', hint: 'popular hosted platform' }
],
condition: ({ database }) => database === 'sqlite'
},
docker: {
question: 'Do you want to run the database locally with docker-compose?',
default: false,
type: 'boolean',
condition: ({ database, mysql, postgresql }) =>
(database === 'mysql' && mysql === 'mysql2') ||
(database === 'postgresql' && postgresql === 'postgres.js')
}
});
export default defineAddon({
id: 'drizzle',
shortDescription: 'database orm',
homepage: 'https://orm.drizzle.team',
options,
setup: ({ kit, unsupported }) => {
if (!kit) unsupported('Requires SvelteKit');
},
run: ({ sv, typescript, options, kit }) => {
const ext = typescript ? 'ts' : 'js';
sv.dependency('drizzle-orm', '^0.40.0');
sv.devDependency('drizzle-kit', '^0.30.2');
sv.devDependency('@types/node', getNodeTypesVersion());
// MySQL
if (options.mysql === 'mysql2') sv.dependency('mysql2', '^3.12.0');
if (options.mysql === 'planetscale') sv.dependency('@planetscale/database', '^1.19.0');
// PostgreSQL
if (options.postgresql === 'neon') sv.dependency('@neondatabase/serverless', '^0.10.4');
if (options.postgresql === 'postgres.js') sv.dependency('postgres', '^3.4.5');
// SQLite
if (options.sqlite === 'better-sqlite3') {
sv.dependency('better-sqlite3', '^11.8.0');
sv.devDependency('@types/better-sqlite3', '^7.6.12');
sv.pnpmBuildDependendency('better-sqlite3');
}
if (options.sqlite === 'libsql' || options.sqlite === 'turso')
sv.dependency('@libsql/client', '^0.14.0');
sv.file('.env', (content) => generateEnvFileContent(content, options));
sv.file('.env.example', (content) => generateEnvFileContent(content, options));
if (options.docker && (options.mysql === 'mysql2' || options.postgresql === 'postgres.js')) {
sv.file('docker-compose.yml', (content) => {
// if the file already exists, don't modify it
// (in the future, we could add some tooling for modifying yaml)
if (content.length > 0) return content;
const imageName = options.database === 'mysql' ? 'mysql' : 'postgres';
const port = PORTS[options.database];
const USER = 'root';
const PASSWORD = 'mysecretpassword';
const DB_NAME = 'local';
let dbSpecificContent = '';
if (options.mysql === 'mysql2') {
dbSpecificContent = `
MYSQL_ROOT_PASSWORD: ${PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
volumes:
- mysqldata:/var/lib/mysql
volumes:
mysqldata:
`;
}
if (options.postgresql === 'postgres.js') {
dbSpecificContent = `
POSTGRES_USER: ${USER}
POSTGRES_PASSWORD: ${PASSWORD}
POSTGRES_DB: ${DB_NAME}
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
`;
}
content = dedent`
services:
db:
image: ${imageName}
restart: always
ports:
- ${port}:${port}
environment: ${dbSpecificContent}
`;
return content;
});
}
sv.file('package.json', (content) => {
const { data, generateCode } = parseJson(content);
data.scripts ??= {};
const scripts: Record<string, string> = data.scripts;
if (options.docker) scripts['db:start'] ??= 'docker compose up';
scripts['db:push'] ??= 'drizzle-kit push';
scripts['db:migrate'] ??= 'drizzle-kit migrate';
scripts['db:studio'] ??= 'drizzle-kit studio';
return generateCode();
});
if (options.database === 'sqlite') {
sv.file('.gitignore', (content) => {
// Adds the db file to the gitignore if an ignore is present
if (content.length === 0) return content;
if (!content.includes('\n*.db')) {
content = content.trimEnd() + '\n\n# SQLite\n*.db';
}
return content;
});
}
sv.file(`drizzle.config.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);
imports.addNamed(ast, 'drizzle-kit', { defineConfig: 'defineConfig' });
const envCheckStatement = common.statementFromString(
"if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');"
);
common.addStatement(ast, envCheckStatement);
const fallback = common.expressionFromString('defineConfig({})');
const { value: exportDefault } = exports.defaultExport(ast, fallback);
if (exportDefault.type !== 'CallExpression') return content;
const objExpression = exportDefault.arguments?.[0];
if (!objExpression || objExpression.type !== 'ObjectExpression') return content;
const authToken =
options.sqlite === 'turso'
? common.expressionFromString('process.env.DATABASE_AUTH_TOKEN')
: undefined;
object.properties(objExpression, {
schema: common.createLiteral(`./src/lib/server/db/schema.${typescript ? 'ts' : 'js'}`),
dbCredentials: object.create({
url: common.expressionFromString('process.env.DATABASE_URL'),
authToken
}),
verbose: { type: 'Literal', value: true },
strict: { type: 'Literal', value: true }
});
const dialect = options.sqlite === 'turso' ? 'turso' : options.database;
object.overrideProperties(objExpression, {
dialect: common.createLiteral(dialect)
});
// The `driver` property is only required for _some_ sqlite DBs.
// We'll need to remove it if it's anything but sqlite
if (options.database !== 'sqlite') object.removeProperty(objExpression, 'driver');
return generateCode();
});
sv.file(`${kit?.libDirectory}/server/db/schema.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);
let userSchemaExpression;
if (options.database === 'sqlite') {
imports.addNamed(ast, 'drizzle-orm/sqlite-core', {
sqliteTable: 'sqliteTable',
text: 'text',
integer: 'integer'
});
userSchemaExpression = common.expressionFromString(`sqliteTable('user', {
id: integer('id').primaryKey(),
age: integer('age')
})`);
}
if (options.database === 'mysql') {
imports.addNamed(ast, 'drizzle-orm/mysql-core', {
mysqlTable: 'mysqlTable',
serial: 'serial',
text: 'text',
int: 'int'
});
userSchemaExpression = common.expressionFromString(`mysqlTable('user', {
id: serial('id').primaryKey(),
age: int('age'),
})`);
}
if (options.database === 'postgresql') {
imports.addNamed(ast, 'drizzle-orm/pg-core', {
pgTable: 'pgTable',
serial: 'serial',
text: 'text',
integer: 'integer'
});
userSchemaExpression = common.expressionFromString(`pgTable('user', {
id: serial('id').primaryKey(),
age: integer('age'),
})`);
}
if (!userSchemaExpression) throw new Error('unreachable state...');
const userIdentifier = variables.declaration(ast, 'const', 'user', userSchemaExpression);
exports.namedExport(ast, 'user', userIdentifier);
return generateCode();
});
sv.file(`${kit?.libDirectory}/server/db/index.${ext}`, (content) => {
const { ast, generateCode } = parseScript(content);
imports.addNamed(ast, '$env/dynamic/private', { env: 'env' });
imports.addNamespace(ast, './schema', 'schema');
// env var checks
const dbURLCheck = common.statementFromString(
"if (!env.DATABASE_URL) throw new Error('DATABASE_URL is not set');"
);
common.addStatement(ast, dbURLCheck);
let clientExpression;
// SQLite
if (options.sqlite === 'better-sqlite3') {
imports.addDefault(ast, 'better-sqlite3', 'Database');
imports.addNamed(ast, 'drizzle-orm/better-sqlite3', { drizzle: 'drizzle' });
clientExpression = common.expressionFromString('new Database(env.DATABASE_URL)');
}
if (options.sqlite === 'libsql' || options.sqlite === 'turso') {
imports.addNamed(ast, '@libsql/client', { createClient: 'createClient' });
imports.addNamed(ast, 'drizzle-orm/libsql', { drizzle: 'drizzle' });
if (options.sqlite === 'turso') {
imports.addNamed(ast, '$app/environment', { dev: 'dev' });
// auth token check in prod
const authTokenCheck = common.statementFromString(
"if (!dev && !env.DATABASE_AUTH_TOKEN) throw new Error('DATABASE_AUTH_TOKEN is not set');"
);
common.addStatement(ast, authTokenCheck);
clientExpression = common.expressionFromString(
'createClient({ url: env.DATABASE_URL, authToken: env.DATABASE_AUTH_TOKEN })'
);
} else {
clientExpression = common.expressionFromString('createClient({ url: env.DATABASE_URL })');
}
}
// MySQL
if (options.mysql === 'mysql2' || options.mysql === 'planetscale') {
imports.addDefault(ast, 'mysql2/promise', 'mysql');
imports.addNamed(ast, 'drizzle-orm/mysql2', { drizzle: 'drizzle' });
clientExpression = common.expressionFromString('mysql.createPool(env.DATABASE_URL)');
}
// PostgreSQL
if (options.postgresql === 'neon') {
imports.addNamed(ast, '@neondatabase/serverless', { neon: 'neon' });
imports.addNamed(ast, 'drizzle-orm/neon-http', { drizzle: 'drizzle' });
clientExpression = common.expressionFromString('neon(env.DATABASE_URL)');
}
if (options.postgresql === 'postgres.js') {
imports.addDefault(ast, 'postgres', 'postgres');
imports.addNamed(ast, 'drizzle-orm/postgres-js', { drizzle: 'drizzle' });
clientExpression = common.expressionFromString('postgres(env.DATABASE_URL)');
}
if (!clientExpression) throw new Error('unreachable state...');
const clientIdentifier = variables.declaration(ast, 'const', 'client', clientExpression);
common.addStatement(ast, clientIdentifier);
// create drizzle function call
const drizzleCall = functions.callByIdentifier('drizzle', ['client']);
// add schema to support `db.query`
const paramObject = object.create({
schema: variables.identifier('schema')
});
if (options.database === 'mysql') {
const mode = options.mysql === 'planetscale' ? 'planetscale' : 'default';
object.property(paramObject, 'mode', common.createLiteral(mode));
}
drizzleCall.arguments.push(paramObject);
// create `db` export
const db = variables.declaration(ast, 'const', 'db', drizzleCall);
exports.namedExport(ast, 'db', db);
return generateCode();
});
},
nextSteps: ({ options, highlighter, packageManager }) => {
const steps = [
`You will need to set ${highlighter.env('DATABASE_URL')} in your production environment`
];
if (options.docker) {
steps.push(
`Run ${highlighter.command(`${packageManager} run db:start`)} to start the docker container`
);
} else {
steps.push(
`Check ${highlighter.env('DATABASE_URL')} in ${highlighter.path('.env')} and adjust it to your needs`
);
}
steps.push(
`Run ${highlighter.command(`${packageManager} run db:push`)} to update your database schema`
);
return steps;
}
});
function generateEnvFileContent(content: string, opts: OptionValues<typeof options>) {
const DB_URL_KEY = 'DATABASE_URL';
if (opts.docker) {
// we'll prefill with the default docker db credentials
const protocol = opts.database === 'mysql' ? 'mysql' : 'postgres';
const port = PORTS[opts.database];
content = addEnvVar(
content,
DB_URL_KEY,
`"${protocol}://root:mysecretpassword@localhost:${port}/local"`
);
return content;
}
if (opts.sqlite === 'better-sqlite3' || opts.sqlite === 'libsql') {
const dbFile = opts.sqlite === 'libsql' ? 'file:local.db' : 'local.db';
content = addEnvVar(content, DB_URL_KEY, dbFile);
return content;
}
content = addEnvComment(content, 'Replace with your DB credentials!');
if (opts.sqlite === 'turso') {
content = addEnvVar(content, DB_URL_KEY, '"libsql://db-name-user.turso.io"');
content = addEnvVar(content, 'DATABASE_AUTH_TOKEN', '""');
content = addEnvComment(content, 'A local DB can also be used in dev as well');
content = addEnvComment(content, `${DB_URL_KEY}="file:local.db"`);
}
if (opts.database === 'mysql') {
content = addEnvVar(content, DB_URL_KEY, '"mysql://user:password@host:port/db-name"');
}
if (opts.database === 'postgresql') {
content = addEnvVar(content, DB_URL_KEY, '"postgres://user:password@host:port/db-name"');
}
return content;
}
function addEnvVar(content: string, key: string, value: string) {
if (!content.includes(key + '=')) {
content = appendEnvContent(content, `${key}=${value}`);
}
return content;
}
function addEnvComment(content: string, comment: string) {
const commented = `# ${comment}`;
if (!content.includes(commented)) {
content = appendEnvContent(content, commented);
}
return content;
}
function appendEnvContent(existing: string, content: string) {
const withNewLine = !existing.length || existing.endsWith('\n') ? existing : existing + '\n';
return withNewLine + content + '\n';
}