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