@ghom/orm 1.9.1 → 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 +3 -3
- package/dist/app/orm.js +2 -2
- package/dist/app/table.d.ts +3 -3
- package/dist/app/table.js +3 -3
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/package.json +2 -1
- package/dist/app/caching.d.ts +0 -18
- package/dist/app/caching.js +0 -36
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 {
|
|
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:
|
|
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 {
|
|
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
|
|
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()];
|
package/dist/app/table.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Knex } from "knex";
|
|
2
2
|
import { ORM } from "./orm.js";
|
|
3
|
-
import {
|
|
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?:
|
|
26
|
+
_whereCache?: CachedQuery<[
|
|
27
27
|
cb: (query: Table<Type>["query"]) => unknown
|
|
28
28
|
], unknown>;
|
|
29
|
-
_countCache?:
|
|
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 {
|
|
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
|
|
67
|
-
this._countCache = new
|
|
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
|
: ""}`;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ghom/orm",
|
|
3
|
-
"version": "1.9.
|
|
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/dist/app/caching.d.ts
DELETED
|
@@ -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
|
-
}
|
package/dist/app/caching.js
DELETED
|
@@ -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
|
-
}
|