@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 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
- verbose?: boolean;
12
+ logger?: ORMLogger;
9
13
  tablePath: string;
10
14
  }
11
15
  export declare class ORM extends Handler {
@@ -18,6 +18,7 @@ export declare class Table<Type> {
18
18
  orm?: ORM;
19
19
  constructor(options: TableOptions<Type>);
20
20
  private get verbose();
21
+ private get logger();
21
22
  get db(): Knex<any, Record<string, any>[]>;
22
23
  get query(): Knex.QueryBuilder<Type, {
23
24
  _base: Type;
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.verbose;
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
- if (this.verbose)
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
- if (this.verbose)
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
- if (this.verbose)
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
- if (this.verbose)
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
- if (this.verbose)
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ghom/orm",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
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
- verbose?: boolean
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.verbose
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
- if (this.verbose) console.log(`created table ${this.options.name}`)
55
+ this.logger?.log(`created table ${this.options.name}`)
51
56
  } catch (error: any) {
52
57
  if (error.toString().includes("syntax error")) {
53
- if (this.verbose)
54
- console.error(
55
- `you need to implement the "setup" method in options of your ${this.options.name} table!`
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
- if (this.verbose) console.log(`loaded table ${this.options.name}`)
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
- if (this.verbose)
69
- console.log(
70
- `migrated table ${this.options.name} to version ${migrated}`
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
- if (this.verbose) console.error(error)
80
+ this.logger?.error(error)
75
81
  }
76
82
 
77
83
  await this.options.then?.bind(this)(this)
package/tests/test.js CHANGED
@@ -7,7 +7,7 @@ const c = require("./tables/c")
7
7
 
8
8
  const orm = new ORM({
9
9
  tablePath: path.join(__dirname, "tables"),
10
- verbose: false,
10
+ logger: console,
11
11
  })
12
12
 
13
13
  beforeAll(async () => {