@3lineas/d1-orm 1.0.3 → 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.
@@ -34,7 +34,7 @@ async function init() {
34
34
  console.log(`Directory already exists: ${folder}`);
35
35
  }
36
36
  });
37
- const userModelContent = `import { Model } from 'd1-orm';
37
+ const userModelContent = `import { Model } from '@3lineas/d1-orm';
38
38
 
39
39
  export class User extends Model {
40
40
  // protected static table = 'users';
@@ -54,7 +54,7 @@ export class User extends Model {
54
54
  }
55
55
  const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").split(".")[0].replace("T", "_");
56
56
  const migrationName = `${timestamp}_create_users_table.ts`;
57
- const migrationContent = `import { Blueprint, Schema } from 'd1-orm';
57
+ const migrationContent = `import { Blueprint, Schema } from '@3lineas/d1-orm';
58
58
 
59
59
  export const up = async () => {
60
60
  return Schema.create('users', (table: Blueprint) => {
@@ -86,8 +86,8 @@ export const down = async () => {
86
86
 
87
87
  export const seed = async () => {
88
88
  await User.create({
89
- name: 'Juan Perez',
90
- email: 'juan@perez.com',
89
+ name: 'John Doe',
90
+ email: 'john@example.com',
91
91
  password: 'password'
92
92
  });
93
93
  };
@@ -101,6 +101,24 @@ export const seed = async () => {
101
101
  fs.writeFileSync(seederPath, seederContent);
102
102
  console.log(`Created seeder: ${seederPath}`);
103
103
  }
104
+ const packageJsonPath = path.join(process.cwd(), "package.json");
105
+ if (fs.existsSync(packageJsonPath)) {
106
+ try {
107
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
108
+ if (!packageJson.scripts) {
109
+ packageJson.scripts = {};
110
+ }
111
+ if (!packageJson.scripts.orm) {
112
+ packageJson.scripts.orm = "d1-orm";
113
+ fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
114
+ console.log('Added "orm" script to package.json');
115
+ } else {
116
+ console.log('"orm" script already exists in package.json');
117
+ }
118
+ } catch (e) {
119
+ console.error("Failed to update package.json:", e);
120
+ }
121
+ }
104
122
  }
105
123
  var init_init = __esm({
106
124
  "src/cli/commands/init.ts"() {
@@ -108,66 +126,154 @@ var init_init = __esm({
108
126
  }
109
127
  });
110
128
 
111
- // src/cli/commands/make-model.ts
129
+ // src/cli/commands/make-migration.ts
112
130
  import * as fs2 from "fs";
113
131
  import * as path2 from "path";
114
- function makeModel(name) {
115
- const filename = `${name}.ts`;
116
- const targetPath = path2.join(process.cwd(), "models", filename);
117
- const template = `import { Model } from 'd1-orm';
132
+ import * as p from "@clack/prompts";
133
+ async function makeMigration(name) {
134
+ let migrationName = name;
135
+ if (!migrationName) {
136
+ migrationName = await p.text({
137
+ message: "What should the migration be named?",
138
+ placeholder: "e.g. create_users_table",
139
+ validate: (value) => {
140
+ if (!value) return "Please enter a name.";
141
+ }
142
+ });
143
+ if (p.isCancel(migrationName)) {
144
+ p.cancel("Operation cancelled.");
145
+ return;
146
+ }
147
+ }
148
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").split(".")[0].replace("T", "_");
149
+ const filename = `${timestamp}_${migrationName}.ts`;
150
+ const targetPath = path2.join(process.cwd(), "database/migrations", filename);
151
+ const template = `import { Blueprint, Schema } from '@3lineas/d1-orm';
118
152
 
119
- export class ${name} extends Model {
120
- // protected static table = '${name.toLowerCase()}s';
121
-
122
- // Define attributes explicitly for type safety if desired
123
- // declare id: number;
124
- // declare created_at: string;
125
- // declare updated_at: string;
126
- }
153
+ export const up = async () => {
154
+ return Schema.create('${migrationName.replace("create_", "").replace("_table", "")}', (table: Blueprint) => {
155
+ table.id();
156
+ table.timestamps();
157
+ });
158
+ };
159
+
160
+ export const down = async () => {
161
+ return Schema.dropIfExists('${migrationName.replace("create_", "").replace("_table", "")}');
162
+ };
127
163
  `;
164
+ if (!fs2.existsSync(path2.join(process.cwd(), "database/migrations"))) {
165
+ fs2.mkdirSync(path2.join(process.cwd(), "database/migrations"), {
166
+ recursive: true
167
+ });
168
+ }
128
169
  if (fs2.existsSync(targetPath)) {
129
- console.error(`Model ${filename} already exists.`);
130
- process.exit(1);
170
+ p.log.error(`Migration ${filename} already exists.`);
171
+ return;
131
172
  }
132
173
  fs2.writeFileSync(targetPath, template);
133
- console.log(`Created model: models/${filename}`);
174
+ p.log.success(`Created migration: database/migrations/${filename}`);
134
175
  }
135
- var init_make_model = __esm({
136
- "src/cli/commands/make-model.ts"() {
176
+ var init_make_migration = __esm({
177
+ "src/cli/commands/make-migration.ts"() {
137
178
  "use strict";
138
179
  }
139
180
  });
140
181
 
141
- // src/cli/commands/make-migration.ts
182
+ // src/cli/commands/make-model.ts
142
183
  import * as fs3 from "fs";
143
184
  import * as path3 from "path";
144
- function makeMigration(name) {
145
- const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").split(".")[0].replace("T", "_");
146
- const filename = `${timestamp}_${name}.ts`;
147
- const targetPath = path3.join(process.cwd(), "database/migrations", filename);
148
- const template = `import { Blueprint, Schema } from 'd1-orm';
149
-
150
- export const up = async () => {
151
- return Schema.create('${name}', (table: Blueprint) => {
152
- table.id();
153
- table.timestamps();
185
+ import * as p2 from "@clack/prompts";
186
+ async function makeModel(name) {
187
+ let modelName = name;
188
+ if (!modelName) {
189
+ modelName = await p2.text({
190
+ message: "What should the model be named?",
191
+ placeholder: "e.g. Flight",
192
+ validate: (value) => {
193
+ if (!value) return "Please enter a name.";
194
+ }
154
195
  });
155
- };
196
+ if (p2.isCancel(modelName)) {
197
+ p2.cancel("Operation cancelled.");
198
+ return;
199
+ }
200
+ }
201
+ const modelPath = await findModelsPath() || "src/models";
202
+ const filename = `${modelName}.ts`;
203
+ const targetPath = path3.join(process.cwd(), modelPath, filename);
204
+ const template = `import { Model } from '@3lineas/d1-orm';
156
205
 
157
- export const down = async () => {
158
- return Schema.dropIfExists('${name}');
206
+ export class ${modelName} extends Model {
207
+ // protected static table = '${modelName.toLowerCase()}s';
208
+
209
+ // declare id: number;
210
+ // declare created_at: string;
211
+ // declare updated_at: string;
212
+ }
213
+ `;
214
+ if (fs3.existsSync(targetPath)) {
215
+ p2.log.error(`Model ${filename} already exists at ${modelPath}.`);
216
+ return;
217
+ }
218
+ if (!fs3.existsSync(path3.join(process.cwd(), modelPath))) {
219
+ fs3.mkdirSync(path3.join(process.cwd(), modelPath), { recursive: true });
220
+ }
221
+ fs3.writeFileSync(targetPath, template);
222
+ p2.log.success(`Created model: ${modelPath}/${filename}`);
223
+ const options = await p2.multiselect({
224
+ message: "Would you like to create any of the following?",
225
+ options: [
226
+ { value: "migration", label: "Migration" },
227
+ { value: "seeder", label: "Database Seeder" }
228
+ ],
229
+ required: false
230
+ });
231
+ if (p2.isCancel(options)) {
232
+ p2.cancel("Operation cancelled.");
233
+ return;
234
+ }
235
+ if (options.includes("migration")) {
236
+ const migrationName = `create_${modelName.toLowerCase()}s_table`;
237
+ await makeMigration(migrationName);
238
+ }
239
+ if (options.includes("seeder")) {
240
+ await makeSeeder(modelName, modelPath);
241
+ }
242
+ }
243
+ async function findModelsPath() {
244
+ const packageJsonPath = path3.join(process.cwd(), "package.json");
245
+ if (!fs3.existsSync(packageJsonPath)) return null;
246
+ const commonPaths = ["src/models", "models", "app/Models"];
247
+ for (const p3 of commonPaths) {
248
+ if (fs3.existsSync(path3.join(process.cwd(), p3))) return p3;
249
+ }
250
+ return null;
251
+ }
252
+ async function makeSeeder(modelName, modelPath) {
253
+ const seederDir = path3.join(process.cwd(), "database/seeders");
254
+ const seederName = `${modelName}Seeder.ts`;
255
+ const targetPath = path3.join(seederDir, seederName);
256
+ if (!fs3.existsSync(seederDir)) {
257
+ fs3.mkdirSync(seederDir, { recursive: true });
258
+ }
259
+ const relativeModelPath = path3.relative(seederDir, path3.join(process.cwd(), modelPath, modelName)).replace(/\\/g, "/");
260
+ const template = `import { ${modelName} } from '${relativeModelPath}';
261
+
262
+ export const seed = async () => {
263
+ // await ${modelName}.create({ ... });
159
264
  };
160
265
  `;
161
266
  if (fs3.existsSync(targetPath)) {
162
- console.error(`Migration ${filename} already exists.`);
163
- process.exit(1);
267
+ p2.log.warn(`Seeder ${seederName} already exists.`);
268
+ return;
164
269
  }
165
270
  fs3.writeFileSync(targetPath, template);
166
- console.log(`Created migration: database/migrations/${filename}`);
271
+ p2.log.success(`Created seeder: database/seeders/${seederName}`);
167
272
  }
168
- var init_make_migration = __esm({
169
- "src/cli/commands/make-migration.ts"() {
273
+ var init_make_model = __esm({
274
+ "src/cli/commands/make-model.ts"() {
170
275
  "use strict";
276
+ init_make_migration();
171
277
  }
172
278
  });
173
279
 
@@ -184,23 +290,22 @@ async function migrate(args) {
184
290
  }
185
291
  const files = fs4.readdirSync(migrationsDir).filter((f) => f.endsWith(".ts") || f.endsWith(".js")).sort();
186
292
  for (const file of files) {
187
- console.log(`Processing ${file}...`);
293
+ console.log(`Processing migration: ${file}`);
188
294
  const filePath = path4.join(migrationsDir, file);
189
295
  try {
190
296
  const migration = await import(filePath);
191
297
  if (migration.up) {
192
298
  const sql = await migration.up();
193
299
  if (sql) {
194
- console.log(`Executing SQL: ${sql}`);
195
300
  const isRemote = args.includes("--remote");
196
301
  const dbName = "DB";
197
302
  const command = isRemote ? "--remote" : "--local";
198
303
  try {
199
304
  const execCmd = `npx wrangler d1 execute ${dbName} --command "${sql.replace(/"/g, '\\"')}" ${command}`;
200
- console.log(`Running: ${execCmd}`);
305
+ console.log(`Executing: ${execCmd}`);
201
306
  execSync(execCmd, { stdio: "inherit" });
202
307
  } catch (e) {
203
- console.error(`Failed to execute migration ${file}.`);
308
+ console.error(`Failed to execute migration: ${file}`);
204
309
  throw e;
205
310
  }
206
311
  }
@@ -231,17 +336,9 @@ var require_cli = __commonJS({
231
336
  init();
232
337
  break;
233
338
  case "make:model":
234
- if (!param) {
235
- console.error("Usage: d1-orm make:model <Name>");
236
- process.exit(1);
237
- }
238
339
  makeModel(param);
239
340
  break;
240
341
  case "make:migration":
241
- if (!param) {
242
- console.error("Usage: d1-orm make:migration <Name>");
243
- process.exit(1);
244
- }
245
342
  makeMigration(param);
246
343
  break;
247
344
  case "migrate":