orchid-orm 1.5.18 → 1.5.20

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.18",
3
+ "version": "1.5.20",
4
4
  "description": "Postgres ORM",
5
5
  "homepage": "https://orchid-orm.netlify.app/guide/orm-setup-and-overview.html",
6
6
  "repository": {
@@ -55,7 +55,7 @@
55
55
  "orchid-orm-schema-to-zod": "0.2.18",
56
56
  "pg": "^8.7.3",
57
57
  "pg-transactional-tests": "^1.0.7",
58
- "rake-db": "2.3.17",
58
+ "rake-db": "2.3.18",
59
59
  "rimraf": "^3.0.2",
60
60
  "rollup": "^2.79.0",
61
61
  "rollup-plugin-dts": "^4.2.2",
@@ -773,6 +773,37 @@ export const seed = async () => {
773
773
 
774
774
  await db.$close();
775
775
  };
776
+ `);
777
+ });
778
+
779
+ it('should create seed file with sample records when demoTables is set to true', async () => {
780
+ await initOrchidORM({
781
+ demoTables: true,
782
+ });
783
+
784
+ const [, content] = asMock(fs.writeFile).mock.calls.find(
785
+ ([to]) => to === seedPath,
786
+ );
787
+ expect(content).toBe(`import { db } from './db';
788
+
789
+ export const seed = async () => {
790
+ await db.post.findBy({ title: 'Sample post' }).orCreate({
791
+ title: 'Post',
792
+ text: 'This is a text for a sample post. It contains words, spaces, and punctuation.',
793
+ comments: {
794
+ create: [
795
+ {
796
+ text: 'Nice post!',
797
+ },
798
+ {
799
+ text: \`Too long, didn't read\`,
800
+ },
801
+ ],
802
+ },
803
+ });
804
+
805
+ await db.$close();
806
+ };
776
807
  `);
777
808
  });
778
809
  });
package/src/bin/init.ts CHANGED
@@ -73,7 +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
+ await createSeed(config);
77
77
 
78
78
  greet();
79
79
  };
@@ -473,14 +473,35 @@ const makeFileTimeStamp = (now: Date) => {
473
473
  .join('');
474
474
  };
475
475
 
476
- const createSeed = async () => {
476
+ const createSeed = async (config: InitConfig) => {
477
477
  const filePath = path.join(dirPath, 'seed.ts');
478
+
479
+ let content;
480
+ if (config.demoTables) {
481
+ content = `await db.post.findBy({ title: 'Sample post' }).orCreate({
482
+ title: 'Post',
483
+ text: 'This is a text for a sample post. It contains words, spaces, and punctuation.',
484
+ comments: {
485
+ create: [
486
+ {
487
+ text: 'Nice post!',
488
+ },
489
+ {
490
+ text: \`Too long, didn't read\`,
491
+ },
492
+ ],
493
+ },
494
+ });`;
495
+ } else {
496
+ content = `// create records here`;
497
+ }
498
+
478
499
  await fs.writeFile(
479
500
  filePath,
480
501
  `import { db } from './db';
481
502
 
482
503
  export const seed = async () => {
483
- // create records here
504
+ ${content}
484
505
 
485
506
  await db.$close();
486
507
  };
@@ -30,10 +30,15 @@ describe('appCodeUpdater', () => {
30
30
  const fn = appCodeUpdater(params);
31
31
 
32
32
  it('should call table and file updaters with proper arguments', async () => {
33
- await fn({ ast: ast.addTable, options: {}, cache: {} });
33
+ await fn({
34
+ ast: ast.addTable,
35
+ options: {},
36
+ basePath: __dirname,
37
+ cache: {},
38
+ });
34
39
 
35
- const mainFilePath = path.resolve(params.mainFilePath);
36
- const tablePath = path.resolve(params.tablePath('table'));
40
+ const mainFilePath = path.resolve(__dirname, params.mainFilePath);
41
+ const tablePath = path.resolve(__dirname, params.tablePath('table'));
37
42
 
38
43
  const main = asMock(updateMainFile).mock.calls[0];
39
44
  expect(main[0]).toBe(mainFilePath);
@@ -55,11 +60,11 @@ describe('appCodeUpdater', () => {
55
60
  const cache = {};
56
61
  expect(createBaseTableFile).not.toBeCalled();
57
62
 
58
- await fn({ ast: ast.addTable, options: {}, cache });
63
+ await fn({ ast: ast.addTable, options: {}, basePath: __dirname, cache });
59
64
 
60
65
  expect(createBaseTableFile).toBeCalledTimes(1);
61
66
 
62
- await fn({ ast: ast.addTable, options: {}, cache });
67
+ await fn({ ast: ast.addTable, options: {}, basePath: __dirname, cache });
63
68
 
64
69
  expect(createBaseTableFile).toBeCalledTimes(1);
65
70
  });
@@ -3,6 +3,7 @@ import * as path from 'path';
3
3
  import { updateMainFile } from './updateMainFile';
4
4
  import { updateTableFile } from './updateTableFile/updateTableFile';
5
5
  import { createBaseTableFile } from './createBaseTableFile';
6
+ import { SetOptional } from 'pqb';
6
7
 
7
8
  export class AppCodeUpdaterError extends Error {}
8
9
 
@@ -14,15 +15,21 @@ export type AppCodeUpdaterConfig = {
14
15
  };
15
16
 
16
17
  export const appCodeUpdater = (
17
- config: AppCodeUpdaterConfig,
18
+ config: SetOptional<AppCodeUpdaterConfig, 'baseTableName'>,
18
19
  ): AppCodeUpdater => {
19
- const params = {
20
- ...config,
21
- tablePath: (name: string) => path.resolve(config.tablePath(name)),
22
- mainFilePath: path.resolve(config.mainFilePath),
23
- };
20
+ return async ({ ast, options, basePath, cache: cacheObject }) => {
21
+ const params: AppCodeUpdaterConfig = {
22
+ ...config,
23
+ baseTableName: config.baseTableName || 'BaseTable',
24
+ tablePath(name: string) {
25
+ const file = config.tablePath(name);
26
+ return path.isAbsolute(file) ? file : path.resolve(basePath, file);
27
+ },
28
+ mainFilePath: path.isAbsolute(config.mainFilePath)
29
+ ? config.mainFilePath
30
+ : path.resolve(basePath, config.mainFilePath),
31
+ };
24
32
 
25
- return async ({ ast, options, cache: cacheObject }) => {
26
33
  const promises: Promise<void>[] = [
27
34
  updateMainFile(params.mainFilePath, params.tablePath, ast, options),
28
35
  updateTableFile({ ...params, ast }),
package/src/orm.test.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { orchidORM } from './orm';
1
+ import { OrchidORM, orchidORM } from './orm';
2
2
  import {
3
3
  assertType,
4
4
  expectSql,
@@ -11,6 +11,14 @@ import { createBaseTable } from './table';
11
11
  describe('orm', () => {
12
12
  useTestDatabase();
13
13
 
14
+ let db:
15
+ | OrchidORM<{ user: typeof UserTable; profile: typeof ProfileTable }>
16
+ | undefined;
17
+
18
+ afterEach(async () => {
19
+ if (db) await db.$close();
20
+ });
21
+
14
22
  const BaseTable = createBaseTable();
15
23
 
16
24
  type User = UserTable['columns']['type'];
@@ -31,7 +39,7 @@ describe('orm', () => {
31
39
  }
32
40
 
33
41
  it('should return object with provided adapter, close and transaction method, tables', () => {
34
- const db = orchidORM(pgConfig, {
42
+ db = orchidORM(pgConfig, {
35
43
  user: UserTable,
36
44
  profile: ProfileTable,
37
45
  });
@@ -45,7 +53,7 @@ describe('orm', () => {
45
53
  });
46
54
 
47
55
  it('should return table which is a queryable interface', async () => {
48
- const db = orchidORM(pgConfig, {
56
+ db = orchidORM(pgConfig, {
49
57
  user: UserTable,
50
58
  profile: ProfileTable,
51
59
  });
@@ -71,7 +79,7 @@ describe('orm', () => {
71
79
  });
72
80
 
73
81
  it('should be able to turn on autoPreparedStatements', () => {
74
- const db = orchidORM(
82
+ db = orchidORM(
75
83
  { ...pgConfig, autoPreparedStatements: true },
76
84
  {
77
85
  user: UserTable,
@@ -4,6 +4,8 @@ import { Client } from 'pg';
4
4
  import { noop } from 'pqb';
5
5
 
6
6
  describe('transaction', () => {
7
+ afterAll(db.$close);
8
+
7
9
  it('should have override transaction method which implicitly connects tables with a single transaction', async () => {
8
10
  const spy = jest.spyOn(Client.prototype, 'query');
9
11