orchid-orm 1.5.11 → 1.5.14

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.
@@ -0,0 +1,767 @@
1
+ import { initOrchidORM } from './init';
2
+ import fs from 'fs/promises';
3
+ import { asMock } from '../codegen/testUtils';
4
+ import path from 'path';
5
+
6
+ jest.mock('https', () => ({
7
+ get(
8
+ this: { result: string },
9
+ _: string,
10
+ cb: (res: {
11
+ on(event: string, cb: (chunk?: string) => void): void;
12
+ }) => void,
13
+ ) {
14
+ cb({
15
+ on: (event: string, cb: (chunk?: string) => void) => {
16
+ if (event === 'data') {
17
+ cb(`{"version":"1.2.3"}`);
18
+ } else if (event === 'end') {
19
+ cb();
20
+ }
21
+ },
22
+ });
23
+ },
24
+ }));
25
+
26
+ jest.mock('fs/promises', () => ({
27
+ readFile: jest.fn(),
28
+ writeFile: jest.fn(),
29
+ mkdir: jest.fn(),
30
+ }));
31
+
32
+ class EnoentError extends Error {
33
+ code = 'ENOENT';
34
+ }
35
+
36
+ const packageJSONPath = path.resolve(process.cwd(), 'package.json');
37
+ const tsConfigPath = path.resolve(process.cwd(), 'tsconfig.json');
38
+ const envPath = path.resolve(process.cwd(), '.env');
39
+ const gitignorePath = path.resolve(process.cwd(), '.gitignore');
40
+ const dbDirPath = path.resolve(process.cwd(), 'src', 'db');
41
+ const baseTablePath = path.resolve(dbDirPath, 'baseTable.ts');
42
+ const tablesDir = path.resolve(dbDirPath, 'tables');
43
+ const postTablePath = path.resolve(tablesDir, 'post.table.ts');
44
+ const commentTablePath = path.resolve(tablesDir, 'comment.table.ts');
45
+ const configPath = path.join(dbDirPath, 'config.ts');
46
+ const dbPath = path.join(dbDirPath, 'db.ts');
47
+ const migrationScriptPath = path.join(dbDirPath, 'dbScripts.ts');
48
+ const migrationsPath = path.join(dbDirPath, 'migrations');
49
+ const seedPath = path.join(dbDirPath, 'seed.ts');
50
+
51
+ describe('initOrchidORM', () => {
52
+ beforeEach(jest.clearAllMocks);
53
+
54
+ it('should create db directory', async () => {
55
+ await initOrchidORM({});
56
+
57
+ expect(fs.mkdir).toBeCalledWith(dbDirPath, { recursive: true });
58
+ });
59
+
60
+ describe('package.json', () => {
61
+ const packageJSONWithoutAdditional = `{
62
+ "scripts": {
63
+ "db": "ts-node src/db/dbScripts.ts"
64
+ },
65
+ "dependencies": {
66
+ "dotenv": "^1.2.3",
67
+ "orchid-orm": "^1.2.3",
68
+ "pqb": "^1.2.3"
69
+ },
70
+ "devDependencies": {
71
+ "rake-db": "^1.2.3",
72
+ "@swc/core": "^1.2.3",
73
+ "@types/node": "^1.2.3",
74
+ "ts-node": "^1.2.3",
75
+ "typescript": "^1.2.3"
76
+ }
77
+ }
78
+ `;
79
+
80
+ const fullPackageJSON = `{
81
+ "scripts": {
82
+ "db": "ts-node src/db/dbScripts.ts"
83
+ },
84
+ "dependencies": {
85
+ "dotenv": "^1.2.3",
86
+ "orchid-orm": "^1.2.3",
87
+ "pqb": "^1.2.3",
88
+ "orchid-orm-schema-to-zod": "^1.2.3"
89
+ },
90
+ "devDependencies": {
91
+ "rake-db": "^1.2.3",
92
+ "orchid-orm-test-factory": "^1.2.3",
93
+ "@swc/core": "^1.2.3",
94
+ "@types/node": "^1.2.3",
95
+ "ts-node": "^1.2.3",
96
+ "typescript": "^1.2.3"
97
+ }
98
+ }
99
+ `;
100
+
101
+ it('should create package.json if not exist', async () => {
102
+ asMock(fs.readFile).mockImplementation((path: string) => {
103
+ if (path.endsWith('package.json')) {
104
+ throw new EnoentError();
105
+ }
106
+ });
107
+
108
+ await initOrchidORM({});
109
+
110
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
111
+ ([to]) => to === packageJSONPath,
112
+ );
113
+ expect(content).toBe(packageJSONWithoutAdditional);
114
+ });
115
+
116
+ it('should create package.json with additional deps if not exist', async () => {
117
+ asMock(fs.readFile).mockImplementation((path: string) => {
118
+ if (path.endsWith('package.json')) {
119
+ throw new EnoentError();
120
+ }
121
+ });
122
+
123
+ await initOrchidORM({
124
+ addSchemaToZod: true,
125
+ addTestFactory: true,
126
+ });
127
+
128
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
129
+ ([to]) => to === packageJSONPath,
130
+ );
131
+ expect(content).toBe(fullPackageJSON);
132
+ });
133
+
134
+ it('should add scripts, dependencies and devDependencies if they are not present in package.json', async () => {
135
+ asMock(fs.readFile).mockImplementation((path: string) => {
136
+ if (path.endsWith('package.json')) {
137
+ return '{}';
138
+ }
139
+ return;
140
+ });
141
+
142
+ await initOrchidORM({
143
+ addSchemaToZod: true,
144
+ addTestFactory: true,
145
+ });
146
+
147
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
148
+ ([to]) => to === packageJSONPath,
149
+ );
150
+ expect(content).toBe(fullPackageJSON);
151
+ });
152
+
153
+ it('should insert scripts and dependencies', async () => {
154
+ asMock(fs.readFile).mockImplementation((path: string) => {
155
+ if (path.endsWith('package.json')) {
156
+ return `{
157
+ "scripts": {
158
+ "ko": "ko"
159
+ },
160
+ "dependencies": {
161
+ "ko": "ko"
162
+ },
163
+ "devDependencies": {
164
+ "ko": "ko"
165
+ }
166
+ }`;
167
+ }
168
+ return;
169
+ });
170
+
171
+ await initOrchidORM({
172
+ addSchemaToZod: true,
173
+ addTestFactory: true,
174
+ });
175
+
176
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
177
+ ([to]) => to === packageJSONPath,
178
+ );
179
+ expect(content).toBe(
180
+ `{
181
+ "scripts": {
182
+ "ko": "ko",
183
+ "db": "ts-node src/db/dbScripts.ts"
184
+ },
185
+ "dependencies": {
186
+ "ko": "ko",
187
+ "dotenv": "^1.2.3",
188
+ "orchid-orm": "^1.2.3",
189
+ "pqb": "^1.2.3",
190
+ "orchid-orm-schema-to-zod": "^1.2.3"
191
+ },
192
+ "devDependencies": {
193
+ "ko": "ko",
194
+ "rake-db": "^1.2.3",
195
+ "orchid-orm-test-factory": "^1.2.3",
196
+ "@swc/core": "^1.2.3",
197
+ "@types/node": "^1.2.3",
198
+ "ts-node": "^1.2.3",
199
+ "typescript": "^1.2.3"
200
+ }
201
+ }
202
+ `,
203
+ );
204
+ });
205
+ });
206
+
207
+ describe('tsconfig.json', () => {
208
+ it('should create tsconfig.json if not not exist', async () => {
209
+ await initOrchidORM({});
210
+
211
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
212
+ ([to]) => to === tsConfigPath,
213
+ );
214
+ expect(content).toBe(`{
215
+ "ts-node": {
216
+ "swc": true
217
+ },
218
+ "compilerOptions": {
219
+ "strict": true
220
+ }
221
+ }
222
+ `);
223
+ });
224
+
225
+ it('should update tsconfig.json if it exists', async () => {
226
+ asMock(fs.readFile).mockImplementation((path: string) => {
227
+ if (path.endsWith('tsconfig.json')) {
228
+ return `{"compilerOptions":{"ko":"ko"}}`;
229
+ }
230
+ return;
231
+ });
232
+
233
+ await initOrchidORM({});
234
+
235
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
236
+ ([to]) => to === tsConfigPath,
237
+ );
238
+ expect(content).toBe(`{
239
+ "compilerOptions": {
240
+ "ko": "ko",
241
+ "strict": true
242
+ },
243
+ "ts-node": {
244
+ "swc": true
245
+ }
246
+ }
247
+ `);
248
+ });
249
+ });
250
+
251
+ describe('.env', () => {
252
+ it('should create .env if not exist', async () => {
253
+ asMock(fs.readFile).mockImplementation((path: string) => {
254
+ if (path.endsWith('.env')) {
255
+ throw new EnoentError();
256
+ }
257
+ });
258
+
259
+ await initOrchidORM({});
260
+
261
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
262
+ ([to]) => to === envPath,
263
+ );
264
+ expect(content)
265
+ .toBe(`DATABASE_URL=postgres://user:password@localhost:5432/dbname?ssl=false
266
+ `);
267
+ });
268
+
269
+ it('should append DATABASE_URL to existing .env', async () => {
270
+ asMock(fs.readFile).mockImplementation((path: string) => {
271
+ if (path.endsWith('.env')) {
272
+ return 'KO=KO';
273
+ }
274
+ return '';
275
+ });
276
+
277
+ await initOrchidORM({});
278
+
279
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
280
+ ([to]) => to === envPath,
281
+ );
282
+ expect(content).toBe(`KO=KO
283
+ DATABASE_URL=postgres://user:password@localhost:5432/dbname?ssl=false
284
+ `);
285
+ });
286
+
287
+ it('should append DATABASE_TEST_URL if testDatabase specified', async () => {
288
+ asMock(fs.readFile).mockImplementation((path: string) => {
289
+ if (path.endsWith('.env')) {
290
+ return 'KO=KO';
291
+ }
292
+ return '';
293
+ });
294
+
295
+ await initOrchidORM({
296
+ testDatabase: true,
297
+ });
298
+
299
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
300
+ ([to]) => to === envPath,
301
+ );
302
+ expect(content).toBe(`KO=KO
303
+ DATABASE_URL=postgres://user:password@localhost:5432/dbname?ssl=false
304
+ DATABASE_TEST_URL=postgres://user:password@localhost:5432/dbname-test?ssl=false
305
+ `);
306
+ });
307
+ });
308
+
309
+ describe('.gitignore', () => {
310
+ it('should create .gitignore if not exists', async () => {
311
+ asMock(fs.readFile).mockImplementation((path: string) => {
312
+ if (path.endsWith('.gitignore')) {
313
+ throw new EnoentError();
314
+ }
315
+ });
316
+
317
+ await initOrchidORM({});
318
+
319
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
320
+ ([to]) => to === gitignorePath,
321
+ );
322
+ expect(content).toBe(`node_modules
323
+ .env
324
+ `);
325
+ });
326
+
327
+ it('should append missing entries if .gitignore exists', async () => {
328
+ asMock(fs.readFile).mockImplementation((path: string) => {
329
+ if (path.endsWith('.gitignore')) {
330
+ return 'node_modules/\nko';
331
+ }
332
+ return;
333
+ });
334
+
335
+ await initOrchidORM({});
336
+
337
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
338
+ ([to]) => to === gitignorePath,
339
+ );
340
+ expect(content).toBe(`node_modules/
341
+ ko
342
+ .env
343
+ `);
344
+ });
345
+ });
346
+
347
+ describe('baseTable', () => {
348
+ it('should create base table', async () => {
349
+ await initOrchidORM({});
350
+
351
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
352
+ ([to]) => to === baseTablePath,
353
+ );
354
+ expect(content).toBe(`import { createBaseTable } from 'orchid-orm';
355
+
356
+ export const BaseTable = createBaseTable({
357
+ columnTypes: (t) => ({
358
+ text: (min: 0, max: Infinity) => t.text(min, max),
359
+ }),
360
+ });
361
+ `);
362
+ });
363
+
364
+ it('should create base table with timestamp as date', async () => {
365
+ await initOrchidORM({
366
+ timestamp: 'date',
367
+ });
368
+
369
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
370
+ ([to]) => to === baseTablePath,
371
+ );
372
+ expect(content).toBe(`import { createBaseTable } from 'orchid-orm';
373
+
374
+ export const BaseTable = createBaseTable({
375
+ columnTypes: (t) => ({
376
+ text: (min: 0, max: Infinity) => t.text(min, max),
377
+ timestamp: <P extends number>(precision?: P) => t.timestamp<P>(precision).asDate(),
378
+ }),
379
+ });
380
+ `);
381
+ });
382
+
383
+ it('should create base table with timestamp as number', async () => {
384
+ await initOrchidORM({
385
+ timestamp: 'number',
386
+ });
387
+
388
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
389
+ ([to]) => to === baseTablePath,
390
+ );
391
+ expect(content).toBe(`import { createBaseTable } from 'orchid-orm';
392
+
393
+ export const BaseTable = createBaseTable({
394
+ columnTypes: (t) => ({
395
+ text: (min: 0, max: Infinity) => t.text(min, max),
396
+ timestamp: <P extends number>(precision?: P) => t.timestamp<P>(precision).asNumber(),
397
+ }),
398
+ });
399
+ `);
400
+ });
401
+ });
402
+
403
+ describe('tables', () => {
404
+ it('should do nothing if demoTables is not specified', async () => {
405
+ await initOrchidORM({});
406
+
407
+ expect(fs.mkdir).not.toBeCalledWith(tablesDir, { recursive: true });
408
+ });
409
+
410
+ it('should create tables dir', async () => {
411
+ await initOrchidORM({
412
+ demoTables: true,
413
+ });
414
+
415
+ expect(fs.mkdir).toBeCalledWith(tablesDir, { recursive: true });
416
+ });
417
+
418
+ it('should create post table', async () => {
419
+ await initOrchidORM({
420
+ demoTables: true,
421
+ });
422
+
423
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
424
+ ([to]) => to === postTablePath,
425
+ );
426
+ expect(content).toBe(`import { BaseTable } from '../baseTable';
427
+ import { CommentTable } from './comment.table';
428
+
429
+ export type Post = PostTable['columns']['type'];
430
+ class PostTable extends BaseTable {
431
+ table = 'post';
432
+ columns = this.setColumns((t) => ({
433
+ id: t.serial().primaryKey(),
434
+ title: t.text(3, 100),
435
+ text: t.text(20, 10000),
436
+ ...t.timestamps(),
437
+ }));
438
+
439
+ relations = {
440
+ comments: this.hasMany(() => CommentTable, {
441
+ primaryKey: 'id',
442
+ foreignKey: 'postId',
443
+ }),
444
+ }
445
+ }
446
+ `);
447
+ });
448
+
449
+ it('should create post table with zod schema', async () => {
450
+ await initOrchidORM({
451
+ demoTables: true,
452
+ addSchemaToZod: true,
453
+ });
454
+
455
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
456
+ ([to]) => to === postTablePath,
457
+ );
458
+ expect(content).toBe(`import { BaseTable } from '../baseTable';
459
+ import { CommentTable } from './comment.table';
460
+ import { tableToZod } from 'orchid-orm-schema-to-zod';
461
+
462
+ export type Post = PostTable['columns']['type'];
463
+ class PostTable extends BaseTable {
464
+ table = 'post';
465
+ columns = this.setColumns((t) => ({
466
+ id: t.serial().primaryKey(),
467
+ title: t.text(3, 100),
468
+ text: t.text(20, 10000),
469
+ ...t.timestamps(),
470
+ }));
471
+
472
+ relations = {
473
+ comments: this.hasMany(() => CommentTable, {
474
+ primaryKey: 'id',
475
+ foreignKey: 'postId',
476
+ }),
477
+ }
478
+ }
479
+
480
+ export const postSchema = tableToZod(PostTable);
481
+ `);
482
+ });
483
+
484
+ it('should create comment table', async () => {
485
+ await initOrchidORM({
486
+ demoTables: true,
487
+ });
488
+
489
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
490
+ ([to]) => to === commentTablePath,
491
+ );
492
+ expect(content).toBe(`import { BaseTable } from '../baseTable';
493
+ import { PostTable } from './post.table';
494
+
495
+ export type Comment = CommentTable['columns']['type'];
496
+ class CommentTable extends BaseTable {
497
+ table = 'comment';
498
+ columns = this.setColumns((t) => ({
499
+ id: t.serial().primaryKey(),
500
+ postId: t.integer().foreignKey(() => PostTable, 'id').index(),
501
+ text: t.text(5, 1000),
502
+ ...t.timestamps(),
503
+ }));
504
+
505
+ relations = {
506
+ post: this.belongsTo(() => PostTable, {
507
+ primaryKey: 'id',
508
+ foreignKey: 'postId',
509
+ }),
510
+ }
511
+ }
512
+ `);
513
+ });
514
+
515
+ it('should create post table with zod schema', async () => {
516
+ await initOrchidORM({
517
+ demoTables: true,
518
+ addSchemaToZod: true,
519
+ });
520
+
521
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
522
+ ([to]) => to === commentTablePath,
523
+ );
524
+ expect(content).toBe(`import { BaseTable } from '../baseTable';
525
+ import { PostTable } from './post.table';
526
+ import { tableToZod } from 'orchid-orm-schema-to-zod';
527
+
528
+ export type Comment = CommentTable['columns']['type'];
529
+ class CommentTable extends BaseTable {
530
+ table = 'comment';
531
+ columns = this.setColumns((t) => ({
532
+ id: t.serial().primaryKey(),
533
+ postId: t.integer().foreignKey(() => PostTable, 'id').index(),
534
+ text: t.text(5, 1000),
535
+ ...t.timestamps(),
536
+ }));
537
+
538
+ relations = {
539
+ post: this.belongsTo(() => PostTable, {
540
+ primaryKey: 'id',
541
+ foreignKey: 'postId',
542
+ }),
543
+ }
544
+ }
545
+
546
+ export const commentSchema = tableToZod(CommentTable);
547
+ `);
548
+ });
549
+ });
550
+
551
+ describe('config', () => {
552
+ it('should create config file', async () => {
553
+ await initOrchidORM({});
554
+
555
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
556
+ ([to]) => to === configPath,
557
+ );
558
+ expect(content).toBe(`import 'dotenv/config';
559
+
560
+ const database = {
561
+ databaseURL: process.env.DATABASE_URL,
562
+ };
563
+ if (!database.databaseURL) throw new Error('DATABASE_URL is missing in .env');
564
+
565
+ export const config = {
566
+ database,
567
+ }
568
+ `);
569
+ });
570
+
571
+ it('should add test database config if specified', async () => {
572
+ await initOrchidORM({
573
+ testDatabase: true,
574
+ });
575
+
576
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
577
+ ([to]) => to === configPath,
578
+ );
579
+ expect(content).toBe(`import 'dotenv/config';
580
+
581
+ const database = {
582
+ databaseURL: process.env.DATABASE_URL,
583
+ };
584
+ if (!database.databaseURL) throw new Error('DATABASE_URL is missing in .env');
585
+
586
+ const testDatabase = {
587
+ databaseURL: process.env.DATABASE_TEST_URL,
588
+ };
589
+
590
+ const allDatabases = [database];
591
+
592
+ if (testDatabase.databaseURL) {
593
+ allDatabases.push(testDatabase);
594
+ }
595
+
596
+ export const config = {
597
+ allDatabases,
598
+ database: process.env.NODE_ENV === 'test' ? testDatabase : database,
599
+ }
600
+ `);
601
+ });
602
+ });
603
+
604
+ describe('db.ts', () => {
605
+ it('should create db.ts', async () => {
606
+ await initOrchidORM({});
607
+
608
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
609
+ ([to]) => to === dbPath,
610
+ );
611
+ expect(content).toBe(`import { orchidORM } from 'orchid-orm';
612
+ import { config } from './config';
613
+
614
+ export const db = orchidORM(
615
+ config.database,
616
+ {
617
+ }
618
+ );
619
+ `);
620
+ });
621
+
622
+ it('should create db.ts with demo tables', async () => {
623
+ await initOrchidORM({
624
+ demoTables: true,
625
+ });
626
+
627
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
628
+ ([to]) => to === dbPath,
629
+ );
630
+ expect(content).toBe(`import { orchidORM } from 'orchid-orm';
631
+ import { config } from './config';
632
+ import { Post } from './tables/post.table';
633
+ import { Comment } from './tables/comment.table';
634
+
635
+ export const db = orchidORM(
636
+ config.database,
637
+ {
638
+ post: Post,
639
+ comment: Comment,
640
+ }
641
+ );
642
+ `);
643
+ });
644
+ });
645
+
646
+ describe('migrationScript', () => {
647
+ it('should create script', async () => {
648
+ await initOrchidORM({});
649
+
650
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
651
+ ([to]) => to === migrationScriptPath,
652
+ );
653
+ expect(content).toBe(`import { rakeDb } from 'rake-db';
654
+ import { config } from './config';
655
+ import { appCodeUpdater } from 'orchid-orm';
656
+
657
+ rakeDb(config.database, {
658
+ migrationsPath: 'src/db/migrations',
659
+ appCodeUpdater: appCodeUpdater({
660
+ tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
661
+ baseTablePath: 'src/db/baseTable.ts',
662
+ baseTableName: 'BaseTable',
663
+ mainFilePath: 'src/db/db.ts',
664
+ }),
665
+ commands: {
666
+ async seed() {
667
+ const { run } = await import('./seed');
668
+ await run();
669
+ },
670
+ },
671
+ });
672
+ `);
673
+ });
674
+
675
+ it('should create script with multiple databases', async () => {
676
+ await initOrchidORM({
677
+ testDatabase: true,
678
+ });
679
+
680
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
681
+ ([to]) => to === migrationScriptPath,
682
+ );
683
+ expect(content).toBe(`import { rakeDb } from 'rake-db';
684
+ import { config } from './config';
685
+ import { appCodeUpdater } from 'orchid-orm';
686
+
687
+ rakeDb(config.allDatabases, {
688
+ migrationsPath: 'src/db/migrations',
689
+ appCodeUpdater: appCodeUpdater({
690
+ tablePath: (tableName) => \`src/db/tables/\${tableName}.ts\`,
691
+ baseTablePath: 'src/db/baseTable.ts',
692
+ baseTableName: 'BaseTable',
693
+ mainFilePath: 'src/db/db.ts',
694
+ }),
695
+ commands: {
696
+ async seed() {
697
+ const { run } = await import('./seed');
698
+ await run();
699
+ },
700
+ },
701
+ });
702
+ `);
703
+ });
704
+ });
705
+
706
+ describe('migrations', () => {
707
+ it('should create migrations directory', async () => {
708
+ await initOrchidORM({});
709
+
710
+ expect(fs.mkdir).toBeCalledWith(migrationsPath);
711
+ });
712
+
713
+ it('should create migrations if demoTables specified', async () => {
714
+ await initOrchidORM({
715
+ demoTables: true,
716
+ });
717
+
718
+ const [, post] = asMock(fs.writeFile).mock.calls.find(([to]) =>
719
+ to.endsWith('createPost.ts'),
720
+ );
721
+ expect(post).toBe(`import { change } from 'rake-db';
722
+
723
+ change(async (db) => {
724
+ await db.createTable('post', (t) => ({
725
+ id: t.serial().primaryKey(),
726
+ title: t.text(),
727
+ text: t.text(),
728
+ ...t.timestamps(),
729
+ }));
730
+ });
731
+ `);
732
+
733
+ const [, comment] = asMock(fs.writeFile).mock.calls.find(([to]) =>
734
+ to.endsWith('createComment.ts'),
735
+ );
736
+ expect(comment).toBe(`import { change } from 'rake-db';
737
+
738
+ change(async (db) => {
739
+ await db.createTable('comment', (t) => ({
740
+ id: t.serial().primaryKey(),
741
+ postId: t.integer().foreignKey('post', 'id').index(),
742
+ text: t.text(),
743
+ ...t.timestamps(),
744
+ }));
745
+ });
746
+ `);
747
+ });
748
+ });
749
+
750
+ describe('seed', () => {
751
+ it('should create seed file', async () => {
752
+ await initOrchidORM({});
753
+
754
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
755
+ ([to]) => to === seedPath,
756
+ );
757
+ expect(content).toBe(`import { db } from './db';
758
+
759
+ export const seed = async () => {
760
+ // create records here
761
+
762
+ await db.close();
763
+ }
764
+ `);
765
+ });
766
+ });
767
+ });