fib-orm 1.4.2 → 2.0.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 +74 -0
- package/lib/cli/commands/dumpModel.d.ts +2 -0
- package/lib/cli/commands/dumpModel.js +106 -0
- package/lib/cli/commands/escape.d.ts +2 -0
- package/lib/cli/commands/escape.js +68 -0
- package/lib/cli/commands/spinner.d.ts +2 -0
- package/lib/cli/commands/spinner.js +36 -0
- package/lib/cli/commands/upgrade.d.ts +2 -0
- package/lib/cli/commands/upgrade.js +65 -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 +26 -0
- package/lib/cli/helpers/package.js +56 -0
- package/lib/cli/index.d.ts +2 -0
- package/lib/cli/index.js +18 -0
- package/lib/codepoints-map.json +762 -0
- package/lib/db-driver/Typo.d.ts +112 -0
- package/lib/db-driver/Typo.js +1 -0
- package/lib/db-driver/built-ins/base.class.d.ts +96 -0
- package/lib/db-driver/built-ins/base.class.js +186 -0
- package/lib/db-driver/built-ins/driver-dm.d.ts +18 -0
- package/lib/db-driver/built-ins/driver-dm.js +53 -0
- package/lib/db-driver/built-ins/driver-mongodb.d.ts +24 -0
- package/lib/db-driver/built-ins/driver-mongodb.js +46 -0
- package/lib/db-driver/built-ins/driver-mysql.d.ts +18 -0
- package/lib/db-driver/built-ins/driver-mysql.js +37 -0
- package/lib/db-driver/built-ins/driver-postgresql.d.ts +22 -0
- package/lib/db-driver/built-ins/driver-postgresql.js +76 -0
- package/lib/db-driver/built-ins/driver-redis.d.ts +12 -0
- package/lib/db-driver/built-ins/driver-redis.js +35 -0
- package/lib/db-driver/built-ins/driver-sqlite.d.ts +17 -0
- package/lib/db-driver/built-ins/driver-sqlite.js +34 -0
- package/lib/db-driver/index.d.ts +3 -0
- package/lib/db-driver/index.js +4 -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 +3 -0
- package/lib/knex/sandbox.d.ts +4 -0
- package/lib/knex/sandbox.js +22 -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 +176 -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 +17 -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 +15 -0
- package/lib/orm/Associations/_utils.js +52 -0
- package/lib/orm/ChainFind.d.ts +3 -0
- package/lib/orm/ChainFind.js +327 -0
- package/lib/orm/ChainInstance.d.ts +6 -0
- package/lib/orm/ChainInstance.js +73 -0
- package/lib/orm/Debug.d.ts +2 -0
- package/lib/orm/Debug.js +17 -0
- package/lib/orm/Drivers/DB/base.d.ts +3 -0
- package/lib/orm/Drivers/DB/base.js +3 -0
- package/lib/orm/Drivers/DB/dm.d.ts +11 -0
- package/lib/orm/Drivers/DB/dm.js +23 -0
- package/lib/orm/Drivers/DB/mysql.d.ts +11 -0
- package/lib/orm/Drivers/DB/mysql.js +23 -0
- package/lib/orm/Drivers/DB/postgres.d.ts +11 -0
- package/lib/orm/Drivers/DB/postgres.js +22 -0
- package/lib/orm/Drivers/DB/sqlite3.d.ts +16 -0
- package/lib/orm/Drivers/DB/sqlite3.js +39 -0
- package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
- package/lib/orm/Drivers/DML/_ddl-sql.js +51 -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 +38 -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 +2 -0
- package/lib/orm/Drivers/DML/dm.d.ts +2 -0
- package/lib/orm/Drivers/DML/dm.js +293 -0
- package/lib/orm/Drivers/DML/index.d.ts +4 -0
- package/lib/orm/Drivers/DML/index.js +6 -0
- package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
- package/lib/orm/Drivers/DML/mysql.js +251 -0
- package/lib/orm/Drivers/DML/postgres.d.ts +3 -0
- package/lib/orm/Drivers/DML/postgres.js +276 -0
- package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
- package/lib/orm/Drivers/DML/sqlite.js +266 -0
- package/lib/orm/Error.d.ts +11 -0
- package/lib/orm/Error.js +40 -0
- package/lib/orm/Helpers.d.ts +35 -0
- package/lib/orm/Helpers.js +233 -0
- package/lib/orm/Hook.d.ts +13 -0
- package/lib/orm/Hook.js +99 -0
- package/lib/orm/Instance.d.ts +2 -0
- package/lib/orm/Instance.js +697 -0
- package/lib/orm/LazyLoad.d.ts +4 -0
- package/lib/orm/LazyLoad.js +76 -0
- package/lib/orm/Model.d.ts +7 -0
- package/lib/orm/Model.js +971 -0
- package/lib/orm/ORM.d.ts +37 -0
- package/lib/orm/ORM.js +241 -0
- package/lib/orm/Patch/fib-cache.d.ts +40 -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 +13 -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 +119 -0
- package/lib/orm/Singleton.d.ts +5 -0
- package/lib/orm/Singleton.js +72 -0
- package/lib/orm/Typo/Adapter.d.ts +5 -0
- package/lib/orm/Typo/Adapter.js +1 -0
- package/lib/orm/Typo/DMLDriver.d.ts +210 -0
- package/lib/orm/Typo/DMLDriver.js +1 -0
- package/lib/orm/Typo/Db.d.ts +38 -0
- package/lib/orm/Typo/Db.js +1 -0
- package/lib/orm/Typo/Error.d.ts +31 -0
- package/lib/orm/Typo/Error.js +1 -0
- package/lib/orm/Typo/ORM.d.ts +233 -0
- package/lib/orm/Typo/ORM.js +3 -0
- package/lib/orm/Typo/Validators.d.ts +32 -0
- package/lib/orm/Typo/Validators.js +2 -0
- package/lib/orm/Typo/_common.d.ts +38 -0
- package/lib/orm/Typo/_common.js +1 -0
- package/lib/orm/Typo/assoc.d.ts +221 -0
- package/lib/orm/Typo/assoc.js +1 -0
- package/lib/orm/Typo/hook.d.ts +42 -0
- package/lib/orm/Typo/hook.js +1 -0
- package/lib/orm/Typo/instance.d.ts +157 -0
- package/lib/orm/Typo/instance.js +1 -0
- package/lib/orm/Typo/model.d.ts +313 -0
- package/lib/orm/Typo/model.js +6 -0
- package/lib/orm/Typo/patch.d.ts +31 -0
- package/lib/orm/Typo/patch.js +1 -0
- package/lib/orm/Typo/property.d.ts +57 -0
- package/lib/orm/Typo/property.js +1 -0
- package/lib/orm/Typo/query.d.ts +216 -0
- package/lib/orm/Typo/query.js +1 -0
- package/lib/orm/Typo/settings.d.ts +35 -0
- package/lib/orm/Typo/settings.js +5 -0
- package/lib/orm/Typo/synchronous.d.ts +47 -0
- package/lib/orm/Typo/synchronous.js +1 -0
- package/lib/orm/Utilities.d.ts +179 -0
- package/lib/orm/Utilities.js +975 -0
- package/lib/orm/Validators.d.ts +34 -0
- package/lib/orm/Validators.js +100 -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 +68 -0
- package/lib/orm/index.js +157 -0
- package/lib/orm-core/callback.d.ts +12 -0
- package/lib/orm-core/callback.js +1 -0
- package/lib/orm-core/dialect.d.ts +80 -0
- package/lib/orm-core/dialect.js +1 -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 +6 -0
- package/lib/orm-core/sync.d.ts +12 -0
- package/lib/orm-core/sync.js +1 -0
- package/lib/orm-core/utils.d.ts +1 -0
- package/lib/orm-core/utils.js +5 -0
- package/lib/orm-property/Property.d.ts +78 -0
- package/lib/orm-property/Property.js +1 -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 +22 -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 +237 -0
- package/lib/orm-property/transformers/index.d.ts +4 -0
- package/lib/orm-property/transformers/index.js +5 -0
- package/lib/orm-property/transformers/mysql.d.ts +102 -0
- package/lib/orm-property/transformers/mysql.js +261 -0
- package/lib/orm-property/transformers/postgresql.d.ts +54 -0
- package/lib/orm-property/transformers/postgresql.js +189 -0
- package/lib/orm-property/transformers/sqlite.d.ts +19 -0
- package/lib/orm-property/transformers/sqlite.js +171 -0
- package/lib/sql-ddl-sync/Dialects/dm.d.ts +40 -0
- package/lib/sql-ddl-sync/Dialects/dm.js +296 -0
- package/lib/sql-ddl-sync/Dialects/index.d.ts +7 -0
- package/lib/sql-ddl-sync/Dialects/index.js +7 -0
- package/lib/sql-ddl-sync/Dialects/mysql.d.ts +39 -0
- package/lib/sql-ddl-sync/Dialects/mysql.js +240 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +40 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.js +286 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +44 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.js +256 -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 +1 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.js +1 -0
- package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
- package/lib/sql-ddl-sync/Typo/Dialect.js +1 -0
- package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
- package/lib/sql-ddl-sync/Typo/Driver.js +1 -0
- package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
- package/lib/sql-ddl-sync/Typo/Query.js +1 -0
- package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
- package/lib/sql-ddl-sync/Typo/SQL.js +1 -0
- package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
- package/lib/sql-ddl-sync/Typo/_common.js +1 -0
- package/lib/sql-ddl-sync/Utils.d.ts +40 -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 +371 -0
- package/lib/sql-query/Comparators.d.ts +3 -0
- package/lib/sql-query/Comparators.js +94 -0
- package/lib/sql-query/Create.d.ts +35 -0
- package/lib/sql-query/Create.js +77 -0
- package/lib/sql-query/Dialects/dm.d.ts +3 -0
- package/lib/sql-query/Dialects/dm.js +74 -0
- package/lib/sql-query/Dialects/index.d.ts +10 -0
- package/lib/sql-query/Dialects/index.js +12 -0
- package/lib/sql-query/Dialects/mssql.d.ts +3 -0
- package/lib/sql-query/Dialects/mssql.js +73 -0
- package/lib/sql-query/Dialects/mysql.d.ts +3 -0
- package/lib/sql-query/Dialects/mysql.js +100 -0
- package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
- package/lib/sql-query/Dialects/postgresql.js +76 -0
- package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
- package/lib/sql-query/Dialects/sqlite.js +61 -0
- package/lib/sql-query/Helpers.d.ts +38 -0
- package/lib/sql-query/Helpers.js +259 -0
- package/lib/sql-query/Insert.d.ts +12 -0
- package/lib/sql-query/Insert.js +33 -0
- package/lib/sql-query/Query.d.ts +29 -0
- package/lib/sql-query/Query.js +119 -0
- package/lib/sql-query/Remove.d.ts +16 -0
- package/lib/sql-query/Remove.js +93 -0
- package/lib/sql-query/Select.d.ts +62 -0
- package/lib/sql-query/Select.js +533 -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 +1 -0
- package/lib/sql-query/Typo/Comparators.d.ts +140 -0
- package/lib/sql-query/Typo/Comparators.js +1 -0
- package/lib/sql-query/Typo/Dialect.d.ts +39 -0
- package/lib/sql-query/Typo/Dialect.js +1 -0
- package/lib/sql-query/Typo/Field.d.ts +8 -0
- package/lib/sql-query/Typo/Field.js +1 -0
- package/lib/sql-query/Typo/Helper.d.ts +4 -0
- package/lib/sql-query/Typo/Helper.js +1 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.js +1 -0
- package/lib/sql-query/Typo/Query.d.ts +34 -0
- package/lib/sql-query/Typo/Query.js +16 -0
- package/lib/sql-query/Typo/Sql.d.ts +143 -0
- package/lib/sql-query/Typo/Sql.js +1 -0
- package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
- package/lib/sql-query/Typo/SubQuery.js +1 -0
- package/lib/sql-query/Typo/index.d.ts +9 -0
- package/lib/sql-query/Typo/index.js +1 -0
- package/lib/sql-query/Update.d.ts +15 -0
- package/lib/sql-query/Update.js +38 -0
- package/lib/sql-query/Where.d.ts +4 -0
- package/lib/sql-query/Where.js +280 -0
- package/package.json +72 -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/Model.js
ADDED
|
@@ -0,0 +1,971 @@
|
|
|
1
|
+
/// <reference lib="es2017" />
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.listFindByChainOrRunSync = exports.Model = void 0;
|
|
4
|
+
const util = require("util");
|
|
5
|
+
const ChainFind = require("./ChainFind");
|
|
6
|
+
const Instance_1 = require("./Instance");
|
|
7
|
+
const LazyLoad = require("./LazyLoad");
|
|
8
|
+
const ManyAssociation = require("./Associations/Many");
|
|
9
|
+
const OneAssociation = require("./Associations/One");
|
|
10
|
+
const ExtendAssociation = require("./Associations/Extend");
|
|
11
|
+
const Property = require("./Property");
|
|
12
|
+
const Singleton = require("./Singleton");
|
|
13
|
+
const Utilities = require("./Utilities");
|
|
14
|
+
const Validators = require("./Validators");
|
|
15
|
+
const Error_1 = require("./Error");
|
|
16
|
+
const Hook = require("./Hook");
|
|
17
|
+
const AggregateFunctions = require("./AggregateFunctions");
|
|
18
|
+
const Helpers = require("./Helpers");
|
|
19
|
+
const _utils_1 = require("./Associations/_utils");
|
|
20
|
+
const fc = require("fib-cache");
|
|
21
|
+
const _dml_helpers_1 = require("./Drivers/DML/_dml-helpers");
|
|
22
|
+
const AvailableHooks = [
|
|
23
|
+
"beforeCreate", "afterCreate",
|
|
24
|
+
"beforeSave", "afterSave",
|
|
25
|
+
"beforeValidation", "afterValidation",
|
|
26
|
+
"beforeRemove", "afterRemove",
|
|
27
|
+
"afterLoad",
|
|
28
|
+
"afterAutoFetch"
|
|
29
|
+
];
|
|
30
|
+
exports.Model = function (m_opts) {
|
|
31
|
+
m_opts = util.extend(m_opts || {}, { keys: m_opts.keys || [] });
|
|
32
|
+
m_opts.keys = (Array.isArray(m_opts.keys) ? m_opts.keys : [m_opts.keys]).filter(Boolean);
|
|
33
|
+
const one_associations = [];
|
|
34
|
+
const many_associations = [];
|
|
35
|
+
const extend_associations = [];
|
|
36
|
+
const association_properties = [];
|
|
37
|
+
const model_selectable_fields = [];
|
|
38
|
+
const fieldToPropertyMap = {};
|
|
39
|
+
/**
|
|
40
|
+
* @description compared to m_opts.properties, allProperties means all properties including:
|
|
41
|
+
* 1. association key properties, such as `{assoc_name}_{id}` property in default `hasOne` case,
|
|
42
|
+
* which was not included by m_opts.properties;
|
|
43
|
+
* 2. dynamically added properties by `model.addProperty(...)`, in fact, all association-about properties
|
|
44
|
+
* were added by `addProperty` method
|
|
45
|
+
*/
|
|
46
|
+
const allProperties = {};
|
|
47
|
+
const virtualProperties = {};
|
|
48
|
+
// const __propertiesByName = new Proxy<typeof allProperties>(allProperties, {
|
|
49
|
+
// get: (target, prop) => {
|
|
50
|
+
// return target[prop as string] || virtualProperties[prop as string];
|
|
51
|
+
// },
|
|
52
|
+
// has: (target, prop) => {
|
|
53
|
+
// return Reflect.has(target, prop) || (prop in virtualProperties);
|
|
54
|
+
// },
|
|
55
|
+
// ownKeys: (target) => {
|
|
56
|
+
// return Reflect.ownKeys(target).concat(Object.keys(virtualProperties));
|
|
57
|
+
// }
|
|
58
|
+
// });
|
|
59
|
+
const keyProperties = [];
|
|
60
|
+
const initialHooks = Object.assign({}, m_opts.hooks);
|
|
61
|
+
const createInstanceSync = function (data, inst_opts, cb) {
|
|
62
|
+
if (!inst_opts) {
|
|
63
|
+
inst_opts = {};
|
|
64
|
+
}
|
|
65
|
+
let found_assoc = false;
|
|
66
|
+
for (let k in data) {
|
|
67
|
+
if (k === "extra_field")
|
|
68
|
+
continue;
|
|
69
|
+
if (m_opts.properties.hasOwnProperty(k))
|
|
70
|
+
continue;
|
|
71
|
+
if (inst_opts.extra && inst_opts.extra.hasOwnProperty(k))
|
|
72
|
+
continue;
|
|
73
|
+
if (m_opts.keys.indexOf(k) >= 0)
|
|
74
|
+
continue;
|
|
75
|
+
if (association_properties.indexOf(k) >= 0)
|
|
76
|
+
continue;
|
|
77
|
+
for (let i = 0; i < one_associations.length; i++) {
|
|
78
|
+
if (one_associations[i].name === k) {
|
|
79
|
+
found_assoc = true;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!found_assoc) {
|
|
84
|
+
for (let i = 0; i < many_associations.length; i++) {
|
|
85
|
+
if (many_associations[i].name === k) {
|
|
86
|
+
found_assoc = true;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (!found_assoc) {
|
|
92
|
+
delete data[k];
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const assoc_opts = {
|
|
96
|
+
autoFetch: inst_opts.autoFetch || false,
|
|
97
|
+
autoFetchLimit: inst_opts.autoFetchLimit,
|
|
98
|
+
cascadeRemove: inst_opts.cascadeRemove
|
|
99
|
+
};
|
|
100
|
+
const __setupAssociations = function (instance) {
|
|
101
|
+
OneAssociation.extend(model, instance, m_opts.driver, one_associations, { assoc_opts });
|
|
102
|
+
ManyAssociation.extend(model, instance, m_opts.driver, many_associations, { assoc_opts });
|
|
103
|
+
ExtendAssociation.extend(model, instance, m_opts.driver, extend_associations, { assoc_opts });
|
|
104
|
+
};
|
|
105
|
+
const instance = new Instance_1.Instance(model, {
|
|
106
|
+
uid: inst_opts.uid,
|
|
107
|
+
keys: m_opts.keys,
|
|
108
|
+
isNew: inst_opts.isNew || false,
|
|
109
|
+
isShell: inst_opts.isShell || false,
|
|
110
|
+
data: data,
|
|
111
|
+
autoSave: inst_opts.autoSave || false,
|
|
112
|
+
extra: inst_opts.extra,
|
|
113
|
+
extra_info: inst_opts.extra_info,
|
|
114
|
+
driver: m_opts.driver,
|
|
115
|
+
table: m_opts.table,
|
|
116
|
+
hooks: m_opts.hooks,
|
|
117
|
+
methods: m_opts.methods,
|
|
118
|
+
validations: m_opts.validations,
|
|
119
|
+
one_associations: one_associations,
|
|
120
|
+
many_associations: many_associations,
|
|
121
|
+
extend_associations: extend_associations,
|
|
122
|
+
association_properties: association_properties,
|
|
123
|
+
__setupAssociations: __setupAssociations,
|
|
124
|
+
fieldToPropertyMap: fieldToPropertyMap,
|
|
125
|
+
keyProperties: keyProperties,
|
|
126
|
+
events: m_opts.ievents
|
|
127
|
+
});
|
|
128
|
+
if (model_selectable_fields !== null) {
|
|
129
|
+
LazyLoad.extend(instance, model, m_opts.properties);
|
|
130
|
+
}
|
|
131
|
+
OneAssociation.autoFetch(instance, one_associations, assoc_opts, false);
|
|
132
|
+
ManyAssociation.autoFetch(instance, many_associations, assoc_opts, false);
|
|
133
|
+
ExtendAssociation.autoFetch(instance, extend_associations, assoc_opts, false);
|
|
134
|
+
Hook.wait(instance, m_opts.hooks.afterAutoFetch, function (err) {
|
|
135
|
+
if (err)
|
|
136
|
+
throw err;
|
|
137
|
+
});
|
|
138
|
+
return instance;
|
|
139
|
+
};
|
|
140
|
+
const model = function (..._data) {
|
|
141
|
+
let data = _data.length > 1 ? _data : _data[0];
|
|
142
|
+
if (Array.isArray(m_opts.keys) && Array.isArray(data)) {
|
|
143
|
+
if (data.length == m_opts.keys.length) {
|
|
144
|
+
const data2 = {};
|
|
145
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
146
|
+
data2[m_opts.keys[i]] = data[i++];
|
|
147
|
+
}
|
|
148
|
+
return createInstanceSync(data2, { isShell: true });
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const err = new Error('Model requires ' + m_opts.keys.length + ' keys, only ' + data.length + ' were provided');
|
|
152
|
+
err.model = m_opts.table;
|
|
153
|
+
throw err;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else if (typeof data === "number" || typeof data === "string") {
|
|
157
|
+
const data2 = {};
|
|
158
|
+
data2[m_opts.keys[0]] = data;
|
|
159
|
+
return createInstanceSync(data2, { isShell: true });
|
|
160
|
+
}
|
|
161
|
+
else if (typeof data === "undefined") {
|
|
162
|
+
data = {};
|
|
163
|
+
}
|
|
164
|
+
let isNew = false;
|
|
165
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
166
|
+
if (!data.hasOwnProperty(m_opts.keys[i])) {
|
|
167
|
+
isNew = true;
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (keyProperties.length != 1 || (keyProperties.length == 1 && keyProperties[0].type != 'serial')) {
|
|
172
|
+
isNew = true;
|
|
173
|
+
}
|
|
174
|
+
return createInstanceSync(data, {
|
|
175
|
+
isNew,
|
|
176
|
+
autoSave: m_opts.autoSave,
|
|
177
|
+
cascadeRemove: m_opts.cascadeRemove
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
Utilities.addUnwritableProperty(model, 'name', m_opts.name || m_opts.table, { configurable: false });
|
|
181
|
+
Utilities.addUnwritableProperty(model, 'allProperties', allProperties, { configurable: false });
|
|
182
|
+
Utilities.addUnwritableProperty(model, 'virtualProperties', virtualProperties, { configurable: false });
|
|
183
|
+
Utilities.addHiddenReadonlyProperty(model, '__propertiesByName', function () {
|
|
184
|
+
return Object.assign({}, allProperties, virtualProperties);
|
|
185
|
+
}, { configurable: false });
|
|
186
|
+
Utilities.addUnwritableProperty(model, 'properties', m_opts.properties, { configurable: false });
|
|
187
|
+
Utilities.addUnwritableProperty(model, 'settings', m_opts.settings, { configurable: false });
|
|
188
|
+
Utilities.addUnwritableProperty(model, 'keys', m_opts.keys, { configurable: false });
|
|
189
|
+
Utilities.addUnwritableProperty(model, 'caches', new fc.LRU({
|
|
190
|
+
max: m_opts.instanceCacheSize > 0 && Number.isInteger(m_opts.instanceCacheSize) ? m_opts.instanceCacheSize : m_opts.settings.get('instance.defaultCacheSize'),
|
|
191
|
+
ttl: (typeof m_opts.identityCache === 'number' ? m_opts.identityCache : 1) * 1000
|
|
192
|
+
}), { configurable: false });
|
|
193
|
+
model.dropSync = function () {
|
|
194
|
+
if (typeof m_opts.driver.doDrop !== "function") {
|
|
195
|
+
throw new Error_1.default("Driver does not support Model.drop()", 'NO_SUPPORT', { model: m_opts.table });
|
|
196
|
+
}
|
|
197
|
+
if (Utilities.isVirtualViewModel(model)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
return m_opts.driver.doDrop({
|
|
201
|
+
table: m_opts.table,
|
|
202
|
+
properties: m_opts.properties,
|
|
203
|
+
one_associations: one_associations,
|
|
204
|
+
many_associations: many_associations
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
model.drop = function (cb) {
|
|
208
|
+
const syncResponse = Utilities.catchBlocking(model.dropSync, [], { thisArg: model });
|
|
209
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
210
|
+
return this;
|
|
211
|
+
};
|
|
212
|
+
model.syncSync = function () {
|
|
213
|
+
if (typeof m_opts.driver.doSync !== "function") {
|
|
214
|
+
throw new Error_1.default("Driver does not support Model.sync()", 'NO_SUPPORT', { model: m_opts.table });
|
|
215
|
+
}
|
|
216
|
+
if (Utilities.isVirtualViewModel(model)) {
|
|
217
|
+
if (m_opts.driver.ddlSync.hasCollectionSync(m_opts.driver.sqlDriver, m_opts.table)) {
|
|
218
|
+
console.warn(`model '${model.name}' defined as virtual view model, but collection with same already exists.`);
|
|
219
|
+
}
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
m_opts.driver.doSync({
|
|
223
|
+
repair_column: !!model.settings.get(`model.dbsync.repair_column.${model.name}`),
|
|
224
|
+
allow_drop_column: !!model.settings.get(`model.dbsync.allow_drop_column.${model.name}`),
|
|
225
|
+
extension: m_opts.__for_extension,
|
|
226
|
+
id: m_opts.keys,
|
|
227
|
+
table: m_opts.table,
|
|
228
|
+
tableComment: m_opts.tableComment,
|
|
229
|
+
allProperties: allProperties,
|
|
230
|
+
indexes: m_opts.indexes || [],
|
|
231
|
+
customTypes: m_opts.db.customTypes,
|
|
232
|
+
one_associations: one_associations,
|
|
233
|
+
many_associations: many_associations,
|
|
234
|
+
extend_associations: extend_associations
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
model.sync = function (cb) {
|
|
238
|
+
const syncResponse = Utilities.catchBlocking(model.syncSync, [], { thisArg: model });
|
|
239
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
240
|
+
return this;
|
|
241
|
+
};
|
|
242
|
+
const collectParamsForGet = function (ids) {
|
|
243
|
+
let options = {};
|
|
244
|
+
ids = ids.filter(x => x !== undefined && x !== null);
|
|
245
|
+
if (typeof ids[ids.length - 1] === "object" && !Array.isArray(ids[ids.length - 1])) {
|
|
246
|
+
options = ids.pop();
|
|
247
|
+
}
|
|
248
|
+
if (ids.length === 1 && Array.isArray(ids[0])) {
|
|
249
|
+
ids = ids[0];
|
|
250
|
+
}
|
|
251
|
+
if (ids.length !== m_opts.keys.length) {
|
|
252
|
+
throw new Error_1.default("Model.get() IDs number mismatch (" + m_opts.keys.length + " needed, " + ids.length + " passed)", 'PARAM_MISMATCH', { model: m_opts.table });
|
|
253
|
+
}
|
|
254
|
+
return { options, ids };
|
|
255
|
+
};
|
|
256
|
+
model.getSync = function (...args) {
|
|
257
|
+
if (!keyProperties.length) {
|
|
258
|
+
Utilities.disAllowOpForVModel(model, 'model.get');
|
|
259
|
+
}
|
|
260
|
+
const conditions = {};
|
|
261
|
+
let prop;
|
|
262
|
+
const { options, ids } = collectParamsForGet(args);
|
|
263
|
+
for (let i = 0; i < keyProperties.length; i++) {
|
|
264
|
+
prop = keyProperties[i];
|
|
265
|
+
conditions[prop.mapsTo] = ids[i];
|
|
266
|
+
}
|
|
267
|
+
Utilities.filterWhereConditionsInput(conditions, model);
|
|
268
|
+
if (!options.hasOwnProperty("autoFetch")) {
|
|
269
|
+
options.autoFetch = m_opts.autoFetch;
|
|
270
|
+
}
|
|
271
|
+
if (!options.hasOwnProperty("autoFetchLimit")) {
|
|
272
|
+
options.autoFetchLimit = m_opts.autoFetchLimit;
|
|
273
|
+
}
|
|
274
|
+
if (!options.hasOwnProperty("cascadeRemove")) {
|
|
275
|
+
options.cascadeRemove = m_opts.cascadeRemove;
|
|
276
|
+
}
|
|
277
|
+
let founditems, err;
|
|
278
|
+
function deferGet() {
|
|
279
|
+
const vFields = Object.entries(model.virtualProperties).map(([k, p]) => p.mapsTo || k);
|
|
280
|
+
;
|
|
281
|
+
const { tableConditions, topConditions } = Utilities.extractSelectTopConditions(conditions, vFields);
|
|
282
|
+
const __pointTypeMapsTo = (0, _dml_helpers_1.pickPointTypeFields)(m_opts.driver, allProperties);
|
|
283
|
+
try {
|
|
284
|
+
founditems = m_opts.driver.find(model_selectable_fields, m_opts.table, tableConditions, {
|
|
285
|
+
limit: 1,
|
|
286
|
+
topConditions,
|
|
287
|
+
selectVirtualFields: vFields,
|
|
288
|
+
generateSqlSelect: m_opts.generateSqlSelect,
|
|
289
|
+
__pointTypeMapsTo,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
catch (ex) {
|
|
293
|
+
err = ex;
|
|
294
|
+
if (err)
|
|
295
|
+
throw new Error_1.default(err.message, 'QUERY_ERROR', { originalCode: err.code });
|
|
296
|
+
}
|
|
297
|
+
if (founditems.length === 0) {
|
|
298
|
+
throw new Error_1.default("Not found", 'NOT_FOUND', { model: m_opts.table });
|
|
299
|
+
}
|
|
300
|
+
Utilities.renameDatastoreFieldsToPropertyNames(founditems[0], fieldToPropertyMap);
|
|
301
|
+
}
|
|
302
|
+
const uid = Utilities.generateUID4SoloGet(m_opts, ids);
|
|
303
|
+
return Singleton.modelGet(model, uid, {
|
|
304
|
+
identityCache: (options.hasOwnProperty("identityCache") ? options.identityCache : m_opts.identityCache),
|
|
305
|
+
saveCheck: m_opts.settings.get("instance.identityCacheSaveCheck")
|
|
306
|
+
}, function () {
|
|
307
|
+
deferGet();
|
|
308
|
+
return createInstanceSync(founditems[0], {
|
|
309
|
+
uid: uid,
|
|
310
|
+
autoSave: options.autoSave,
|
|
311
|
+
autoFetch: (options.autoFetchLimit === 0 ? false : options.autoFetch),
|
|
312
|
+
autoFetchLimit: options.autoFetchLimit,
|
|
313
|
+
cascadeRemove: options.cascadeRemove
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
};
|
|
317
|
+
model.get = function (...args) {
|
|
318
|
+
let cb = null;
|
|
319
|
+
Helpers.selectArgs(args, function (arg_type, arg) {
|
|
320
|
+
switch (arg_type) {
|
|
321
|
+
case 'function':
|
|
322
|
+
cb = arg;
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
args = args.filter(x => x !== cb);
|
|
327
|
+
if (typeof cb !== "function")
|
|
328
|
+
throw new Error_1.default("Missing Model.get() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
329
|
+
collectParamsForGet(args);
|
|
330
|
+
process.nextTick(() => {
|
|
331
|
+
const syncReponse = Utilities.catchBlocking(model.getSync, args, { thisArg: model });
|
|
332
|
+
Utilities.takeAwayResult(syncReponse, { no_throw: true, callback: cb });
|
|
333
|
+
});
|
|
334
|
+
return this;
|
|
335
|
+
};
|
|
336
|
+
const chainOrRunSync = function () {
|
|
337
|
+
let conditions = null;
|
|
338
|
+
let options = {};
|
|
339
|
+
let order = null;
|
|
340
|
+
let merges = [];
|
|
341
|
+
Helpers.selectArgs(arguments, (arg_type, arg) => {
|
|
342
|
+
switch (arg_type) {
|
|
343
|
+
case "number":
|
|
344
|
+
options.limit = arg;
|
|
345
|
+
break;
|
|
346
|
+
case "object":
|
|
347
|
+
if (Array.isArray(arg)) {
|
|
348
|
+
if (arg.length > 0) {
|
|
349
|
+
order = arg;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
else {
|
|
353
|
+
if (conditions === null) {
|
|
354
|
+
conditions = Object.assign({}, arg);
|
|
355
|
+
Utilities.filterWhereConditionsInput(conditions, model);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
if (options.hasOwnProperty("limit")) {
|
|
359
|
+
arg.limit = options.limit;
|
|
360
|
+
}
|
|
361
|
+
options = arg;
|
|
362
|
+
if (options.hasOwnProperty("__merge")) {
|
|
363
|
+
merges = Utilities.combineMergeInfoToArray(options.__merge);
|
|
364
|
+
merges.forEach(merge => {
|
|
365
|
+
if (!Array.isArray(merge.select) || !merge.select.length) {
|
|
366
|
+
merge.select = [];
|
|
367
|
+
if (options.extra && Object.keys(options.extra).length)
|
|
368
|
+
merge.select = merge.select.concat(Object.keys(options.extra));
|
|
369
|
+
}
|
|
370
|
+
merge.select = merge.select.map(field => {
|
|
371
|
+
var _a;
|
|
372
|
+
if (typeof field === 'object')
|
|
373
|
+
return field;
|
|
374
|
+
const propname = field;
|
|
375
|
+
const mapsTo = ((_a = options.extra[field]) === null || _a === void 0 ? void 0 : _a.mapsTo) || field;
|
|
376
|
+
return {
|
|
377
|
+
as: propname,
|
|
378
|
+
column_name: mapsTo
|
|
379
|
+
};
|
|
380
|
+
});
|
|
381
|
+
merge.select = Array.from(new Set(merge.select));
|
|
382
|
+
});
|
|
383
|
+
delete options.__merge;
|
|
384
|
+
}
|
|
385
|
+
if (options.hasOwnProperty("order")) {
|
|
386
|
+
order = options.order;
|
|
387
|
+
delete options.order;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
break;
|
|
392
|
+
case "string":
|
|
393
|
+
order = arg;
|
|
394
|
+
// if (arg[0] === "-") {
|
|
395
|
+
// order = [ arg.substr(1), "Z" ];
|
|
396
|
+
// } else {
|
|
397
|
+
// order = [ arg ];
|
|
398
|
+
// }
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
if (!options.hasOwnProperty("limit")) {
|
|
403
|
+
options.limit = m_opts.settings.get('instance.defaultFindLimit');
|
|
404
|
+
}
|
|
405
|
+
if (!options.hasOwnProperty("identityCache")) {
|
|
406
|
+
options.identityCache = m_opts.identityCache;
|
|
407
|
+
}
|
|
408
|
+
if (!options.hasOwnProperty("autoFetchLimit")) {
|
|
409
|
+
options.autoFetchLimit = m_opts.autoFetchLimit;
|
|
410
|
+
}
|
|
411
|
+
if (!options.hasOwnProperty("cascadeRemove")) {
|
|
412
|
+
options.cascadeRemove = m_opts.cascadeRemove;
|
|
413
|
+
}
|
|
414
|
+
let normalized_order_without_table = [];
|
|
415
|
+
if (order) {
|
|
416
|
+
normalized_order_without_table = Utilities.standardizeOrder(order);
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
normalized_order_without_table = [];
|
|
420
|
+
}
|
|
421
|
+
let base_table = m_opts.table;
|
|
422
|
+
let normalized_order = normalized_order_without_table;
|
|
423
|
+
if (merges && merges.length) {
|
|
424
|
+
base_table = options.chainfind_linktable || base_table;
|
|
425
|
+
normalized_order = Utilities.addTableToStandardedOrder(normalized_order_without_table, Utilities.parseTableInputForSelect(base_table).alias);
|
|
426
|
+
}
|
|
427
|
+
if (conditions) {
|
|
428
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
429
|
+
}
|
|
430
|
+
return new ChainFind(model, {
|
|
431
|
+
only: options.only || model_selectable_fields,
|
|
432
|
+
keys: m_opts.keys,
|
|
433
|
+
table: base_table,
|
|
434
|
+
driver: m_opts.driver,
|
|
435
|
+
conditions: conditions,
|
|
436
|
+
associations: many_associations,
|
|
437
|
+
limit: options.limit,
|
|
438
|
+
order: normalized_order,
|
|
439
|
+
merge: merges,
|
|
440
|
+
exists: options.exists || [],
|
|
441
|
+
offset: options.offset,
|
|
442
|
+
properties: model.__propertiesByName,
|
|
443
|
+
keyProperties: keyProperties,
|
|
444
|
+
generateSqlSelect: m_opts.generateSqlSelect,
|
|
445
|
+
newInstanceSync: function (data) {
|
|
446
|
+
// We need to do the rename before we construct the UID & do the cache lookup
|
|
447
|
+
// because the cache is loaded using propertyName rather than fieldName
|
|
448
|
+
Utilities.renameDatastoreFieldsToPropertyNames(data, fieldToPropertyMap);
|
|
449
|
+
// Construct UID
|
|
450
|
+
const uid = Utilities.generateUID4ChainFind(m_opts, merges, data);
|
|
451
|
+
// Now we can do the cache lookup
|
|
452
|
+
return Singleton.modelGet(model, uid, {
|
|
453
|
+
identityCache: options.identityCache,
|
|
454
|
+
saveCheck: m_opts.settings.get("instance.identityCacheSaveCheck")
|
|
455
|
+
}, function () {
|
|
456
|
+
return createInstanceSync(data, {
|
|
457
|
+
uid: uid,
|
|
458
|
+
autoSave: m_opts.autoSave,
|
|
459
|
+
autoFetch: (options.autoFetchLimit === 0 ? false : (options.autoFetch || m_opts.autoFetch)),
|
|
460
|
+
autoFetchLimit: options.autoFetchLimit,
|
|
461
|
+
cascadeRemove: options.cascadeRemove,
|
|
462
|
+
extra: options.extra,
|
|
463
|
+
extra_info: options.extra_info
|
|
464
|
+
});
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
};
|
|
469
|
+
model.findSync = function (...args) {
|
|
470
|
+
const chain = chainOrRunSync.apply(model, args);
|
|
471
|
+
return chain.runSync();
|
|
472
|
+
};
|
|
473
|
+
model.find = function (...args) {
|
|
474
|
+
var cb = null;
|
|
475
|
+
if (typeof util.last(args) === 'function')
|
|
476
|
+
cb = args.pop();
|
|
477
|
+
const chain = chainOrRunSync.apply(model, args);
|
|
478
|
+
if (cb)
|
|
479
|
+
chain.run(cb);
|
|
480
|
+
return chain;
|
|
481
|
+
};
|
|
482
|
+
model.where = model.all = model.find;
|
|
483
|
+
model.whereSync = model.allSync = model.findSync;
|
|
484
|
+
model.oneSync = function (...args) {
|
|
485
|
+
const results = this.find(...args).limit(1).runSync();
|
|
486
|
+
return results.length ? results[0] : null;
|
|
487
|
+
};
|
|
488
|
+
model.one = function (...args) {
|
|
489
|
+
var cb = null;
|
|
490
|
+
Helpers.selectArgs(args, function (arg_type, arg) {
|
|
491
|
+
switch (arg_type) {
|
|
492
|
+
case 'function':
|
|
493
|
+
cb = arg;
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
args = args.filter(x => x !== cb);
|
|
498
|
+
if (typeof cb !== "function") {
|
|
499
|
+
throw new Error_1.default("Missing Model.one() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
500
|
+
}
|
|
501
|
+
const newCb = function (err, results) {
|
|
502
|
+
if (err)
|
|
503
|
+
return cb(err);
|
|
504
|
+
return cb(null, results.length ? results[0] : null);
|
|
505
|
+
};
|
|
506
|
+
args.push(1);
|
|
507
|
+
const chain = this.find(...args);
|
|
508
|
+
chain.run(newCb);
|
|
509
|
+
return chain;
|
|
510
|
+
};
|
|
511
|
+
model.countSync = function (conditions) {
|
|
512
|
+
Utilities.disAllowOpForVModel(model, 'model.count');
|
|
513
|
+
if (conditions) {
|
|
514
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
515
|
+
Utilities.filterWhereConditionsInput(conditions, model);
|
|
516
|
+
}
|
|
517
|
+
const data = m_opts.driver.count(m_opts.table, conditions, {});
|
|
518
|
+
if (data.length === 0)
|
|
519
|
+
return 0;
|
|
520
|
+
return data[0].c;
|
|
521
|
+
};
|
|
522
|
+
model.count = function () {
|
|
523
|
+
var conditions = null;
|
|
524
|
+
var cb = null;
|
|
525
|
+
Helpers.selectArgs(arguments, (arg_type, arg) => {
|
|
526
|
+
switch (arg_type) {
|
|
527
|
+
case "object":
|
|
528
|
+
conditions = arg;
|
|
529
|
+
break;
|
|
530
|
+
case "function":
|
|
531
|
+
cb = arg;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
if (typeof cb !== "function") {
|
|
536
|
+
throw new Error_1.default("Missing Model.count() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
537
|
+
}
|
|
538
|
+
process.nextTick(() => {
|
|
539
|
+
const syncResponse = Utilities.catchBlocking(model.countSync, [conditions, cb], { thisArg: model });
|
|
540
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
541
|
+
});
|
|
542
|
+
return this;
|
|
543
|
+
};
|
|
544
|
+
model.aggregate = function () {
|
|
545
|
+
Utilities.disAllowOpForVModel(model, 'model.aggregate');
|
|
546
|
+
var conditions = {};
|
|
547
|
+
var propertyList = [];
|
|
548
|
+
Helpers.selectArgs(arguments, (arg_type, arg) => {
|
|
549
|
+
if (arg_type === 'object') {
|
|
550
|
+
if (Array.isArray(arg)) {
|
|
551
|
+
propertyList = arg;
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
conditions = arg;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
if (conditions) {
|
|
559
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
560
|
+
Utilities.filterWhereConditionsInput(conditions, model);
|
|
561
|
+
}
|
|
562
|
+
return new AggregateFunctions({
|
|
563
|
+
table: m_opts.table,
|
|
564
|
+
driver_name: m_opts.driver_name,
|
|
565
|
+
driver: m_opts.driver,
|
|
566
|
+
conditions: conditions,
|
|
567
|
+
propertyList: propertyList,
|
|
568
|
+
properties: allProperties
|
|
569
|
+
});
|
|
570
|
+
};
|
|
571
|
+
model.existsSync = function (...ids) {
|
|
572
|
+
Utilities.disAllowOpForVModel(model, 'model.exists');
|
|
573
|
+
let conditions = {};
|
|
574
|
+
/**
|
|
575
|
+
* assign keys' id columns comparator-eq value
|
|
576
|
+
* as its order in `m_opts.keys`
|
|
577
|
+
*
|
|
578
|
+
* @example
|
|
579
|
+
* define('test', {
|
|
580
|
+
* key_1: {
|
|
581
|
+
* type: 'integer', key: true
|
|
582
|
+
* },
|
|
583
|
+
* key_2: {
|
|
584
|
+
* type: 'integer', key: true
|
|
585
|
+
* }
|
|
586
|
+
* })
|
|
587
|
+
*/
|
|
588
|
+
const firstItem = ids[0];
|
|
589
|
+
if (ids.length === 1 && typeof firstItem === "object") {
|
|
590
|
+
if (Array.isArray(firstItem)) {
|
|
591
|
+
/**
|
|
592
|
+
* @example
|
|
593
|
+
* Person.exists([1, 7])
|
|
594
|
+
*/
|
|
595
|
+
const col_values = firstItem;
|
|
596
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
597
|
+
conditions[m_opts.keys[i]] = col_values[i];
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
/**
|
|
602
|
+
* @access general usage
|
|
603
|
+
* @example
|
|
604
|
+
* Person.exists({key_1: 1, key_2: 7})
|
|
605
|
+
*/
|
|
606
|
+
conditions = firstItem;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
/**
|
|
611
|
+
* @example
|
|
612
|
+
* Person.exists(1, 7)
|
|
613
|
+
*/
|
|
614
|
+
for (let i = 0; i < m_opts.keys.length; i++) {
|
|
615
|
+
conditions[m_opts.keys[i]] = ids[i];
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
if (conditions) {
|
|
619
|
+
conditions = Utilities.checkConditions(conditions, one_associations);
|
|
620
|
+
Utilities.filterWhereConditionsInput(conditions, model);
|
|
621
|
+
}
|
|
622
|
+
const data = m_opts.driver.count(m_opts.table, conditions, {});
|
|
623
|
+
if (data.length === 0)
|
|
624
|
+
return false;
|
|
625
|
+
return data[0].c > 0;
|
|
626
|
+
};
|
|
627
|
+
model.exists = function (...ids) {
|
|
628
|
+
var cb = ids.pop();
|
|
629
|
+
if (typeof cb !== "function") {
|
|
630
|
+
throw new Error_1.default("Missing Model.exists() callback", 'MISSING_CALLBACK', { model: m_opts.table });
|
|
631
|
+
}
|
|
632
|
+
const syncResponse = Utilities.catchBlocking(model.existsSync, ids, { thisArg: model });
|
|
633
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
634
|
+
return this;
|
|
635
|
+
};
|
|
636
|
+
model.create = function (...args) {
|
|
637
|
+
var done = null;
|
|
638
|
+
Helpers.selectArgs(arguments, (arg_type, arg, idx) => {
|
|
639
|
+
switch (arg_type) {
|
|
640
|
+
case "function":
|
|
641
|
+
done = arg;
|
|
642
|
+
args = args.filter(x => x !== done);
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
const syncResponse = Utilities.catchBlocking(model.createSync, args, { thisArg: model });
|
|
647
|
+
Utilities.takeAwayResult(syncResponse, { callback: done });
|
|
648
|
+
return syncResponse.result;
|
|
649
|
+
};
|
|
650
|
+
model.createSync = function () {
|
|
651
|
+
Utilities.disAllowOpForVModel(model, 'model.create');
|
|
652
|
+
let create_single = false;
|
|
653
|
+
let opts = {};
|
|
654
|
+
let itemsParams = [];
|
|
655
|
+
Helpers.selectArgs(arguments, (arg_type, arg, idx) => {
|
|
656
|
+
switch (arg_type) {
|
|
657
|
+
case "object":
|
|
658
|
+
if (!create_single && Array.isArray(arg)) {
|
|
659
|
+
itemsParams = itemsParams.concat(arg);
|
|
660
|
+
}
|
|
661
|
+
else if (idx === 0) {
|
|
662
|
+
create_single = true;
|
|
663
|
+
itemsParams.push(arg);
|
|
664
|
+
}
|
|
665
|
+
else {
|
|
666
|
+
opts = { parallel: false };
|
|
667
|
+
}
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
const items = Utilities.parallelQueryIfPossible(false, itemsParams, (data) => {
|
|
672
|
+
const item = createInstanceSync(data, {
|
|
673
|
+
isNew: true,
|
|
674
|
+
autoSave: m_opts.autoSave,
|
|
675
|
+
// not fetch associated instance on its creation.
|
|
676
|
+
autoFetch: false
|
|
677
|
+
});
|
|
678
|
+
item.saveSync();
|
|
679
|
+
return item;
|
|
680
|
+
});
|
|
681
|
+
const results = create_single ? items[0] : items;
|
|
682
|
+
return results;
|
|
683
|
+
};
|
|
684
|
+
model.clearSync = function () {
|
|
685
|
+
Utilities.disAllowOpForVModel(model, 'model.clear');
|
|
686
|
+
return m_opts.driver.clear(m_opts.table);
|
|
687
|
+
};
|
|
688
|
+
model.clear = function (cb) {
|
|
689
|
+
Utilities.disAllowOpForVModel(model, 'model.clear');
|
|
690
|
+
m_opts.driver.clear(m_opts.table, function (err) {
|
|
691
|
+
if (typeof cb === "function")
|
|
692
|
+
cb(err);
|
|
693
|
+
});
|
|
694
|
+
return this;
|
|
695
|
+
};
|
|
696
|
+
model.prependValidation = function (key, validation) {
|
|
697
|
+
if (m_opts.validations.hasOwnProperty(key)) {
|
|
698
|
+
m_opts.validations[key].splice(0, 0, validation);
|
|
699
|
+
}
|
|
700
|
+
else {
|
|
701
|
+
m_opts.validations[key] = [validation];
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
// control current owned fields
|
|
705
|
+
const currFields = {};
|
|
706
|
+
model.findBy = function (...args) {
|
|
707
|
+
if (Array.isArray(args[0])) {
|
|
708
|
+
const [by_list, self_conditions = {}, self_options, cb] = args;
|
|
709
|
+
return listFindByChainOrRunSync(model, self_conditions, by_list, self_options, { callback: cb });
|
|
710
|
+
}
|
|
711
|
+
const [association_name, self_conditions, findby_options, cb] = args;
|
|
712
|
+
return soloFindByChainOrRunSync(model, association_name, self_conditions, findby_options, { callback: cb, is_sync: false });
|
|
713
|
+
};
|
|
714
|
+
model.findBySync = function (...args) {
|
|
715
|
+
if (Array.isArray(args[0])) {
|
|
716
|
+
const [by_list, self_conditions = {}, self_options] = args;
|
|
717
|
+
return listFindByChainOrRunSync(model, self_conditions, by_list, self_options, { is_sync: true });
|
|
718
|
+
}
|
|
719
|
+
const [association_name, self_conditions, findby_options] = args;
|
|
720
|
+
return soloFindByChainOrRunSync(model, association_name, self_conditions, findby_options, { is_sync: true });
|
|
721
|
+
};
|
|
722
|
+
model.addProperty = function (propIn, options) {
|
|
723
|
+
var cType;
|
|
724
|
+
var prop = Property.normalize({
|
|
725
|
+
prop: propIn, name: (options && options.name || propIn.name),
|
|
726
|
+
customTypes: m_opts.db.customTypes, settings: m_opts.settings
|
|
727
|
+
});
|
|
728
|
+
if (!m_opts.virtualView.disabled) {
|
|
729
|
+
prop.virtual = true;
|
|
730
|
+
}
|
|
731
|
+
if (prop.type === 'serial') {
|
|
732
|
+
prop.key = true;
|
|
733
|
+
prop.klass = prop.klass || 'primary';
|
|
734
|
+
}
|
|
735
|
+
// Maintains backwards compatibility
|
|
736
|
+
if (m_opts.keys.indexOf(prop.name) !== -1) {
|
|
737
|
+
prop.key = true;
|
|
738
|
+
}
|
|
739
|
+
else if (prop.key) {
|
|
740
|
+
m_opts.keys.push(prop.name);
|
|
741
|
+
}
|
|
742
|
+
if (options && options.klass) {
|
|
743
|
+
prop.klass = options.klass;
|
|
744
|
+
}
|
|
745
|
+
switch (prop.klass) {
|
|
746
|
+
default:
|
|
747
|
+
m_opts.properties[prop.name] = prop;
|
|
748
|
+
break;
|
|
749
|
+
case 'extendsTo':
|
|
750
|
+
association_properties.push(prop.name);
|
|
751
|
+
break;
|
|
752
|
+
case 'hasOne':
|
|
753
|
+
association_properties.push(prop.name);
|
|
754
|
+
break;
|
|
755
|
+
}
|
|
756
|
+
if (!prop.virtual) {
|
|
757
|
+
allProperties[prop.name] = prop;
|
|
758
|
+
}
|
|
759
|
+
else {
|
|
760
|
+
virtualProperties[prop.name] = prop;
|
|
761
|
+
// TODO: should we force virtual view without keys?
|
|
762
|
+
m_opts.keys = m_opts.keys.filter(key => key !== prop.name);
|
|
763
|
+
prop.required = false;
|
|
764
|
+
prop.index = false;
|
|
765
|
+
prop.primary = false;
|
|
766
|
+
delete prop.klass;
|
|
767
|
+
}
|
|
768
|
+
fieldToPropertyMap[prop.mapsTo] = prop;
|
|
769
|
+
if (prop.required) {
|
|
770
|
+
model.prependValidation(prop.name, Validators.required());
|
|
771
|
+
}
|
|
772
|
+
if (Utilities.isKeyProperty(prop) && !m_opts.__for_extension) {
|
|
773
|
+
keyProperties.push(prop);
|
|
774
|
+
}
|
|
775
|
+
if (prop.lazyload !== true && !currFields[prop.name]) {
|
|
776
|
+
currFields[prop.name] = true;
|
|
777
|
+
if (prop.virtual) {
|
|
778
|
+
return prop;
|
|
779
|
+
}
|
|
780
|
+
if ((cType = m_opts.db.customTypes[prop.type]) && cType.datastoreGet) {
|
|
781
|
+
model_selectable_fields.push({
|
|
782
|
+
a: prop.mapsTo, sql: cType.datastoreGet(prop, m_opts.db.driver.query)
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
else {
|
|
786
|
+
model_selectable_fields.push(prop.mapsTo);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
return prop;
|
|
790
|
+
};
|
|
791
|
+
Object.defineProperty(model, "table", {
|
|
792
|
+
value: m_opts.table,
|
|
793
|
+
enumerable: false
|
|
794
|
+
});
|
|
795
|
+
Object.defineProperty(model, "id", {
|
|
796
|
+
value: m_opts.keys,
|
|
797
|
+
enumerable: false
|
|
798
|
+
});
|
|
799
|
+
Object.defineProperty(model, "uid", {
|
|
800
|
+
value: Utilities.generateUID4Model(m_opts),
|
|
801
|
+
enumerable: false
|
|
802
|
+
});
|
|
803
|
+
// Standardize validations
|
|
804
|
+
for (let k in m_opts.validations) {
|
|
805
|
+
const validationHash = m_opts.validations[k];
|
|
806
|
+
if (!Array.isArray(validationHash)) {
|
|
807
|
+
m_opts.validations[k] = [validationHash];
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
if (Object.keys(m_opts.properties).every((k) => m_opts.properties[k].virtual === true)) {
|
|
811
|
+
m_opts.virtualView.disabled = false;
|
|
812
|
+
}
|
|
813
|
+
// If no keys are defined add the default one
|
|
814
|
+
if (m_opts.virtualView.disabled && m_opts.keys.length == 0 && !Object.values(m_opts.properties).some((p) => p.key === true)) {
|
|
815
|
+
m_opts.properties[m_opts.settings.get("properties.primary_key")] = {
|
|
816
|
+
type: 'serial', key: true, required: false, klass: 'primary'
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
let p, keyPrimaryP = null, keyP = null;
|
|
820
|
+
// standardize properties
|
|
821
|
+
for (let k in m_opts.properties) {
|
|
822
|
+
p = model.addProperty(m_opts.properties[k], { name: k });
|
|
823
|
+
if (Utilities.isKeyPrimaryProperty(p))
|
|
824
|
+
keyPrimaryP = keyPrimaryP || p;
|
|
825
|
+
else if (Utilities.isKeyProperty(p))
|
|
826
|
+
keyP = keyP || p;
|
|
827
|
+
}
|
|
828
|
+
if (keyP && keyPrimaryP) {
|
|
829
|
+
keyProperties.splice(0, keyProperties.length);
|
|
830
|
+
keyProperties.push(keyPrimaryP);
|
|
831
|
+
}
|
|
832
|
+
// if no any serial-type, keyPrimiary property
|
|
833
|
+
if (m_opts.virtualView.disabled && keyProperties.length === 0) {
|
|
834
|
+
if (keyP) // use keyPrimary as the only keyProperties
|
|
835
|
+
keyProperties.push(keyP);
|
|
836
|
+
else
|
|
837
|
+
throw new Error_1.default("Model defined without any keys", 'BAD_MODEL', { model: m_opts.table });
|
|
838
|
+
}
|
|
839
|
+
// setup hooks
|
|
840
|
+
for (let k in AvailableHooks) {
|
|
841
|
+
model[AvailableHooks[k]] = Utilities.createHookHelper(m_opts.hooks, AvailableHooks[k], { initialHooks });
|
|
842
|
+
}
|
|
843
|
+
Utilities.addUnwritableProperty(model, 'associations', {}, { configurable: false });
|
|
844
|
+
Utilities.addUnwritableProperty(model, '__keyProperties', keyProperties, { enumerable: false, configurable: false });
|
|
845
|
+
OneAssociation.prepare(model, { one_associations, many_associations, extend_associations }, { db: m_opts.db });
|
|
846
|
+
ManyAssociation.prepare(model, { one_associations, many_associations, extend_associations }, { db: m_opts.db });
|
|
847
|
+
ExtendAssociation.prepare(model, { one_associations, many_associations, extend_associations }, { db: m_opts.db });
|
|
848
|
+
return model;
|
|
849
|
+
};
|
|
850
|
+
function soloFindByChainOrRunSync(model, association_name, conditions, findby_options, opts) {
|
|
851
|
+
const { callback: cb, is_sync = false } = opts;
|
|
852
|
+
if (is_sync) {
|
|
853
|
+
const modelFindBySyncAccessor = model.associations[association_name].association.modelFindBySyncAccessor;
|
|
854
|
+
if (!modelFindBySyncAccessor || typeof model[modelFindBySyncAccessor] !== 'function')
|
|
855
|
+
throw new Error(`[soloFindByChainOrRunSync] invalid association name ${association_name} provided!`);
|
|
856
|
+
return model[modelFindBySyncAccessor](conditions, findby_options);
|
|
857
|
+
}
|
|
858
|
+
const findByAccessor = model.associations[association_name].association.modelFindByAccessor;
|
|
859
|
+
if (!findByAccessor || typeof model[findByAccessor] !== 'function')
|
|
860
|
+
throw new Error(`[soloFindByChainOrRunSync] invalid association name ${association_name} provided!`);
|
|
861
|
+
if (typeof cb === 'function')
|
|
862
|
+
return model[findByAccessor](conditions, findby_options, cb);
|
|
863
|
+
return model[findByAccessor](conditions, findby_options);
|
|
864
|
+
}
|
|
865
|
+
function listFindByChainOrRunSync(model, self_conditions, by_list, self_options, opts) {
|
|
866
|
+
const { callback: cb, is_sync = false } = opts;
|
|
867
|
+
self_options = self_options || {};
|
|
868
|
+
const merges = Utilities.combineMergeInfoToArray(self_options.__merge);
|
|
869
|
+
const tableCountHash = {};
|
|
870
|
+
function countTable(t, is_add = false) {
|
|
871
|
+
if (!tableCountHash[t])
|
|
872
|
+
tableCountHash[t] = 0;
|
|
873
|
+
if (is_add)
|
|
874
|
+
tableCountHash[t]++;
|
|
875
|
+
return tableCountHash[t];
|
|
876
|
+
}
|
|
877
|
+
function getTableAlias(alias_from_t) {
|
|
878
|
+
return `${alias_from_t}${countTable(alias_from_t, true)}`;
|
|
879
|
+
}
|
|
880
|
+
by_list.forEach(by_item => {
|
|
881
|
+
const association = Helpers.tryGetAssociationItemFromModel(by_item.association_name, model);
|
|
882
|
+
if (!association)
|
|
883
|
+
return;
|
|
884
|
+
const isHasMany = Helpers.getManyAssociationItemFromModel(by_item.association_name, model) === association;
|
|
885
|
+
const isHasOne = Helpers.getOneAssociationItemFromModel(by_item.association_name, model) === association;
|
|
886
|
+
const isExtendsTo = Helpers.getExtendsToAssociationItemFromModel(by_item.association_name, model) === association;
|
|
887
|
+
const by_item_conditions = Object.assign({}, by_item.conditions);
|
|
888
|
+
Utilities.filterWhereConditionsInput(by_item_conditions, model);
|
|
889
|
+
if (isHasMany) { // support hasmany
|
|
890
|
+
const left_info = {
|
|
891
|
+
table: `${model.table}`,
|
|
892
|
+
alias: getTableAlias(`lt_${model.table}`),
|
|
893
|
+
ids: model.id
|
|
894
|
+
};
|
|
895
|
+
const ljoin_info = {
|
|
896
|
+
table: `${association.mergeTable}`,
|
|
897
|
+
alias: getTableAlias(`lj_${association.mergeTable}`),
|
|
898
|
+
ids: (0, _utils_1.getMapsToFromPropertyHash)(association.mergeId)
|
|
899
|
+
};
|
|
900
|
+
const rjoin_info = {
|
|
901
|
+
table: `${association.mergeTable}`,
|
|
902
|
+
alias: getTableAlias(`rj_${association.mergeTable}`),
|
|
903
|
+
ids: (0, _utils_1.getMapsToFromPropertyHash)(association.mergeAssocId)
|
|
904
|
+
};
|
|
905
|
+
const right_info = {
|
|
906
|
+
table: `${association.model.table}`,
|
|
907
|
+
alias: getTableAlias(`rt_${association.model.table}`),
|
|
908
|
+
ids: association.model.id
|
|
909
|
+
};
|
|
910
|
+
const extraProps = association.props;
|
|
911
|
+
let extra_where = Utilities.extractHasManyExtraConditions(association, by_item_conditions);
|
|
912
|
+
extra_where = Object.assign(Object.assign({}, by_item.join_where), extra_where);
|
|
913
|
+
Utilities.filterWhereConditionsInput(extra_where, model);
|
|
914
|
+
merges.push({
|
|
915
|
+
from: { table: Utilities.tableAlias(ljoin_info.table, ljoin_info.alias), field: ljoin_info.ids },
|
|
916
|
+
to: { table: left_info.table, field: left_info.ids },
|
|
917
|
+
where: [ljoin_info.alias, extra_where],
|
|
918
|
+
table: left_info.alias,
|
|
919
|
+
select: (by_item.extra_select || []).filter(x => extraProps.hasOwnProperty(x))
|
|
920
|
+
});
|
|
921
|
+
merges.push({
|
|
922
|
+
from: { table: Utilities.tableAlias(right_info.table, right_info.alias), field: right_info.ids },
|
|
923
|
+
to: { table: rjoin_info.table, field: rjoin_info.ids },
|
|
924
|
+
where: [right_info.alias, by_item_conditions],
|
|
925
|
+
table: ljoin_info.alias,
|
|
926
|
+
select: [],
|
|
927
|
+
});
|
|
928
|
+
self_options.chainfind_linktable = Utilities.tableAlias(model.table);
|
|
929
|
+
}
|
|
930
|
+
else if (isHasOne) { // support hasone
|
|
931
|
+
const reled_ids = typeof association.field === 'string' ? [association.field] : (Array.isArray(association.field) ? association.field : (0, _utils_1.getMapsToFromPropertyHash)(association.field));
|
|
932
|
+
const base_info = {
|
|
933
|
+
table: `${model.table}`,
|
|
934
|
+
alias: `_base$${model.table}`,
|
|
935
|
+
ids: !association.reversed ? reled_ids : association.model.id
|
|
936
|
+
};
|
|
937
|
+
const rel_info = {
|
|
938
|
+
table: `${association.model.table}`,
|
|
939
|
+
alias: `_rel$${association.model.table}`,
|
|
940
|
+
ids: !association.reversed ? association.model.id : reled_ids
|
|
941
|
+
};
|
|
942
|
+
merges.push({
|
|
943
|
+
from: { table: Utilities.tableAlias(rel_info.table, rel_info.alias), field: rel_info.ids },
|
|
944
|
+
to: { table: Utilities.tableAlias(base_info.table, base_info.alias), field: base_info.ids },
|
|
945
|
+
where: [rel_info.alias, by_item_conditions],
|
|
946
|
+
table: base_info.alias,
|
|
947
|
+
select: []
|
|
948
|
+
});
|
|
949
|
+
self_options.chainfind_linktable = Utilities.tableAlias(base_info.table, base_info.alias);
|
|
950
|
+
}
|
|
951
|
+
else if (isExtendsTo) { // support extendsTo
|
|
952
|
+
merges.push({
|
|
953
|
+
from: { table: association.model.table, field: Object.keys(association.field) },
|
|
954
|
+
to: { table: model.table, field: model.id },
|
|
955
|
+
where: [association.model.table, by_item_conditions],
|
|
956
|
+
table: model.table,
|
|
957
|
+
select: []
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
});
|
|
961
|
+
self_options.__merge = merges;
|
|
962
|
+
self_options.extra = {};
|
|
963
|
+
if (typeof cb === "function") {
|
|
964
|
+
return model.find.call(model, self_conditions, self_options, cb);
|
|
965
|
+
}
|
|
966
|
+
const chain = model.find(self_conditions, self_options);
|
|
967
|
+
if (is_sync)
|
|
968
|
+
return chain.runSync();
|
|
969
|
+
return chain;
|
|
970
|
+
}
|
|
971
|
+
exports.listFindByChainOrRunSync = listFindByChainOrRunSync;
|