fib-orm 1.4.2 → 2.1.0
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/License +1 -1
- package/README.md +53 -0
- package/lib/cli/commands/ddlFrom.d.ts +2 -0
- package/lib/cli/commands/ddlFrom.js +75 -0
- package/lib/cli/commands/dumpModel.d.ts +2 -0
- package/lib/cli/commands/dumpModel.js +107 -0
- package/lib/cli/commands/escape.d.ts +2 -0
- package/lib/cli/commands/escape.js +69 -0
- package/lib/cli/commands/spinner.d.ts +2 -0
- package/lib/cli/commands/spinner.js +37 -0
- package/lib/cli/commands/upgrade.d.ts +2 -0
- package/lib/cli/commands/upgrade.js +66 -0
- package/lib/cli/helpers/ddl.d.ts +17 -0
- package/lib/cli/helpers/ddl.js +55 -0
- package/lib/cli/helpers/package.d.ts +25 -0
- package/lib/cli/helpers/package.js +56 -0
- package/lib/cli/index.d.ts +2 -0
- package/lib/cli/index.js +19 -0
- package/lib/codepoints-map.json +762 -0
- package/lib/db-driver/Typo.d.ts +111 -0
- package/lib/db-driver/Typo.js +2 -0
- package/lib/db-driver/built-ins/base.class.d.ts +91 -0
- package/lib/db-driver/built-ins/base.class.js +187 -0
- package/lib/db-driver/built-ins/driver-dm.d.ts +17 -0
- package/lib/db-driver/built-ins/driver-dm.js +54 -0
- package/lib/db-driver/built-ins/driver-mongodb.d.ts +23 -0
- package/lib/db-driver/built-ins/driver-mongodb.js +47 -0
- package/lib/db-driver/built-ins/driver-mysql.d.ts +17 -0
- package/lib/db-driver/built-ins/driver-mysql.js +38 -0
- package/lib/db-driver/built-ins/driver-postgresql.d.ts +21 -0
- package/lib/db-driver/built-ins/driver-postgresql.js +77 -0
- package/lib/db-driver/built-ins/driver-redis.d.ts +11 -0
- package/lib/db-driver/built-ins/driver-redis.js +36 -0
- package/lib/db-driver/built-ins/driver-sqlite.d.ts +16 -0
- package/lib/db-driver/built-ins/driver-sqlite.js +35 -0
- package/lib/db-driver/index.d.ts +3 -0
- package/lib/db-driver/index.js +5 -0
- package/lib/db-driver/utils.d.ts +14 -0
- package/lib/db-driver/utils.js +187 -0
- package/lib/knex/deps/tarn/dist/PendingOperation.d.ts +12 -0
- package/lib/knex/deps/tarn/dist/PendingOperation.js +48 -0
- package/lib/knex/deps/tarn/dist/Pool.d.ts +97 -0
- package/lib/knex/deps/tarn/dist/Pool.js +516 -0
- package/lib/knex/deps/tarn/dist/PromiseInspection.d.ts +16 -0
- package/lib/knex/deps/tarn/dist/PromiseInspection.js +21 -0
- package/lib/knex/deps/tarn/dist/Resource.d.ts +9 -0
- package/lib/knex/deps/tarn/dist/Resource.js +19 -0
- package/lib/knex/deps/tarn/dist/TimeoutError.d.ts +2 -0
- package/lib/knex/deps/tarn/dist/TimeoutError.js +5 -0
- package/lib/knex/deps/tarn/dist/tarn.d.ts +3 -0
- package/lib/knex/deps/tarn/dist/tarn.js +10 -0
- package/lib/knex/deps/tarn/dist/utils.d.ts +14 -0
- package/lib/knex/deps/tarn/dist/utils.js +60 -0
- package/lib/knex/knex.d.ts +2 -0
- package/lib/knex/knex.js +4 -0
- package/lib/knex/sandbox.d.ts +4 -0
- package/lib/knex/sandbox.js +23 -0
- package/lib/orm/Adapters.d.ts +3 -0
- package/lib/orm/Adapters.js +44 -0
- package/lib/orm/AggregateFunctions.d.ts +3 -0
- package/lib/orm/AggregateFunctions.js +177 -0
- package/lib/orm/Associations/Extend.d.ts +22 -0
- package/lib/orm/Associations/Extend.js +274 -0
- package/lib/orm/Associations/Many.d.ts +16 -0
- package/lib/orm/Associations/Many.js +569 -0
- package/lib/orm/Associations/One.d.ts +16 -0
- package/lib/orm/Associations/One.js +310 -0
- package/lib/orm/Associations/_utils.d.ts +14 -0
- package/lib/orm/Associations/_utils.js +53 -0
- package/lib/orm/ChainFind.d.ts +3 -0
- package/lib/orm/ChainFind.js +328 -0
- package/lib/orm/ChainInstance.d.ts +6 -0
- package/lib/orm/ChainInstance.js +74 -0
- package/lib/orm/Debug.d.ts +2 -0
- package/lib/orm/Debug.js +17 -0
- package/lib/orm/Drivers/DB/base.d.ts +2 -0
- package/lib/orm/Drivers/DB/base.js +4 -0
- package/lib/orm/Drivers/DB/dm.d.ts +9 -0
- package/lib/orm/Drivers/DB/dm.js +24 -0
- package/lib/orm/Drivers/DB/mysql.d.ts +9 -0
- package/lib/orm/Drivers/DB/mysql.js +24 -0
- package/lib/orm/Drivers/DB/postgres.d.ts +9 -0
- package/lib/orm/Drivers/DB/postgres.js +23 -0
- package/lib/orm/Drivers/DB/sqlite3.d.ts +14 -0
- package/lib/orm/Drivers/DB/sqlite3.js +40 -0
- package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
- package/lib/orm/Drivers/DML/_ddl-sql.js +52 -0
- package/lib/orm/Drivers/DML/_dml-helpers.d.ts +19 -0
- package/lib/orm/Drivers/DML/_dml-helpers.js +51 -0
- package/lib/orm/Drivers/DML/_shared.d.ts +9 -0
- package/lib/orm/Drivers/DML/_shared.js +39 -0
- package/lib/orm/Drivers/DML/_utils.d.ts +8 -0
- package/lib/orm/Drivers/DML/_utils.js +67 -0
- package/lib/orm/Drivers/DML/constants.d.ts +3 -0
- package/lib/orm/Drivers/DML/constants.js +3 -0
- package/lib/orm/Drivers/DML/dm.d.ts +2 -0
- package/lib/orm/Drivers/DML/dm.js +294 -0
- package/lib/orm/Drivers/DML/index.d.ts +4 -0
- package/lib/orm/Drivers/DML/index.js +7 -0
- package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
- package/lib/orm/Drivers/DML/mysql.js +252 -0
- package/lib/orm/Drivers/DML/postgres.d.ts +2 -0
- package/lib/orm/Drivers/DML/postgres.js +277 -0
- package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
- package/lib/orm/Drivers/DML/sqlite.js +267 -0
- package/lib/orm/Error.d.ts +11 -0
- package/lib/orm/Error.js +41 -0
- package/lib/orm/Helpers.d.ts +35 -0
- package/lib/orm/Helpers.js +234 -0
- package/lib/orm/Hook.d.ts +13 -0
- package/lib/orm/Hook.js +100 -0
- package/lib/orm/Instance.d.ts +2 -0
- package/lib/orm/Instance.js +698 -0
- package/lib/orm/LazyLoad.d.ts +4 -0
- package/lib/orm/LazyLoad.js +76 -0
- package/lib/orm/Model.d.ts +6 -0
- package/lib/orm/Model.js +974 -0
- package/lib/orm/ORM.d.ts +37 -0
- package/lib/orm/ORM.js +242 -0
- package/lib/orm/Patch/fib-cache.d.ts +39 -0
- package/lib/orm/Patch/fib-cache.js +2 -0
- package/lib/orm/Patch/plugin.d.ts +4 -0
- package/lib/orm/Patch/plugin.js +14 -0
- package/lib/orm/Property.d.ts +9 -0
- package/lib/orm/Property.js +71 -0
- package/lib/orm/Settings.d.ts +42 -0
- package/lib/orm/Settings.js +120 -0
- package/lib/orm/Singleton.d.ts +5 -0
- package/lib/orm/Singleton.js +73 -0
- package/lib/orm/Typo/Adapter.d.ts +5 -0
- package/lib/orm/Typo/Adapter.js +2 -0
- package/lib/orm/Typo/DMLDriver.d.ts +209 -0
- package/lib/orm/Typo/DMLDriver.js +2 -0
- package/lib/orm/Typo/Db.d.ts +35 -0
- package/lib/orm/Typo/Db.js +2 -0
- package/lib/orm/Typo/Error.d.ts +29 -0
- package/lib/orm/Typo/Error.js +2 -0
- package/lib/orm/Typo/ORM.d.ts +231 -0
- package/lib/orm/Typo/ORM.js +4 -0
- package/lib/orm/Typo/Validators.d.ts +31 -0
- package/lib/orm/Typo/Validators.js +3 -0
- package/lib/orm/Typo/_common.d.ts +37 -0
- package/lib/orm/Typo/_common.js +2 -0
- package/lib/orm/Typo/assoc.d.ts +221 -0
- package/lib/orm/Typo/assoc.js +2 -0
- package/lib/orm/Typo/hook.d.ts +42 -0
- package/lib/orm/Typo/hook.js +2 -0
- package/lib/orm/Typo/instance.d.ts +156 -0
- package/lib/orm/Typo/instance.js +2 -0
- package/lib/orm/Typo/model.d.ts +311 -0
- package/lib/orm/Typo/model.js +7 -0
- package/lib/orm/Typo/patch.d.ts +31 -0
- package/lib/orm/Typo/patch.js +2 -0
- package/lib/orm/Typo/property.d.ts +57 -0
- package/lib/orm/Typo/property.js +2 -0
- package/lib/orm/Typo/query.d.ts +216 -0
- package/lib/orm/Typo/query.js +2 -0
- package/lib/orm/Typo/settings.d.ts +35 -0
- package/lib/orm/Typo/settings.js +6 -0
- package/lib/orm/Typo/synchronous.d.ts +47 -0
- package/lib/orm/Typo/synchronous.js +2 -0
- package/lib/orm/Utilities.d.ts +178 -0
- package/lib/orm/Utilities.js +975 -0
- package/lib/orm/Validators.d.ts +33 -0
- package/lib/orm/Validators.js +101 -0
- package/lib/orm/Where/common.d.ts +1 -0
- package/lib/orm/Where/common.js +7 -0
- package/lib/orm/Where/filterDate.d.ts +11 -0
- package/lib/orm/Where/filterDate.js +58 -0
- package/lib/orm/index.d.ts +67 -0
- package/lib/orm/index.js +158 -0
- package/lib/orm-core/callback.d.ts +12 -0
- package/lib/orm-core/callback.js +2 -0
- package/lib/orm-core/dialect.d.ts +80 -0
- package/lib/orm-core/dialect.js +2 -0
- package/lib/orm-core/error.d.ts +16 -0
- package/lib/orm-core/error.js +29 -0
- package/lib/orm-core/index.d.ts +6 -0
- package/lib/orm-core/index.js +7 -0
- package/lib/orm-core/sync.d.ts +12 -0
- package/lib/orm-core/sync.js +2 -0
- package/lib/orm-core/utils.d.ts +1 -0
- package/lib/orm-core/utils.js +6 -0
- package/lib/orm-property/Property.d.ts +78 -0
- package/lib/orm-property/Property.js +2 -0
- package/lib/orm-property/Utils.d.ts +9 -0
- package/lib/orm-property/Utils.js +13 -0
- package/lib/orm-property/customTypes.d.ts +2 -0
- package/lib/orm-property/customTypes.js +13 -0
- package/lib/orm-property/index.d.ts +10 -0
- package/lib/orm-property/index.js +23 -0
- package/lib/orm-property/transformer.d.ts +5 -0
- package/lib/orm-property/transformer.js +10 -0
- package/lib/orm-property/transformers/dm.d.ts +29 -0
- package/lib/orm-property/transformers/dm.js +238 -0
- package/lib/orm-property/transformers/index.d.ts +4 -0
- package/lib/orm-property/transformers/index.js +6 -0
- package/lib/orm-property/transformers/mysql.d.ts +101 -0
- package/lib/orm-property/transformers/mysql.js +262 -0
- package/lib/orm-property/transformers/postgresql.d.ts +54 -0
- package/lib/orm-property/transformers/postgresql.js +190 -0
- package/lib/orm-property/transformers/sqlite.d.ts +19 -0
- package/lib/orm-property/transformers/sqlite.js +172 -0
- package/lib/sql-ddl-sync/Dialects/dm.d.ts +39 -0
- package/lib/sql-ddl-sync/Dialects/dm.js +297 -0
- package/lib/sql-ddl-sync/Dialects/index.d.ts +6 -0
- package/lib/sql-ddl-sync/Dialects/index.js +8 -0
- package/lib/sql-ddl-sync/Dialects/mysql.d.ts +38 -0
- package/lib/sql-ddl-sync/Dialects/mysql.js +241 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +39 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.js +287 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +43 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.js +257 -0
- package/lib/sql-ddl-sync/SQL.d.ts +12 -0
- package/lib/sql-ddl-sync/SQL.js +108 -0
- package/lib/sql-ddl-sync/Typo/Collection.d.ts +13 -0
- package/lib/sql-ddl-sync/Typo/Collection.js +2 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.js +2 -0
- package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
- package/lib/sql-ddl-sync/Typo/Dialect.js +2 -0
- package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
- package/lib/sql-ddl-sync/Typo/Driver.js +2 -0
- package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
- package/lib/sql-ddl-sync/Typo/Query.js +2 -0
- package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
- package/lib/sql-ddl-sync/Typo/SQL.js +2 -0
- package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
- package/lib/sql-ddl-sync/Typo/_common.js +2 -0
- package/lib/sql-ddl-sync/Utils.d.ts +39 -0
- package/lib/sql-ddl-sync/Utils.js +217 -0
- package/lib/sql-ddl-sync/index.d.ts +87 -0
- package/lib/sql-ddl-sync/index.js +372 -0
- package/lib/sql-query/Comparators.d.ts +3 -0
- package/lib/sql-query/Comparators.js +95 -0
- package/lib/sql-query/Create.d.ts +35 -0
- package/lib/sql-query/Create.js +78 -0
- package/lib/sql-query/Dialects/dm.d.ts +3 -0
- package/lib/sql-query/Dialects/dm.js +75 -0
- package/lib/sql-query/Dialects/index.d.ts +10 -0
- package/lib/sql-query/Dialects/index.js +13 -0
- package/lib/sql-query/Dialects/mssql.d.ts +3 -0
- package/lib/sql-query/Dialects/mssql.js +74 -0
- package/lib/sql-query/Dialects/mysql.d.ts +3 -0
- package/lib/sql-query/Dialects/mysql.js +101 -0
- package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
- package/lib/sql-query/Dialects/postgresql.js +77 -0
- package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
- package/lib/sql-query/Dialects/sqlite.js +62 -0
- package/lib/sql-query/Helpers.d.ts +37 -0
- package/lib/sql-query/Helpers.js +260 -0
- package/lib/sql-query/Insert.d.ts +12 -0
- package/lib/sql-query/Insert.js +34 -0
- package/lib/sql-query/Query.d.ts +28 -0
- package/lib/sql-query/Query.js +120 -0
- package/lib/sql-query/Remove.d.ts +16 -0
- package/lib/sql-query/Remove.js +94 -0
- package/lib/sql-query/Select.d.ts +62 -0
- package/lib/sql-query/Select.js +534 -0
- package/lib/sql-query/Set.d.ts +4 -0
- package/lib/sql-query/Set.js +16 -0
- package/lib/sql-query/Typo/Aggregation.d.ts +32 -0
- package/lib/sql-query/Typo/Aggregation.js +2 -0
- package/lib/sql-query/Typo/Comparators.d.ts +140 -0
- package/lib/sql-query/Typo/Comparators.js +2 -0
- package/lib/sql-query/Typo/Dialect.d.ts +39 -0
- package/lib/sql-query/Typo/Dialect.js +2 -0
- package/lib/sql-query/Typo/Field.d.ts +8 -0
- package/lib/sql-query/Typo/Field.js +2 -0
- package/lib/sql-query/Typo/Helper.d.ts +4 -0
- package/lib/sql-query/Typo/Helper.js +2 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.js +2 -0
- package/lib/sql-query/Typo/Query.d.ts +34 -0
- package/lib/sql-query/Typo/Query.js +17 -0
- package/lib/sql-query/Typo/Sql.d.ts +143 -0
- package/lib/sql-query/Typo/Sql.js +2 -0
- package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
- package/lib/sql-query/Typo/SubQuery.js +2 -0
- package/lib/sql-query/Typo/index.d.ts +9 -0
- package/lib/sql-query/Typo/index.js +2 -0
- package/lib/sql-query/Update.d.ts +15 -0
- package/lib/sql-query/Update.js +39 -0
- package/lib/sql-query/Where.d.ts +4 -0
- package/lib/sql-query/Where.js +280 -0
- package/package.json +71 -36
- package/.travis.yml +0 -14
- package/@types/index.d.ts +0 -254
- package/@types/orm_mirror/orm.d.ts +0 -275
- package/@types/orm_mirror/sql-query.d.ts +0 -79
- package/Readme.md +0 -843
- package/lib/index.js +0 -13
- package/lib/modules/mysql.js +0 -35
- package/lib/modules/sqlite3.js +0 -27
- package/lib/patch.js +0 -271
package/lib/orm/ORM.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import events = require("events");
|
|
2
|
+
import type { FxOrmNS } from "./Typo/ORM";
|
|
3
|
+
import type { FxOrmCommon } from "./Typo/_common";
|
|
4
|
+
import type { FxOrmDMLDriver } from "./Typo/DMLDriver";
|
|
5
|
+
import type { FxOrmCoreCallbackNS } from "../orm-core/index";
|
|
6
|
+
import type { FxOrmSettings } from "./Typo/settings";
|
|
7
|
+
export declare class ORM extends events.EventEmitter implements FxOrmNS.ORM {
|
|
8
|
+
validators: FxOrmNS.ORM['validators'];
|
|
9
|
+
enforce: FxOrmNS.ORM['enforce'];
|
|
10
|
+
settings: FxOrmNS.ORM['settings'];
|
|
11
|
+
driver_name: FxOrmNS.ORM['driver_name'];
|
|
12
|
+
driver: FxOrmNS.ORM['driver'];
|
|
13
|
+
comparators: FxOrmNS.ORM['comparators'];
|
|
14
|
+
models: FxOrmNS.ORM['models'];
|
|
15
|
+
plugins: FxOrmNS.ORM['plugins'];
|
|
16
|
+
customTypes: FxOrmNS.ORM['customTypes'];
|
|
17
|
+
constructor(driver_name: string, driver: FxOrmDMLDriver.DMLDriver, settings: FxOrmSettings.SettingInstance);
|
|
18
|
+
use(...[plugin_const, opts]: Parameters<FxOrmNS.ORM['use']>): this;
|
|
19
|
+
define(...[name, properties, opts]: Parameters<FxOrmNS.ORM['define']>): any;
|
|
20
|
+
defineType(...[name, opts]: Parameters<FxOrmNS.ORM['defineType']>): this;
|
|
21
|
+
pingSync(): void;
|
|
22
|
+
ping(...[cb]: Parameters<FxOrmNS.ORM['ping']>): this;
|
|
23
|
+
closeSync(): void;
|
|
24
|
+
close(...[cb]: Parameters<FxOrmNS.ORM['close']>): this;
|
|
25
|
+
load(): any;
|
|
26
|
+
syncSync(): void;
|
|
27
|
+
sync(...[cb]: Parameters<FxOrmNS.ORM['sync']>): this;
|
|
28
|
+
dropSync(): void;
|
|
29
|
+
drop(...[cb]: Parameters<FxOrmNS.ORM['drop']>): this;
|
|
30
|
+
queryParamCastserial(...chains: any[]): {
|
|
31
|
+
get: (cb: FxOrmCommon.GenericCallback<any[]>) => any;
|
|
32
|
+
};
|
|
33
|
+
begin(): void;
|
|
34
|
+
commit(): void;
|
|
35
|
+
rollback(): void;
|
|
36
|
+
trans<T>(func: FxOrmCoreCallbackNS.ExecutionCallback<T>): boolean;
|
|
37
|
+
}
|
package/lib/orm/ORM.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ORM = void 0;
|
|
4
|
+
const util = require("util");
|
|
5
|
+
const events = require("events");
|
|
6
|
+
const uuid = require("uuid");
|
|
7
|
+
const SqlQuery = require("../sql-query/Query");
|
|
8
|
+
const plugin_1 = require("./Patch/plugin");
|
|
9
|
+
const Model_1 = require("./Model");
|
|
10
|
+
const Error_1 = require("./Error");
|
|
11
|
+
const Utilities = require("./Utilities");
|
|
12
|
+
const Enforces = require("@fibjs/enforce");
|
|
13
|
+
const validators = require("./Validators");
|
|
14
|
+
const Settings = require("./Settings");
|
|
15
|
+
class ORM extends events.EventEmitter {
|
|
16
|
+
constructor(driver_name, driver, settings) {
|
|
17
|
+
super();
|
|
18
|
+
this.validators = validators;
|
|
19
|
+
this.enforce = Enforces;
|
|
20
|
+
this.settings = settings;
|
|
21
|
+
this.driver_name = driver_name;
|
|
22
|
+
this.driver = driver;
|
|
23
|
+
this.driver.uid = uuid.node().hex();
|
|
24
|
+
this.comparators = Object.assign({}, SqlQuery.comparators);
|
|
25
|
+
this.models = {};
|
|
26
|
+
this.plugins = [];
|
|
27
|
+
this.customTypes = {};
|
|
28
|
+
events.EventEmitter.call(this);
|
|
29
|
+
var onError = (err) => {
|
|
30
|
+
if (this.settings.get("connection.reconnect")) {
|
|
31
|
+
if (typeof this.driver.reconnect === "undefined") {
|
|
32
|
+
return this.emit("error", new Error_1.default("Connection lost - driver does not support reconnection", 'CONNECTION_LOST'));
|
|
33
|
+
}
|
|
34
|
+
this.driver.reconnect(() => {
|
|
35
|
+
this.driver.on("error", onError);
|
|
36
|
+
});
|
|
37
|
+
if (this.listeners("error").length === 0) {
|
|
38
|
+
// since user want auto reconnect,
|
|
39
|
+
// don't emit without listeners or it will throw
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
this.emit("error", err);
|
|
44
|
+
};
|
|
45
|
+
driver.on("error", onError);
|
|
46
|
+
this.use(plugin_1.default);
|
|
47
|
+
}
|
|
48
|
+
use(...[plugin_const, opts]) {
|
|
49
|
+
if (typeof plugin_const === "string") {
|
|
50
|
+
try {
|
|
51
|
+
plugin_const = require(Utilities.getRealPath(plugin_const));
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
throw e;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
var plugin = plugin_const(this, opts || {});
|
|
58
|
+
if (typeof plugin.define === "function") {
|
|
59
|
+
for (let k in this.models) {
|
|
60
|
+
plugin.define(this.models[k], this);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
this.plugins.push(plugin);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
;
|
|
67
|
+
define(...[name, properties, opts]) {
|
|
68
|
+
properties = properties || {};
|
|
69
|
+
opts = opts || {};
|
|
70
|
+
for (let i = 0; i < this.plugins.length; i++) {
|
|
71
|
+
// TODO: only pass normalized properties to beforeDefine
|
|
72
|
+
if (typeof this.plugins[i].beforeDefine === "function") {
|
|
73
|
+
this.plugins[i].beforeDefine(name, properties, opts);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const m_settings = opts.useSelfSettings ? Settings.Container(this.settings.get('*')) : this.settings;
|
|
77
|
+
const virtualView = Utilities.normalizeVirtualViewOption(opts.virtualView, this.driver.knex);
|
|
78
|
+
const modelTable = opts.table || opts.collection || ((m_settings.get("model.namePrefix") || "") + name);
|
|
79
|
+
const generateSqlSelect = Utilities.__wrapTableSourceAsGneratingSqlSelect({
|
|
80
|
+
virtualView,
|
|
81
|
+
customSelect: opts.customSelect,
|
|
82
|
+
generateSqlSelect: opts.generateSqlSelect,
|
|
83
|
+
}, {
|
|
84
|
+
dialect: this.driver.query.Dialect,
|
|
85
|
+
modelTable,
|
|
86
|
+
});
|
|
87
|
+
this.models[name] = new Model_1.Model({
|
|
88
|
+
name: name,
|
|
89
|
+
db: this,
|
|
90
|
+
settings: m_settings,
|
|
91
|
+
driver_name: this.driver_name,
|
|
92
|
+
driver: this.driver,
|
|
93
|
+
table: opts.table || opts.collection || ((m_settings.get("model.namePrefix") || "") + name),
|
|
94
|
+
tableComment: opts.tableComment || '',
|
|
95
|
+
virtualView: virtualView,
|
|
96
|
+
generateSqlSelect,
|
|
97
|
+
// not standard Record<string, FxOrmProperty.NormalizedProperty> here, but we should pass it firstly
|
|
98
|
+
properties: properties,
|
|
99
|
+
__for_extension: opts.__for_extension || false,
|
|
100
|
+
indexes: opts.indexes || [],
|
|
101
|
+
identityCache: opts.hasOwnProperty("identityCache") ? opts.identityCache : m_settings.get("instance.identityCache"),
|
|
102
|
+
keys: opts.id,
|
|
103
|
+
autoSave: opts.hasOwnProperty("autoSave") ? opts.autoSave : m_settings.get("instance.autoSave"),
|
|
104
|
+
autoFetch: opts.hasOwnProperty("autoFetch") ? opts.autoFetch : m_settings.get("instance.autoFetch"),
|
|
105
|
+
autoFetchLimit: opts.autoFetchLimit || m_settings.get("instance.autoFetchLimit"),
|
|
106
|
+
cascadeRemove: opts.hasOwnProperty("cascadeRemove") ? opts.cascadeRemove : m_settings.get("instance.cascadeRemove"),
|
|
107
|
+
hooks: opts.hooks || {},
|
|
108
|
+
methods: opts.methods || {},
|
|
109
|
+
validations: opts.validations || {},
|
|
110
|
+
ievents: opts.ievents || {},
|
|
111
|
+
instanceCacheSize: opts.hasOwnProperty("instanceCacheSize") ? opts.instanceCacheSize : m_settings.get("instance.cacheSize"),
|
|
112
|
+
});
|
|
113
|
+
for (let i = 0; i < this.plugins.length; i++) {
|
|
114
|
+
if (typeof this.plugins[i].define === "function") {
|
|
115
|
+
this.plugins[i].define(this.models[name], this);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return this.models[name];
|
|
119
|
+
}
|
|
120
|
+
;
|
|
121
|
+
defineType(...[name, opts]) {
|
|
122
|
+
this.customTypes[name] = opts;
|
|
123
|
+
this.driver.customTypes[name] = opts;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
;
|
|
127
|
+
pingSync() {
|
|
128
|
+
this.driver.ping();
|
|
129
|
+
}
|
|
130
|
+
;
|
|
131
|
+
ping(...[cb]) {
|
|
132
|
+
this.driver.ping(cb);
|
|
133
|
+
return this;
|
|
134
|
+
}
|
|
135
|
+
;
|
|
136
|
+
closeSync() {
|
|
137
|
+
this.driver.close();
|
|
138
|
+
}
|
|
139
|
+
close(...[cb]) {
|
|
140
|
+
const syncResponse = Utilities.catchBlocking(this.closeSync, [], { thisArg: this });
|
|
141
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
;
|
|
145
|
+
load() {
|
|
146
|
+
var files = util.flatten(Array.prototype.slice.apply(arguments));
|
|
147
|
+
var cb = function (err) { };
|
|
148
|
+
if (typeof files[files.length - 1] == "function") {
|
|
149
|
+
cb = files.pop();
|
|
150
|
+
}
|
|
151
|
+
var loadNext = function () {
|
|
152
|
+
if (files.length === 0) {
|
|
153
|
+
return cb(null);
|
|
154
|
+
}
|
|
155
|
+
var file = files.shift();
|
|
156
|
+
try {
|
|
157
|
+
return require(Utilities.getRealPath(file, 4))(this, function (err) {
|
|
158
|
+
if (err)
|
|
159
|
+
return cb(err);
|
|
160
|
+
return loadNext();
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
catch (ex) {
|
|
164
|
+
return cb(ex);
|
|
165
|
+
}
|
|
166
|
+
}.bind(this);
|
|
167
|
+
return loadNext();
|
|
168
|
+
}
|
|
169
|
+
;
|
|
170
|
+
syncSync() {
|
|
171
|
+
var modelIds = Object.keys(this.models);
|
|
172
|
+
if (modelIds.length === 0)
|
|
173
|
+
return;
|
|
174
|
+
modelIds.forEach(modelId => {
|
|
175
|
+
this.models[modelId].syncSync();
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
;
|
|
179
|
+
sync(...[cb]) {
|
|
180
|
+
const syncResponse = Utilities.catchBlocking(this.syncSync, [], { thisArg: this });
|
|
181
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: !!cb, callback: cb });
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
;
|
|
185
|
+
dropSync() {
|
|
186
|
+
var modelIds = Object.keys(this.models);
|
|
187
|
+
if (modelIds.length === 0)
|
|
188
|
+
return;
|
|
189
|
+
modelIds.forEach(modelId => {
|
|
190
|
+
this.models[modelId].dropSync();
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
;
|
|
194
|
+
drop(...[cb]) {
|
|
195
|
+
const syncResponse = Utilities.catchBlocking(this.dropSync, [], { thisArg: this });
|
|
196
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: !!cb, callback: cb });
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
;
|
|
200
|
+
queryParamCastserial(...chains) {
|
|
201
|
+
return {
|
|
202
|
+
get: function (cb) {
|
|
203
|
+
var params = [];
|
|
204
|
+
var getNext = function () {
|
|
205
|
+
if (params.length === chains.length) {
|
|
206
|
+
params.unshift(null);
|
|
207
|
+
return cb.apply(null, params);
|
|
208
|
+
}
|
|
209
|
+
chains[params.length].run(function (err, instances) {
|
|
210
|
+
if (err) {
|
|
211
|
+
params.unshift(err);
|
|
212
|
+
return cb.apply(null, params);
|
|
213
|
+
}
|
|
214
|
+
params.push(instances);
|
|
215
|
+
return getNext();
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
getNext();
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
;
|
|
224
|
+
begin() {
|
|
225
|
+
return this.driver.db.connection.begin();
|
|
226
|
+
}
|
|
227
|
+
;
|
|
228
|
+
commit() {
|
|
229
|
+
return this.driver.db.connection.commit();
|
|
230
|
+
}
|
|
231
|
+
;
|
|
232
|
+
rollback() {
|
|
233
|
+
return this.driver.db.connection.rollback();
|
|
234
|
+
}
|
|
235
|
+
;
|
|
236
|
+
trans(func) {
|
|
237
|
+
const connection = this.driver.db.connection;
|
|
238
|
+
return connection.trans(func.bind(connection));
|
|
239
|
+
}
|
|
240
|
+
;
|
|
241
|
+
}
|
|
242
|
+
exports.ORM = ORM;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
declare const fibCache: any;
|
|
2
|
+
declare module "fib-cache" {
|
|
3
|
+
interface LRUOptions {
|
|
4
|
+
max?: number;
|
|
5
|
+
ttl?: number;
|
|
6
|
+
resolver?: (key: string) => any;
|
|
7
|
+
}
|
|
8
|
+
interface LRUItem {
|
|
9
|
+
expiry: number;
|
|
10
|
+
key: string;
|
|
11
|
+
prev: LRUItem;
|
|
12
|
+
next: LRUItem;
|
|
13
|
+
owner: LRU;
|
|
14
|
+
value: any;
|
|
15
|
+
ready: Class_Event;
|
|
16
|
+
}
|
|
17
|
+
class LRU {
|
|
18
|
+
constructor(options?: LRUOptions);
|
|
19
|
+
items: Record<string, LRUItem>;
|
|
20
|
+
size: number;
|
|
21
|
+
first: LRUItem;
|
|
22
|
+
last: LRUItem;
|
|
23
|
+
max: number;
|
|
24
|
+
ttl: number;
|
|
25
|
+
resolver: (key: string) => any;
|
|
26
|
+
clear(): LRU;
|
|
27
|
+
delete_item(item: LRUItem): LRU;
|
|
28
|
+
delete(key: string): LRU;
|
|
29
|
+
entries(keys?: string[]): [string, any][];
|
|
30
|
+
evict(): LRU;
|
|
31
|
+
get(key: string): any;
|
|
32
|
+
has(key: string): boolean;
|
|
33
|
+
keys(): string[];
|
|
34
|
+
new_item(key: string, value?: any): LRUItem;
|
|
35
|
+
order_item(item: LRUItem): void;
|
|
36
|
+
set(key: string, value: any): LRU;
|
|
37
|
+
values(keys?: string[]): any[];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = default_1;
|
|
4
|
+
function default_1(orm, plugin_opts) {
|
|
5
|
+
function beforeDefine(name, properties, opts) {
|
|
6
|
+
opts.hooks = opts.hooks || {};
|
|
7
|
+
}
|
|
8
|
+
function define(m) {
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
beforeDefine,
|
|
12
|
+
define
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FxOrmModel } from './Typo/model';
|
|
2
|
+
import { FxOrmNS } from './Typo/ORM';
|
|
3
|
+
import { FxOrmProperty } from './Typo/property';
|
|
4
|
+
export declare function normalize(opts: {
|
|
5
|
+
prop: FxOrmModel.ComplexModelPropertyDefinition;
|
|
6
|
+
name: string;
|
|
7
|
+
customTypes: FxOrmNS.ORM['customTypes'];
|
|
8
|
+
settings: FxOrmNS.ORM['settings'];
|
|
9
|
+
}): FxOrmProperty.NormalizedProperty;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalize = normalize;
|
|
4
|
+
const _cloneDeep = require("lodash.clonedeep");
|
|
5
|
+
const Error_1 = require("./Error");
|
|
6
|
+
const KNOWN_TYPES = [
|
|
7
|
+
"text", "number", "integer", "boolean", "date", "enum", "object",
|
|
8
|
+
"binary", "point", "serial"
|
|
9
|
+
];
|
|
10
|
+
function normalize(opts) {
|
|
11
|
+
const orig_prop = opts.prop;
|
|
12
|
+
let result_prop = orig_prop;
|
|
13
|
+
if (typeof opts.prop === "function") {
|
|
14
|
+
const primitiveProp = opts.prop;
|
|
15
|
+
switch (primitiveProp.name) {
|
|
16
|
+
case "String":
|
|
17
|
+
result_prop = { type: "text" };
|
|
18
|
+
break;
|
|
19
|
+
case "Number":
|
|
20
|
+
result_prop = { type: "number" };
|
|
21
|
+
break;
|
|
22
|
+
case "Boolean":
|
|
23
|
+
result_prop = { type: "boolean" };
|
|
24
|
+
break;
|
|
25
|
+
case "Date":
|
|
26
|
+
result_prop = { type: "date" };
|
|
27
|
+
break;
|
|
28
|
+
case "Object":
|
|
29
|
+
result_prop = { type: "object" };
|
|
30
|
+
break;
|
|
31
|
+
case "Buffer":
|
|
32
|
+
result_prop = { type: "binary" };
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (typeof opts.prop === "string") {
|
|
37
|
+
result_prop = {
|
|
38
|
+
type: opts.prop
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
else if (Array.isArray(opts.prop)) {
|
|
42
|
+
result_prop = { type: "enum", values: opts.prop };
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
result_prop = _cloneDeep(orig_prop);
|
|
46
|
+
}
|
|
47
|
+
if (!KNOWN_TYPES.includes(result_prop.type) && !(result_prop.type in opts.customTypes)) {
|
|
48
|
+
throw new Error_1.default("Unknown property type: " + result_prop.type, 'NO_SUPPORT');
|
|
49
|
+
}
|
|
50
|
+
if (!result_prop.hasOwnProperty("required") && opts.settings.get("properties.required")) {
|
|
51
|
+
result_prop.required = true;
|
|
52
|
+
}
|
|
53
|
+
// Defaults to true. Setting to false hides properties from JSON.stringify(modelInstance).
|
|
54
|
+
if (!result_prop.hasOwnProperty("enumerable") || result_prop.enumerable === true) {
|
|
55
|
+
result_prop.enumerable = true;
|
|
56
|
+
}
|
|
57
|
+
// Defaults to true. Rational means floating point here.
|
|
58
|
+
if (result_prop.type == "number" && result_prop.rational === undefined) {
|
|
59
|
+
result_prop.rational = true;
|
|
60
|
+
}
|
|
61
|
+
if (!('mapsTo' in result_prop)) {
|
|
62
|
+
result_prop.mapsTo = opts.name;
|
|
63
|
+
}
|
|
64
|
+
if (result_prop.type == "number" && result_prop.rational === false) {
|
|
65
|
+
result_prop.type = "integer";
|
|
66
|
+
delete result_prop.rational;
|
|
67
|
+
}
|
|
68
|
+
result_prop.name = opts.name;
|
|
69
|
+
return result_prop;
|
|
70
|
+
}
|
|
71
|
+
;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { FxOrmSettings } from './Typo/settings';
|
|
2
|
+
export declare const defaults: () => {
|
|
3
|
+
model: {
|
|
4
|
+
namePrefix: string;
|
|
5
|
+
repair_column: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* @dangerous
|
|
8
|
+
*/
|
|
9
|
+
allow_drop_column: boolean;
|
|
10
|
+
};
|
|
11
|
+
properties: {
|
|
12
|
+
primary_key: string;
|
|
13
|
+
association_key: string;
|
|
14
|
+
required: boolean;
|
|
15
|
+
};
|
|
16
|
+
instance: {
|
|
17
|
+
defaultFindLimit: number;
|
|
18
|
+
defaultCacheSize: number;
|
|
19
|
+
identityCache: boolean;
|
|
20
|
+
identityCacheSaveCheck: boolean;
|
|
21
|
+
autoSave: boolean;
|
|
22
|
+
autoFetch: boolean;
|
|
23
|
+
autoFetchLimit: number;
|
|
24
|
+
cascadeRemove: boolean;
|
|
25
|
+
returnAllErrors: boolean;
|
|
26
|
+
saveAssociationsByDefault: boolean;
|
|
27
|
+
};
|
|
28
|
+
hasOne: {};
|
|
29
|
+
hasMany: {
|
|
30
|
+
key: boolean;
|
|
31
|
+
};
|
|
32
|
+
extendsTo: {
|
|
33
|
+
throwWhenNotFound: boolean;
|
|
34
|
+
};
|
|
35
|
+
connection: {
|
|
36
|
+
reconnect: boolean;
|
|
37
|
+
pool: boolean;
|
|
38
|
+
debug: boolean;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export declare function Container(settings: object): FxOrmSettings.SettingInstance;
|
|
42
|
+
export declare const defaultSettingsInstance: FxOrmSettings.SettingInstance;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultSettingsInstance = exports.defaults = void 0;
|
|
4
|
+
exports.Container = Container;
|
|
5
|
+
const _cloneDeep = require("lodash.clonedeep");
|
|
6
|
+
const default_settings = {
|
|
7
|
+
model: {
|
|
8
|
+
namePrefix: '',
|
|
9
|
+
// TODO: add test about it
|
|
10
|
+
repair_column: false,
|
|
11
|
+
/**
|
|
12
|
+
* @dangerous
|
|
13
|
+
*/
|
|
14
|
+
allow_drop_column: false,
|
|
15
|
+
},
|
|
16
|
+
properties: {
|
|
17
|
+
primary_key: "id",
|
|
18
|
+
association_key: "{name}_{field}",
|
|
19
|
+
required: false
|
|
20
|
+
},
|
|
21
|
+
instance: {
|
|
22
|
+
defaultFindLimit: 1000,
|
|
23
|
+
defaultCacheSize: 1000,
|
|
24
|
+
identityCache: false,
|
|
25
|
+
identityCacheSaveCheck: true,
|
|
26
|
+
autoSave: false,
|
|
27
|
+
autoFetch: false,
|
|
28
|
+
autoFetchLimit: 1,
|
|
29
|
+
cascadeRemove: true,
|
|
30
|
+
returnAllErrors: false,
|
|
31
|
+
saveAssociationsByDefault: true
|
|
32
|
+
},
|
|
33
|
+
hasOne: {
|
|
34
|
+
// allowRemoveReverse : false,
|
|
35
|
+
},
|
|
36
|
+
hasMany: {
|
|
37
|
+
// Makes the foreign key fields a composite key
|
|
38
|
+
key: false
|
|
39
|
+
},
|
|
40
|
+
extendsTo: {
|
|
41
|
+
// would default as false in the future
|
|
42
|
+
throwWhenNotFound: true,
|
|
43
|
+
},
|
|
44
|
+
connection: {
|
|
45
|
+
reconnect: true,
|
|
46
|
+
pool: false,
|
|
47
|
+
debug: false
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const defaults = function () {
|
|
51
|
+
return default_settings;
|
|
52
|
+
};
|
|
53
|
+
exports.defaults = defaults;
|
|
54
|
+
function Container(settings) {
|
|
55
|
+
return {
|
|
56
|
+
set: function (key, value) {
|
|
57
|
+
set(key, value, settings);
|
|
58
|
+
return this;
|
|
59
|
+
},
|
|
60
|
+
get: function (key, def) {
|
|
61
|
+
const v = get(key, def, settings);
|
|
62
|
+
if (v instanceof Function) {
|
|
63
|
+
return v;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return _cloneDeep(v);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
unset: function () {
|
|
70
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
71
|
+
if (typeof arguments[i] === "string") {
|
|
72
|
+
unset(arguments[i], settings);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function set(key, value, obj) {
|
|
80
|
+
const p = key.indexOf(".");
|
|
81
|
+
if (p === -1) {
|
|
82
|
+
return obj[key] = value;
|
|
83
|
+
}
|
|
84
|
+
if (!obj.hasOwnProperty(key.substr(0, p))) {
|
|
85
|
+
obj[key.substr(0, p)] = {};
|
|
86
|
+
}
|
|
87
|
+
return set(key.substr(p + 1), value, obj[key.substr(0, p)]);
|
|
88
|
+
}
|
|
89
|
+
function get(key, def, obj) {
|
|
90
|
+
const p = key.indexOf(".");
|
|
91
|
+
if (p === -1) {
|
|
92
|
+
if (key === '*') {
|
|
93
|
+
return obj;
|
|
94
|
+
}
|
|
95
|
+
return obj.hasOwnProperty(key) ? obj[key] : def;
|
|
96
|
+
}
|
|
97
|
+
if (!obj.hasOwnProperty(key.substr(0, p))) {
|
|
98
|
+
return def;
|
|
99
|
+
}
|
|
100
|
+
return get(key.substr(p + 1), def, obj[key.substr(0, p)]);
|
|
101
|
+
}
|
|
102
|
+
function unset(key, obj) {
|
|
103
|
+
const p = key.indexOf(".");
|
|
104
|
+
if (p === -1) {
|
|
105
|
+
if (key === '*') {
|
|
106
|
+
return 'reset';
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
delete obj[key];
|
|
110
|
+
}
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (!obj.hasOwnProperty(key.substr(0, p))) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (unset(key.substr(p + 1), obj[key.substr(0, p)]) === 'reset') {
|
|
117
|
+
obj[key.substr(0, p)] = {};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.defaultSettingsInstance = Container((0, exports.defaults)());
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { FxOrmNS } from "./Typo/ORM";
|
|
2
|
+
export declare const clear: FxOrmNS.SingletonModule['clear'];
|
|
3
|
+
export declare const modelClear: FxOrmNS.SingletonModule['modelClear'];
|
|
4
|
+
export declare const get: FxOrmNS.SingletonModule['get'];
|
|
5
|
+
export declare const modelGet: FxOrmNS.SingletonModule['modelGet'];
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelGet = exports.get = exports.modelClear = exports.clear = void 0;
|
|
4
|
+
const Helpers = require("./Helpers");
|
|
5
|
+
const coroutine = require("coroutine");
|
|
6
|
+
/**
|
|
7
|
+
* refactor this module with LRUCache
|
|
8
|
+
*/
|
|
9
|
+
const models = {};
|
|
10
|
+
const clear = function (key) {
|
|
11
|
+
let driver_table_uid2 = '';
|
|
12
|
+
if (key && typeof key === 'string')
|
|
13
|
+
driver_table_uid2 = Helpers.parseDriverUidAndTableNameFromUID(key).driver_table_uid;
|
|
14
|
+
Object.values(models).forEach(model => {
|
|
15
|
+
const { driver_table_uid: driver_table_uid1 } = Helpers.parseDriverUidAndTableNameFromUID(model.uid);
|
|
16
|
+
if (driver_table_uid2 && driver_table_uid2 !== driver_table_uid1)
|
|
17
|
+
return;
|
|
18
|
+
(0, exports.modelClear)(model, key);
|
|
19
|
+
});
|
|
20
|
+
return this;
|
|
21
|
+
};
|
|
22
|
+
exports.clear = clear;
|
|
23
|
+
const modelClear = function (model, key) {
|
|
24
|
+
if (key && typeof key === "string") {
|
|
25
|
+
model.caches.delete(key);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
model.caches.clear();
|
|
29
|
+
}
|
|
30
|
+
return this;
|
|
31
|
+
};
|
|
32
|
+
exports.modelClear = modelClear;
|
|
33
|
+
// synchronous get
|
|
34
|
+
const get = function (key, opts, reFetchSync) {
|
|
35
|
+
const { driver_table_uid } = Helpers.parseDriverUidAndTableNameFromUID(key);
|
|
36
|
+
return (0, exports.modelGet)(models[driver_table_uid], key, opts, reFetchSync);
|
|
37
|
+
};
|
|
38
|
+
exports.get = get;
|
|
39
|
+
const modelGet = function (model, key, opts, reFetchSync) {
|
|
40
|
+
if (opts.identityCache === false)
|
|
41
|
+
return reFetchSync();
|
|
42
|
+
var value = model.caches.get(key);
|
|
43
|
+
if (value !== undefined) {
|
|
44
|
+
if (opts && opts.saveCheck && typeof value.saved === "function" && !value.saved())
|
|
45
|
+
// if not saved, don't return it, fetch original from db
|
|
46
|
+
return reFetchSync();
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
value = reFetchSync();
|
|
50
|
+
model.caches.set(key, value);
|
|
51
|
+
}
|
|
52
|
+
const expected_expire = typeof opts.identityCache === "number" ? (opts.identityCache * 1000) : model.caches.ttl;
|
|
53
|
+
const expire_expection_delta = expected_expire - model.caches.ttl;
|
|
54
|
+
if (expire_expection_delta > 0) {
|
|
55
|
+
coroutine.start(() => {
|
|
56
|
+
while (true) {
|
|
57
|
+
// quit when cached key-value has not been here
|
|
58
|
+
if (!model.caches.has(key))
|
|
59
|
+
break;
|
|
60
|
+
model.caches.set(key, value);
|
|
61
|
+
if (expected_expire > Date.now()) {
|
|
62
|
+
model.caches.delete(key);
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
else if (expire_expection_delta < 0) {
|
|
69
|
+
setTimeout(() => model.caches.delete(key), expected_expire);
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
};
|
|
73
|
+
exports.modelGet = modelGet;
|