@ghom/orm 1.3.3 → 1.4.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/cjs/app/orm.js +32 -38
- package/dist/cjs/app/table.js +8 -17
- package/dist/esm/app/orm.js +35 -40
- package/dist/esm/app/table.js +8 -17
- package/dist/typings/app/orm.d.ts +29 -16
- package/dist/typings/app/table.d.ts +0 -2
- package/package.json +6 -6
- package/src/app/orm.ts +68 -57
- package/src/app/table.ts +25 -27
- package/tests/test.js +8 -9
package/dist/cjs/app/orm.js
CHANGED
|
@@ -29,53 +29,47 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
exports.ORM = void 0;
|
|
30
30
|
const fs_1 = __importDefault(require("fs"));
|
|
31
31
|
const url_1 = __importDefault(require("url"));
|
|
32
|
+
const path_1 = __importDefault(require("path"));
|
|
32
33
|
const handler_1 = require("@ghom/handler");
|
|
33
34
|
const knex_1 = __importDefault(require("knex"));
|
|
34
35
|
const table_js_1 = require("./table.js");
|
|
35
|
-
const pack = JSON.parse(fs_1.default.readFileSync("
|
|
36
|
+
const pack = JSON.parse(fs_1.default.readFileSync(path_1.default.join(process.cwd(), "package.json"), "utf8"));
|
|
36
37
|
const isCJS = pack.type === "commonjs" || pack.type == void 0;
|
|
37
|
-
class ORM
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
this.ormConfig =
|
|
51
|
-
typeof ormConfig === "string" ? { tablePath: ormConfig } : ormConfig;
|
|
52
|
-
this.db = (0, knex_1.default)(knexConfig);
|
|
38
|
+
class ORM {
|
|
39
|
+
constructor(config) {
|
|
40
|
+
this.config = config;
|
|
41
|
+
this.database = (0, knex_1.default)(config.database ?? {
|
|
42
|
+
client: "sqlite3",
|
|
43
|
+
useNullAsDefault: true,
|
|
44
|
+
connection: {
|
|
45
|
+
filename: ":memory:",
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
this.handler = new handler_1.Handler(config.location, {
|
|
49
|
+
loader: (filepath) => Promise.resolve(`${isCJS ? filepath : url_1.default.pathToFileURL(filepath).href}`).then(s => __importStar(require(s))).then((file) => file.default),
|
|
50
|
+
});
|
|
53
51
|
}
|
|
54
52
|
async init() {
|
|
55
|
-
this.
|
|
56
|
-
|
|
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
|
-
});
|
|
53
|
+
await this.handler.init();
|
|
54
|
+
const tables = [...this.handler.elements.values()];
|
|
74
55
|
try {
|
|
75
|
-
await this.
|
|
56
|
+
await this.database.raw("PRAGMA foreign_keys = ON;");
|
|
76
57
|
}
|
|
77
58
|
catch (error) { }
|
|
78
|
-
|
|
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
|
+
}
|
|
79
73
|
}
|
|
80
74
|
}
|
|
81
75
|
exports.ORM = ORM;
|
package/dist/cjs/app/table.js
CHANGED
|
@@ -4,26 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Table = void 0;
|
|
7
|
-
const path_1 = __importDefault(require("path"));
|
|
8
7
|
const chalk_1 = __importDefault(require("chalk"));
|
|
9
8
|
class Table {
|
|
10
9
|
constructor(options) {
|
|
11
10
|
this.options = options;
|
|
12
11
|
}
|
|
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
12
|
get db() {
|
|
24
13
|
if (!this.orm)
|
|
25
14
|
throw new Error("missing ORM");
|
|
26
|
-
return this.orm.
|
|
15
|
+
return this.orm.database;
|
|
27
16
|
}
|
|
28
17
|
get query() {
|
|
29
18
|
return this.db(this.options.name);
|
|
@@ -38,27 +27,29 @@ class Table {
|
|
|
38
27
|
.then((rows) => rows.length === 0);
|
|
39
28
|
}
|
|
40
29
|
async make() {
|
|
30
|
+
if (!this.orm)
|
|
31
|
+
throw new Error("missing ORM");
|
|
41
32
|
try {
|
|
42
33
|
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
43
|
-
this.logger?.log(`created table ${chalk_1.default.blueBright(this.options.name)}`);
|
|
34
|
+
this.orm.config.logger?.log(`created table ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}`);
|
|
44
35
|
}
|
|
45
36
|
catch (error) {
|
|
46
37
|
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
|
|
38
|
+
this.orm.config.logger?.error(`you need to implement the "setup" method in options of your ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)} table!`);
|
|
48
39
|
throw error;
|
|
49
40
|
}
|
|
50
41
|
else {
|
|
51
|
-
this.logger?.log(`loaded table ${chalk_1.default.blueBright(this.options.name)}`);
|
|
42
|
+
this.orm.config.logger?.log(`loaded table ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}`);
|
|
52
43
|
}
|
|
53
44
|
}
|
|
54
45
|
try {
|
|
55
46
|
const migrated = await this.migrate();
|
|
56
47
|
if (migrated !== false) {
|
|
57
|
-
this.logger?.log(`migrated table ${chalk_1.default.blueBright(this.options.name)} to version ${chalk_1.default.magentaBright(migrated)}`);
|
|
48
|
+
this.orm.config.logger?.log(`migrated table ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)} to version ${chalk_1.default[this.orm.config.loggerColors?.rawValue ?? "magentaBright"](migrated)}`);
|
|
58
49
|
}
|
|
59
50
|
}
|
|
60
51
|
catch (error) {
|
|
61
|
-
this.logger?.error(error
|
|
52
|
+
this.orm.config.logger?.error(error);
|
|
62
53
|
}
|
|
63
54
|
await this.options.then?.bind(this)(this);
|
|
64
55
|
return this;
|
package/dist/esm/app/orm.js
CHANGED
|
@@ -1,53 +1,48 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import url from "url";
|
|
3
|
+
import path from "path";
|
|
3
4
|
import { Handler } from "@ghom/handler";
|
|
4
5
|
import { default as knex } from "knex";
|
|
5
6
|
import { Table } from "./table.js";
|
|
6
|
-
const pack = JSON.parse(fs.readFileSync("
|
|
7
|
+
const pack = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"));
|
|
7
8
|
const isCJS = pack.type === "commonjs" || pack.type == void 0;
|
|
8
|
-
export class ORM
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
typeof ormConfig === "string" ? { tablePath: ormConfig } : ormConfig;
|
|
25
|
-
this.db = knex(knexConfig);
|
|
9
|
+
export class ORM {
|
|
10
|
+
config;
|
|
11
|
+
database;
|
|
12
|
+
handler;
|
|
13
|
+
constructor(config) {
|
|
14
|
+
this.config = config;
|
|
15
|
+
this.database = knex(config.database ?? {
|
|
16
|
+
client: "sqlite3",
|
|
17
|
+
useNullAsDefault: true,
|
|
18
|
+
connection: {
|
|
19
|
+
filename: ":memory:",
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
this.handler = new Handler(config.location, {
|
|
23
|
+
loader: (filepath) => import(isCJS ? filepath : url.pathToFileURL(filepath).href).then((file) => file.default),
|
|
24
|
+
});
|
|
26
25
|
}
|
|
27
26
|
async init() {
|
|
28
|
-
this.
|
|
29
|
-
|
|
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
|
-
});
|
|
27
|
+
await this.handler.init();
|
|
28
|
+
const tables = [...this.handler.elements.values()];
|
|
47
29
|
try {
|
|
48
|
-
await this.
|
|
30
|
+
await this.database.raw("PRAGMA foreign_keys = ON;");
|
|
49
31
|
}
|
|
50
32
|
catch (error) { }
|
|
51
|
-
|
|
33
|
+
const migration = new Table({
|
|
34
|
+
name: "migration",
|
|
35
|
+
priority: Infinity,
|
|
36
|
+
setup: (table) => {
|
|
37
|
+
table.string("table").unique().notNullable();
|
|
38
|
+
table.integer("version").notNullable();
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
migration.orm = this;
|
|
42
|
+
await migration.make();
|
|
43
|
+
for (const table of tables.sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0))) {
|
|
44
|
+
table.orm = this;
|
|
45
|
+
await table.make();
|
|
46
|
+
}
|
|
52
47
|
}
|
|
53
48
|
}
|
package/dist/esm/app/table.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import path from "path";
|
|
2
1
|
import chalk from "chalk";
|
|
3
2
|
export class Table {
|
|
4
3
|
options;
|
|
@@ -6,20 +5,10 @@ export class Table {
|
|
|
6
5
|
constructor(options) {
|
|
7
6
|
this.options = options;
|
|
8
7
|
}
|
|
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
8
|
get db() {
|
|
20
9
|
if (!this.orm)
|
|
21
10
|
throw new Error("missing ORM");
|
|
22
|
-
return this.orm.
|
|
11
|
+
return this.orm.database;
|
|
23
12
|
}
|
|
24
13
|
get query() {
|
|
25
14
|
return this.db(this.options.name);
|
|
@@ -34,27 +23,29 @@ export class Table {
|
|
|
34
23
|
.then((rows) => rows.length === 0);
|
|
35
24
|
}
|
|
36
25
|
async make() {
|
|
26
|
+
if (!this.orm)
|
|
27
|
+
throw new Error("missing ORM");
|
|
37
28
|
try {
|
|
38
29
|
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
39
|
-
this.logger?.log(`created table ${chalk.blueBright(this.options.name)}`);
|
|
30
|
+
this.orm.config.logger?.log(`created table ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}`);
|
|
40
31
|
}
|
|
41
32
|
catch (error) {
|
|
42
33
|
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
|
|
34
|
+
this.orm.config.logger?.error(`you need to implement the "setup" method in options of your ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)} table!`);
|
|
44
35
|
throw error;
|
|
45
36
|
}
|
|
46
37
|
else {
|
|
47
|
-
this.logger?.log(`loaded table ${chalk.blueBright(this.options.name)}`);
|
|
38
|
+
this.orm.config.logger?.log(`loaded table ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}`);
|
|
48
39
|
}
|
|
49
40
|
}
|
|
50
41
|
try {
|
|
51
42
|
const migrated = await this.migrate();
|
|
52
43
|
if (migrated !== false) {
|
|
53
|
-
this.logger?.log(`migrated table ${chalk.blueBright(this.options.name)} to version ${chalk.magentaBright(migrated)}`);
|
|
44
|
+
this.orm.config.logger?.log(`migrated table ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)} to version ${chalk[this.orm.config.loggerColors?.rawValue ?? "magentaBright"](migrated)}`);
|
|
54
45
|
}
|
|
55
46
|
}
|
|
56
47
|
catch (error) {
|
|
57
|
-
this.logger?.error(error
|
|
48
|
+
this.orm.config.logger?.error(error);
|
|
58
49
|
}
|
|
59
50
|
await this.options.then?.bind(this)(this);
|
|
60
51
|
return this;
|
|
@@ -1,24 +1,37 @@
|
|
|
1
1
|
import { Handler } from "@ghom/handler";
|
|
2
2
|
import { Knex } from "knex";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import { Table } from "./table.js";
|
|
4
|
+
import { Color } from "chalk";
|
|
5
|
+
export interface ILogger {
|
|
6
|
+
log: (...message: string[]) => void;
|
|
7
|
+
error: (error: Error | string, ...message: string[]) => void;
|
|
6
8
|
}
|
|
7
|
-
/**
|
|
8
|
-
* @property tablePath - path to directory that contains js files of tables
|
|
9
|
-
* @property verbose - show console logs or not
|
|
10
|
-
*/
|
|
11
9
|
export interface ORMConfig {
|
|
12
|
-
logger?: ORMLogger;
|
|
13
|
-
tablePath: string;
|
|
14
|
-
}
|
|
15
|
-
export declare class ORM extends Handler {
|
|
16
|
-
db: Knex;
|
|
17
|
-
ormConfig: ORMConfig;
|
|
18
10
|
/**
|
|
19
|
-
*
|
|
20
|
-
|
|
11
|
+
* path to the directory that contains js files of tables
|
|
12
|
+
*/
|
|
13
|
+
location: string;
|
|
14
|
+
/**
|
|
15
|
+
* database configuration
|
|
16
|
+
*/
|
|
17
|
+
database?: Knex.Config;
|
|
18
|
+
/**
|
|
19
|
+
* Logger used to log the table files loaded or created.
|
|
20
|
+
*/
|
|
21
|
+
logger?: ILogger;
|
|
22
|
+
/**
|
|
23
|
+
* Pattern used on logs when the table files are loaded or created. <br>
|
|
24
|
+
* Based on Chalk color-method names.
|
|
21
25
|
*/
|
|
22
|
-
|
|
26
|
+
loggerColors?: {
|
|
27
|
+
highlight: typeof Color;
|
|
28
|
+
rawValue: typeof Color;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export declare class ORM {
|
|
32
|
+
config: ORMConfig;
|
|
33
|
+
database: Knex;
|
|
34
|
+
handler: Handler<Table<any>>;
|
|
35
|
+
constructor(config: ORMConfig);
|
|
23
36
|
init(): Promise<void>;
|
|
24
37
|
}
|
|
@@ -17,8 +17,6 @@ export declare class Table<Type extends {}> {
|
|
|
17
17
|
readonly options: TableOptions<Type>;
|
|
18
18
|
orm?: ORM;
|
|
19
19
|
constructor(options: TableOptions<Type>);
|
|
20
|
-
private get filepath();
|
|
21
|
-
private get logger();
|
|
22
20
|
get db(): Knex<any, any[]>;
|
|
23
21
|
get query(): Knex.QueryBuilder<Type, {
|
|
24
22
|
_base: Type;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ghom/orm",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/esm/index.js",
|
|
@@ -20,20 +20,20 @@
|
|
|
20
20
|
"prepublishOnly": "npm run format && npm test"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@types/jest": "^29.5.
|
|
24
|
-
"@types/node": "^18.
|
|
23
|
+
"@types/jest": "^29.5.1",
|
|
24
|
+
"@types/node": "^18.16.3",
|
|
25
25
|
"dotenv": "^16.0.3",
|
|
26
26
|
"jest": "^29.5.0",
|
|
27
|
-
"prettier": "^2.8.
|
|
27
|
+
"prettier": "^2.8.8",
|
|
28
28
|
"typescript": "^5.0.4"
|
|
29
29
|
},
|
|
30
30
|
"optionalDependencies": {
|
|
31
|
-
"mysql2": "^3.2.
|
|
31
|
+
"mysql2": "^3.2.4",
|
|
32
32
|
"pg": "^8.10.0",
|
|
33
33
|
"sqlite3": "^5.1.6"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@ghom/handler": "^1.
|
|
36
|
+
"@ghom/handler": "^1.2.0",
|
|
37
37
|
"knex": "^2.4.2"
|
|
38
38
|
}
|
|
39
39
|
}
|
package/src/app/orm.ts
CHANGED
|
@@ -1,84 +1,95 @@
|
|
|
1
1
|
import fs from "fs"
|
|
2
2
|
import url from "url"
|
|
3
|
+
import path from "path"
|
|
3
4
|
import { Handler } from "@ghom/handler"
|
|
4
5
|
import { Knex, default as knex } from "knex"
|
|
5
6
|
import { MigrationData, Table } from "./table.js"
|
|
7
|
+
import { Color } from "chalk"
|
|
6
8
|
|
|
7
|
-
const pack = JSON.parse(
|
|
9
|
+
const pack = JSON.parse(
|
|
10
|
+
fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8")
|
|
11
|
+
)
|
|
8
12
|
const isCJS = pack.type === "commonjs" || pack.type == void 0
|
|
9
13
|
|
|
10
|
-
export interface
|
|
11
|
-
log: (message: string
|
|
12
|
-
error: (
|
|
14
|
+
export interface ILogger {
|
|
15
|
+
log: (...message: string[]) => void
|
|
16
|
+
error: (error: Error | string, ...message: string[]) => void
|
|
13
17
|
}
|
|
14
18
|
|
|
15
|
-
/**
|
|
16
|
-
* @property tablePath - path to directory that contains js files of tables
|
|
17
|
-
* @property verbose - show console logs or not
|
|
18
|
-
*/
|
|
19
19
|
export interface ORMConfig {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
/**
|
|
21
|
+
* path to the directory that contains js files of tables
|
|
22
|
+
*/
|
|
23
|
+
location: string
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
/**
|
|
26
|
+
* database configuration
|
|
27
|
+
*/
|
|
28
|
+
database?: Knex.Config
|
|
27
29
|
|
|
28
30
|
/**
|
|
29
|
-
*
|
|
30
|
-
* @param knexConfig configuration for connect to database
|
|
31
|
+
* Logger used to log the table files loaded or created.
|
|
31
32
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
) {
|
|
42
|
-
super(typeof ormConfig === "string" ? ormConfig : ormConfig.tablePath)
|
|
43
|
-
this.ormConfig =
|
|
44
|
-
typeof ormConfig === "string" ? { tablePath: ormConfig } : ormConfig
|
|
45
|
-
this.db = knex(knexConfig)
|
|
33
|
+
logger?: ILogger
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Pattern used on logs when the table files are loaded or created. <br>
|
|
37
|
+
* Based on Chalk color-method names.
|
|
38
|
+
*/
|
|
39
|
+
loggerColors?: {
|
|
40
|
+
highlight: typeof Color
|
|
41
|
+
rawValue: typeof Color
|
|
46
42
|
}
|
|
43
|
+
}
|
|
47
44
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
pathList.map(async (filepath) => {
|
|
52
|
-
return import(
|
|
53
|
-
isCJS ? filepath : url.pathToFileURL(filepath).href
|
|
54
|
-
).then((file) => file.default)
|
|
55
|
-
})
|
|
56
|
-
)
|
|
45
|
+
export class ORM {
|
|
46
|
+
database: Knex
|
|
47
|
+
handler: Handler<Table<any>>
|
|
57
48
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
49
|
+
constructor(public config: ORMConfig) {
|
|
50
|
+
this.database = knex(
|
|
51
|
+
config.database ?? {
|
|
52
|
+
client: "sqlite3",
|
|
53
|
+
useNullAsDefault: true,
|
|
54
|
+
connection: {
|
|
55
|
+
filename: ":memory:",
|
|
64
56
|
},
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
migration.orm = this
|
|
68
|
-
await migration.make()
|
|
69
|
-
|
|
70
|
-
for (const table of tables.sort(
|
|
71
|
-
(a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0)
|
|
72
|
-
)) {
|
|
73
|
-
table.orm = this
|
|
74
|
-
await table.make()
|
|
75
57
|
}
|
|
58
|
+
)
|
|
59
|
+
this.handler = new Handler(config.location, {
|
|
60
|
+
loader: (filepath) =>
|
|
61
|
+
import(isCJS ? filepath : url.pathToFileURL(filepath).href).then(
|
|
62
|
+
(file) => file.default
|
|
63
|
+
),
|
|
76
64
|
})
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async init() {
|
|
68
|
+
await this.handler.init()
|
|
69
|
+
|
|
70
|
+
const tables = [...this.handler.elements.values()]
|
|
77
71
|
|
|
78
72
|
try {
|
|
79
|
-
await this.
|
|
73
|
+
await this.database.raw("PRAGMA foreign_keys = ON;")
|
|
80
74
|
} catch (error) {}
|
|
81
75
|
|
|
82
|
-
|
|
76
|
+
const migration = new Table<MigrationData>({
|
|
77
|
+
name: "migration",
|
|
78
|
+
priority: Infinity,
|
|
79
|
+
setup: (table) => {
|
|
80
|
+
table.string("table").unique().notNullable()
|
|
81
|
+
table.integer("version").notNullable()
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
migration.orm = this
|
|
86
|
+
await migration.make()
|
|
87
|
+
|
|
88
|
+
for (const table of tables.sort(
|
|
89
|
+
(a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0)
|
|
90
|
+
)) {
|
|
91
|
+
table.orm = this
|
|
92
|
+
await table.make()
|
|
93
|
+
}
|
|
83
94
|
}
|
|
84
95
|
}
|
package/src/app/table.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import path from "path"
|
|
2
1
|
import chalk from "chalk"
|
|
3
2
|
import { Knex } from "knex"
|
|
4
3
|
import { ORM } from "./orm.js"
|
|
@@ -21,22 +20,9 @@ export class Table<Type extends {}> {
|
|
|
21
20
|
|
|
22
21
|
constructor(public readonly options: TableOptions<Type>) {}
|
|
23
22
|
|
|
24
|
-
private get filepath() {
|
|
25
|
-
if (!this.orm) throw new Error("missing ORM")
|
|
26
|
-
return path.relative(
|
|
27
|
-
process.cwd(),
|
|
28
|
-
path.join(this.orm.ormConfig.tablePath, this.options.name + ".ts")
|
|
29
|
-
)
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
private get logger() {
|
|
33
|
-
if (!this.orm) throw new Error("missing ORM")
|
|
34
|
-
return this.orm.ormConfig.logger
|
|
35
|
-
}
|
|
36
|
-
|
|
37
23
|
get db() {
|
|
38
24
|
if (!this.orm) throw new Error("missing ORM")
|
|
39
|
-
return this.orm.
|
|
25
|
+
return this.orm.database
|
|
40
26
|
}
|
|
41
27
|
|
|
42
28
|
get query() {
|
|
@@ -55,21 +41,31 @@ export class Table<Type extends {}> {
|
|
|
55
41
|
}
|
|
56
42
|
|
|
57
43
|
async make(): Promise<this> {
|
|
44
|
+
if (!this.orm) throw new Error("missing ORM")
|
|
45
|
+
|
|
58
46
|
try {
|
|
59
47
|
await this.db.schema.createTable(this.options.name, this.options.setup)
|
|
60
|
-
|
|
48
|
+
|
|
49
|
+
this.orm.config.logger?.log(
|
|
50
|
+
`created table ${chalk[
|
|
51
|
+
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
52
|
+
](this.options.name)}`
|
|
53
|
+
)
|
|
61
54
|
} catch (error: any) {
|
|
62
55
|
if (error.toString().includes("syntax error")) {
|
|
63
|
-
this.logger?.error(
|
|
64
|
-
`you need to implement the "setup" method in options of your ${chalk
|
|
65
|
-
this.
|
|
66
|
-
)} table
|
|
67
|
-
this.filepath
|
|
56
|
+
this.orm.config.logger?.error(
|
|
57
|
+
`you need to implement the "setup" method in options of your ${chalk[
|
|
58
|
+
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
59
|
+
](this.options.name)} table!`
|
|
68
60
|
)
|
|
69
61
|
|
|
70
62
|
throw error
|
|
71
63
|
} else {
|
|
72
|
-
this.logger?.log(
|
|
64
|
+
this.orm.config.logger?.log(
|
|
65
|
+
`loaded table ${chalk[
|
|
66
|
+
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
67
|
+
](this.options.name)}`
|
|
68
|
+
)
|
|
73
69
|
}
|
|
74
70
|
}
|
|
75
71
|
|
|
@@ -77,14 +73,16 @@ export class Table<Type extends {}> {
|
|
|
77
73
|
const migrated = await this.migrate()
|
|
78
74
|
|
|
79
75
|
if (migrated !== false) {
|
|
80
|
-
this.logger?.log(
|
|
81
|
-
`migrated table ${chalk
|
|
82
|
-
this.
|
|
83
|
-
)} to version ${chalk
|
|
76
|
+
this.orm.config.logger?.log(
|
|
77
|
+
`migrated table ${chalk[
|
|
78
|
+
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
79
|
+
](this.options.name)} to version ${chalk[
|
|
80
|
+
this.orm.config.loggerColors?.rawValue ?? "magentaBright"
|
|
81
|
+
](migrated)}`
|
|
84
82
|
)
|
|
85
83
|
}
|
|
86
84
|
} catch (error: any) {
|
|
87
|
-
this.logger?.error(error
|
|
85
|
+
this.orm.config.logger?.error(error)
|
|
88
86
|
}
|
|
89
87
|
|
|
90
88
|
await this.options.then?.bind(this)(this)
|
package/tests/test.js
CHANGED
|
@@ -10,8 +10,7 @@ import b from "./tables/b"
|
|
|
10
10
|
import c from "./tables/c"
|
|
11
11
|
|
|
12
12
|
const orm = new ORM({
|
|
13
|
-
|
|
14
|
-
logger: console,
|
|
13
|
+
location: path.join("tests","tables")
|
|
15
14
|
})
|
|
16
15
|
|
|
17
16
|
beforeAll(async () => {
|
|
@@ -19,18 +18,18 @@ beforeAll(async () => {
|
|
|
19
18
|
})
|
|
20
19
|
|
|
21
20
|
test("tables created", async () => {
|
|
22
|
-
expect(await orm.
|
|
23
|
-
expect(await orm.
|
|
24
|
-
expect(await orm.
|
|
25
|
-
expect(await orm.
|
|
21
|
+
expect(await orm.database.schema.hasTable("migration")).toBeTruthy()
|
|
22
|
+
expect(await orm.database.schema.hasTable("a")).toBeTruthy()
|
|
23
|
+
expect(await orm.database.schema.hasTable("b")).toBeTruthy()
|
|
24
|
+
expect(await orm.database.schema.hasTable("c")).toBeTruthy()
|
|
26
25
|
})
|
|
27
26
|
|
|
28
27
|
test("migrations ran", async () => {
|
|
29
|
-
expect(await orm.
|
|
28
|
+
expect(await orm.database.schema.hasColumn("b", "c_id")).toBeTruthy()
|
|
30
29
|
})
|
|
31
30
|
|
|
32
31
|
test("then ran", async () => {
|
|
33
|
-
const rows = await orm.
|
|
32
|
+
const rows = await orm.database("a").select()
|
|
34
33
|
expect(rows.length).toBe(1)
|
|
35
34
|
})
|
|
36
35
|
|
|
@@ -40,5 +39,5 @@ test("cascade delete", async () => {
|
|
|
40
39
|
})
|
|
41
40
|
|
|
42
41
|
afterAll(async () => {
|
|
43
|
-
await orm.
|
|
42
|
+
await orm.database.destroy()
|
|
44
43
|
})
|