orchid-orm 1.5.14 → 1.5.16
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 +12 -0
- package/dist/bin.js +50 -28
- package/dist/bin.js.map +1 -1
- package/dist/index.js +13 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +13 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/bin/init.test.ts +57 -45
- package/src/bin/init.ts +56 -30
- package/src/codegen/testUtils.ts +6 -5
- package/src/codegen/updateMainFile.test.ts +16 -16
- package/src/codegen/updateMainFile.ts +2 -2
- package/src/codegen/updateTableFile/changeTable.test.ts +72 -72
- package/src/codegen/updateTableFile/changeTable.ts +1 -1
- package/src/codegen/updateTableFile/createTable.test.ts +17 -6
- package/src/codegen/updateTableFile/createTable.ts +10 -2
- package/src/codegen/updateTableFile/renameTable.test.ts +17 -17
- package/src/codegen/updateTableFile/renameTable.ts +2 -2
package/package.json
CHANGED
package/src/bin/init.test.ts
CHANGED
|
@@ -48,6 +48,8 @@ const migrationScriptPath = path.join(dbDirPath, 'dbScripts.ts');
|
|
|
48
48
|
const migrationsPath = path.join(dbDirPath, 'migrations');
|
|
49
49
|
const seedPath = path.join(dbDirPath, 'seed.ts');
|
|
50
50
|
|
|
51
|
+
console.log = jest.fn();
|
|
52
|
+
|
|
51
53
|
describe('initOrchidORM', () => {
|
|
52
54
|
beforeEach(jest.clearAllMocks);
|
|
53
55
|
|
|
@@ -65,7 +67,8 @@ describe('initOrchidORM', () => {
|
|
|
65
67
|
"dependencies": {
|
|
66
68
|
"dotenv": "^1.2.3",
|
|
67
69
|
"orchid-orm": "^1.2.3",
|
|
68
|
-
"pqb": "^1.2.3"
|
|
70
|
+
"pqb": "^1.2.3",
|
|
71
|
+
"pg": "^1.2.3"
|
|
69
72
|
},
|
|
70
73
|
"devDependencies": {
|
|
71
74
|
"rake-db": "^1.2.3",
|
|
@@ -85,6 +88,7 @@ describe('initOrchidORM', () => {
|
|
|
85
88
|
"dotenv": "^1.2.3",
|
|
86
89
|
"orchid-orm": "^1.2.3",
|
|
87
90
|
"pqb": "^1.2.3",
|
|
91
|
+
"pg": "^1.2.3",
|
|
88
92
|
"orchid-orm-schema-to-zod": "^1.2.3"
|
|
89
93
|
},
|
|
90
94
|
"devDependencies": {
|
|
@@ -187,6 +191,7 @@ describe('initOrchidORM', () => {
|
|
|
187
191
|
"dotenv": "^1.2.3",
|
|
188
192
|
"orchid-orm": "^1.2.3",
|
|
189
193
|
"pqb": "^1.2.3",
|
|
194
|
+
"pg": "^1.2.3",
|
|
190
195
|
"orchid-orm-schema-to-zod": "^1.2.3"
|
|
191
196
|
},
|
|
192
197
|
"devDependencies": {
|
|
@@ -355,7 +360,8 @@ ko
|
|
|
355
360
|
|
|
356
361
|
export const BaseTable = createBaseTable({
|
|
357
362
|
columnTypes: (t) => ({
|
|
358
|
-
|
|
363
|
+
...t,
|
|
364
|
+
text: (min = 0, max = Infinity) => t.text(min, max),
|
|
359
365
|
}),
|
|
360
366
|
});
|
|
361
367
|
`);
|
|
@@ -373,8 +379,10 @@ export const BaseTable = createBaseTable({
|
|
|
373
379
|
|
|
374
380
|
export const BaseTable = createBaseTable({
|
|
375
381
|
columnTypes: (t) => ({
|
|
376
|
-
|
|
377
|
-
|
|
382
|
+
...t,
|
|
383
|
+
text: (min = 0, max = Infinity) => t.text(min, max),
|
|
384
|
+
timestamp: <P extends number>(precision?: P) =>
|
|
385
|
+
t.timestamp<P>(precision).asDate(),
|
|
378
386
|
}),
|
|
379
387
|
});
|
|
380
388
|
`);
|
|
@@ -392,8 +400,10 @@ export const BaseTable = createBaseTable({
|
|
|
392
400
|
|
|
393
401
|
export const BaseTable = createBaseTable({
|
|
394
402
|
columnTypes: (t) => ({
|
|
395
|
-
|
|
396
|
-
|
|
403
|
+
...t,
|
|
404
|
+
text: (min = 0, max = Infinity) => t.text(min, max),
|
|
405
|
+
timestamp: <P extends number>(precision?: P) =>
|
|
406
|
+
t.timestamp<P>(precision).asNumber(),
|
|
397
407
|
}),
|
|
398
408
|
});
|
|
399
409
|
`);
|
|
@@ -427,7 +437,7 @@ export const BaseTable = createBaseTable({
|
|
|
427
437
|
import { CommentTable } from './comment.table';
|
|
428
438
|
|
|
429
439
|
export type Post = PostTable['columns']['type'];
|
|
430
|
-
class PostTable extends BaseTable {
|
|
440
|
+
export class PostTable extends BaseTable {
|
|
431
441
|
table = 'post';
|
|
432
442
|
columns = this.setColumns((t) => ({
|
|
433
443
|
id: t.serial().primaryKey(),
|
|
@@ -435,13 +445,13 @@ class PostTable extends BaseTable {
|
|
|
435
445
|
text: t.text(20, 10000),
|
|
436
446
|
...t.timestamps(),
|
|
437
447
|
}));
|
|
438
|
-
|
|
448
|
+
|
|
439
449
|
relations = {
|
|
440
450
|
comments: this.hasMany(() => CommentTable, {
|
|
441
451
|
primaryKey: 'id',
|
|
442
452
|
foreignKey: 'postId',
|
|
443
453
|
}),
|
|
444
|
-
}
|
|
454
|
+
};
|
|
445
455
|
}
|
|
446
456
|
`);
|
|
447
457
|
});
|
|
@@ -460,7 +470,7 @@ import { CommentTable } from './comment.table';
|
|
|
460
470
|
import { tableToZod } from 'orchid-orm-schema-to-zod';
|
|
461
471
|
|
|
462
472
|
export type Post = PostTable['columns']['type'];
|
|
463
|
-
class PostTable extends BaseTable {
|
|
473
|
+
export class PostTable extends BaseTable {
|
|
464
474
|
table = 'post';
|
|
465
475
|
columns = this.setColumns((t) => ({
|
|
466
476
|
id: t.serial().primaryKey(),
|
|
@@ -468,13 +478,13 @@ class PostTable extends BaseTable {
|
|
|
468
478
|
text: t.text(20, 10000),
|
|
469
479
|
...t.timestamps(),
|
|
470
480
|
}));
|
|
471
|
-
|
|
481
|
+
|
|
472
482
|
relations = {
|
|
473
483
|
comments: this.hasMany(() => CommentTable, {
|
|
474
484
|
primaryKey: 'id',
|
|
475
485
|
foreignKey: 'postId',
|
|
476
486
|
}),
|
|
477
|
-
}
|
|
487
|
+
};
|
|
478
488
|
}
|
|
479
489
|
|
|
480
490
|
export const postSchema = tableToZod(PostTable);
|
|
@@ -493,21 +503,24 @@ export const postSchema = tableToZod(PostTable);
|
|
|
493
503
|
import { PostTable } from './post.table';
|
|
494
504
|
|
|
495
505
|
export type Comment = CommentTable['columns']['type'];
|
|
496
|
-
class CommentTable extends BaseTable {
|
|
506
|
+
export class CommentTable extends BaseTable {
|
|
497
507
|
table = 'comment';
|
|
498
508
|
columns = this.setColumns((t) => ({
|
|
499
509
|
id: t.serial().primaryKey(),
|
|
500
|
-
postId: t
|
|
510
|
+
postId: t
|
|
511
|
+
.integer()
|
|
512
|
+
.foreignKey(() => PostTable, 'id')
|
|
513
|
+
.index(),
|
|
501
514
|
text: t.text(5, 1000),
|
|
502
515
|
...t.timestamps(),
|
|
503
516
|
}));
|
|
504
|
-
|
|
517
|
+
|
|
505
518
|
relations = {
|
|
506
519
|
post: this.belongsTo(() => PostTable, {
|
|
507
520
|
primaryKey: 'id',
|
|
508
521
|
foreignKey: 'postId',
|
|
509
522
|
}),
|
|
510
|
-
}
|
|
523
|
+
};
|
|
511
524
|
}
|
|
512
525
|
`);
|
|
513
526
|
});
|
|
@@ -526,21 +539,24 @@ import { PostTable } from './post.table';
|
|
|
526
539
|
import { tableToZod } from 'orchid-orm-schema-to-zod';
|
|
527
540
|
|
|
528
541
|
export type Comment = CommentTable['columns']['type'];
|
|
529
|
-
class CommentTable extends BaseTable {
|
|
542
|
+
export class CommentTable extends BaseTable {
|
|
530
543
|
table = 'comment';
|
|
531
544
|
columns = this.setColumns((t) => ({
|
|
532
545
|
id: t.serial().primaryKey(),
|
|
533
|
-
postId: t
|
|
546
|
+
postId: t
|
|
547
|
+
.integer()
|
|
548
|
+
.foreignKey(() => PostTable, 'id')
|
|
549
|
+
.index(),
|
|
534
550
|
text: t.text(5, 1000),
|
|
535
551
|
...t.timestamps(),
|
|
536
552
|
}));
|
|
537
|
-
|
|
553
|
+
|
|
538
554
|
relations = {
|
|
539
555
|
post: this.belongsTo(() => PostTable, {
|
|
540
556
|
primaryKey: 'id',
|
|
541
557
|
foreignKey: 'postId',
|
|
542
558
|
}),
|
|
543
|
-
}
|
|
559
|
+
};
|
|
544
560
|
}
|
|
545
561
|
|
|
546
562
|
export const commentSchema = tableToZod(CommentTable);
|
|
@@ -564,7 +580,7 @@ if (!database.databaseURL) throw new Error('DATABASE_URL is missing in .env');
|
|
|
564
580
|
|
|
565
581
|
export const config = {
|
|
566
582
|
database,
|
|
567
|
-
}
|
|
583
|
+
};
|
|
568
584
|
`);
|
|
569
585
|
});
|
|
570
586
|
|
|
@@ -596,7 +612,7 @@ if (testDatabase.databaseURL) {
|
|
|
596
612
|
export const config = {
|
|
597
613
|
allDatabases,
|
|
598
614
|
database: process.env.NODE_ENV === 'test' ? testDatabase : database,
|
|
599
|
-
}
|
|
615
|
+
};
|
|
600
616
|
`);
|
|
601
617
|
});
|
|
602
618
|
});
|
|
@@ -611,11 +627,8 @@ export const config = {
|
|
|
611
627
|
expect(content).toBe(`import { orchidORM } from 'orchid-orm';
|
|
612
628
|
import { config } from './config';
|
|
613
629
|
|
|
614
|
-
export const db = orchidORM(
|
|
615
|
-
|
|
616
|
-
{
|
|
617
|
-
}
|
|
618
|
-
);
|
|
630
|
+
export const db = orchidORM(config.database, {
|
|
631
|
+
});
|
|
619
632
|
`);
|
|
620
633
|
});
|
|
621
634
|
|
|
@@ -629,16 +642,13 @@ export const db = orchidORM(
|
|
|
629
642
|
);
|
|
630
643
|
expect(content).toBe(`import { orchidORM } from 'orchid-orm';
|
|
631
644
|
import { config } from './config';
|
|
632
|
-
import {
|
|
633
|
-
import {
|
|
634
|
-
|
|
635
|
-
export const db = orchidORM(
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
comment: Comment,
|
|
640
|
-
}
|
|
641
|
-
);
|
|
645
|
+
import { PostTable } from './tables/post.table';
|
|
646
|
+
import { CommentTable } from './tables/comment.table';
|
|
647
|
+
|
|
648
|
+
export const db = orchidORM(config.database, {
|
|
649
|
+
post: PostTable,
|
|
650
|
+
comment: CommentTable,
|
|
651
|
+
});
|
|
642
652
|
`);
|
|
643
653
|
});
|
|
644
654
|
});
|
|
@@ -657,15 +667,16 @@ import { appCodeUpdater } from 'orchid-orm';
|
|
|
657
667
|
rakeDb(config.database, {
|
|
658
668
|
migrationsPath: 'src/db/migrations',
|
|
659
669
|
appCodeUpdater: appCodeUpdater({
|
|
660
|
-
tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
|
|
670
|
+
tablePath: (tableName) => \`src/db/tables/\${tableName}.table.ts\`,
|
|
661
671
|
baseTablePath: 'src/db/baseTable.ts',
|
|
662
672
|
baseTableName: 'BaseTable',
|
|
663
673
|
mainFilePath: 'src/db/db.ts',
|
|
664
674
|
}),
|
|
675
|
+
useCodeUpdater: true, // set to false to disable code updater
|
|
665
676
|
commands: {
|
|
666
677
|
async seed() {
|
|
667
|
-
const {
|
|
668
|
-
await
|
|
678
|
+
const { seed } = await import('./seed');
|
|
679
|
+
await seed();
|
|
669
680
|
},
|
|
670
681
|
},
|
|
671
682
|
});
|
|
@@ -687,15 +698,16 @@ import { appCodeUpdater } from 'orchid-orm';
|
|
|
687
698
|
rakeDb(config.allDatabases, {
|
|
688
699
|
migrationsPath: 'src/db/migrations',
|
|
689
700
|
appCodeUpdater: appCodeUpdater({
|
|
690
|
-
tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
|
|
701
|
+
tablePath: (tableName) => \`src/db/tables/\${tableName}.table.ts\`,
|
|
691
702
|
baseTablePath: 'src/db/baseTable.ts',
|
|
692
703
|
baseTableName: 'BaseTable',
|
|
693
704
|
mainFilePath: 'src/db/db.ts',
|
|
694
705
|
}),
|
|
706
|
+
useCodeUpdater: true, // set to false to disable code updater
|
|
695
707
|
commands: {
|
|
696
708
|
async seed() {
|
|
697
|
-
const {
|
|
698
|
-
await
|
|
709
|
+
const { seed } = await import('./seed');
|
|
710
|
+
await seed();
|
|
699
711
|
},
|
|
700
712
|
},
|
|
701
713
|
});
|
|
@@ -759,8 +771,8 @@ change(async (db) => {
|
|
|
759
771
|
export const seed = async () => {
|
|
760
772
|
// create records here
|
|
761
773
|
|
|
762
|
-
await db
|
|
763
|
-
}
|
|
774
|
+
await db.$close();
|
|
775
|
+
};
|
|
764
776
|
`);
|
|
765
777
|
});
|
|
766
778
|
});
|
package/src/bin/init.ts
CHANGED
|
@@ -74,6 +74,8 @@ export const initOrchidORM = async (config: InitConfig) => {
|
|
|
74
74
|
await setupMigrationScript(config);
|
|
75
75
|
await createMigrations(config);
|
|
76
76
|
await createSeed();
|
|
77
|
+
|
|
78
|
+
greet();
|
|
77
79
|
};
|
|
78
80
|
|
|
79
81
|
const setupPackageJson = async (config: InitConfig) => {
|
|
@@ -81,6 +83,7 @@ const setupPackageJson = async (config: InitConfig) => {
|
|
|
81
83
|
getLatestPackageVersion('dotenv', 'dependencies'),
|
|
82
84
|
getLatestPackageVersion('orchid-orm', 'dependencies'),
|
|
83
85
|
getLatestPackageVersion('pqb', 'dependencies'),
|
|
86
|
+
getLatestPackageVersion('pg', 'dependencies'),
|
|
84
87
|
config.addSchemaToZod &&
|
|
85
88
|
getLatestPackageVersion('orchid-orm-schema-to-zod', 'dependencies'),
|
|
86
89
|
getLatestPackageVersion('rake-db', 'devDependencies'),
|
|
@@ -213,14 +216,16 @@ const setupBaseTable = async (config: InitConfig) => {
|
|
|
213
216
|
|
|
214
217
|
export const BaseTable = createBaseTable({
|
|
215
218
|
columnTypes: (t) => ({
|
|
216
|
-
|
|
219
|
+
...t,
|
|
220
|
+
text: (min = 0, max = Infinity) => t.text(min, max),`;
|
|
217
221
|
|
|
218
222
|
const { timestamp } = config;
|
|
219
223
|
if (timestamp) {
|
|
220
224
|
content += `
|
|
221
|
-
timestamp: <P extends number>(precision?: P) =>
|
|
222
|
-
timestamp
|
|
223
|
-
|
|
225
|
+
timestamp: <P extends number>(precision?: P) =>
|
|
226
|
+
t.timestamp<P>(precision).${
|
|
227
|
+
timestamp === 'date' ? 'asDate' : 'asNumber'
|
|
228
|
+
}(),`;
|
|
224
229
|
}
|
|
225
230
|
|
|
226
231
|
content += `
|
|
@@ -247,7 +252,7 @@ ${
|
|
|
247
252
|
: ''
|
|
248
253
|
}
|
|
249
254
|
export type Post = PostTable['columns']['type'];
|
|
250
|
-
class PostTable extends BaseTable {
|
|
255
|
+
export class PostTable extends BaseTable {
|
|
251
256
|
table = 'post';
|
|
252
257
|
columns = this.setColumns((t) => ({
|
|
253
258
|
id: t.serial().primaryKey(),
|
|
@@ -255,13 +260,13 @@ class PostTable extends BaseTable {
|
|
|
255
260
|
text: t.text(20, 10000),
|
|
256
261
|
...t.timestamps(),
|
|
257
262
|
}));
|
|
258
|
-
|
|
263
|
+
|
|
259
264
|
relations = {
|
|
260
265
|
comments: this.hasMany(() => CommentTable, {
|
|
261
266
|
primaryKey: 'id',
|
|
262
267
|
foreignKey: 'postId',
|
|
263
268
|
}),
|
|
264
|
-
}
|
|
269
|
+
};
|
|
265
270
|
}
|
|
266
271
|
${
|
|
267
272
|
config.addSchemaToZod
|
|
@@ -280,21 +285,24 @@ ${
|
|
|
280
285
|
: ''
|
|
281
286
|
}
|
|
282
287
|
export type Comment = CommentTable['columns']['type'];
|
|
283
|
-
class CommentTable extends BaseTable {
|
|
288
|
+
export class CommentTable extends BaseTable {
|
|
284
289
|
table = 'comment';
|
|
285
290
|
columns = this.setColumns((t) => ({
|
|
286
291
|
id: t.serial().primaryKey(),
|
|
287
|
-
postId: t
|
|
292
|
+
postId: t
|
|
293
|
+
.integer()
|
|
294
|
+
.foreignKey(() => PostTable, 'id')
|
|
295
|
+
.index(),
|
|
288
296
|
text: t.text(5, 1000),
|
|
289
297
|
...t.timestamps(),
|
|
290
298
|
}));
|
|
291
|
-
|
|
299
|
+
|
|
292
300
|
relations = {
|
|
293
301
|
post: this.belongsTo(() => PostTable, {
|
|
294
302
|
primaryKey: 'id',
|
|
295
303
|
foreignKey: 'postId',
|
|
296
304
|
}),
|
|
297
|
-
}
|
|
305
|
+
};
|
|
298
306
|
}
|
|
299
307
|
${
|
|
300
308
|
config.addSchemaToZod
|
|
@@ -345,7 +353,7 @@ export const config = {`;
|
|
|
345
353
|
database,`;
|
|
346
354
|
}
|
|
347
355
|
content += `
|
|
348
|
-
}
|
|
356
|
+
};
|
|
349
357
|
`;
|
|
350
358
|
|
|
351
359
|
await fs.writeFile(configPath, content);
|
|
@@ -356,11 +364,11 @@ const setupMainDb = async (config: InitConfig) => {
|
|
|
356
364
|
let tables = '';
|
|
357
365
|
if (config.demoTables) {
|
|
358
366
|
imports += `
|
|
359
|
-
import {
|
|
360
|
-
import {
|
|
367
|
+
import { PostTable } from './tables/post.table';
|
|
368
|
+
import { CommentTable } from './tables/comment.table';`;
|
|
361
369
|
tables += `
|
|
362
|
-
|
|
363
|
-
|
|
370
|
+
post: PostTable,
|
|
371
|
+
comment: CommentTable,`;
|
|
364
372
|
}
|
|
365
373
|
|
|
366
374
|
const dbPath = path.join(dirPath, 'db.ts');
|
|
@@ -369,11 +377,8 @@ import { Comment } from './tables/comment.table';`;
|
|
|
369
377
|
`import { orchidORM } from 'orchid-orm';
|
|
370
378
|
import { config } from './config';${imports}
|
|
371
379
|
|
|
372
|
-
export const db = orchidORM(
|
|
373
|
-
|
|
374
|
-
{${tables}
|
|
375
|
-
}
|
|
376
|
-
);
|
|
380
|
+
export const db = orchidORM(config.database, {${tables}
|
|
381
|
+
});
|
|
377
382
|
`,
|
|
378
383
|
);
|
|
379
384
|
};
|
|
@@ -389,15 +394,16 @@ import { appCodeUpdater } from 'orchid-orm';
|
|
|
389
394
|
rakeDb(${config.testDatabase ? 'config.allDatabases' : 'config.database'}, {
|
|
390
395
|
migrationsPath: 'src/db/migrations',
|
|
391
396
|
appCodeUpdater: appCodeUpdater({
|
|
392
|
-
tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
|
|
397
|
+
tablePath: (tableName) => \`src/db/tables/\${tableName}.table.ts\`,
|
|
393
398
|
baseTablePath: 'src/db/baseTable.ts',
|
|
394
399
|
baseTableName: 'BaseTable',
|
|
395
400
|
mainFilePath: 'src/db/db.ts',
|
|
396
401
|
}),
|
|
402
|
+
useCodeUpdater: true, // set to false to disable code updater
|
|
397
403
|
commands: {
|
|
398
404
|
async seed() {
|
|
399
|
-
const {
|
|
400
|
-
await
|
|
405
|
+
const { seed } = await import('./seed');
|
|
406
|
+
await seed();
|
|
401
407
|
},
|
|
402
408
|
},
|
|
403
409
|
});
|
|
@@ -411,9 +417,11 @@ const createMigrations = async (config: InitConfig) => {
|
|
|
411
417
|
|
|
412
418
|
if (!config.demoTables) return;
|
|
413
419
|
|
|
420
|
+
const now = new Date();
|
|
421
|
+
|
|
414
422
|
const postPath = path.join(
|
|
415
423
|
migrationsPath,
|
|
416
|
-
`${makeFileTimeStamp()}_createPost.ts`,
|
|
424
|
+
`${makeFileTimeStamp(now)}_createPost.ts`,
|
|
417
425
|
);
|
|
418
426
|
await fs.writeFile(
|
|
419
427
|
postPath,
|
|
@@ -430,9 +438,11 @@ change(async (db) => {
|
|
|
430
438
|
`,
|
|
431
439
|
);
|
|
432
440
|
|
|
441
|
+
now.setTime(now.getTime() + 1000);
|
|
442
|
+
|
|
433
443
|
const commentPath = path.join(
|
|
434
444
|
migrationsPath,
|
|
435
|
-
`${makeFileTimeStamp()}_createComment.ts`,
|
|
445
|
+
`${makeFileTimeStamp(now)}_createComment.ts`,
|
|
436
446
|
);
|
|
437
447
|
await fs.writeFile(
|
|
438
448
|
commentPath,
|
|
@@ -450,8 +460,7 @@ change(async (db) => {
|
|
|
450
460
|
);
|
|
451
461
|
};
|
|
452
462
|
|
|
453
|
-
const makeFileTimeStamp = () => {
|
|
454
|
-
const now = new Date();
|
|
463
|
+
const makeFileTimeStamp = (now: Date) => {
|
|
455
464
|
return [
|
|
456
465
|
now.getUTCFullYear(),
|
|
457
466
|
now.getUTCMonth() + 1,
|
|
@@ -473,8 +482,25 @@ const createSeed = async () => {
|
|
|
473
482
|
export const seed = async () => {
|
|
474
483
|
// create records here
|
|
475
484
|
|
|
476
|
-
await db
|
|
477
|
-
}
|
|
485
|
+
await db.$close();
|
|
486
|
+
};
|
|
478
487
|
`,
|
|
479
488
|
);
|
|
480
489
|
};
|
|
490
|
+
|
|
491
|
+
const greet = () => {
|
|
492
|
+
console.log(`Thank you for trying Orchid ORM!
|
|
493
|
+
|
|
494
|
+
To finish setup, install dependencies:
|
|
495
|
+
|
|
496
|
+
> npm i
|
|
497
|
+
|
|
498
|
+
Enter the correct database credentials to the .env file,
|
|
499
|
+
then create the database:
|
|
500
|
+
|
|
501
|
+
> npm run db create
|
|
502
|
+
|
|
503
|
+
And run the migrations:
|
|
504
|
+
|
|
505
|
+
> npm run db migrate`);
|
|
506
|
+
};
|
package/src/codegen/testUtils.ts
CHANGED
|
@@ -4,13 +4,14 @@ import path from 'path';
|
|
|
4
4
|
import fs from 'fs/promises';
|
|
5
5
|
|
|
6
6
|
export const asMock = (fn: unknown) => fn as jest.Mock;
|
|
7
|
-
export const tablePath = (table: string) =>
|
|
7
|
+
export const tablePath = (table: string) =>
|
|
8
|
+
path.resolve(`tables/${table}.table.ts`);
|
|
8
9
|
|
|
9
10
|
const makeAst = () => {
|
|
10
11
|
const addTable: RakeDbAst.Table = {
|
|
11
12
|
type: 'table',
|
|
12
13
|
action: 'create',
|
|
13
|
-
name: '
|
|
14
|
+
name: 'some',
|
|
14
15
|
shape: {
|
|
15
16
|
id: columnTypes.serial().primaryKey(),
|
|
16
17
|
},
|
|
@@ -26,8 +27,8 @@ const makeAst = () => {
|
|
|
26
27
|
|
|
27
28
|
const renameTable: RakeDbAst.RenameTable = {
|
|
28
29
|
type: 'renameTable',
|
|
29
|
-
from: '
|
|
30
|
-
to: '
|
|
30
|
+
from: 'some',
|
|
31
|
+
to: 'another',
|
|
31
32
|
};
|
|
32
33
|
|
|
33
34
|
const tableData = {
|
|
@@ -37,7 +38,7 @@ const makeAst = () => {
|
|
|
37
38
|
|
|
38
39
|
const changeTable: RakeDbAst.ChangeTable = {
|
|
39
40
|
type: 'changeTable',
|
|
40
|
-
name: '
|
|
41
|
+
name: 'some',
|
|
41
42
|
shape: {},
|
|
42
43
|
add: tableData,
|
|
43
44
|
drop: tableData,
|
|
@@ -31,14 +31,14 @@ describe('updateMainFile', () => {
|
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
testWritten(`import { orchidORM } from 'orchid-orm';
|
|
34
|
-
import {
|
|
34
|
+
import { SomeTable } from './tables/some.table';
|
|
35
35
|
|
|
36
36
|
export const db = orchidORM(
|
|
37
37
|
{
|
|
38
38
|
databaseURL: 'url',
|
|
39
39
|
},
|
|
40
40
|
{
|
|
41
|
-
|
|
41
|
+
some: SomeTable,
|
|
42
42
|
}
|
|
43
43
|
);
|
|
44
44
|
`);
|
|
@@ -55,10 +55,10 @@ export const db = orchidORM({}, {});
|
|
|
55
55
|
|
|
56
56
|
testWritten(`
|
|
57
57
|
import { orchidORM } from 'orchid-orm';
|
|
58
|
-
import {
|
|
58
|
+
import { SomeTable } from './tables/some.table';
|
|
59
59
|
|
|
60
60
|
export const db = orchidORM({}, {
|
|
61
|
-
|
|
61
|
+
some: SomeTable,
|
|
62
62
|
});
|
|
63
63
|
`);
|
|
64
64
|
});
|
|
@@ -74,10 +74,10 @@ export const db = custom({}, {});
|
|
|
74
74
|
|
|
75
75
|
testWritten(`
|
|
76
76
|
import { orchidORM as custom } from 'orchid-orm';
|
|
77
|
-
import {
|
|
77
|
+
import { SomeTable } from './tables/some.table';
|
|
78
78
|
|
|
79
79
|
export const db = custom({}, {
|
|
80
|
-
|
|
80
|
+
some: SomeTable,
|
|
81
81
|
});
|
|
82
82
|
`);
|
|
83
83
|
});
|
|
@@ -97,11 +97,11 @@ export const db = orchidORM({}, {
|
|
|
97
97
|
testWritten(`
|
|
98
98
|
import { orchidORM } from 'orchid-orm';
|
|
99
99
|
import { Some } from './tables/some';
|
|
100
|
-
import {
|
|
100
|
+
import { SomeTable } from './tables/some.table';
|
|
101
101
|
|
|
102
102
|
export const db = orchidORM({}, {
|
|
103
103
|
some: Some,
|
|
104
|
-
|
|
104
|
+
some: SomeTable,
|
|
105
105
|
});
|
|
106
106
|
`);
|
|
107
107
|
});
|
|
@@ -121,11 +121,11 @@ export const db = orchidORM({}, {
|
|
|
121
121
|
testWritten(`
|
|
122
122
|
import { orchidORM } from 'orchid-orm';
|
|
123
123
|
import { Some } from './tables/some';
|
|
124
|
-
import {
|
|
124
|
+
import { SomeTable } from './tables/some.table';
|
|
125
125
|
|
|
126
126
|
export const db = orchidORM({}, {
|
|
127
127
|
some: Some,
|
|
128
|
-
|
|
128
|
+
some: SomeTable,
|
|
129
129
|
});
|
|
130
130
|
`);
|
|
131
131
|
});
|
|
@@ -135,10 +135,10 @@ export const db = orchidORM({}, {
|
|
|
135
135
|
it('should remove table', async () => {
|
|
136
136
|
asMock(fs.readFile).mockResolvedValue(`
|
|
137
137
|
import { orchidORM } from 'orchid-orm';
|
|
138
|
-
import {
|
|
138
|
+
import { SomeTable } from './tables/some.table';
|
|
139
139
|
|
|
140
140
|
export const db = orchidORM({}, {
|
|
141
|
-
|
|
141
|
+
some: SomeTable,
|
|
142
142
|
});
|
|
143
143
|
`);
|
|
144
144
|
|
|
@@ -155,7 +155,7 @@ export const db = orchidORM({}, {
|
|
|
155
155
|
it('should remove aliased import', async () => {
|
|
156
156
|
asMock(fs.readFile).mockResolvedValue(`
|
|
157
157
|
import { orchidORM } from 'orchid-orm';
|
|
158
|
-
import {
|
|
158
|
+
import { SomeTable as koko } from './tables/some.table';
|
|
159
159
|
|
|
160
160
|
export const db = orchidORM({}, {
|
|
161
161
|
koko: koko,
|
|
@@ -175,7 +175,7 @@ export const db = orchidORM({}, {
|
|
|
175
175
|
it('should remove short form of key and value', async () => {
|
|
176
176
|
asMock(fs.readFile).mockResolvedValue(`
|
|
177
177
|
import { orchidORM } from 'orchid-orm';
|
|
178
|
-
import {
|
|
178
|
+
import { SomeTable as koko } from './tables/some.table';
|
|
179
179
|
|
|
180
180
|
export const db = orchidORM({}, {
|
|
181
181
|
koko,
|
|
@@ -196,12 +196,12 @@ export const db = orchidORM({}, {
|
|
|
196
196
|
asMock(fs.readFile).mockResolvedValue(`
|
|
197
197
|
import { orchidORM } from 'orchid-orm';
|
|
198
198
|
import { One } from './tables/one';
|
|
199
|
-
import {
|
|
199
|
+
import { SomeTable } from './tables/some.table';
|
|
200
200
|
import { Two } from './tables/two';
|
|
201
201
|
|
|
202
202
|
export const db = orchidORM({}, {
|
|
203
203
|
one,
|
|
204
|
-
|
|
204
|
+
some: SomeTable,
|
|
205
205
|
two,
|
|
206
206
|
});
|
|
207
207
|
`);
|
|
@@ -117,7 +117,7 @@ const createTable = (
|
|
|
117
117
|
ast: RakeDbAst.Table,
|
|
118
118
|
) => {
|
|
119
119
|
const key = toCamelCase(ast.name);
|
|
120
|
-
const value = toPascalCase(ast.name)
|
|
120
|
+
const value = `${toPascalCase(ast.name)}Table`;
|
|
121
121
|
|
|
122
122
|
const changes = new FileChanges(content);
|
|
123
123
|
|
|
@@ -147,7 +147,7 @@ const dropTable = (
|
|
|
147
147
|
const changes = new FileChanges(content);
|
|
148
148
|
|
|
149
149
|
const importPath = getImportPath(filePath, tablePath(ast.name));
|
|
150
|
-
const tableClassName = toPascalCase(ast.name)
|
|
150
|
+
const tableClassName = `${toPascalCase(ast.name)}Table`;
|
|
151
151
|
const importNames: string[] = [];
|
|
152
152
|
for (const node of ts.import.iterateWithSource(statements, importPath)) {
|
|
153
153
|
changes.remove(node.pos, node.end);
|