@ghom/orm 1.9.0 → 1.9.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
@@ -2,7 +2,7 @@ import { Handler } from "@ghom/handler";
2
2
  import { Knex } from "knex";
3
3
  import { TextStyle } from "./util.js";
4
4
  import { Table } from "./table.js";
5
- import { ResponseCache } from "./caching.js";
5
+ import { CachedQuery } from "@ghom/query";
6
6
  export interface ILogger {
7
7
  log: (message: string) => void;
8
8
  error: (error: string | Error) => void;
@@ -49,7 +49,7 @@ export declare class ORM {
49
49
  private _ready;
50
50
  client: Knex<any, unknown[]>;
51
51
  handler: Handler<Table<any>>;
52
- _rawCache: ResponseCache<[raw: string], Knex.Raw<any>>;
52
+ _rawCache: CachedQuery<[raw: string], any>;
53
53
  constructor(config: ORMConfig);
54
54
  get cachedTables(): Table<any>[];
55
55
  get cachedTableNames(): string[];
@@ -61,7 +61,7 @@ export declare class ORM {
61
61
  init(): Promise<void>;
62
62
  raw(sql: Knex.Value): Knex.Raw;
63
63
  cache: {
64
- raw: (sql: string, anyDataUpdated?: boolean) => Knex.Raw;
64
+ raw: (sql: string, anyDataUpdated?: boolean) => Promise<Knex.Raw>;
65
65
  invalidate: () => void;
66
66
  };
67
67
  clientBasedOperation<Return>(operation: Partial<Record<"pg" | "mysql2" | "sqlite3", () => Return>>): Return | undefined;
package/dist/app/orm.js CHANGED
@@ -3,7 +3,7 @@ import { Handler } from "@ghom/handler";
3
3
  import { default as knex } from "knex";
4
4
  import { isCJS } from "./util.js";
5
5
  import { Table } from "./table.js";
6
- import { ResponseCache } from "./caching.js";
6
+ import { CachedQuery } from "@ghom/query";
7
7
  import { backupTable, restoreBackup, disableForeignKeys, enableForeignKeys, } from "./backup.js";
8
8
  export class ORM {
9
9
  config;
@@ -29,7 +29,7 @@ export class ORM {
29
29
  throw new Error(`${filepath}: default export must be a Table instance`);
30
30
  },
31
31
  });
32
- this._rawCache = new ResponseCache((raw) => this.raw(raw), config.caching ?? Infinity);
32
+ this._rawCache = new CachedQuery(async (raw) => await this.raw(raw), config.caching ?? Infinity);
33
33
  }
34
34
  get cachedTables() {
35
35
  return [...this.handler.elements.values()];
@@ -1,6 +1,6 @@
1
1
  import { Knex } from "knex";
2
2
  import { ORM } from "./orm.js";
3
- import { ResponseCache } from "./caching.js";
3
+ import { CachedQuery } from "@ghom/query";
4
4
  export interface MigrationData {
5
5
  table: string;
6
6
  version: number;
@@ -23,10 +23,10 @@ export interface TableOptions<Type extends object = object> {
23
23
  export declare class Table<Type extends object = object> {
24
24
  readonly options: TableOptions<Type>;
25
25
  orm?: ORM;
26
- _whereCache?: ResponseCache<[
26
+ _whereCache?: CachedQuery<[
27
27
  cb: (query: Table<Type>["query"]) => unknown
28
28
  ], unknown>;
29
- _countCache?: ResponseCache<[where: string | null], Promise<number>>;
29
+ _countCache?: CachedQuery<[where: string | null], number>;
30
30
  constructor(options: TableOptions<Type>);
31
31
  get db(): Knex<any, unknown[]>;
32
32
  get query(): Knex.QueryBuilder<Type, {
package/dist/app/table.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { styled } from "./util.js";
2
- import { ResponseCache } from "./caching.js";
2
+ import { CachedQuery } from "@ghom/query";
3
3
  export class Table {
4
4
  options;
5
5
  orm;
@@ -63,8 +63,8 @@ export class Table {
63
63
  }
64
64
  async make(orm) {
65
65
  this.orm = orm;
66
- this._whereCache = new ResponseCache((cb) => cb(this.query), this.options.caching ?? this.orm?.config.caching ?? Infinity);
67
- this._countCache = new ResponseCache((where) => this.count(where ?? undefined), this.options.caching ?? this.orm?.config.caching ?? Infinity);
66
+ this._whereCache = new CachedQuery((cb) => cb(this.query), this.options.caching ?? this.orm?.config.caching ?? Infinity);
67
+ this._countCache = new CachedQuery((where) => this.count(where ?? undefined), this.options.caching ?? this.orm?.config.caching ?? Infinity);
68
68
  const tableNameLog = `table ${styled(this.orm, this.options.name, "highlight")}${this.options.description
69
69
  ? ` ${styled(this.orm, this.options.description, "description")}`
70
70
  : ""}`;
@@ -109,14 +109,14 @@ export class Table {
109
109
  version: -Infinity,
110
110
  };
111
111
  const baseVersion = data.version;
112
- await this.db.schema.alterTable(this.options.name, (builder) => {
113
- migrations.forEach((migration, version) => {
112
+ for (const [version, migration] of migrations) {
113
+ await this.db.schema.alterTable(this.options.name, (builder) => {
114
114
  if (version <= data.version)
115
115
  return;
116
116
  migration(builder);
117
117
  data.version = version;
118
118
  });
119
- });
119
+ }
120
120
  await this.db("migration")
121
121
  .insert(data)
122
122
  .onConflict("table")
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./app/orm.js";
2
2
  export * from "./app/table.js";
3
- export * from "./app/caching.js";
4
3
  export * from "./app/backup.js";
5
4
  export * from "./app/util.js";
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./app/orm.js";
2
2
  export * from "./app/table.js";
3
- export * from "./app/caching.js";
4
3
  export * from "./app/backup.js";
5
4
  export * from "./app/util.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ghom/orm",
3
- "version": "1.9.0",
3
+ "version": "1.9.2",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -32,6 +32,7 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@ghom/handler": "^3.1.0",
35
+ "@ghom/query": "1.0.0",
35
36
  "csv-parser": "^3.0.0",
36
37
  "json-2-csv": "^5.5.6",
37
38
  "knex": "^3.0.1"
package/tests/tables/b.js CHANGED
@@ -6,7 +6,9 @@ import { Table } from "../.."
6
6
  export default new Table({
7
7
  name: "b",
8
8
  migrations: {
9
- 0: (table) =>
9
+ 0: (table) => table.string("c_id"),
10
+ 1: (table) => table.dropColumn("c_id"),
11
+ 2: (table) =>
10
12
  table
11
13
  .integer("c_id")
12
14
  .unsigned()
@@ -1,18 +0,0 @@
1
- export interface ResponseCacheData<Value> {
2
- value: Value;
3
- expires: number;
4
- outdated?: boolean;
5
- }
6
- /**
7
- * Advanced cache for async queries
8
- */
9
- export declare class ResponseCache<Params extends any[], Value> {
10
- private _request;
11
- private _timeout;
12
- private _cache;
13
- constructor(_request: (...params: Params) => Value, _timeout: number);
14
- get(id: string, ...params: Params): Value;
15
- fetch(id: string, ...params: Params): Value;
16
- invalidate(): void;
17
- invalidate(id: string): void;
18
- }
@@ -1,36 +0,0 @@
1
- /**
2
- * Advanced cache for async queries
3
- */
4
- export class ResponseCache {
5
- _request;
6
- _timeout;
7
- _cache = new Map();
8
- constructor(_request, _timeout) {
9
- this._request = _request;
10
- this._timeout = _timeout;
11
- }
12
- get(id, ...params) {
13
- const cached = this._cache.get(id);
14
- if (!cached || cached.expires < Date.now()) {
15
- this._cache.set(id, {
16
- value: this._request(...params),
17
- expires: Date.now() + this._timeout,
18
- });
19
- }
20
- return this._cache.get(id).value;
21
- }
22
- fetch(id, ...params) {
23
- this._cache.set(id, {
24
- value: this._request(...params),
25
- expires: Date.now() + this._timeout,
26
- });
27
- return this._cache.get(id).value;
28
- }
29
- invalidate(id) {
30
- if (!id) {
31
- this._cache.clear();
32
- return;
33
- }
34
- this._cache.delete(id);
35
- }
36
- }