@ghom/orm 1.1.0 → 1.1.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/app/orm.d.ts +5 -1
- package/dist/app/table.d.ts +1 -0
- package/dist/app/table.js +11 -11
- package/package.json +1 -1
- package/src/app/orm.ts +9 -1
- package/src/app/table.ts +18 -12
- package/tests/test.js +1 -1
package/dist/app/orm.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import { Handler } from "@ghom/handler";
|
|
2
2
|
import { Knex } from "knex";
|
|
3
|
+
export interface ORMLogger {
|
|
4
|
+
log: (message: string | number, ...parts: (string | number)[]) => unknown;
|
|
5
|
+
error: (err: Error | string | number, ...parts: (string | number)[]) => unknown;
|
|
6
|
+
}
|
|
3
7
|
/**
|
|
4
8
|
* @property tablePath - path to directory that contains js files of tables
|
|
5
9
|
* @property verbose - show console logs or not
|
|
6
10
|
*/
|
|
7
11
|
export interface ORMConfig {
|
|
8
|
-
|
|
12
|
+
logger?: ORMLogger;
|
|
9
13
|
tablePath: string;
|
|
10
14
|
}
|
|
11
15
|
export declare class ORM extends Handler {
|
package/dist/app/table.d.ts
CHANGED
package/dist/app/table.js
CHANGED
|
@@ -8,7 +8,12 @@ class Table {
|
|
|
8
8
|
get verbose() {
|
|
9
9
|
if (!this.orm)
|
|
10
10
|
throw new Error("missing ORM");
|
|
11
|
-
return this.orm.ormConfig.
|
|
11
|
+
return !!this.orm.ormConfig.logger;
|
|
12
|
+
}
|
|
13
|
+
get logger() {
|
|
14
|
+
if (!this.orm)
|
|
15
|
+
throw new Error("missing ORM");
|
|
16
|
+
return this.orm.ormConfig.logger;
|
|
12
17
|
}
|
|
13
18
|
get db() {
|
|
14
19
|
if (!this.orm)
|
|
@@ -30,30 +35,25 @@ class Table {
|
|
|
30
35
|
async make() {
|
|
31
36
|
try {
|
|
32
37
|
await this.db.schema.createTable(this.options.name, this.options.setup);
|
|
33
|
-
|
|
34
|
-
console.log(`created table ${this.options.name}`);
|
|
38
|
+
this.logger?.log(`created table ${this.options.name}`);
|
|
35
39
|
}
|
|
36
40
|
catch (error) {
|
|
37
41
|
if (error.toString().includes("syntax error")) {
|
|
38
|
-
|
|
39
|
-
console.error(`you need to implement the "setup" method in options of your ${this.options.name} table!`);
|
|
42
|
+
this.logger?.error(`you need to implement the "setup" method in options of your ${this.options.name} table!`);
|
|
40
43
|
throw error;
|
|
41
44
|
}
|
|
42
45
|
else {
|
|
43
|
-
|
|
44
|
-
console.log(`loaded table ${this.options.name}`);
|
|
46
|
+
this.logger?.log("loaded table", this.options.name);
|
|
45
47
|
}
|
|
46
48
|
}
|
|
47
49
|
try {
|
|
48
50
|
const migrated = await this.migrate();
|
|
49
51
|
if (migrated !== false) {
|
|
50
|
-
|
|
51
|
-
console.log(`migrated table ${this.options.name} to version ${migrated}`);
|
|
52
|
+
this.logger?.log("migrated table", this.options.name, "to version", migrated);
|
|
52
53
|
}
|
|
53
54
|
}
|
|
54
55
|
catch (error) {
|
|
55
|
-
|
|
56
|
-
console.error(error);
|
|
56
|
+
this.logger?.error(error);
|
|
57
57
|
}
|
|
58
58
|
await this.options.then?.bind(this)(this);
|
|
59
59
|
return this;
|
package/package.json
CHANGED
package/src/app/orm.ts
CHANGED
|
@@ -2,12 +2,20 @@ import { Handler } from "@ghom/handler"
|
|
|
2
2
|
import { Knex, default as knex } from "knex"
|
|
3
3
|
import { MigrationData, Table } from "./table"
|
|
4
4
|
|
|
5
|
+
export interface ORMLogger {
|
|
6
|
+
log: (message: string | number, ...parts: (string | number)[]) => unknown
|
|
7
|
+
error: (
|
|
8
|
+
err: Error | string | number,
|
|
9
|
+
...parts: (string | number)[]
|
|
10
|
+
) => unknown
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
/**
|
|
6
14
|
* @property tablePath - path to directory that contains js files of tables
|
|
7
15
|
* @property verbose - show console logs or not
|
|
8
16
|
*/
|
|
9
17
|
export interface ORMConfig {
|
|
10
|
-
|
|
18
|
+
logger?: ORMLogger
|
|
11
19
|
tablePath: string
|
|
12
20
|
}
|
|
13
21
|
|
package/src/app/table.ts
CHANGED
|
@@ -21,7 +21,12 @@ export class Table<Type> {
|
|
|
21
21
|
|
|
22
22
|
private get verbose() {
|
|
23
23
|
if (!this.orm) throw new Error("missing ORM")
|
|
24
|
-
return this.orm.ormConfig.
|
|
24
|
+
return !!this.orm.ormConfig.logger
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private get logger() {
|
|
28
|
+
if (!this.orm) throw new Error("missing ORM")
|
|
29
|
+
return this.orm.ormConfig.logger
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
get db() {
|
|
@@ -47,17 +52,16 @@ export class Table<Type> {
|
|
|
47
52
|
async make(): Promise<this> {
|
|
48
53
|
try {
|
|
49
54
|
await this.db.schema.createTable(this.options.name, this.options.setup)
|
|
50
|
-
|
|
55
|
+
this.logger?.log(`created table ${this.options.name}`)
|
|
51
56
|
} catch (error: any) {
|
|
52
57
|
if (error.toString().includes("syntax error")) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
)
|
|
58
|
+
this.logger?.error(
|
|
59
|
+
`you need to implement the "setup" method in options of your ${this.options.name} table!`
|
|
60
|
+
)
|
|
57
61
|
|
|
58
62
|
throw error
|
|
59
63
|
} else {
|
|
60
|
-
|
|
64
|
+
this.logger?.log("loaded table", this.options.name)
|
|
61
65
|
}
|
|
62
66
|
}
|
|
63
67
|
|
|
@@ -65,13 +69,15 @@ export class Table<Type> {
|
|
|
65
69
|
const migrated = await this.migrate()
|
|
66
70
|
|
|
67
71
|
if (migrated !== false) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
+
this.logger?.log(
|
|
73
|
+
"migrated table",
|
|
74
|
+
this.options.name,
|
|
75
|
+
"to version",
|
|
76
|
+
migrated
|
|
77
|
+
)
|
|
72
78
|
}
|
|
73
79
|
} catch (error: any) {
|
|
74
|
-
|
|
80
|
+
this.logger?.error(error)
|
|
75
81
|
}
|
|
76
82
|
|
|
77
83
|
await this.options.then?.bind(this)(this)
|