@ghom/orm 1.0.0 → 1.1.0

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/dist/app/orm.js CHANGED
@@ -48,21 +48,25 @@ class ORM extends handler_1.Handler {
48
48
  const tables = await Promise.all(pathList.map(async (filepath) => {
49
49
  return Promise.resolve().then(() => __importStar(require(filepath))).then((file) => file.default);
50
50
  }));
51
- tables.unshift(new table_1.Table({
51
+ const migration = new table_1.Table({
52
52
  name: "migration",
53
53
  priority: Infinity,
54
54
  setup: (table) => {
55
55
  table.string("table").unique().notNullable();
56
56
  table.integer("version").notNullable();
57
57
  },
58
- }));
59
- tables.forEach((table) => (table.orm = this));
60
- return Promise.all(tables
61
- .sort((a, b) => {
62
- return (b.options.priority ?? 0) - (a.options.priority ?? 0);
63
- })
64
- .map((table) => table.make()));
58
+ });
59
+ migration.orm = this;
60
+ await migration.make();
61
+ for (const table of tables.sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0))) {
62
+ table.orm = this;
63
+ await table.make();
64
+ }
65
65
  });
66
+ try {
67
+ await this.db.raw("PRAGMA foreign_keys = ON;");
68
+ }
69
+ catch (error) { }
66
70
  await this.load();
67
71
  }
68
72
  }
@@ -10,6 +10,7 @@ export interface TableOptions<Type> {
10
10
  migrations?: {
11
11
  [version: number]: (table: Knex.CreateTableBuilder) => void;
12
12
  };
13
+ then?: (this: Table<Type>, table: Table<Type>) => unknown;
13
14
  setup: (table: Knex.CreateTableBuilder) => void;
14
15
  }
15
16
  export declare class Table<Type> {
@@ -27,6 +28,8 @@ export declare class Table<Type> {
27
28
  _intersectProps: {};
28
29
  _unionProps: never;
29
30
  }[]>;
31
+ hasColumn(name: keyof Type): Promise<boolean>;
32
+ isEmpty(): Promise<boolean>;
30
33
  make(): Promise<this>;
31
34
  private migrate;
32
35
  }
package/dist/app/table.js CHANGED
@@ -18,6 +18,15 @@ class Table {
18
18
  get query() {
19
19
  return this.db(this.options.name);
20
20
  }
21
+ async hasColumn(name) {
22
+ return this.db.schema.hasColumn(this.options.name, name);
23
+ }
24
+ async isEmpty() {
25
+ return this.query
26
+ .select()
27
+ .limit(1)
28
+ .then((rows) => rows.length === 0);
29
+ }
21
30
  async make() {
22
31
  try {
23
32
  await this.db.schema.createTable(this.options.name, this.options.setup);
@@ -46,6 +55,7 @@ class Table {
46
55
  if (this.verbose)
47
56
  console.error(error);
48
57
  }
58
+ await this.options.then?.bind(this)(this);
49
59
  return this;
50
60
  }
51
61
  async migrate() {
@@ -59,7 +69,7 @@ class Table {
59
69
  .first();
60
70
  const data = fromDatabase || {
61
71
  table: this.options.name,
62
- version: -1,
72
+ version: -Infinity,
63
73
  };
64
74
  const baseVersion = data.version;
65
75
  await this.db.schema.alterTable(this.options.name, (builder) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ghom/orm",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -17,6 +17,7 @@
17
17
  "devDependencies": {
18
18
  "@vscode/sqlite3": "^5.0.7",
19
19
  "better-sqlite3": "^7.5.0",
20
+ "dotenv": "^16.0.0",
20
21
  "jest": "^27.5.1",
21
22
  "prettier": "^2.5.1",
22
23
  "typescript": "^4.5.5"
package/src/app/orm.ts CHANGED
@@ -43,28 +43,30 @@ export class ORM extends Handler {
43
43
  })
44
44
  )
45
45
 
46
- tables.unshift(
47
- new Table<MigrationData>({
48
- name: "migration",
49
- priority: Infinity,
50
- setup: (table) => {
51
- table.string("table").unique().notNullable()
52
- table.integer("version").notNullable()
53
- },
54
- })
55
- )
46
+ const migration = new Table<MigrationData>({
47
+ name: "migration",
48
+ priority: Infinity,
49
+ setup: (table) => {
50
+ table.string("table").unique().notNullable()
51
+ table.integer("version").notNullable()
52
+ },
53
+ })
56
54
 
57
- tables.forEach((table) => (table.orm = this))
55
+ migration.orm = this
56
+ await migration.make()
58
57
 
59
- return Promise.all(
60
- tables
61
- .sort((a, b) => {
62
- return (b.options.priority ?? 0) - (a.options.priority ?? 0)
63
- })
64
- .map((table) => table.make())
65
- )
58
+ for (const table of tables.sort(
59
+ (a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0)
60
+ )) {
61
+ table.orm = this
62
+ await table.make()
63
+ }
66
64
  })
67
65
 
66
+ try {
67
+ await this.db.raw("PRAGMA foreign_keys = ON;")
68
+ } catch (error) {}
69
+
68
70
  await this.load()
69
71
  }
70
72
  }
package/src/app/table.ts CHANGED
@@ -10,6 +10,7 @@ export interface TableOptions<Type> {
10
10
  name: string
11
11
  priority?: number
12
12
  migrations?: { [version: number]: (table: Knex.CreateTableBuilder) => void }
13
+ then?: (this: Table<Type>, table: Table<Type>) => unknown
13
14
  setup: (table: Knex.CreateTableBuilder) => void
14
15
  }
15
16
 
@@ -32,6 +33,17 @@ export class Table<Type> {
32
33
  return this.db<Type>(this.options.name)
33
34
  }
34
35
 
36
+ async hasColumn(name: keyof Type): Promise<boolean> {
37
+ return this.db.schema.hasColumn(this.options.name, name as string)
38
+ }
39
+
40
+ async isEmpty(): Promise<boolean> {
41
+ return this.query
42
+ .select()
43
+ .limit(1)
44
+ .then((rows) => rows.length === 0)
45
+ }
46
+
35
47
  async make(): Promise<this> {
36
48
  try {
37
49
  await this.db.schema.createTable(this.options.name, this.options.setup)
@@ -62,6 +74,8 @@ export class Table<Type> {
62
74
  if (this.verbose) console.error(error)
63
75
  }
64
76
 
77
+ await this.options.then?.bind(this)(this)
78
+
65
79
  return this
66
80
  }
67
81
 
@@ -83,7 +97,7 @@ export class Table<Type> {
83
97
 
84
98
  const data = fromDatabase || {
85
99
  table: this.options.name,
86
- version: -1,
100
+ version: -Infinity,
87
101
  }
88
102
 
89
103
  const baseVersion = data.version
package/tests/.env ADDED
@@ -0,0 +1 @@
1
+ DEBUG=knex*
package/tests/tables/a.js CHANGED
@@ -3,8 +3,19 @@ const { Table } = require("../../dist/index")
3
3
  module.exports = new Table({
4
4
  name: "a",
5
5
  priority: 0,
6
- setup: (table) => {
7
- table.increments("id").primary()
8
- table.integer("b_id").references("id").inTable("b").notNullable()
6
+ setup(table) {
7
+ table.increments("id").primary().notNullable()
8
+ table
9
+ .integer("b_id")
10
+ .references("id")
11
+ .inTable("b")
12
+ .onDelete("cascade")
13
+ .notNullable()
14
+ },
15
+ async then({ query }) {
16
+ await query.insert({
17
+ id: 0,
18
+ b_id: 0,
19
+ })
9
20
  },
10
21
  })
package/tests/tables/b.js CHANGED
@@ -4,10 +4,21 @@ module.exports = new Table({
4
4
  name: "b",
5
5
  migrations: {
6
6
  0: (table) =>
7
- table.integer("c_id").references("id").inTable("c").notNullable(),
7
+ table
8
+ .integer("c_id")
9
+ .references("id")
10
+ .inTable("c")
11
+ .onDelete("cascade")
12
+ .notNullable(),
8
13
  },
9
14
  priority: 1,
10
- setup: (table) => {
11
- table.increments("id").primary()
15
+ setup(table) {
16
+ table.increments("id").primary().notNullable()
17
+ },
18
+ async then({ query }) {
19
+ await query.insert({
20
+ id: 0,
21
+ c_id: 0,
22
+ })
12
23
  },
13
24
  })
package/tests/tables/c.js CHANGED
@@ -3,7 +3,10 @@ const { Table } = require("../../dist/index")
3
3
  module.exports = new Table({
4
4
  name: "c",
5
5
  priority: 2,
6
- setup: (table) => {
7
- table.increments("id").primary()
6
+ setup(table) {
7
+ table.increments("id").primary().notNullable()
8
+ },
9
+ async then({ query }) {
10
+ await query.insert({ id: 0 })
8
11
  },
9
12
  })
package/tests/test.js CHANGED
@@ -1,5 +1,9 @@
1
+ require("dotenv").config({ path: "./.env" })
1
2
  const path = require("path")
2
3
  const { ORM } = require("../dist/index")
4
+ const a = require("./tables/a")
5
+ const b = require("./tables/b")
6
+ const c = require("./tables/c")
3
7
 
4
8
  const orm = new ORM({
5
9
  tablePath: path.join(__dirname, "tables"),
@@ -21,6 +25,16 @@ test("migrations ran", async () => {
21
25
  expect(await orm.db.schema.hasColumn("b", "c_id")).toBeTruthy()
22
26
  })
23
27
 
28
+ test("then ran", async () => {
29
+ const rows = await orm.db("a").select()
30
+ expect(rows.length).toBe(1)
31
+ })
32
+
33
+ test("cascade delete", async () => {
34
+ await c.query.del()
35
+ expect(await a.isEmpty()).toBeTruthy()
36
+ })
37
+
24
38
  afterAll(async () => {
25
39
  await orm.db.destroy()
26
40
  })