orchid-orm 1.5.12 → 1.5.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orchid-orm",
3
- "version": "1.5.12",
3
+ "version": "1.5.15",
4
4
  "description": "Postgres ORM",
5
5
  "homepage": "https://orchid-orm.netlify.app/guide/orm-setup-and-overview.html",
6
6
  "repository": {
package/rollup.config.js CHANGED
@@ -8,6 +8,7 @@ export default [
8
8
  plugins: [esbuild()],
9
9
  output: [
10
10
  {
11
+ banner: '#!/usr/bin/env node',
11
12
  file: 'dist/bin.js',
12
13
  format: 'cjs',
13
14
  sourcemap: true,
package/src/bin/bin.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { astOrchidORMConfig, initOrchidORM } from './init';
1
+ import { askOrchidORMConfig, initOrchidORM } from './init';
2
2
 
3
- astOrchidORMConfig().then(initOrchidORM);
3
+ askOrchidORMConfig().then(initOrchidORM);
@@ -46,6 +46,7 @@ const configPath = path.join(dbDirPath, 'config.ts');
46
46
  const dbPath = path.join(dbDirPath, 'db.ts');
47
47
  const migrationScriptPath = path.join(dbDirPath, 'dbScripts.ts');
48
48
  const migrationsPath = path.join(dbDirPath, 'migrations');
49
+ const seedPath = path.join(dbDirPath, 'seed.ts');
49
50
 
50
51
  describe('initOrchidORM', () => {
51
52
  beforeEach(jest.clearAllMocks);
@@ -64,7 +65,8 @@ describe('initOrchidORM', () => {
64
65
  "dependencies": {
65
66
  "dotenv": "^1.2.3",
66
67
  "orchid-orm": "^1.2.3",
67
- "pqb": "^1.2.3"
68
+ "pqb": "^1.2.3",
69
+ "pg": "^1.2.3"
68
70
  },
69
71
  "devDependencies": {
70
72
  "rake-db": "^1.2.3",
@@ -84,6 +86,7 @@ describe('initOrchidORM', () => {
84
86
  "dotenv": "^1.2.3",
85
87
  "orchid-orm": "^1.2.3",
86
88
  "pqb": "^1.2.3",
89
+ "pg": "^1.2.3",
87
90
  "orchid-orm-schema-to-zod": "^1.2.3"
88
91
  },
89
92
  "devDependencies": {
@@ -186,6 +189,7 @@ describe('initOrchidORM', () => {
186
189
  "dotenv": "^1.2.3",
187
190
  "orchid-orm": "^1.2.3",
188
191
  "pqb": "^1.2.3",
192
+ "pg": "^1.2.3",
189
193
  "orchid-orm-schema-to-zod": "^1.2.3"
190
194
  },
191
195
  "devDependencies": {
@@ -354,7 +358,8 @@ ko
354
358
 
355
359
  export const BaseTable = createBaseTable({
356
360
  columnTypes: (t) => ({
357
- text: (min: 0, max: Infinity) => t.text(min, max),
361
+ ...t,
362
+ text: (min = 0, max = Infinity) => t.text(min, max),
358
363
  }),
359
364
  });
360
365
  `);
@@ -372,8 +377,10 @@ export const BaseTable = createBaseTable({
372
377
 
373
378
  export const BaseTable = createBaseTable({
374
379
  columnTypes: (t) => ({
375
- text: (min: 0, max: Infinity) => t.text(min, max),
376
- timestamp: <P extends number>(precision?: P) => t.timestamp<P>(precision).asDate(),
380
+ ...t,
381
+ text: (min = 0, max = Infinity) => t.text(min, max),
382
+ timestamp: <P extends number>(precision?: P) =>
383
+ t.timestamp<P>(precision).asDate(),
377
384
  }),
378
385
  });
379
386
  `);
@@ -391,8 +398,10 @@ export const BaseTable = createBaseTable({
391
398
 
392
399
  export const BaseTable = createBaseTable({
393
400
  columnTypes: (t) => ({
394
- text: (min: 0, max: Infinity) => t.text(min, max),
395
- timestamp: <P extends number>(precision?: P) => t.timestamp<P>(precision).asNumber(),
401
+ ...t,
402
+ text: (min = 0, max = Infinity) => t.text(min, max),
403
+ timestamp: <P extends number>(precision?: P) =>
404
+ t.timestamp<P>(precision).asNumber(),
396
405
  }),
397
406
  });
398
407
  `);
@@ -426,7 +435,7 @@ export const BaseTable = createBaseTable({
426
435
  import { CommentTable } from './comment.table';
427
436
 
428
437
  export type Post = PostTable['columns']['type'];
429
- class PostTable extends BaseTable {
438
+ export class PostTable extends BaseTable {
430
439
  table = 'post';
431
440
  columns = this.setColumns((t) => ({
432
441
  id: t.serial().primaryKey(),
@@ -434,13 +443,13 @@ class PostTable extends BaseTable {
434
443
  text: t.text(20, 10000),
435
444
  ...t.timestamps(),
436
445
  }));
437
-
446
+
438
447
  relations = {
439
448
  comments: this.hasMany(() => CommentTable, {
440
449
  primaryKey: 'id',
441
450
  foreignKey: 'postId',
442
451
  }),
443
- }
452
+ };
444
453
  }
445
454
  `);
446
455
  });
@@ -459,7 +468,7 @@ import { CommentTable } from './comment.table';
459
468
  import { tableToZod } from 'orchid-orm-schema-to-zod';
460
469
 
461
470
  export type Post = PostTable['columns']['type'];
462
- class PostTable extends BaseTable {
471
+ export class PostTable extends BaseTable {
463
472
  table = 'post';
464
473
  columns = this.setColumns((t) => ({
465
474
  id: t.serial().primaryKey(),
@@ -467,13 +476,13 @@ class PostTable extends BaseTable {
467
476
  text: t.text(20, 10000),
468
477
  ...t.timestamps(),
469
478
  }));
470
-
479
+
471
480
  relations = {
472
481
  comments: this.hasMany(() => CommentTable, {
473
482
  primaryKey: 'id',
474
483
  foreignKey: 'postId',
475
484
  }),
476
- }
485
+ };
477
486
  }
478
487
 
479
488
  export const postSchema = tableToZod(PostTable);
@@ -492,21 +501,24 @@ export const postSchema = tableToZod(PostTable);
492
501
  import { PostTable } from './post.table';
493
502
 
494
503
  export type Comment = CommentTable['columns']['type'];
495
- class CommentTable extends BaseTable {
504
+ export class CommentTable extends BaseTable {
496
505
  table = 'comment';
497
506
  columns = this.setColumns((t) => ({
498
507
  id: t.serial().primaryKey(),
499
- postId: t.integer().foreignKey(() => PostTable, 'id').index(),
508
+ postId: t
509
+ .integer()
510
+ .foreignKey(() => PostTable, 'id')
511
+ .index(),
500
512
  text: t.text(5, 1000),
501
513
  ...t.timestamps(),
502
514
  }));
503
-
515
+
504
516
  relations = {
505
517
  post: this.belongsTo(() => PostTable, {
506
518
  primaryKey: 'id',
507
519
  foreignKey: 'postId',
508
520
  }),
509
- }
521
+ };
510
522
  }
511
523
  `);
512
524
  });
@@ -525,21 +537,24 @@ import { PostTable } from './post.table';
525
537
  import { tableToZod } from 'orchid-orm-schema-to-zod';
526
538
 
527
539
  export type Comment = CommentTable['columns']['type'];
528
- class CommentTable extends BaseTable {
540
+ export class CommentTable extends BaseTable {
529
541
  table = 'comment';
530
542
  columns = this.setColumns((t) => ({
531
543
  id: t.serial().primaryKey(),
532
- postId: t.integer().foreignKey(() => PostTable, 'id').index(),
544
+ postId: t
545
+ .integer()
546
+ .foreignKey(() => PostTable, 'id')
547
+ .index(),
533
548
  text: t.text(5, 1000),
534
549
  ...t.timestamps(),
535
550
  }));
536
-
551
+
537
552
  relations = {
538
553
  post: this.belongsTo(() => PostTable, {
539
554
  primaryKey: 'id',
540
555
  foreignKey: 'postId',
541
556
  }),
542
- }
557
+ };
543
558
  }
544
559
 
545
560
  export const commentSchema = tableToZod(CommentTable);
@@ -563,7 +578,7 @@ if (!database.databaseURL) throw new Error('DATABASE_URL is missing in .env');
563
578
 
564
579
  export const config = {
565
580
  database,
566
- }
581
+ };
567
582
  `);
568
583
  });
569
584
 
@@ -595,7 +610,7 @@ if (testDatabase.databaseURL) {
595
610
  export const config = {
596
611
  allDatabases,
597
612
  database: process.env.NODE_ENV === 'test' ? testDatabase : database,
598
- }
613
+ };
599
614
  `);
600
615
  });
601
616
  });
@@ -610,11 +625,8 @@ export const config = {
610
625
  expect(content).toBe(`import { orchidORM } from 'orchid-orm';
611
626
  import { config } from './config';
612
627
 
613
- export const db = orchidORM(
614
- config.database,
615
- {
616
- }
617
- );
628
+ export const db = orchidORM(config.database, {
629
+ });
618
630
  `);
619
631
  });
620
632
 
@@ -628,16 +640,13 @@ export const db = orchidORM(
628
640
  );
629
641
  expect(content).toBe(`import { orchidORM } from 'orchid-orm';
630
642
  import { config } from './config';
631
- import { Post } from './tables/post.table';
632
- import { Comment } from './tables/comment.table';
633
-
634
- export const db = orchidORM(
635
- config.database,
636
- {
637
- post: Post,
638
- comment: Comment,
639
- }
640
- );
643
+ import { PostTable } from './tables/post.table';
644
+ import { CommentTable } from './tables/comment.table';
645
+
646
+ export const db = orchidORM(config.database, {
647
+ post: PostTable,
648
+ comment: CommentTable,
649
+ });
641
650
  `);
642
651
  });
643
652
  });
@@ -656,11 +665,18 @@ import { appCodeUpdater } from 'orchid-orm';
656
665
  rakeDb(config.database, {
657
666
  migrationsPath: 'src/db/migrations',
658
667
  appCodeUpdater: appCodeUpdater({
659
- tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
660
- baseTablePath: 'src/lib/baseTable.ts',
668
+ tablePath: (tableName) => \`src/db/tables/\${tableName}.table.ts\`,
669
+ baseTablePath: 'src/db/baseTable.ts',
661
670
  baseTableName: 'BaseTable',
662
- mainFilePath: 'src/db.ts',
671
+ mainFilePath: 'src/db/db.ts',
663
672
  }),
673
+ useCodeUpdater: true, // set to false to disable code updater
674
+ commands: {
675
+ async seed() {
676
+ const { seed } = await import('./seed');
677
+ await seed();
678
+ },
679
+ },
664
680
  });
665
681
  `);
666
682
  });
@@ -680,11 +696,18 @@ import { appCodeUpdater } from 'orchid-orm';
680
696
  rakeDb(config.allDatabases, {
681
697
  migrationsPath: 'src/db/migrations',
682
698
  appCodeUpdater: appCodeUpdater({
683
- tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
684
- baseTablePath: 'src/lib/baseTable.ts',
699
+ tablePath: (tableName) => \`src/db/tables/\${tableName}.table.ts\`,
700
+ baseTablePath: 'src/db/baseTable.ts',
685
701
  baseTableName: 'BaseTable',
686
- mainFilePath: 'src/db.ts',
702
+ mainFilePath: 'src/db/db.ts',
687
703
  }),
704
+ useCodeUpdater: true, // set to false to disable code updater
705
+ commands: {
706
+ async seed() {
707
+ const { seed } = await import('./seed');
708
+ await seed();
709
+ },
710
+ },
688
711
  });
689
712
  `);
690
713
  });
@@ -730,6 +753,24 @@ change(async (db) => {
730
753
  ...t.timestamps(),
731
754
  }));
732
755
  });
756
+ `);
757
+ });
758
+ });
759
+
760
+ describe('seed', () => {
761
+ it('should create seed file', async () => {
762
+ await initOrchidORM({});
763
+
764
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
765
+ ([to]) => to === seedPath,
766
+ );
767
+ expect(content).toBe(`import { db } from './db';
768
+
769
+ export const seed = async () => {
770
+ // create records here
771
+
772
+ await db.$close();
773
+ };
733
774
  `);
734
775
  });
735
776
  });
package/src/bin/init.ts CHANGED
@@ -15,7 +15,7 @@ type DependencyKind = 'dependencies' | 'devDependencies';
15
15
 
16
16
  const dirPath = path.resolve(process.cwd(), 'src', 'db');
17
17
 
18
- export const astOrchidORMConfig = async () => {
18
+ export const askOrchidORMConfig = async () => {
19
19
  const response = await prompts([
20
20
  {
21
21
  type: 'select',
@@ -73,6 +73,7 @@ export const initOrchidORM = async (config: InitConfig) => {
73
73
  await setupMainDb(config);
74
74
  await setupMigrationScript(config);
75
75
  await createMigrations(config);
76
+ await createSeed();
76
77
  };
77
78
 
78
79
  const setupPackageJson = async (config: InitConfig) => {
@@ -80,6 +81,7 @@ const setupPackageJson = async (config: InitConfig) => {
80
81
  getLatestPackageVersion('dotenv', 'dependencies'),
81
82
  getLatestPackageVersion('orchid-orm', 'dependencies'),
82
83
  getLatestPackageVersion('pqb', 'dependencies'),
84
+ getLatestPackageVersion('pg', 'dependencies'),
83
85
  config.addSchemaToZod &&
84
86
  getLatestPackageVersion('orchid-orm-schema-to-zod', 'dependencies'),
85
87
  getLatestPackageVersion('rake-db', 'devDependencies'),
@@ -212,14 +214,16 @@ const setupBaseTable = async (config: InitConfig) => {
212
214
 
213
215
  export const BaseTable = createBaseTable({
214
216
  columnTypes: (t) => ({
215
- text: (min: 0, max: Infinity) => t.text(min, max),`;
217
+ ...t,
218
+ text: (min = 0, max = Infinity) => t.text(min, max),`;
216
219
 
217
220
  const { timestamp } = config;
218
221
  if (timestamp) {
219
222
  content += `
220
- timestamp: <P extends number>(precision?: P) => t.timestamp<P>(precision).${
221
- timestamp === 'date' ? 'asDate' : 'asNumber'
222
- }(),`;
223
+ timestamp: <P extends number>(precision?: P) =>
224
+ t.timestamp<P>(precision).${
225
+ timestamp === 'date' ? 'asDate' : 'asNumber'
226
+ }(),`;
223
227
  }
224
228
 
225
229
  content += `
@@ -246,7 +250,7 @@ ${
246
250
  : ''
247
251
  }
248
252
  export type Post = PostTable['columns']['type'];
249
- class PostTable extends BaseTable {
253
+ export class PostTable extends BaseTable {
250
254
  table = 'post';
251
255
  columns = this.setColumns((t) => ({
252
256
  id: t.serial().primaryKey(),
@@ -254,13 +258,13 @@ class PostTable extends BaseTable {
254
258
  text: t.text(20, 10000),
255
259
  ...t.timestamps(),
256
260
  }));
257
-
261
+
258
262
  relations = {
259
263
  comments: this.hasMany(() => CommentTable, {
260
264
  primaryKey: 'id',
261
265
  foreignKey: 'postId',
262
266
  }),
263
- }
267
+ };
264
268
  }
265
269
  ${
266
270
  config.addSchemaToZod
@@ -279,21 +283,24 @@ ${
279
283
  : ''
280
284
  }
281
285
  export type Comment = CommentTable['columns']['type'];
282
- class CommentTable extends BaseTable {
286
+ export class CommentTable extends BaseTable {
283
287
  table = 'comment';
284
288
  columns = this.setColumns((t) => ({
285
289
  id: t.serial().primaryKey(),
286
- postId: t.integer().foreignKey(() => PostTable, 'id').index(),
290
+ postId: t
291
+ .integer()
292
+ .foreignKey(() => PostTable, 'id')
293
+ .index(),
287
294
  text: t.text(5, 1000),
288
295
  ...t.timestamps(),
289
296
  }));
290
-
297
+
291
298
  relations = {
292
299
  post: this.belongsTo(() => PostTable, {
293
300
  primaryKey: 'id',
294
301
  foreignKey: 'postId',
295
302
  }),
296
- }
303
+ };
297
304
  }
298
305
  ${
299
306
  config.addSchemaToZod
@@ -344,7 +351,7 @@ export const config = {`;
344
351
  database,`;
345
352
  }
346
353
  content += `
347
- }
354
+ };
348
355
  `;
349
356
 
350
357
  await fs.writeFile(configPath, content);
@@ -355,11 +362,11 @@ const setupMainDb = async (config: InitConfig) => {
355
362
  let tables = '';
356
363
  if (config.demoTables) {
357
364
  imports += `
358
- import { Post } from './tables/post.table';
359
- import { Comment } from './tables/comment.table';`;
365
+ import { PostTable } from './tables/post.table';
366
+ import { CommentTable } from './tables/comment.table';`;
360
367
  tables += `
361
- post: Post,
362
- comment: Comment,`;
368
+ post: PostTable,
369
+ comment: CommentTable,`;
363
370
  }
364
371
 
365
372
  const dbPath = path.join(dirPath, 'db.ts');
@@ -368,11 +375,8 @@ import { Comment } from './tables/comment.table';`;
368
375
  `import { orchidORM } from 'orchid-orm';
369
376
  import { config } from './config';${imports}
370
377
 
371
- export const db = orchidORM(
372
- config.database,
373
- {${tables}
374
- }
375
- );
378
+ export const db = orchidORM(config.database, {${tables}
379
+ });
376
380
  `,
377
381
  );
378
382
  };
@@ -388,11 +392,18 @@ import { appCodeUpdater } from 'orchid-orm';
388
392
  rakeDb(${config.testDatabase ? 'config.allDatabases' : 'config.database'}, {
389
393
  migrationsPath: 'src/db/migrations',
390
394
  appCodeUpdater: appCodeUpdater({
391
- tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
392
- baseTablePath: 'src/lib/baseTable.ts',
395
+ tablePath: (tableName) => \`src/db/tables/\${tableName}.table.ts\`,
396
+ baseTablePath: 'src/db/baseTable.ts',
393
397
  baseTableName: 'BaseTable',
394
- mainFilePath: 'src/db.ts',
398
+ mainFilePath: 'src/db/db.ts',
395
399
  }),
400
+ useCodeUpdater: true, // set to false to disable code updater
401
+ commands: {
402
+ async seed() {
403
+ const { seed } = await import('./seed');
404
+ await seed();
405
+ },
406
+ },
396
407
  });
397
408
  `,
398
409
  );
@@ -404,9 +415,11 @@ const createMigrations = async (config: InitConfig) => {
404
415
 
405
416
  if (!config.demoTables) return;
406
417
 
418
+ const now = new Date();
419
+
407
420
  const postPath = path.join(
408
421
  migrationsPath,
409
- `${makeFileTimeStamp()}_createPost.ts`,
422
+ `${makeFileTimeStamp(now)}_createPost.ts`,
410
423
  );
411
424
  await fs.writeFile(
412
425
  postPath,
@@ -423,9 +436,11 @@ change(async (db) => {
423
436
  `,
424
437
  );
425
438
 
439
+ now.setTime(now.getTime() + 1000);
440
+
426
441
  const commentPath = path.join(
427
442
  migrationsPath,
428
- `${makeFileTimeStamp()}_createComment.ts`,
443
+ `${makeFileTimeStamp(now)}_createComment.ts`,
429
444
  );
430
445
  await fs.writeFile(
431
446
  commentPath,
@@ -443,8 +458,7 @@ change(async (db) => {
443
458
  );
444
459
  };
445
460
 
446
- const makeFileTimeStamp = () => {
447
- const now = new Date();
461
+ const makeFileTimeStamp = (now: Date) => {
448
462
  return [
449
463
  now.getUTCFullYear(),
450
464
  now.getUTCMonth() + 1,
@@ -456,3 +470,18 @@ const makeFileTimeStamp = () => {
456
470
  .map((value) => (value < 10 ? `0${value}` : value))
457
471
  .join('');
458
472
  };
473
+
474
+ const createSeed = async () => {
475
+ const filePath = path.join(dirPath, 'seed.ts');
476
+ await fs.writeFile(
477
+ filePath,
478
+ `import { db } from './db';
479
+
480
+ export const seed = async () => {
481
+ // create records here
482
+
483
+ await db.$close();
484
+ };
485
+ `,
486
+ );
487
+ };