@3lineas/d1-orm 1.0.7 → 1.0.8
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/README.md +41 -148
- package/dist/chunk-N3G6NOJP.js +127 -0
- package/dist/cli/index.cjs +781 -0
- package/dist/cli/index.js +244 -254
- package/dist/{index.mjs → index.cjs} +159 -14
- package/dist/{index.d.mts → index.d.cts} +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -157
- package/package.json +2 -2
- package/dist/chunk-5BBZKUNZ.mjs +0 -147
- package/dist/cli/index.mjs +0 -559
- /package/dist/cli/{index.d.mts → index.d.cts} +0 -0
package/dist/cli/index.mjs
DELETED
|
@@ -1,559 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
Database,
|
|
4
|
-
__commonJS,
|
|
5
|
-
__esm,
|
|
6
|
-
init_database
|
|
7
|
-
} from "../chunk-5BBZKUNZ.mjs";
|
|
8
|
-
|
|
9
|
-
// src/cli/commands/init.ts
|
|
10
|
-
import * as fs from "fs";
|
|
11
|
-
import * as path from "path";
|
|
12
|
-
import * as readline from "readline";
|
|
13
|
-
async function init() {
|
|
14
|
-
const rl = readline.createInterface({
|
|
15
|
-
input: process.stdin,
|
|
16
|
-
output: process.stdout
|
|
17
|
-
});
|
|
18
|
-
const question = (query) => {
|
|
19
|
-
return new Promise((resolve) => {
|
|
20
|
-
rl.question(query, resolve);
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
console.log("Initializing D1 ORM...");
|
|
24
|
-
const defaultModelsPath = "src/models";
|
|
25
|
-
const userModelsPath = await question(
|
|
26
|
-
`Where would you like to install your models? (default: ${defaultModelsPath}): `
|
|
27
|
-
) || defaultModelsPath;
|
|
28
|
-
rl.close();
|
|
29
|
-
const folders = [userModelsPath, "database/migrations", "database/seeders"];
|
|
30
|
-
folders.forEach((folder) => {
|
|
31
|
-
const fullPath = path.join(process.cwd(), folder);
|
|
32
|
-
if (!fs.existsSync(fullPath)) {
|
|
33
|
-
fs.mkdirSync(fullPath, { recursive: true });
|
|
34
|
-
console.log(`Created directory: ${folder}`);
|
|
35
|
-
} else {
|
|
36
|
-
console.log(`Directory already exists: ${folder}`);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
const userModelContent = `import { Model } from '@3lineas/d1-orm';
|
|
40
|
-
|
|
41
|
-
export class User extends Model {
|
|
42
|
-
// protected static table = 'users';
|
|
43
|
-
|
|
44
|
-
declare id: number;
|
|
45
|
-
declare name: string;
|
|
46
|
-
declare email: string;
|
|
47
|
-
declare password?: string;
|
|
48
|
-
declare created_at: string;
|
|
49
|
-
declare updated_at: string;
|
|
50
|
-
}
|
|
51
|
-
`;
|
|
52
|
-
const userModelPath = path.join(process.cwd(), userModelsPath, "User.ts");
|
|
53
|
-
if (!fs.existsSync(userModelPath)) {
|
|
54
|
-
fs.writeFileSync(userModelPath, userModelContent);
|
|
55
|
-
console.log(`Created model: ${userModelPath}`);
|
|
56
|
-
}
|
|
57
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").split(".")[0].replace("T", "_");
|
|
58
|
-
const migrationName = `${timestamp}_create_users_table.ts`;
|
|
59
|
-
const migrationContent = `import { Blueprint, Schema } from '@3lineas/d1-orm';
|
|
60
|
-
|
|
61
|
-
export const up = async () => {
|
|
62
|
-
return Schema.create('users', (table: Blueprint) => {
|
|
63
|
-
table.id();
|
|
64
|
-
table.string('name');
|
|
65
|
-
table.string('email').unique();
|
|
66
|
-
table.string('password').nullable();
|
|
67
|
-
table.timestamps();
|
|
68
|
-
});
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
export const down = async () => {
|
|
72
|
-
return Schema.dropIfExists('users');
|
|
73
|
-
};
|
|
74
|
-
`;
|
|
75
|
-
const migrationPath = path.join(
|
|
76
|
-
process.cwd(),
|
|
77
|
-
"database/migrations",
|
|
78
|
-
migrationName
|
|
79
|
-
);
|
|
80
|
-
if (!fs.existsSync(migrationPath)) {
|
|
81
|
-
fs.writeFileSync(migrationPath, migrationContent);
|
|
82
|
-
console.log(`Created migration: ${migrationPath}`);
|
|
83
|
-
}
|
|
84
|
-
const seederContent = `import { User } from '${path.relative(
|
|
85
|
-
path.join(process.cwd(), "database/seeders"),
|
|
86
|
-
path.join(process.cwd(), userModelsPath, "User")
|
|
87
|
-
).replace(/\\/g, "/")}.ts';
|
|
88
|
-
|
|
89
|
-
export const seed = async () => {
|
|
90
|
-
await User.create({
|
|
91
|
-
name: 'John Doe',
|
|
92
|
-
email: 'john@example.com',
|
|
93
|
-
password: 'password'
|
|
94
|
-
});
|
|
95
|
-
};
|
|
96
|
-
`;
|
|
97
|
-
const seederPath = path.join(
|
|
98
|
-
process.cwd(),
|
|
99
|
-
"database/seeders",
|
|
100
|
-
"UserSeeder.ts"
|
|
101
|
-
);
|
|
102
|
-
if (!fs.existsSync(seederPath)) {
|
|
103
|
-
fs.writeFileSync(seederPath, seederContent);
|
|
104
|
-
console.log(`Created seeder: ${seederPath}`);
|
|
105
|
-
}
|
|
106
|
-
const packageJsonPath = path.join(process.cwd(), "package.json");
|
|
107
|
-
if (fs.existsSync(packageJsonPath)) {
|
|
108
|
-
try {
|
|
109
|
-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
110
|
-
if (!packageJson.scripts) {
|
|
111
|
-
packageJson.scripts = {};
|
|
112
|
-
}
|
|
113
|
-
if (!packageJson.scripts.orm) {
|
|
114
|
-
packageJson.scripts.orm = "d1-orm";
|
|
115
|
-
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
116
|
-
console.log('Added "orm" script to package.json');
|
|
117
|
-
} else {
|
|
118
|
-
console.log('"orm" script already exists in package.json');
|
|
119
|
-
}
|
|
120
|
-
} catch (e) {
|
|
121
|
-
console.error("Failed to update package.json:", e);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
var init_init = __esm({
|
|
126
|
-
"src/cli/commands/init.ts"() {
|
|
127
|
-
"use strict";
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
// src/cli/commands/make-migration.ts
|
|
132
|
-
import * as fs2 from "fs";
|
|
133
|
-
import * as path2 from "path";
|
|
134
|
-
import * as p from "@clack/prompts";
|
|
135
|
-
async function makeMigration(name) {
|
|
136
|
-
let migrationName = name;
|
|
137
|
-
if (!migrationName) {
|
|
138
|
-
migrationName = await p.text({
|
|
139
|
-
message: "What should the migration be named?",
|
|
140
|
-
placeholder: "e.g. create_users_table",
|
|
141
|
-
validate: (value) => {
|
|
142
|
-
if (!value) return "Please enter a name.";
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
if (p.isCancel(migrationName)) {
|
|
146
|
-
p.cancel("Operation cancelled.");
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").split(".")[0].replace("T", "_");
|
|
151
|
-
const filename = `${timestamp}_${migrationName}.ts`;
|
|
152
|
-
const targetPath = path2.join(process.cwd(), "database/migrations", filename);
|
|
153
|
-
const template = `import { Blueprint, Schema } from '@3lineas/d1-orm';
|
|
154
|
-
|
|
155
|
-
export const up = async () => {
|
|
156
|
-
return Schema.create('${migrationName.replace("create_", "").replace("_table", "")}', (table: Blueprint) => {
|
|
157
|
-
table.id();
|
|
158
|
-
table.timestamps();
|
|
159
|
-
});
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
export const down = async () => {
|
|
163
|
-
return Schema.dropIfExists('${migrationName.replace("create_", "").replace("_table", "")}');
|
|
164
|
-
};
|
|
165
|
-
`;
|
|
166
|
-
if (!fs2.existsSync(path2.join(process.cwd(), "database/migrations"))) {
|
|
167
|
-
fs2.mkdirSync(path2.join(process.cwd(), "database/migrations"), {
|
|
168
|
-
recursive: true
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
if (fs2.existsSync(targetPath)) {
|
|
172
|
-
p.log.error(`Migration ${filename} already exists.`);
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
fs2.writeFileSync(targetPath, template);
|
|
176
|
-
p.log.success(`Created migration: database/migrations/${filename}`);
|
|
177
|
-
}
|
|
178
|
-
var init_make_migration = __esm({
|
|
179
|
-
"src/cli/commands/make-migration.ts"() {
|
|
180
|
-
"use strict";
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
// src/cli/commands/make-model.ts
|
|
185
|
-
import * as fs3 from "fs";
|
|
186
|
-
import * as path3 from "path";
|
|
187
|
-
import * as p2 from "@clack/prompts";
|
|
188
|
-
async function makeModel(name) {
|
|
189
|
-
let modelName = name;
|
|
190
|
-
if (!modelName) {
|
|
191
|
-
modelName = await p2.text({
|
|
192
|
-
message: "What should the model be named?",
|
|
193
|
-
placeholder: "e.g. Flight",
|
|
194
|
-
validate: (value) => {
|
|
195
|
-
if (!value) return "Please enter a name.";
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
if (p2.isCancel(modelName)) {
|
|
199
|
-
p2.cancel("Operation cancelled.");
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
const modelPath = await findModelsPath() || "src/models";
|
|
204
|
-
const filename = `${modelName}.ts`;
|
|
205
|
-
const targetPath = path3.join(process.cwd(), modelPath, filename);
|
|
206
|
-
const template = `import { Model } from '@3lineas/d1-orm';
|
|
207
|
-
|
|
208
|
-
export class ${modelName} extends Model {
|
|
209
|
-
// protected static table = '${modelName.toLowerCase()}s';
|
|
210
|
-
|
|
211
|
-
// declare id: number;
|
|
212
|
-
// declare created_at: string;
|
|
213
|
-
// declare updated_at: string;
|
|
214
|
-
}
|
|
215
|
-
`;
|
|
216
|
-
if (fs3.existsSync(targetPath)) {
|
|
217
|
-
p2.log.error(`Model ${filename} already exists at ${modelPath}.`);
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
if (!fs3.existsSync(path3.join(process.cwd(), modelPath))) {
|
|
221
|
-
fs3.mkdirSync(path3.join(process.cwd(), modelPath), { recursive: true });
|
|
222
|
-
}
|
|
223
|
-
fs3.writeFileSync(targetPath, template);
|
|
224
|
-
p2.log.success(`Created model: ${modelPath}/${filename}`);
|
|
225
|
-
const options = await p2.multiselect({
|
|
226
|
-
message: "Would you like to create any of the following?",
|
|
227
|
-
options: [
|
|
228
|
-
{ value: "migration", label: "Migration" },
|
|
229
|
-
{ value: "seeder", label: "Database Seeder" }
|
|
230
|
-
],
|
|
231
|
-
required: false
|
|
232
|
-
});
|
|
233
|
-
if (p2.isCancel(options)) {
|
|
234
|
-
p2.cancel("Operation cancelled.");
|
|
235
|
-
return;
|
|
236
|
-
}
|
|
237
|
-
if (options.includes("migration")) {
|
|
238
|
-
const migrationName = `create_${modelName.toLowerCase()}s_table`;
|
|
239
|
-
await makeMigration(migrationName);
|
|
240
|
-
}
|
|
241
|
-
if (options.includes("seeder")) {
|
|
242
|
-
await makeSeeder(modelName, modelPath);
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
async function findModelsPath() {
|
|
246
|
-
const packageJsonPath = path3.join(process.cwd(), "package.json");
|
|
247
|
-
if (!fs3.existsSync(packageJsonPath)) return null;
|
|
248
|
-
const commonPaths = ["src/models", "models", "app/Models"];
|
|
249
|
-
for (const p3 of commonPaths) {
|
|
250
|
-
if (fs3.existsSync(path3.join(process.cwd(), p3))) return p3;
|
|
251
|
-
}
|
|
252
|
-
return null;
|
|
253
|
-
}
|
|
254
|
-
async function makeSeeder(modelName, modelPath) {
|
|
255
|
-
const seederDir = path3.join(process.cwd(), "database/seeders");
|
|
256
|
-
const seederName = `${modelName}Seeder.ts`;
|
|
257
|
-
const targetPath = path3.join(seederDir, seederName);
|
|
258
|
-
if (!fs3.existsSync(seederDir)) {
|
|
259
|
-
fs3.mkdirSync(seederDir, { recursive: true });
|
|
260
|
-
}
|
|
261
|
-
const relativeModelPath = path3.relative(seederDir, path3.join(process.cwd(), modelPath, modelName)).replace(/\\/g, "/");
|
|
262
|
-
const template = `import { ${modelName} } from '${relativeModelPath}.ts';
|
|
263
|
-
|
|
264
|
-
export const seed = async () => {
|
|
265
|
-
// await ${modelName}.create({ ... });
|
|
266
|
-
};
|
|
267
|
-
`;
|
|
268
|
-
if (fs3.existsSync(targetPath)) {
|
|
269
|
-
p2.log.warn(`Seeder ${seederName} already exists.`);
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
fs3.writeFileSync(targetPath, template);
|
|
273
|
-
p2.log.success(`Created seeder: database/seeders/${seederName}`);
|
|
274
|
-
}
|
|
275
|
-
var init_make_model = __esm({
|
|
276
|
-
"src/cli/commands/make-model.ts"() {
|
|
277
|
-
"use strict";
|
|
278
|
-
init_make_migration();
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
// src/cli/cli-connection.ts
|
|
283
|
-
import { execSync } from "child_process";
|
|
284
|
-
var CLIConnection, CLIPREparedStatement;
|
|
285
|
-
var init_cli_connection = __esm({
|
|
286
|
-
"src/cli/cli-connection.ts"() {
|
|
287
|
-
"use strict";
|
|
288
|
-
CLIConnection = class {
|
|
289
|
-
constructor(dbName = "DB", isRemote = false) {
|
|
290
|
-
this.dbName = dbName;
|
|
291
|
-
this.isRemote = isRemote;
|
|
292
|
-
}
|
|
293
|
-
prepare(query) {
|
|
294
|
-
return new CLIPREparedStatement(query, this.dbName, this.isRemote);
|
|
295
|
-
}
|
|
296
|
-
async dump() {
|
|
297
|
-
throw new Error("dump() is not supported in CLI mode.");
|
|
298
|
-
}
|
|
299
|
-
async batch(statements) {
|
|
300
|
-
const results = [];
|
|
301
|
-
for (const stmt of statements) {
|
|
302
|
-
results.push(await stmt.run());
|
|
303
|
-
}
|
|
304
|
-
return results;
|
|
305
|
-
}
|
|
306
|
-
async exec(query) {
|
|
307
|
-
const start = Date.now();
|
|
308
|
-
await this.executeWrangler(query);
|
|
309
|
-
return {
|
|
310
|
-
count: 1,
|
|
311
|
-
// Approximation
|
|
312
|
-
duration: Date.now() - start
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
executeWrangler(sql) {
|
|
316
|
-
const flag = this.isRemote ? "--remote" : "--local";
|
|
317
|
-
const command = `npx wrangler d1 execute ${this.dbName} --command "${sql.replace(/"/g, '\\"')}" ${flag} --json`;
|
|
318
|
-
try {
|
|
319
|
-
const output = execSync(command, {
|
|
320
|
-
encoding: "utf-8",
|
|
321
|
-
stdio: ["ignore", "pipe", "inherit"]
|
|
322
|
-
});
|
|
323
|
-
const jsonStart = output.indexOf("[");
|
|
324
|
-
if (jsonStart !== -1) {
|
|
325
|
-
return JSON.parse(output.substring(jsonStart));
|
|
326
|
-
}
|
|
327
|
-
return null;
|
|
328
|
-
} catch (e) {
|
|
329
|
-
console.error(`Error executing wrangler command: ${command}`);
|
|
330
|
-
throw e;
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
};
|
|
334
|
-
CLIPREparedStatement = class {
|
|
335
|
-
constructor(query, dbName, isRemote) {
|
|
336
|
-
this.query = query;
|
|
337
|
-
this.dbName = dbName;
|
|
338
|
-
this.isRemote = isRemote;
|
|
339
|
-
}
|
|
340
|
-
bindings = [];
|
|
341
|
-
bind(...values) {
|
|
342
|
-
this.bindings = values;
|
|
343
|
-
return this;
|
|
344
|
-
}
|
|
345
|
-
resolveQuery() {
|
|
346
|
-
let sql = this.query;
|
|
347
|
-
this.bindings.forEach((value) => {
|
|
348
|
-
const formattedValue = typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value;
|
|
349
|
-
sql = sql.replace("?", String(formattedValue));
|
|
350
|
-
});
|
|
351
|
-
return sql;
|
|
352
|
-
}
|
|
353
|
-
async first(colName) {
|
|
354
|
-
const result = await this.all();
|
|
355
|
-
const firstRow = result.results[0] || null;
|
|
356
|
-
if (firstRow && colName) {
|
|
357
|
-
return firstRow[colName];
|
|
358
|
-
}
|
|
359
|
-
return firstRow;
|
|
360
|
-
}
|
|
361
|
-
async run() {
|
|
362
|
-
return this.all();
|
|
363
|
-
}
|
|
364
|
-
async all() {
|
|
365
|
-
const sql = this.resolveQuery();
|
|
366
|
-
const flag = this.isRemote ? "--remote" : "--local";
|
|
367
|
-
const command = `npx wrangler d1 execute ${this.dbName} --command "${sql.replace(/"/g, '\\"')}" ${flag} --json`;
|
|
368
|
-
try {
|
|
369
|
-
const output = execSync(command, {
|
|
370
|
-
encoding: "utf-8",
|
|
371
|
-
stdio: ["ignore", "pipe", "pipe"]
|
|
372
|
-
});
|
|
373
|
-
const jsonStart = output.indexOf("[");
|
|
374
|
-
if (jsonStart !== -1) {
|
|
375
|
-
const results = JSON.parse(output.substring(jsonStart));
|
|
376
|
-
const primaryResult = results[0];
|
|
377
|
-
return {
|
|
378
|
-
results: primaryResult.results || [],
|
|
379
|
-
success: primaryResult.success ?? true,
|
|
380
|
-
meta: primaryResult.meta || {}
|
|
381
|
-
};
|
|
382
|
-
}
|
|
383
|
-
return { results: [], success: true, meta: {} };
|
|
384
|
-
} catch (e) {
|
|
385
|
-
return { results: [], success: false, meta: {}, error: e.message };
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
async raw() {
|
|
389
|
-
const result = await this.all();
|
|
390
|
-
return result.results;
|
|
391
|
-
}
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
// src/cli/commands/seed.ts
|
|
397
|
-
import * as fs4 from "fs";
|
|
398
|
-
import * as path4 from "path";
|
|
399
|
-
async function seed(args) {
|
|
400
|
-
console.log("Seeding database...");
|
|
401
|
-
const seedersDir = path4.join(process.cwd(), "database/seeders");
|
|
402
|
-
if (!fs4.existsSync(seedersDir)) {
|
|
403
|
-
console.log("No seeders directory found.");
|
|
404
|
-
return;
|
|
405
|
-
}
|
|
406
|
-
const isRemote = args.includes("--remote");
|
|
407
|
-
const dbName = "DB";
|
|
408
|
-
Database.setup(new CLIConnection(dbName, isRemote));
|
|
409
|
-
const files = fs4.readdirSync(seedersDir).filter((f) => f.endsWith(".ts") || f.endsWith(".js")).sort();
|
|
410
|
-
for (const file of files) {
|
|
411
|
-
console.log(`Running seeder: ${file}`);
|
|
412
|
-
const filePath = path4.join(seedersDir, file);
|
|
413
|
-
try {
|
|
414
|
-
const seeder = await import(filePath);
|
|
415
|
-
if (seeder.seed) {
|
|
416
|
-
await seeder.seed();
|
|
417
|
-
console.log(`Seeder ${file} completed successfully.`);
|
|
418
|
-
} else {
|
|
419
|
-
console.warn(`Seeder ${file} does not export a seed function.`);
|
|
420
|
-
}
|
|
421
|
-
} catch (error) {
|
|
422
|
-
console.error(`Error running seeder ${file}:`, error);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
var init_seed = __esm({
|
|
427
|
-
"src/cli/commands/seed.ts"() {
|
|
428
|
-
"use strict";
|
|
429
|
-
init_database();
|
|
430
|
-
init_cli_connection();
|
|
431
|
-
}
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
// src/cli/commands/migrate.ts
|
|
435
|
-
import * as fs5 from "fs";
|
|
436
|
-
import * as path5 from "path";
|
|
437
|
-
import { execSync as execSync2 } from "child_process";
|
|
438
|
-
async function migrate(args) {
|
|
439
|
-
console.log("Running migrations...");
|
|
440
|
-
const migrationsDir = path5.join(process.cwd(), "database/migrations");
|
|
441
|
-
if (!fs5.existsSync(migrationsDir)) {
|
|
442
|
-
console.log("No migrations directory found.");
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
const files = fs5.readdirSync(migrationsDir).filter((f) => f.endsWith(".ts") || f.endsWith(".js")).sort();
|
|
446
|
-
for (const file of files) {
|
|
447
|
-
console.log(`Processing migration: ${file}`);
|
|
448
|
-
const filePath = path5.join(migrationsDir, file);
|
|
449
|
-
try {
|
|
450
|
-
const migration = await import(filePath);
|
|
451
|
-
if (migration.up) {
|
|
452
|
-
const sql = await migration.up();
|
|
453
|
-
if (sql) {
|
|
454
|
-
const isRemote = args.includes("--remote");
|
|
455
|
-
const dbName = "DB";
|
|
456
|
-
const command = isRemote ? "--remote" : "--local";
|
|
457
|
-
try {
|
|
458
|
-
const execCmd = `npx wrangler d1 execute ${dbName} --command "${sql.replace(/"/g, '\\"')}" ${command}`;
|
|
459
|
-
console.log(`Executing: ${execCmd}`);
|
|
460
|
-
execSync2(execCmd, { stdio: "inherit" });
|
|
461
|
-
} catch (e) {
|
|
462
|
-
console.error(`Failed to execute migration: ${file}`);
|
|
463
|
-
throw e;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
} catch (error) {
|
|
468
|
-
console.error(`Error processing migration ${file}:`, error);
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
if (args.includes("--seed")) {
|
|
472
|
-
await seed(args);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
var init_migrate = __esm({
|
|
476
|
-
"src/cli/commands/migrate.ts"() {
|
|
477
|
-
"use strict";
|
|
478
|
-
init_seed();
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
// src/cli/commands/migrate-fresh.ts
|
|
483
|
-
import { execSync as execSync3 } from "child_process";
|
|
484
|
-
async function migrateFresh(args) {
|
|
485
|
-
console.log("Dropping all tables...");
|
|
486
|
-
const isRemote = args.includes("--remote");
|
|
487
|
-
const dbName = "DB";
|
|
488
|
-
const flag = isRemote ? "--remote" : "--local";
|
|
489
|
-
try {
|
|
490
|
-
const listTablesCmd = `npx wrangler d1 execute ${dbName} --command "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_cf_%'" ${flag} --json`;
|
|
491
|
-
const output = execSync3(listTablesCmd, {
|
|
492
|
-
encoding: "utf-8",
|
|
493
|
-
stdio: ["ignore", "pipe", "inherit"]
|
|
494
|
-
});
|
|
495
|
-
const jsonStart = output.indexOf("[");
|
|
496
|
-
if (jsonStart !== -1) {
|
|
497
|
-
const results = JSON.parse(output.substring(jsonStart));
|
|
498
|
-
const tables = results[0]?.results || [];
|
|
499
|
-
if (tables.length > 0) {
|
|
500
|
-
const dropCommands = tables.map((t) => `DROP TABLE IF EXISTS ${t.name};`).join(" ");
|
|
501
|
-
const dropCmd = `npx wrangler d1 execute ${dbName} --command "${dropCommands}" ${flag}`;
|
|
502
|
-
console.log("Executing drop tables...");
|
|
503
|
-
execSync3(dropCmd, { stdio: "inherit" });
|
|
504
|
-
} else {
|
|
505
|
-
console.log("No tables found to drop.");
|
|
506
|
-
}
|
|
507
|
-
}
|
|
508
|
-
await migrate(args);
|
|
509
|
-
} catch (error) {
|
|
510
|
-
console.error("Error during migrate:fresh:", error);
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
var init_migrate_fresh = __esm({
|
|
514
|
-
"src/cli/commands/migrate-fresh.ts"() {
|
|
515
|
-
"use strict";
|
|
516
|
-
init_migrate();
|
|
517
|
-
}
|
|
518
|
-
});
|
|
519
|
-
|
|
520
|
-
// src/cli/index.ts
|
|
521
|
-
var require_cli = __commonJS({
|
|
522
|
-
"src/cli/index.ts"() {
|
|
523
|
-
init_init();
|
|
524
|
-
init_make_model();
|
|
525
|
-
init_make_migration();
|
|
526
|
-
init_migrate();
|
|
527
|
-
init_migrate_fresh();
|
|
528
|
-
init_seed();
|
|
529
|
-
var args = process.argv.slice(2);
|
|
530
|
-
var command = args[0];
|
|
531
|
-
var param = args[1];
|
|
532
|
-
switch (command) {
|
|
533
|
-
case "init":
|
|
534
|
-
init();
|
|
535
|
-
break;
|
|
536
|
-
case "make:model":
|
|
537
|
-
makeModel(param);
|
|
538
|
-
break;
|
|
539
|
-
case "make:migration":
|
|
540
|
-
makeMigration(param);
|
|
541
|
-
break;
|
|
542
|
-
case "migrate":
|
|
543
|
-
migrate(args);
|
|
544
|
-
break;
|
|
545
|
-
case "migrate:fresh":
|
|
546
|
-
migrateFresh(args);
|
|
547
|
-
break;
|
|
548
|
-
case "db:seed":
|
|
549
|
-
seed(args);
|
|
550
|
-
break;
|
|
551
|
-
default:
|
|
552
|
-
console.log(
|
|
553
|
-
"Available commands: init, make:model, make:migration, migrate, migrate:fresh, db:seed"
|
|
554
|
-
);
|
|
555
|
-
break;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
});
|
|
559
|
-
export default require_cli();
|
|
File without changes
|