@ghom/orm 1.2.5 → 1.2.6
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/cjs/app/orm.js +81 -77
- package/dist/cjs/app/table.js +95 -95
- package/dist/cjs/index.js +18 -14
- package/dist/esm/app/orm.js +53 -53
- package/dist/esm/app/table.js +90 -90
- package/dist/esm/index.js +2 -2
- package/dist/typings/app/orm.d.ts +24 -24
- package/dist/typings/app/table.d.ts +36 -36
- package/dist/typings/index.d.ts +2 -2
- package/fixup.sh +11 -11
- package/package.json +13 -9
- package/src/app/table.ts +2 -2
- package/tests/tables/a.js +21 -21
- package/tests/tables/b.js +24 -24
- package/tests/tables/c.js +12 -12
- package/tests/test.js +40 -40
- package/tsconfig-cjs.json +10 -10
- package/tsconfig-esm.json +8 -8
- package/dist/cjs/package.json +0 -3
- package/dist/esm/package.json +0 -3
package/dist/cjs/app/orm.js
CHANGED
|
@@ -1,77 +1,81 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return
|
|
20
|
-
};
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.ORM = void 0;
|
|
30
|
+
const fs_1 = __importDefault(require("fs"));
|
|
31
|
+
const url_1 = __importDefault(require("url"));
|
|
32
|
+
const handler_1 = require("@ghom/handler");
|
|
33
|
+
const knex_1 = __importDefault(require("knex"));
|
|
34
|
+
const table_js_1 = require("./table.js");
|
|
35
|
+
const pack = JSON.parse(fs_1.default.readFileSync("./package.json", "utf8"));
|
|
36
|
+
const isCJS = pack.type === "commonjs" || pack.type == void 0;
|
|
37
|
+
class ORM extends handler_1.Handler {
|
|
38
|
+
/**
|
|
39
|
+
* @param ormConfig configuration for table handler or just tablePath (path to directory that contains js files of tables)
|
|
40
|
+
* @param knexConfig configuration for connect to database
|
|
41
|
+
*/
|
|
42
|
+
constructor(ormConfig, knexConfig = {
|
|
43
|
+
client: "sqlite3",
|
|
44
|
+
useNullAsDefault: true,
|
|
45
|
+
connection: {
|
|
46
|
+
filename: ":memory:",
|
|
47
|
+
},
|
|
48
|
+
}) {
|
|
49
|
+
super(typeof ormConfig === "string" ? ormConfig : ormConfig.tablePath);
|
|
50
|
+
this.ormConfig =
|
|
51
|
+
typeof ormConfig === "string" ? { tablePath: ormConfig } : ormConfig;
|
|
52
|
+
this.db = (0, knex_1.default)(knexConfig);
|
|
53
|
+
}
|
|
54
|
+
async init() {
|
|
55
|
+
this.once("finish", async (pathList) => {
|
|
56
|
+
const tables = await Promise.all(pathList.map(async (filepath) => {
|
|
57
|
+
return Promise.resolve(`${isCJS ? filepath : url_1.default.pathToFileURL(filepath).href}`).then(s => __importStar(require(s))).then((file) => file.default);
|
|
58
|
+
}));
|
|
59
|
+
const migration = new table_js_1.Table({
|
|
60
|
+
name: "migration",
|
|
61
|
+
priority: Infinity,
|
|
62
|
+
setup: (table) => {
|
|
63
|
+
table.string("table").unique().notNullable();
|
|
64
|
+
table.integer("version").notNullable();
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
migration.orm = this;
|
|
68
|
+
await migration.make();
|
|
69
|
+
for (const table of tables.sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0))) {
|
|
70
|
+
table.orm = this;
|
|
71
|
+
await table.make();
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
try {
|
|
75
|
+
await this.db.raw("PRAGMA foreign_keys = ON;");
|
|
76
|
+
}
|
|
77
|
+
catch (error) { }
|
|
78
|
+
await this.load();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.ORM = ORM;
|
package/dist/cjs/app/table.js
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.Table = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
-
class Table {
|
|
10
|
-
constructor(options) {
|
|
11
|
-
this.options = options;
|
|
12
|
-
}
|
|
13
|
-
get filepath() {
|
|
14
|
-
if (!this.orm)
|
|
15
|
-
throw new Error("missing ORM");
|
|
16
|
-
return path_1.default.relative(process.cwd(), path_1.default.join(this.orm.ormConfig.tablePath, this.options.name + ".ts"));
|
|
17
|
-
}
|
|
18
|
-
get logger() {
|
|
19
|
-
if (!this.orm)
|
|
20
|
-
throw new Error("missing ORM");
|
|
21
|
-
return this.orm.ormConfig.logger;
|
|
22
|
-
}
|
|
23
|
-
get db() {
|
|
24
|
-
if (!this.orm)
|
|
25
|
-
throw new Error("missing ORM");
|
|
26
|
-
return this.orm.db;
|
|
27
|
-
}
|
|
28
|
-
get query() {
|
|
29
|
-
return this.db(this.options.name);
|
|
30
|
-
}
|
|
31
|
-
async hasColumn(name) {
|
|
32
|
-
return this.db.schema.hasColumn(this.options.name, name);
|
|
33
|
-
}
|
|
34
|
-
async isEmpty() {
|
|
35
|
-
return this.query
|
|
36
|
-
.select()
|
|
37
|
-
.limit(1)
|
|
38
|
-
.then((rows) => rows.length === 0);
|
|
39
|
-
}
|
|
40
|
-
async make() {
|
|
41
|
-
try {
|
|
42
|
-
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
43
|
-
this.logger?.log(`created table ${chalk_1.default.blueBright(this.options.name)}`);
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
if (error.toString().includes("syntax error")) {
|
|
47
|
-
this.logger?.error(`you need to implement the "setup" method in options of your ${chalk_1.default.blueBright(this.options.name)} table!`, this.filepath);
|
|
48
|
-
throw error;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
this.logger?.log(`loaded table ${chalk_1.default.blueBright(this.options.name)}`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
try {
|
|
55
|
-
const migrated = await this.migrate();
|
|
56
|
-
if (migrated !== false) {
|
|
57
|
-
this.logger?.log(`migrated table ${chalk_1.default.blueBright(this.options.name)} to version ${chalk_1.default.magentaBright(migrated)}`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
catch (error) {
|
|
61
|
-
this.logger?.error(error, this.filepath);
|
|
62
|
-
}
|
|
63
|
-
await this.options.then?.bind(this)(this);
|
|
64
|
-
return this;
|
|
65
|
-
}
|
|
66
|
-
async migrate() {
|
|
67
|
-
if (!this.options.migrations)
|
|
68
|
-
return false;
|
|
69
|
-
const migrations = new Map(Object.entries(this.options.migrations)
|
|
70
|
-
.sort((a, b) => Number(a[0]) - Number(b[0]))
|
|
71
|
-
.map((entry) => [Number(entry[0]), entry[1]]));
|
|
72
|
-
const fromDatabase = await this.db("migration")
|
|
73
|
-
.where("table", this.options.name)
|
|
74
|
-
.first();
|
|
75
|
-
const data = fromDatabase || {
|
|
76
|
-
table: this.options.name,
|
|
77
|
-
version: -Infinity,
|
|
78
|
-
};
|
|
79
|
-
const baseVersion = data.version;
|
|
80
|
-
await this.db.schema.alterTable(this.options.name, (builder) => {
|
|
81
|
-
migrations.forEach((migration, version) => {
|
|
82
|
-
if (version <= data.version)
|
|
83
|
-
return;
|
|
84
|
-
migration(builder);
|
|
85
|
-
data.version = version;
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
await this.db("migration")
|
|
89
|
-
.insert(data)
|
|
90
|
-
.onConflict("table")
|
|
91
|
-
.merge();
|
|
92
|
-
return baseVersion === data.version ? false : data.version;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
exports.Table = Table;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Table = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
+
class Table {
|
|
10
|
+
constructor(options) {
|
|
11
|
+
this.options = options;
|
|
12
|
+
}
|
|
13
|
+
get filepath() {
|
|
14
|
+
if (!this.orm)
|
|
15
|
+
throw new Error("missing ORM");
|
|
16
|
+
return path_1.default.relative(process.cwd(), path_1.default.join(this.orm.ormConfig.tablePath, this.options.name + ".ts"));
|
|
17
|
+
}
|
|
18
|
+
get logger() {
|
|
19
|
+
if (!this.orm)
|
|
20
|
+
throw new Error("missing ORM");
|
|
21
|
+
return this.orm.ormConfig.logger;
|
|
22
|
+
}
|
|
23
|
+
get db() {
|
|
24
|
+
if (!this.orm)
|
|
25
|
+
throw new Error("missing ORM");
|
|
26
|
+
return this.orm.db;
|
|
27
|
+
}
|
|
28
|
+
get query() {
|
|
29
|
+
return this.db(this.options.name);
|
|
30
|
+
}
|
|
31
|
+
async hasColumn(name) {
|
|
32
|
+
return this.db.schema.hasColumn(this.options.name, name);
|
|
33
|
+
}
|
|
34
|
+
async isEmpty() {
|
|
35
|
+
return this.query
|
|
36
|
+
.select()
|
|
37
|
+
.limit(1)
|
|
38
|
+
.then((rows) => rows.length === 0);
|
|
39
|
+
}
|
|
40
|
+
async make() {
|
|
41
|
+
try {
|
|
42
|
+
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
43
|
+
this.logger?.log(`created table ${chalk_1.default.blueBright(this.options.name)}`);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error.toString().includes("syntax error")) {
|
|
47
|
+
this.logger?.error(`you need to implement the "setup" method in options of your ${chalk_1.default.blueBright(this.options.name)} table!`, this.filepath);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
this.logger?.log(`loaded table ${chalk_1.default.blueBright(this.options.name)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const migrated = await this.migrate();
|
|
56
|
+
if (migrated !== false) {
|
|
57
|
+
this.logger?.log(`migrated table ${chalk_1.default.blueBright(this.options.name)} to version ${chalk_1.default.magentaBright(migrated)}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
this.logger?.error(error, this.filepath);
|
|
62
|
+
}
|
|
63
|
+
await this.options.then?.bind(this)(this);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
async migrate() {
|
|
67
|
+
if (!this.options.migrations)
|
|
68
|
+
return false;
|
|
69
|
+
const migrations = new Map(Object.entries(this.options.migrations)
|
|
70
|
+
.sort((a, b) => Number(a[0]) - Number(b[0]))
|
|
71
|
+
.map((entry) => [Number(entry[0]), entry[1]]));
|
|
72
|
+
const fromDatabase = await this.db("migration")
|
|
73
|
+
.where("table", this.options.name)
|
|
74
|
+
.first();
|
|
75
|
+
const data = fromDatabase || {
|
|
76
|
+
table: this.options.name,
|
|
77
|
+
version: -Infinity,
|
|
78
|
+
};
|
|
79
|
+
const baseVersion = data.version;
|
|
80
|
+
await this.db.schema.alterTable(this.options.name, (builder) => {
|
|
81
|
+
migrations.forEach((migration, version) => {
|
|
82
|
+
if (version <= data.version)
|
|
83
|
+
return;
|
|
84
|
+
migration(builder);
|
|
85
|
+
data.version = version;
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
await this.db("migration")
|
|
89
|
+
.insert(data)
|
|
90
|
+
.onConflict("table")
|
|
91
|
+
.merge();
|
|
92
|
+
return baseVersion === data.version ? false : data.version;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.Table = Table;
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
__exportStar(
|
|
14
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./app/orm.js"), exports);
|
|
18
|
+
__exportStar(require("./app/table.js"), exports);
|
package/dist/esm/app/orm.js
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import fs from "fs";
|
|
2
|
-
import url from "url";
|
|
3
|
-
import { Handler } from "@ghom/handler";
|
|
4
|
-
import { default as knex } from "knex";
|
|
5
|
-
import { Table } from "./table.js";
|
|
6
|
-
const pack = JSON.parse(fs.readFileSync("./package.json", "utf8"));
|
|
7
|
-
const isCJS = pack.type === "commonjs" || pack.type == void 0;
|
|
8
|
-
export class ORM extends Handler {
|
|
9
|
-
db;
|
|
10
|
-
ormConfig;
|
|
11
|
-
/**
|
|
12
|
-
* @param ormConfig configuration for table handler or just tablePath (path to directory that contains js files of tables)
|
|
13
|
-
* @param knexConfig configuration for connect to database
|
|
14
|
-
*/
|
|
15
|
-
constructor(ormConfig, knexConfig = {
|
|
16
|
-
client: "sqlite3",
|
|
17
|
-
useNullAsDefault: true,
|
|
18
|
-
connection: {
|
|
19
|
-
filename: ":memory:",
|
|
20
|
-
},
|
|
21
|
-
}) {
|
|
22
|
-
super(typeof ormConfig === "string" ? ormConfig : ormConfig.tablePath);
|
|
23
|
-
this.ormConfig =
|
|
24
|
-
typeof ormConfig === "string" ? { tablePath: ormConfig } : ormConfig;
|
|
25
|
-
this.db = knex(knexConfig);
|
|
26
|
-
}
|
|
27
|
-
async init() {
|
|
28
|
-
this.once("finish", async (pathList) => {
|
|
29
|
-
const tables = await Promise.all(pathList.map(async (filepath) => {
|
|
30
|
-
return import(isCJS ? filepath : url.pathToFileURL(filepath).href).then((file) => file.default);
|
|
31
|
-
}));
|
|
32
|
-
const migration = new Table({
|
|
33
|
-
name: "migration",
|
|
34
|
-
priority: Infinity,
|
|
35
|
-
setup: (table) => {
|
|
36
|
-
table.string("table").unique().notNullable();
|
|
37
|
-
table.integer("version").notNullable();
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
migration.orm = this;
|
|
41
|
-
await migration.make();
|
|
42
|
-
for (const table of tables.sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0))) {
|
|
43
|
-
table.orm = this;
|
|
44
|
-
await table.make();
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
try {
|
|
48
|
-
await this.db.raw("PRAGMA foreign_keys = ON;");
|
|
49
|
-
}
|
|
50
|
-
catch (error) { }
|
|
51
|
-
await this.load();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import url from "url";
|
|
3
|
+
import { Handler } from "@ghom/handler";
|
|
4
|
+
import { default as knex } from "knex";
|
|
5
|
+
import { Table } from "./table.js";
|
|
6
|
+
const pack = JSON.parse(fs.readFileSync("./package.json", "utf8"));
|
|
7
|
+
const isCJS = pack.type === "commonjs" || pack.type == void 0;
|
|
8
|
+
export class ORM extends Handler {
|
|
9
|
+
db;
|
|
10
|
+
ormConfig;
|
|
11
|
+
/**
|
|
12
|
+
* @param ormConfig configuration for table handler or just tablePath (path to directory that contains js files of tables)
|
|
13
|
+
* @param knexConfig configuration for connect to database
|
|
14
|
+
*/
|
|
15
|
+
constructor(ormConfig, knexConfig = {
|
|
16
|
+
client: "sqlite3",
|
|
17
|
+
useNullAsDefault: true,
|
|
18
|
+
connection: {
|
|
19
|
+
filename: ":memory:",
|
|
20
|
+
},
|
|
21
|
+
}) {
|
|
22
|
+
super(typeof ormConfig === "string" ? ormConfig : ormConfig.tablePath);
|
|
23
|
+
this.ormConfig =
|
|
24
|
+
typeof ormConfig === "string" ? { tablePath: ormConfig } : ormConfig;
|
|
25
|
+
this.db = knex(knexConfig);
|
|
26
|
+
}
|
|
27
|
+
async init() {
|
|
28
|
+
this.once("finish", async (pathList) => {
|
|
29
|
+
const tables = await Promise.all(pathList.map(async (filepath) => {
|
|
30
|
+
return import(isCJS ? filepath : url.pathToFileURL(filepath).href).then((file) => file.default);
|
|
31
|
+
}));
|
|
32
|
+
const migration = new Table({
|
|
33
|
+
name: "migration",
|
|
34
|
+
priority: Infinity,
|
|
35
|
+
setup: (table) => {
|
|
36
|
+
table.string("table").unique().notNullable();
|
|
37
|
+
table.integer("version").notNullable();
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
migration.orm = this;
|
|
41
|
+
await migration.make();
|
|
42
|
+
for (const table of tables.sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0))) {
|
|
43
|
+
table.orm = this;
|
|
44
|
+
await table.make();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
try {
|
|
48
|
+
await this.db.raw("PRAGMA foreign_keys = ON;");
|
|
49
|
+
}
|
|
50
|
+
catch (error) { }
|
|
51
|
+
await this.load();
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/esm/app/table.js
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
import path from "path";
|
|
2
|
-
import chalk from "chalk";
|
|
3
|
-
export class Table {
|
|
4
|
-
options;
|
|
5
|
-
orm;
|
|
6
|
-
constructor(options) {
|
|
7
|
-
this.options = options;
|
|
8
|
-
}
|
|
9
|
-
get filepath() {
|
|
10
|
-
if (!this.orm)
|
|
11
|
-
throw new Error("missing ORM");
|
|
12
|
-
return path.relative(process.cwd(), path.join(this.orm.ormConfig.tablePath, this.options.name + ".ts"));
|
|
13
|
-
}
|
|
14
|
-
get logger() {
|
|
15
|
-
if (!this.orm)
|
|
16
|
-
throw new Error("missing ORM");
|
|
17
|
-
return this.orm.ormConfig.logger;
|
|
18
|
-
}
|
|
19
|
-
get db() {
|
|
20
|
-
if (!this.orm)
|
|
21
|
-
throw new Error("missing ORM");
|
|
22
|
-
return this.orm.db;
|
|
23
|
-
}
|
|
24
|
-
get query() {
|
|
25
|
-
return this.db(this.options.name);
|
|
26
|
-
}
|
|
27
|
-
async hasColumn(name) {
|
|
28
|
-
return this.db.schema.hasColumn(this.options.name, name);
|
|
29
|
-
}
|
|
30
|
-
async isEmpty() {
|
|
31
|
-
return this.query
|
|
32
|
-
.select()
|
|
33
|
-
.limit(1)
|
|
34
|
-
.then((rows) => rows.length === 0);
|
|
35
|
-
}
|
|
36
|
-
async make() {
|
|
37
|
-
try {
|
|
38
|
-
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
39
|
-
this.logger?.log(`created table ${chalk.blueBright(this.options.name)}`);
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
if (error.toString().includes("syntax error")) {
|
|
43
|
-
this.logger?.error(`you need to implement the "setup" method in options of your ${chalk.blueBright(this.options.name)} table!`, this.filepath);
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
this.logger?.log(`loaded table ${chalk.blueBright(this.options.name)}`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
try {
|
|
51
|
-
const migrated = await this.migrate();
|
|
52
|
-
if (migrated !== false) {
|
|
53
|
-
this.logger?.log(`migrated table ${chalk.blueBright(this.options.name)} to version ${chalk.magentaBright(migrated)}`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
catch (error) {
|
|
57
|
-
this.logger?.error(error, this.filepath);
|
|
58
|
-
}
|
|
59
|
-
await this.options.then?.bind(this)(this);
|
|
60
|
-
return this;
|
|
61
|
-
}
|
|
62
|
-
async migrate() {
|
|
63
|
-
if (!this.options.migrations)
|
|
64
|
-
return false;
|
|
65
|
-
const migrations = new Map(Object.entries(this.options.migrations)
|
|
66
|
-
.sort((a, b) => Number(a[0]) - Number(b[0]))
|
|
67
|
-
.map((entry) => [Number(entry[0]), entry[1]]));
|
|
68
|
-
const fromDatabase = await this.db("migration")
|
|
69
|
-
.where("table", this.options.name)
|
|
70
|
-
.first();
|
|
71
|
-
const data = fromDatabase || {
|
|
72
|
-
table: this.options.name,
|
|
73
|
-
version: -Infinity,
|
|
74
|
-
};
|
|
75
|
-
const baseVersion = data.version;
|
|
76
|
-
await this.db.schema.alterTable(this.options.name, (builder) => {
|
|
77
|
-
migrations.forEach((migration, version) => {
|
|
78
|
-
if (version <= data.version)
|
|
79
|
-
return;
|
|
80
|
-
migration(builder);
|
|
81
|
-
data.version = version;
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
await this.db("migration")
|
|
85
|
-
.insert(data)
|
|
86
|
-
.onConflict("table")
|
|
87
|
-
.merge();
|
|
88
|
-
return baseVersion === data.version ? false : data.version;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
1
|
+
import path from "path";
|
|
2
|
+
import chalk from "chalk";
|
|
3
|
+
export class Table {
|
|
4
|
+
options;
|
|
5
|
+
orm;
|
|
6
|
+
constructor(options) {
|
|
7
|
+
this.options = options;
|
|
8
|
+
}
|
|
9
|
+
get filepath() {
|
|
10
|
+
if (!this.orm)
|
|
11
|
+
throw new Error("missing ORM");
|
|
12
|
+
return path.relative(process.cwd(), path.join(this.orm.ormConfig.tablePath, this.options.name + ".ts"));
|
|
13
|
+
}
|
|
14
|
+
get logger() {
|
|
15
|
+
if (!this.orm)
|
|
16
|
+
throw new Error("missing ORM");
|
|
17
|
+
return this.orm.ormConfig.logger;
|
|
18
|
+
}
|
|
19
|
+
get db() {
|
|
20
|
+
if (!this.orm)
|
|
21
|
+
throw new Error("missing ORM");
|
|
22
|
+
return this.orm.db;
|
|
23
|
+
}
|
|
24
|
+
get query() {
|
|
25
|
+
return this.db(this.options.name);
|
|
26
|
+
}
|
|
27
|
+
async hasColumn(name) {
|
|
28
|
+
return this.db.schema.hasColumn(this.options.name, name);
|
|
29
|
+
}
|
|
30
|
+
async isEmpty() {
|
|
31
|
+
return this.query
|
|
32
|
+
.select()
|
|
33
|
+
.limit(1)
|
|
34
|
+
.then((rows) => rows.length === 0);
|
|
35
|
+
}
|
|
36
|
+
async make() {
|
|
37
|
+
try {
|
|
38
|
+
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
39
|
+
this.logger?.log(`created table ${chalk.blueBright(this.options.name)}`);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error.toString().includes("syntax error")) {
|
|
43
|
+
this.logger?.error(`you need to implement the "setup" method in options of your ${chalk.blueBright(this.options.name)} table!`, this.filepath);
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.logger?.log(`loaded table ${chalk.blueBright(this.options.name)}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const migrated = await this.migrate();
|
|
52
|
+
if (migrated !== false) {
|
|
53
|
+
this.logger?.log(`migrated table ${chalk.blueBright(this.options.name)} to version ${chalk.magentaBright(migrated)}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
this.logger?.error(error, this.filepath);
|
|
58
|
+
}
|
|
59
|
+
await this.options.then?.bind(this)(this);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
async migrate() {
|
|
63
|
+
if (!this.options.migrations)
|
|
64
|
+
return false;
|
|
65
|
+
const migrations = new Map(Object.entries(this.options.migrations)
|
|
66
|
+
.sort((a, b) => Number(a[0]) - Number(b[0]))
|
|
67
|
+
.map((entry) => [Number(entry[0]), entry[1]]));
|
|
68
|
+
const fromDatabase = await this.db("migration")
|
|
69
|
+
.where("table", this.options.name)
|
|
70
|
+
.first();
|
|
71
|
+
const data = fromDatabase || {
|
|
72
|
+
table: this.options.name,
|
|
73
|
+
version: -Infinity,
|
|
74
|
+
};
|
|
75
|
+
const baseVersion = data.version;
|
|
76
|
+
await this.db.schema.alterTable(this.options.name, (builder) => {
|
|
77
|
+
migrations.forEach((migration, version) => {
|
|
78
|
+
if (version <= data.version)
|
|
79
|
+
return;
|
|
80
|
+
migration(builder);
|
|
81
|
+
data.version = version;
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
await this.db("migration")
|
|
85
|
+
.insert(data)
|
|
86
|
+
.onConflict("table")
|
|
87
|
+
.merge();
|
|
88
|
+
return baseVersion === data.version ? false : data.version;
|
|
89
|
+
}
|
|
90
|
+
}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./app/orm.js";
|
|
2
|
-
export * from "./app/table.js";
|
|
1
|
+
export * from "./app/orm.js";
|
|
2
|
+
export * from "./app/table.js";
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { Handler } from "@ghom/handler";
|
|
2
|
-
import { Knex } from "knex";
|
|
3
|
-
export interface ORMLogger {
|
|
4
|
-
log: (message: string, section?: string) => void;
|
|
5
|
-
error: (text: string | Error, _path: string, full?: boolean) => void;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* @property tablePath - path to directory that contains js files of tables
|
|
9
|
-
* @property verbose - show console logs or not
|
|
10
|
-
*/
|
|
11
|
-
export interface ORMConfig {
|
|
12
|
-
logger?: ORMLogger;
|
|
13
|
-
tablePath: string;
|
|
14
|
-
}
|
|
15
|
-
export declare class ORM extends Handler {
|
|
16
|
-
db: Knex;
|
|
17
|
-
ormConfig: ORMConfig;
|
|
18
|
-
/**
|
|
19
|
-
* @param ormConfig configuration for table handler or just tablePath (path to directory that contains js files of tables)
|
|
20
|
-
* @param knexConfig configuration for connect to database
|
|
21
|
-
*/
|
|
22
|
-
constructor(ormConfig: ORMConfig | string, knexConfig?: Knex.Config);
|
|
23
|
-
init(): Promise<void>;
|
|
24
|
-
}
|
|
1
|
+
import { Handler } from "@ghom/handler";
|
|
2
|
+
import { Knex } from "knex";
|
|
3
|
+
export interface ORMLogger {
|
|
4
|
+
log: (message: string, section?: string) => void;
|
|
5
|
+
error: (text: string | Error, _path: string, full?: boolean) => void;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @property tablePath - path to directory that contains js files of tables
|
|
9
|
+
* @property verbose - show console logs or not
|
|
10
|
+
*/
|
|
11
|
+
export interface ORMConfig {
|
|
12
|
+
logger?: ORMLogger;
|
|
13
|
+
tablePath: string;
|
|
14
|
+
}
|
|
15
|
+
export declare class ORM extends Handler {
|
|
16
|
+
db: Knex;
|
|
17
|
+
ormConfig: ORMConfig;
|
|
18
|
+
/**
|
|
19
|
+
* @param ormConfig configuration for table handler or just tablePath (path to directory that contains js files of tables)
|
|
20
|
+
* @param knexConfig configuration for connect to database
|
|
21
|
+
*/
|
|
22
|
+
constructor(ormConfig: ORMConfig | string, knexConfig?: Knex.Config);
|
|
23
|
+
init(): Promise<void>;
|
|
24
|
+
}
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
import { Knex } from "knex";
|
|
2
|
-
import { ORM } from "./orm.js";
|
|
3
|
-
export interface MigrationData {
|
|
4
|
-
table: string;
|
|
5
|
-
version: number;
|
|
6
|
-
}
|
|
7
|
-
export interface TableOptions<Type> {
|
|
8
|
-
name: string;
|
|
9
|
-
priority?: number;
|
|
10
|
-
migrations?: {
|
|
11
|
-
[version: number]: (table: Knex.CreateTableBuilder) => void;
|
|
12
|
-
};
|
|
13
|
-
then?: (this: Table<Type>, table: Table<Type>) => unknown;
|
|
14
|
-
setup: (table: Knex.CreateTableBuilder) => void;
|
|
15
|
-
}
|
|
16
|
-
export declare class Table<Type> {
|
|
17
|
-
readonly options: TableOptions<Type>;
|
|
18
|
-
orm?: ORM;
|
|
19
|
-
constructor(options: TableOptions<Type>);
|
|
20
|
-
private get filepath();
|
|
21
|
-
private get logger();
|
|
22
|
-
get db(): Knex<any,
|
|
23
|
-
get query(): Knex.QueryBuilder<Type, {
|
|
24
|
-
_base: Type;
|
|
25
|
-
_hasSelection: false;
|
|
26
|
-
_keys: never;
|
|
27
|
-
_aliases: {};
|
|
28
|
-
_single: false;
|
|
29
|
-
_intersectProps: {};
|
|
30
|
-
_unionProps: never;
|
|
31
|
-
}[]>;
|
|
32
|
-
hasColumn(name: keyof Type): Promise<boolean>;
|
|
33
|
-
isEmpty(): Promise<boolean>;
|
|
34
|
-
make(): Promise<this>;
|
|
35
|
-
private migrate;
|
|
36
|
-
}
|
|
1
|
+
import { Knex } from "knex";
|
|
2
|
+
import { ORM } from "./orm.js";
|
|
3
|
+
export interface MigrationData {
|
|
4
|
+
table: string;
|
|
5
|
+
version: number;
|
|
6
|
+
}
|
|
7
|
+
export interface TableOptions<Type extends {}> {
|
|
8
|
+
name: string;
|
|
9
|
+
priority?: number;
|
|
10
|
+
migrations?: {
|
|
11
|
+
[version: number]: (table: Knex.CreateTableBuilder) => void;
|
|
12
|
+
};
|
|
13
|
+
then?: (this: Table<Type>, table: Table<Type>) => unknown;
|
|
14
|
+
setup: (table: Knex.CreateTableBuilder) => void;
|
|
15
|
+
}
|
|
16
|
+
export declare class Table<Type extends {}> {
|
|
17
|
+
readonly options: TableOptions<Type>;
|
|
18
|
+
orm?: ORM;
|
|
19
|
+
constructor(options: TableOptions<Type>);
|
|
20
|
+
private get filepath();
|
|
21
|
+
private get logger();
|
|
22
|
+
get db(): Knex<any, any[]>;
|
|
23
|
+
get query(): Knex.QueryBuilder<Type, {
|
|
24
|
+
_base: Type;
|
|
25
|
+
_hasSelection: false;
|
|
26
|
+
_keys: never;
|
|
27
|
+
_aliases: {};
|
|
28
|
+
_single: false;
|
|
29
|
+
_intersectProps: {};
|
|
30
|
+
_unionProps: never;
|
|
31
|
+
}[]>;
|
|
32
|
+
hasColumn(name: keyof Type): Promise<boolean>;
|
|
33
|
+
isEmpty(): Promise<boolean>;
|
|
34
|
+
make(): Promise<this>;
|
|
35
|
+
private migrate;
|
|
36
|
+
}
|
package/dist/typings/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./app/orm.js";
|
|
2
|
-
export * from "./app/table.js";
|
|
1
|
+
export * from "./app/orm.js";
|
|
2
|
+
export * from "./app/table.js";
|
package/fixup.sh
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
cat >dist/cjs/package.json <<!EOF
|
|
2
|
-
{
|
|
3
|
-
"type": "commonjs"
|
|
4
|
-
}
|
|
5
|
-
!EOF
|
|
6
|
-
|
|
7
|
-
cat >dist/esm/package.json <<!EOF
|
|
8
|
-
{
|
|
9
|
-
"type": "module"
|
|
10
|
-
}
|
|
11
|
-
!EOF
|
|
1
|
+
cat >dist/cjs/package.json <<!EOF
|
|
2
|
+
{
|
|
3
|
+
"type": "commonjs"
|
|
4
|
+
}
|
|
5
|
+
!EOF
|
|
6
|
+
|
|
7
|
+
cat >dist/esm/package.json <<!EOF
|
|
8
|
+
{
|
|
9
|
+
"type": "module"
|
|
10
|
+
}
|
|
11
|
+
!EOF
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ghom/orm",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -20,16 +20,20 @@
|
|
|
20
20
|
"prepublishOnly": "npm run format && npm test"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@
|
|
24
|
-
"
|
|
25
|
-
"dotenv": "^16.0.
|
|
26
|
-
"jest": "^
|
|
27
|
-
"prettier": "^2.
|
|
28
|
-
"typescript": "^
|
|
23
|
+
"@types/jest": "^29.5.0",
|
|
24
|
+
"@types/node": "^18.15.11",
|
|
25
|
+
"dotenv": "^16.0.3",
|
|
26
|
+
"jest": "^29.5.0",
|
|
27
|
+
"prettier": "^2.8.7",
|
|
28
|
+
"typescript": "^5.0.4"
|
|
29
|
+
},
|
|
30
|
+
"optionalDependencies": {
|
|
31
|
+
"mysql2": "^3.2.1",
|
|
32
|
+
"pg": "^8.10.0",
|
|
33
|
+
"sqlite3": "^5.1.6"
|
|
29
34
|
},
|
|
30
35
|
"dependencies": {
|
|
31
36
|
"@ghom/handler": "^1.1.0",
|
|
32
|
-
"
|
|
33
|
-
"knex": "^1.0.3"
|
|
37
|
+
"knex": "^2.4.2"
|
|
34
38
|
}
|
|
35
39
|
}
|
package/src/app/table.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface MigrationData {
|
|
|
8
8
|
version: number
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export interface TableOptions<Type> {
|
|
11
|
+
export interface TableOptions<Type extends {}> {
|
|
12
12
|
name: string
|
|
13
13
|
priority?: number
|
|
14
14
|
migrations?: { [version: number]: (table: Knex.CreateTableBuilder) => void }
|
|
@@ -16,7 +16,7 @@ export interface TableOptions<Type> {
|
|
|
16
16
|
setup: (table: Knex.CreateTableBuilder) => void
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export class Table<Type> {
|
|
19
|
+
export class Table<Type extends {}> {
|
|
20
20
|
orm?: ORM
|
|
21
21
|
|
|
22
22
|
constructor(public readonly options: TableOptions<Type>) {}
|
package/tests/tables/a.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
const { Table } = require("../..")
|
|
2
|
-
|
|
3
|
-
module.exports = new Table({
|
|
4
|
-
name: "a",
|
|
5
|
-
priority: 0,
|
|
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
|
-
})
|
|
20
|
-
},
|
|
21
|
-
})
|
|
1
|
+
const { Table } = require("../..")
|
|
2
|
+
|
|
3
|
+
module.exports = new Table({
|
|
4
|
+
name: "a",
|
|
5
|
+
priority: 0,
|
|
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
|
+
})
|
|
20
|
+
},
|
|
21
|
+
})
|
package/tests/tables/b.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
const { Table } = require("../..")
|
|
2
|
-
|
|
3
|
-
module.exports = new Table({
|
|
4
|
-
name: "b",
|
|
5
|
-
migrations: {
|
|
6
|
-
0: (table) =>
|
|
7
|
-
table
|
|
8
|
-
.integer("c_id")
|
|
9
|
-
.references("id")
|
|
10
|
-
.inTable("c")
|
|
11
|
-
.onDelete("cascade")
|
|
12
|
-
.notNullable(),
|
|
13
|
-
},
|
|
14
|
-
priority: 1,
|
|
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
|
-
})
|
|
23
|
-
},
|
|
24
|
-
})
|
|
1
|
+
const { Table } = require("../..")
|
|
2
|
+
|
|
3
|
+
module.exports = new Table({
|
|
4
|
+
name: "b",
|
|
5
|
+
migrations: {
|
|
6
|
+
0: (table) =>
|
|
7
|
+
table
|
|
8
|
+
.integer("c_id")
|
|
9
|
+
.references("id")
|
|
10
|
+
.inTable("c")
|
|
11
|
+
.onDelete("cascade")
|
|
12
|
+
.notNullable(),
|
|
13
|
+
},
|
|
14
|
+
priority: 1,
|
|
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
|
+
})
|
|
23
|
+
},
|
|
24
|
+
})
|
package/tests/tables/c.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
const { Table } = require("../..")
|
|
2
|
-
|
|
3
|
-
module.exports = new Table({
|
|
4
|
-
name: "c",
|
|
5
|
-
priority: 2,
|
|
6
|
-
setup(table) {
|
|
7
|
-
table.increments("id").primary().notNullable()
|
|
8
|
-
},
|
|
9
|
-
async then({ query }) {
|
|
10
|
-
await query.insert({ id: 0 })
|
|
11
|
-
},
|
|
12
|
-
})
|
|
1
|
+
const { Table } = require("../..")
|
|
2
|
+
|
|
3
|
+
module.exports = new Table({
|
|
4
|
+
name: "c",
|
|
5
|
+
priority: 2,
|
|
6
|
+
setup(table) {
|
|
7
|
+
table.increments("id").primary().notNullable()
|
|
8
|
+
},
|
|
9
|
+
async then({ query }) {
|
|
10
|
+
await query.insert({ id: 0 })
|
|
11
|
+
},
|
|
12
|
+
})
|
package/tests/test.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
require("dotenv").config({ path: "./.env" })
|
|
2
|
-
const path = require("path")
|
|
3
|
-
const { ORM } = require("..")
|
|
4
|
-
const a = require("./tables/a")
|
|
5
|
-
const b = require("./tables/b")
|
|
6
|
-
const c = require("./tables/c")
|
|
7
|
-
|
|
8
|
-
const orm = new ORM({
|
|
9
|
-
tablePath: path.join(__dirname, "tables"),
|
|
10
|
-
logger: console,
|
|
11
|
-
})
|
|
12
|
-
|
|
13
|
-
beforeAll(async () => {
|
|
14
|
-
await orm.init()
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
test("tables created", async () => {
|
|
18
|
-
expect(await orm.db.schema.hasTable("migration")).toBeTruthy()
|
|
19
|
-
expect(await orm.db.schema.hasTable("a")).toBeTruthy()
|
|
20
|
-
expect(await orm.db.schema.hasTable("b")).toBeTruthy()
|
|
21
|
-
expect(await orm.db.schema.hasTable("c")).toBeTruthy()
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
test("migrations ran", async () => {
|
|
25
|
-
expect(await orm.db.schema.hasColumn("b", "c_id")).toBeTruthy()
|
|
26
|
-
})
|
|
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
|
-
|
|
38
|
-
afterAll(async () => {
|
|
39
|
-
await orm.db.destroy()
|
|
40
|
-
})
|
|
1
|
+
require("dotenv").config({ path: "./.env" })
|
|
2
|
+
const path = require("path")
|
|
3
|
+
const { ORM } = require("..")
|
|
4
|
+
const a = require("./tables/a")
|
|
5
|
+
const b = require("./tables/b")
|
|
6
|
+
const c = require("./tables/c")
|
|
7
|
+
|
|
8
|
+
const orm = new ORM({
|
|
9
|
+
tablePath: path.join(__dirname, "tables"),
|
|
10
|
+
logger: console,
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
beforeAll(async () => {
|
|
14
|
+
await orm.init()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test("tables created", async () => {
|
|
18
|
+
expect(await orm.db.schema.hasTable("migration")).toBeTruthy()
|
|
19
|
+
expect(await orm.db.schema.hasTable("a")).toBeTruthy()
|
|
20
|
+
expect(await orm.db.schema.hasTable("b")).toBeTruthy()
|
|
21
|
+
expect(await orm.db.schema.hasTable("c")).toBeTruthy()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
test("migrations ran", async () => {
|
|
25
|
+
expect(await orm.db.schema.hasColumn("b", "c_id")).toBeTruthy()
|
|
26
|
+
})
|
|
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
|
+
|
|
38
|
+
afterAll(async () => {
|
|
39
|
+
await orm.db.destroy()
|
|
40
|
+
})
|
package/tsconfig-cjs.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "dist/cjs",
|
|
5
|
-
"module": "CommonJS",
|
|
6
|
-
"target": "es2020",
|
|
7
|
-
"declaration": true,
|
|
8
|
-
"declarationDir": "dist/typings"
|
|
9
|
-
}
|
|
10
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist/cjs",
|
|
5
|
+
"module": "CommonJS",
|
|
6
|
+
"target": "es2020",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationDir": "dist/typings"
|
|
9
|
+
}
|
|
10
|
+
}
|
package/tsconfig-esm.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "dist/esm",
|
|
5
|
-
"module": "esnext",
|
|
6
|
-
"target": "esnext",
|
|
7
|
-
}
|
|
8
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "dist/esm",
|
|
5
|
+
"module": "esnext",
|
|
6
|
+
"target": "esnext",
|
|
7
|
+
}
|
|
8
|
+
}
|
package/dist/cjs/package.json
DELETED
package/dist/esm/package.json
DELETED