@ghom/orm 1.1.1 → 1.1.2

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,8 +1,8 @@
1
1
  import { Handler } from "@ghom/handler";
2
2
  import { Knex } from "knex";
3
3
  export interface ORMLogger {
4
- log: (message: string | number, ...parts: (string | number)[]) => unknown;
5
- error: (err: Error | string | number, ...parts: (string | number)[]) => unknown;
4
+ log: (message: string, section?: string) => void;
5
+ error: (text: string | Error, _path: string, full?: boolean) => void;
6
6
  }
7
7
  /**
8
8
  * @property tablePath - path to directory that contains js files of tables
@@ -17,7 +17,7 @@ export declare class Table<Type> {
17
17
  readonly options: TableOptions<Type>;
18
18
  orm?: ORM;
19
19
  constructor(options: TableOptions<Type>);
20
- private get verbose();
20
+ private get filepath();
21
21
  private get logger();
22
22
  get db(): Knex<any, Record<string, any>[]>;
23
23
  get query(): Knex.QueryBuilder<Type, {
package/dist/app/table.js CHANGED
@@ -1,14 +1,19 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Table = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const chalk_1 = __importDefault(require("chalk"));
4
9
  class Table {
5
10
  constructor(options) {
6
11
  this.options = options;
7
12
  }
8
- get verbose() {
13
+ get filepath() {
9
14
  if (!this.orm)
10
15
  throw new Error("missing ORM");
11
- return !!this.orm.ormConfig.logger;
16
+ return path_1.default.relative(process.cwd(), path_1.default.join(this.orm.ormConfig.tablePath, this.options.name + ".ts"));
12
17
  }
13
18
  get logger() {
14
19
  if (!this.orm)
@@ -35,25 +40,25 @@ class Table {
35
40
  async make() {
36
41
  try {
37
42
  await this.db.schema.createTable(this.options.name, this.options.setup);
38
- this.logger?.log(`created table ${this.options.name}`);
43
+ this.logger?.log(`created table ${chalk_1.default.blueBright(this.options.name)}`);
39
44
  }
40
45
  catch (error) {
41
46
  if (error.toString().includes("syntax error")) {
42
- this.logger?.error(`you need to implement the "setup" method in options of your ${this.options.name} table!`);
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);
43
48
  throw error;
44
49
  }
45
50
  else {
46
- this.logger?.log("loaded table", this.options.name);
51
+ this.logger?.log(`loaded table ${chalk_1.default.blueBright(this.options.name)}`);
47
52
  }
48
53
  }
49
54
  try {
50
55
  const migrated = await this.migrate();
51
56
  if (migrated !== false) {
52
- this.logger?.log("migrated table", this.options.name, "to version", migrated);
57
+ this.logger?.log(`migrated table ${chalk_1.default.blueBright(this.options.name)} to version ${chalk_1.default.magentaBright(migrated)}`);
53
58
  }
54
59
  }
55
60
  catch (error) {
56
- this.logger?.error(error);
61
+ this.logger?.error(error, this.filepath);
57
62
  }
58
63
  await this.options.then?.bind(this)(this);
59
64
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ghom/orm",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,6 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@ghom/handler": "^1.1.0",
27
+ "chalk": "^4.1.2",
27
28
  "knex": "^1.0.3"
28
29
  }
29
30
  }
package/src/app/orm.ts CHANGED
@@ -3,11 +3,8 @@ import { Knex, default as knex } from "knex"
3
3
  import { MigrationData, Table } from "./table"
4
4
 
5
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
6
+ log: (message: string, section?: string) => void
7
+ error: (text: string | Error, _path: string, full?: boolean) => void
11
8
  }
12
9
 
13
10
  /**
package/src/app/table.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import path from "path"
2
+ import chalk from "chalk"
1
3
  import { Knex } from "knex"
2
4
  import { ORM } from "./orm.js"
3
5
 
@@ -19,9 +21,12 @@ export class Table<Type> {
19
21
 
20
22
  constructor(public readonly options: TableOptions<Type>) {}
21
23
 
22
- private get verbose() {
24
+ private get filepath() {
23
25
  if (!this.orm) throw new Error("missing ORM")
24
- return !!this.orm.ormConfig.logger
26
+ return path.relative(
27
+ process.cwd(),
28
+ path.join(this.orm.ormConfig.tablePath, this.options.name + ".ts")
29
+ )
25
30
  }
26
31
 
27
32
  private get logger() {
@@ -52,16 +57,19 @@ export class Table<Type> {
52
57
  async make(): Promise<this> {
53
58
  try {
54
59
  await this.db.schema.createTable(this.options.name, this.options.setup)
55
- this.logger?.log(`created table ${this.options.name}`)
60
+ this.logger?.log(`created table ${chalk.blueBright(this.options.name)}`)
56
61
  } catch (error: any) {
57
62
  if (error.toString().includes("syntax error")) {
58
63
  this.logger?.error(
59
- `you need to implement the "setup" method in options of your ${this.options.name} table!`
64
+ `you need to implement the "setup" method in options of your ${chalk.blueBright(
65
+ this.options.name
66
+ )} table!`,
67
+ this.filepath
60
68
  )
61
69
 
62
70
  throw error
63
71
  } else {
64
- this.logger?.log("loaded table", this.options.name)
72
+ this.logger?.log(`loaded table ${chalk.blueBright(this.options.name)}`)
65
73
  }
66
74
  }
67
75
 
@@ -70,14 +78,13 @@ export class Table<Type> {
70
78
 
71
79
  if (migrated !== false) {
72
80
  this.logger?.log(
73
- "migrated table",
74
- this.options.name,
75
- "to version",
76
- migrated
81
+ `migrated table ${chalk.blueBright(
82
+ this.options.name
83
+ )} to version ${chalk.magentaBright(migrated)}`
77
84
  )
78
85
  }
79
86
  } catch (error: any) {
80
- this.logger?.error(error)
87
+ this.logger?.error(error, this.filepath)
81
88
  }
82
89
 
83
90
  await this.options.then?.bind(this)(this)