@ghom/orm 1.6.1 → 1.7.1
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 +1 -47
- package/dist/cjs/app/table.js +6 -2
- package/dist/esm/app/orm.js +1 -47
- package/dist/esm/app/table.js +6 -2
- package/dist/typings/app/orm.d.ts +4 -10
- package/dist/typings/app/table.d.ts +5 -4
- package/package.json +10 -10
- package/src/app/orm.ts +83 -73
- package/src/app/table.ts +25 -10
- package/tests/test.js +44 -44
package/dist/cjs/app/orm.js
CHANGED
|
@@ -33,6 +33,7 @@ const path_1 = __importDefault(require("path"));
|
|
|
33
33
|
const handler_1 = require("@ghom/handler");
|
|
34
34
|
const knex_1 = __importDefault(require("knex"));
|
|
35
35
|
const table_js_1 = require("./table.js");
|
|
36
|
+
const defaultBackupDir = path_1.default.join(process.cwd(), "backup");
|
|
36
37
|
const pack = JSON.parse(fs_1.default.readFileSync(path_1.default.join(process.cwd(), "package.json"), "utf8"));
|
|
37
38
|
const isCJS = pack.type === "commonjs" || pack.type == void 0;
|
|
38
39
|
class ORM {
|
|
@@ -89,52 +90,5 @@ class ORM {
|
|
|
89
90
|
raw(sql) {
|
|
90
91
|
return this.database.raw(sql);
|
|
91
92
|
}
|
|
92
|
-
/**
|
|
93
|
-
* Extract the database to a CSV file.
|
|
94
|
-
*/
|
|
95
|
-
async extract(dir = process.cwd()) {
|
|
96
|
-
const tables = [...this.handler.elements.values()];
|
|
97
|
-
for (const table of tables) {
|
|
98
|
-
await this.database
|
|
99
|
-
.select()
|
|
100
|
-
.from(table.options.name)
|
|
101
|
-
.then((rows) => {
|
|
102
|
-
const csv = rows.map((row) => Object.values(row).join(",")).join("\n");
|
|
103
|
-
fs_1.default.writeFileSync(path_1.default.join(dir, `${table.options.name}.csv`), csv, "utf8");
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Import a CSV file to the database.
|
|
109
|
-
*/
|
|
110
|
-
async import(dir = process.cwd()) {
|
|
111
|
-
const tables = [...this.handler.elements.values()].sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0));
|
|
112
|
-
for (const table of tables) {
|
|
113
|
-
const columnInfo = await table.getColumns();
|
|
114
|
-
let csv;
|
|
115
|
-
try {
|
|
116
|
-
csv = fs_1.default.readFileSync(path_1.default.join(dir, `${table.options.name}.csv`), "utf8");
|
|
117
|
-
}
|
|
118
|
-
catch (error) {
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
if (csv.trim().length === 0)
|
|
122
|
-
continue;
|
|
123
|
-
const rows = csv
|
|
124
|
-
.split("\n")
|
|
125
|
-
.map((row) => row.split(","))
|
|
126
|
-
.map((row) => {
|
|
127
|
-
const data = {};
|
|
128
|
-
let index = 0;
|
|
129
|
-
for (const [name, info] of Object.entries(columnInfo)) {
|
|
130
|
-
data[name] =
|
|
131
|
-
info.type === "integer" ? Number(row[index]) : row[index];
|
|
132
|
-
index++;
|
|
133
|
-
}
|
|
134
|
-
return data;
|
|
135
|
-
});
|
|
136
|
-
await this.database(table.options.name).insert(rows);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
93
|
}
|
|
140
94
|
exports.ORM = ORM;
|
package/dist/cjs/app/table.js
CHANGED
|
@@ -43,7 +43,9 @@ class Table {
|
|
|
43
43
|
throw new Error("missing ORM");
|
|
44
44
|
try {
|
|
45
45
|
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
46
|
-
this.orm.config.logger?.log(`created table ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}
|
|
46
|
+
this.orm.config.logger?.log(`created table ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}${this.options.description
|
|
47
|
+
? ` ${chalk_1.default[this.orm.config.loggerColors?.description ?? "grey"](this.options.description)}`
|
|
48
|
+
: ""}`);
|
|
47
49
|
}
|
|
48
50
|
catch (error) {
|
|
49
51
|
if (error.toString().includes("syntax error")) {
|
|
@@ -51,7 +53,9 @@ class Table {
|
|
|
51
53
|
throw error;
|
|
52
54
|
}
|
|
53
55
|
else {
|
|
54
|
-
this.orm.config.logger?.log(`loaded table ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}
|
|
56
|
+
this.orm.config.logger?.log(`loaded table ${chalk_1.default[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}${this.options.description
|
|
57
|
+
? ` ${chalk_1.default[this.orm.config.loggerColors?.description ?? "grey"](this.options.description)}`
|
|
58
|
+
: ""}`);
|
|
55
59
|
}
|
|
56
60
|
}
|
|
57
61
|
try {
|
package/dist/esm/app/orm.js
CHANGED
|
@@ -4,6 +4,7 @@ import path from "path";
|
|
|
4
4
|
import { Handler } from "@ghom/handler";
|
|
5
5
|
import { default as knex } from "knex";
|
|
6
6
|
import { Table } from "./table.js";
|
|
7
|
+
const defaultBackupDir = path.join(process.cwd(), "backup");
|
|
7
8
|
const pack = JSON.parse(fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"));
|
|
8
9
|
const isCJS = pack.type === "commonjs" || pack.type == void 0;
|
|
9
10
|
export class ORM {
|
|
@@ -63,51 +64,4 @@ export class ORM {
|
|
|
63
64
|
raw(sql) {
|
|
64
65
|
return this.database.raw(sql);
|
|
65
66
|
}
|
|
66
|
-
/**
|
|
67
|
-
* Extract the database to a CSV file.
|
|
68
|
-
*/
|
|
69
|
-
async extract(dir = process.cwd()) {
|
|
70
|
-
const tables = [...this.handler.elements.values()];
|
|
71
|
-
for (const table of tables) {
|
|
72
|
-
await this.database
|
|
73
|
-
.select()
|
|
74
|
-
.from(table.options.name)
|
|
75
|
-
.then((rows) => {
|
|
76
|
-
const csv = rows.map((row) => Object.values(row).join(",")).join("\n");
|
|
77
|
-
fs.writeFileSync(path.join(dir, `${table.options.name}.csv`), csv, "utf8");
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Import a CSV file to the database.
|
|
83
|
-
*/
|
|
84
|
-
async import(dir = process.cwd()) {
|
|
85
|
-
const tables = [...this.handler.elements.values()].sort((a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0));
|
|
86
|
-
for (const table of tables) {
|
|
87
|
-
const columnInfo = await table.getColumns();
|
|
88
|
-
let csv;
|
|
89
|
-
try {
|
|
90
|
-
csv = fs.readFileSync(path.join(dir, `${table.options.name}.csv`), "utf8");
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
continue;
|
|
94
|
-
}
|
|
95
|
-
if (csv.trim().length === 0)
|
|
96
|
-
continue;
|
|
97
|
-
const rows = csv
|
|
98
|
-
.split("\n")
|
|
99
|
-
.map((row) => row.split(","))
|
|
100
|
-
.map((row) => {
|
|
101
|
-
const data = {};
|
|
102
|
-
let index = 0;
|
|
103
|
-
for (const [name, info] of Object.entries(columnInfo)) {
|
|
104
|
-
data[name] =
|
|
105
|
-
info.type === "integer" ? Number(row[index]) : row[index];
|
|
106
|
-
index++;
|
|
107
|
-
}
|
|
108
|
-
return data;
|
|
109
|
-
});
|
|
110
|
-
await this.database(table.options.name).insert(rows);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
67
|
}
|
package/dist/esm/app/table.js
CHANGED
|
@@ -39,7 +39,9 @@ export class Table {
|
|
|
39
39
|
throw new Error("missing ORM");
|
|
40
40
|
try {
|
|
41
41
|
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
42
|
-
this.orm.config.logger?.log(`created table ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}
|
|
42
|
+
this.orm.config.logger?.log(`created table ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}${this.options.description
|
|
43
|
+
? ` ${chalk[this.orm.config.loggerColors?.description ?? "grey"](this.options.description)}`
|
|
44
|
+
: ""}`);
|
|
43
45
|
}
|
|
44
46
|
catch (error) {
|
|
45
47
|
if (error.toString().includes("syntax error")) {
|
|
@@ -47,7 +49,9 @@ export class Table {
|
|
|
47
49
|
throw error;
|
|
48
50
|
}
|
|
49
51
|
else {
|
|
50
|
-
this.orm.config.logger?.log(`loaded table ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}
|
|
52
|
+
this.orm.config.logger?.log(`loaded table ${chalk[this.orm.config.loggerColors?.highlight ?? "blueBright"](this.options.name)}${this.options.description
|
|
53
|
+
? ` ${chalk[this.orm.config.loggerColors?.description ?? "grey"](this.options.description)}`
|
|
54
|
+
: ""}`);
|
|
51
55
|
}
|
|
52
56
|
}
|
|
53
57
|
try {
|
|
@@ -3,8 +3,9 @@ import { Knex } from "knex";
|
|
|
3
3
|
import { Table } from "./table.js";
|
|
4
4
|
import { Color } from "chalk";
|
|
5
5
|
export interface ILogger {
|
|
6
|
-
log: (
|
|
7
|
-
error: (error:
|
|
6
|
+
log: (message: string) => void;
|
|
7
|
+
error: (error: string | Error) => void;
|
|
8
|
+
warn: (warning: string) => void;
|
|
8
9
|
}
|
|
9
10
|
export interface ORMConfig {
|
|
10
11
|
/**
|
|
@@ -26,6 +27,7 @@ export interface ORMConfig {
|
|
|
26
27
|
loggerColors?: {
|
|
27
28
|
highlight: typeof Color;
|
|
28
29
|
rawValue: typeof Color;
|
|
30
|
+
description: typeof Color;
|
|
29
31
|
};
|
|
30
32
|
}
|
|
31
33
|
export declare class ORM {
|
|
@@ -42,12 +44,4 @@ export declare class ORM {
|
|
|
42
44
|
*/
|
|
43
45
|
init(): Promise<void>;
|
|
44
46
|
raw(sql: Knex.Value): Knex.Raw;
|
|
45
|
-
/**
|
|
46
|
-
* Extract the database to a CSV file.
|
|
47
|
-
*/
|
|
48
|
-
extract(dir?: string): Promise<void>;
|
|
49
|
-
/**
|
|
50
|
-
* Import a CSV file to the database.
|
|
51
|
-
*/
|
|
52
|
-
import(dir?: string): Promise<void>;
|
|
53
47
|
}
|
|
@@ -6,6 +6,7 @@ export interface MigrationData {
|
|
|
6
6
|
}
|
|
7
7
|
export interface TableOptions<Type extends object = object> {
|
|
8
8
|
name: string;
|
|
9
|
+
description?: string;
|
|
9
10
|
priority?: number;
|
|
10
11
|
migrations?: {
|
|
11
12
|
[version: number]: (table: Knex.CreateTableBuilder) => void;
|
|
@@ -28,10 +29,10 @@ export declare class Table<Type extends object = object> {
|
|
|
28
29
|
_unionProps: never;
|
|
29
30
|
}[]>;
|
|
30
31
|
count(where?: string): Promise<number>;
|
|
31
|
-
hasColumn(name: keyof Type): Promise<boolean>;
|
|
32
|
-
getColumn(name: keyof Type): Promise<Knex.ColumnInfo>;
|
|
33
|
-
getColumns(): Promise<Record<keyof Type, Knex.ColumnInfo>>;
|
|
34
|
-
getColumnNames(): Promise<Array<keyof Type>>;
|
|
32
|
+
hasColumn(name: keyof Type & string): Promise<boolean>;
|
|
33
|
+
getColumn(name: keyof Type & string): Promise<Knex.ColumnInfo>;
|
|
34
|
+
getColumns(): Promise<Record<keyof Type & string, Knex.ColumnInfo>>;
|
|
35
|
+
getColumnNames(): Promise<Array<keyof Type & string>>;
|
|
35
36
|
isEmpty(): Promise<boolean>;
|
|
36
37
|
make(): Promise<this>;
|
|
37
38
|
private migrate;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ghom/orm",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.7.1",
|
|
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": "^
|
|
25
|
-
"dotenv": "^16.
|
|
26
|
-
"jest": "^29.
|
|
27
|
-
"prettier": "^
|
|
28
|
-
"typescript": "^5.
|
|
23
|
+
"@types/jest": "^29.5.6",
|
|
24
|
+
"@types/node": "^20.8.9",
|
|
25
|
+
"dotenv": "^16.3.1",
|
|
26
|
+
"jest": "^29.7.0",
|
|
27
|
+
"prettier": "^3.0.3",
|
|
28
|
+
"typescript": "^5.2.2"
|
|
29
29
|
},
|
|
30
30
|
"optionalDependencies": {
|
|
31
|
-
"mysql2": "^3.2
|
|
32
|
-
"pg": "^8.
|
|
31
|
+
"mysql2": "^3.6.2",
|
|
32
|
+
"pg": "^8.11.3",
|
|
33
33
|
"sqlite3": "^5.1.6"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@ghom/handler": "^2.0.0",
|
|
37
|
-
"knex": "^
|
|
37
|
+
"knex": "^3.0.1"
|
|
38
38
|
}
|
|
39
39
|
}
|
package/src/app/orm.ts
CHANGED
|
@@ -4,16 +4,19 @@ import path from "path"
|
|
|
4
4
|
import { Handler } from "@ghom/handler"
|
|
5
5
|
import { Knex, default as knex } from "knex"
|
|
6
6
|
import { MigrationData, Table } from "./table.js"
|
|
7
|
-
import { Color } from "chalk"
|
|
7
|
+
import chalk, { Color } from "chalk"
|
|
8
|
+
|
|
9
|
+
const defaultBackupDir = path.join(process.cwd(), "backup")
|
|
8
10
|
|
|
9
11
|
const pack = JSON.parse(
|
|
10
|
-
fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8")
|
|
12
|
+
fs.readFileSync(path.join(process.cwd(), "package.json"), "utf8"),
|
|
11
13
|
)
|
|
12
14
|
const isCJS = pack.type === "commonjs" || pack.type == void 0
|
|
13
15
|
|
|
14
16
|
export interface ILogger {
|
|
15
|
-
log: (
|
|
16
|
-
error: (error:
|
|
17
|
+
log: (message: string) => void
|
|
18
|
+
error: (error: string | Error) => void
|
|
19
|
+
warn: (warning: string) => void
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
export interface ORMConfig {
|
|
@@ -39,6 +42,7 @@ export interface ORMConfig {
|
|
|
39
42
|
loggerColors?: {
|
|
40
43
|
highlight: typeof Color
|
|
41
44
|
rawValue: typeof Color
|
|
45
|
+
description: typeof Color
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
48
|
|
|
@@ -54,13 +58,13 @@ export class ORM {
|
|
|
54
58
|
connection: {
|
|
55
59
|
filename: ":memory:",
|
|
56
60
|
},
|
|
57
|
-
}
|
|
61
|
+
},
|
|
58
62
|
)
|
|
59
63
|
|
|
60
64
|
this.handler = new Handler(config.location, {
|
|
61
65
|
loader: (filepath) =>
|
|
62
66
|
import(isCJS ? filepath : url.pathToFileURL(filepath).href).then(
|
|
63
|
-
(file) => file.default
|
|
67
|
+
(file) => file.default,
|
|
64
68
|
),
|
|
65
69
|
pattern: /\.js$/,
|
|
66
70
|
})
|
|
@@ -105,7 +109,7 @@ export class ORM {
|
|
|
105
109
|
await migration.make()
|
|
106
110
|
|
|
107
111
|
for (const table of this.cachedTables.sort(
|
|
108
|
-
(a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0)
|
|
112
|
+
(a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0),
|
|
109
113
|
)) {
|
|
110
114
|
table.orm = this
|
|
111
115
|
await table.make()
|
|
@@ -116,70 +120,76 @@ export class ORM {
|
|
|
116
120
|
return this.database.raw(sql)
|
|
117
121
|
}
|
|
118
122
|
|
|
119
|
-
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
async
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
async
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
123
|
+
// /**
|
|
124
|
+
// * Extract the database to a CSV file for each table.
|
|
125
|
+
// */
|
|
126
|
+
// async createBackup(dir = defaultBackupDir) {
|
|
127
|
+
// const tables = [...this.handler.elements.values()]
|
|
128
|
+
//
|
|
129
|
+
// for (const table of tables) {
|
|
130
|
+
// await this.database
|
|
131
|
+
// .select()
|
|
132
|
+
// .from(table.options.name)
|
|
133
|
+
// .then(async (rows) => {
|
|
134
|
+
// const csv = rows.map((row) => Object.values(row).join(",")).join("\n")
|
|
135
|
+
//
|
|
136
|
+
// return fs.promises.writeFile(
|
|
137
|
+
// path.join(dir, `${table.options.name}.csv`),
|
|
138
|
+
// csv,
|
|
139
|
+
// "utf8",
|
|
140
|
+
// )
|
|
141
|
+
// })
|
|
142
|
+
// }
|
|
143
|
+
// }
|
|
144
|
+
//
|
|
145
|
+
// /**
|
|
146
|
+
// * Import a CSV file for each table to the database.
|
|
147
|
+
// */
|
|
148
|
+
// async restoreBackup(dir = defaultBackupDir) {
|
|
149
|
+
// const tables = [...this.handler.elements.values()].sort(
|
|
150
|
+
// (a, b) => (b.options.priority ?? 0) - (a.options.priority ?? 0),
|
|
151
|
+
// )
|
|
152
|
+
//
|
|
153
|
+
// for (const table of tables) {
|
|
154
|
+
// const columnInfo = await table.getColumns()
|
|
155
|
+
//
|
|
156
|
+
// let csv: string
|
|
157
|
+
//
|
|
158
|
+
// try {
|
|
159
|
+
// csv = await fs.promises.readFile(
|
|
160
|
+
// path.join(dir, `${table.options.name}.csv`),
|
|
161
|
+
// "utf8",
|
|
162
|
+
// )
|
|
163
|
+
// } catch (error) {
|
|
164
|
+
// this.config.logger?.warn(
|
|
165
|
+
// `missing backup file for table ${chalk[
|
|
166
|
+
// this.config.loggerColors?.highlight ?? "blueBright"
|
|
167
|
+
// ](table.options.name)}`,
|
|
168
|
+
// )
|
|
169
|
+
//
|
|
170
|
+
// continue
|
|
171
|
+
// }
|
|
172
|
+
//
|
|
173
|
+
// if (csv.trim().length === 0) continue
|
|
174
|
+
//
|
|
175
|
+
// const rows = csv
|
|
176
|
+
// .split("\n")
|
|
177
|
+
// .map((row) => row.split(","))
|
|
178
|
+
// .map((row) => {
|
|
179
|
+
// const data: any = {}
|
|
180
|
+
//
|
|
181
|
+
// let index = 0
|
|
182
|
+
//
|
|
183
|
+
// for (const [name, info] of Object.entries(columnInfo)) {
|
|
184
|
+
// data[name] =
|
|
185
|
+
// info.type === "integer" ? Number(row[index]) : row[index]
|
|
186
|
+
// index++
|
|
187
|
+
// }
|
|
188
|
+
//
|
|
189
|
+
// return data
|
|
190
|
+
// })
|
|
191
|
+
//
|
|
192
|
+
// await this.database(table.options.name).insert(rows)
|
|
193
|
+
// }
|
|
194
|
+
// }
|
|
185
195
|
}
|
package/src/app/table.ts
CHANGED
|
@@ -9,6 +9,7 @@ export interface MigrationData {
|
|
|
9
9
|
|
|
10
10
|
export interface TableOptions<Type extends object = object> {
|
|
11
11
|
name: string
|
|
12
|
+
description?: string
|
|
12
13
|
priority?: number
|
|
13
14
|
migrations?: { [version: number]: (table: Knex.CreateTableBuilder) => void }
|
|
14
15
|
then?: (this: Table<Type>, table: Table<Type>) => unknown
|
|
@@ -36,20 +37,22 @@ export class Table<Type extends object = object> {
|
|
|
36
37
|
.then((rows) => (rows[0] as unknown as { total: number }).total)
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
async hasColumn(name: keyof Type): Promise<boolean> {
|
|
40
|
+
async hasColumn(name: keyof Type & string): Promise<boolean> {
|
|
40
41
|
return this.db.schema.hasColumn(this.options.name, name as string)
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
async getColumn(name: keyof Type): Promise<Knex.ColumnInfo> {
|
|
44
|
+
async getColumn(name: keyof Type & string): Promise<Knex.ColumnInfo> {
|
|
44
45
|
return this.db(this.options.name).columnInfo(name)
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
async getColumns(): Promise<Record<keyof Type, Knex.ColumnInfo>> {
|
|
48
|
+
async getColumns(): Promise<Record<keyof Type & string, Knex.ColumnInfo>> {
|
|
48
49
|
return this.db(this.options.name).columnInfo()
|
|
49
50
|
}
|
|
50
51
|
|
|
51
|
-
async getColumnNames(): Promise<Array<keyof Type>> {
|
|
52
|
-
return this.getColumns().then(Object.keys) as Promise<
|
|
52
|
+
async getColumnNames(): Promise<Array<keyof Type & string>> {
|
|
53
|
+
return this.getColumns().then(Object.keys) as Promise<
|
|
54
|
+
Array<keyof Type & string>
|
|
55
|
+
>
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
async isEmpty(): Promise<boolean> {
|
|
@@ -65,14 +68,20 @@ export class Table<Type extends object = object> {
|
|
|
65
68
|
this.orm.config.logger?.log(
|
|
66
69
|
`created table ${chalk[
|
|
67
70
|
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
68
|
-
](this.options.name)}
|
|
71
|
+
](this.options.name)}${
|
|
72
|
+
this.options.description
|
|
73
|
+
? ` ${chalk[this.orm.config.loggerColors?.description ?? "grey"](
|
|
74
|
+
this.options.description,
|
|
75
|
+
)}`
|
|
76
|
+
: ""
|
|
77
|
+
}`,
|
|
69
78
|
)
|
|
70
79
|
} catch (error: any) {
|
|
71
80
|
if (error.toString().includes("syntax error")) {
|
|
72
81
|
this.orm.config.logger?.error(
|
|
73
82
|
`you need to implement the "setup" method in options of your ${chalk[
|
|
74
83
|
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
75
|
-
](this.options.name)} table
|
|
84
|
+
](this.options.name)} table!`,
|
|
76
85
|
)
|
|
77
86
|
|
|
78
87
|
throw error
|
|
@@ -80,7 +89,13 @@ export class Table<Type extends object = object> {
|
|
|
80
89
|
this.orm.config.logger?.log(
|
|
81
90
|
`loaded table ${chalk[
|
|
82
91
|
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
83
|
-
](this.options.name)}
|
|
92
|
+
](this.options.name)}${
|
|
93
|
+
this.options.description
|
|
94
|
+
? ` ${chalk[this.orm.config.loggerColors?.description ?? "grey"](
|
|
95
|
+
this.options.description,
|
|
96
|
+
)}`
|
|
97
|
+
: ""
|
|
98
|
+
}`,
|
|
84
99
|
)
|
|
85
100
|
}
|
|
86
101
|
}
|
|
@@ -94,7 +109,7 @@ export class Table<Type extends object = object> {
|
|
|
94
109
|
this.orm.config.loggerColors?.highlight ?? "blueBright"
|
|
95
110
|
](this.options.name)} to version ${chalk[
|
|
96
111
|
this.orm.config.loggerColors?.rawValue ?? "magentaBright"
|
|
97
|
-
](migrated)}
|
|
112
|
+
](migrated)}`,
|
|
98
113
|
)
|
|
99
114
|
}
|
|
100
115
|
} catch (error: any) {
|
|
@@ -117,7 +132,7 @@ export class Table<Type extends object = object> {
|
|
|
117
132
|
>(
|
|
118
133
|
Object.entries(this.options.migrations)
|
|
119
134
|
.sort((a, b) => Number(a[0]) - Number(b[0]))
|
|
120
|
-
.map((entry) => [Number(entry[0]), entry[1]])
|
|
135
|
+
.map((entry) => [Number(entry[0]), entry[1]]),
|
|
121
136
|
)
|
|
122
137
|
|
|
123
138
|
const fromDatabase = await this.db<MigrationData>("migration")
|
package/tests/test.js
CHANGED
|
@@ -54,7 +54,7 @@ describe("table column types", () => {
|
|
|
54
54
|
await orm
|
|
55
55
|
.database("a")
|
|
56
56
|
.columnInfo("id")
|
|
57
|
-
.then((info) => info.type)
|
|
57
|
+
.then((info) => info.type),
|
|
58
58
|
).toBe("integer")
|
|
59
59
|
})
|
|
60
60
|
|
|
@@ -63,50 +63,50 @@ describe("table column types", () => {
|
|
|
63
63
|
await orm
|
|
64
64
|
.database("a")
|
|
65
65
|
.columnInfo("b_id")
|
|
66
|
-
.then((info) => info.type)
|
|
66
|
+
.then((info) => info.type),
|
|
67
67
|
).toBe("integer")
|
|
68
68
|
})
|
|
69
69
|
})
|
|
70
70
|
|
|
71
|
-
describe("database
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
})
|
|
71
|
+
// describe("database extraction", () => {
|
|
72
|
+
// beforeAll(async () => {
|
|
73
|
+
// await c.query.insert({ id: 0 })
|
|
74
|
+
// await b.query.insert({
|
|
75
|
+
// id: 0,
|
|
76
|
+
// c_id: 0,
|
|
77
|
+
// })
|
|
78
|
+
// await a.query.insert({
|
|
79
|
+
// id: 0,
|
|
80
|
+
// b_id: 0,
|
|
81
|
+
// })
|
|
82
|
+
// })
|
|
83
|
+
//
|
|
84
|
+
// test("extract CSV", async () => {
|
|
85
|
+
// await orm.extract()
|
|
86
|
+
//
|
|
87
|
+
// expect(fs.existsSync("a.csv")).toBeTruthy()
|
|
88
|
+
// expect(fs.existsSync("b.csv")).toBeTruthy()
|
|
89
|
+
// expect(fs.existsSync("c.csv")).toBeTruthy()
|
|
90
|
+
// })
|
|
91
|
+
//
|
|
92
|
+
// test("empty tables", async () => {
|
|
93
|
+
// await a.query.del()
|
|
94
|
+
// await b.query.del()
|
|
95
|
+
// await c.query.del()
|
|
96
|
+
//
|
|
97
|
+
// expect(await a.isEmpty()).toBeTruthy()
|
|
98
|
+
// expect(await b.isEmpty()).toBeTruthy()
|
|
99
|
+
// expect(await c.isEmpty()).toBeTruthy()
|
|
100
|
+
// })
|
|
101
|
+
//
|
|
102
|
+
// test("import CSV", async () => {
|
|
103
|
+
// await orm.import()
|
|
104
|
+
//
|
|
105
|
+
// expect(await a.isEmpty()).toBeFalsy()
|
|
106
|
+
// expect(await b.isEmpty()).toBeFalsy()
|
|
107
|
+
// expect(await c.isEmpty()).toBeFalsy()
|
|
108
|
+
// })
|
|
109
|
+
// })
|
|
110
110
|
|
|
111
111
|
describe("table getters", () => {
|
|
112
112
|
test("table info", async () => {
|
|
@@ -128,7 +128,7 @@ describe("table getters", () => {
|
|
|
128
128
|
|
|
129
129
|
afterAll(async () => {
|
|
130
130
|
await orm.database.destroy()
|
|
131
|
-
fs.unlinkSync("a.csv")
|
|
132
|
-
fs.unlinkSync("b.csv")
|
|
133
|
-
fs.unlinkSync("c.csv")
|
|
131
|
+
// fs.unlinkSync("a.csv")
|
|
132
|
+
// fs.unlinkSync("b.csv")
|
|
133
|
+
// fs.unlinkSync("c.csv")
|
|
134
134
|
})
|