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
|
@@ -0,0 +1,569 @@
|
|
|
1
|
+
/// <reference lib="es5" />
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.autoFetch = exports.extend = exports.prepare = void 0;
|
|
4
|
+
const util = require("util");
|
|
5
|
+
const _flatten = require("lodash.flatten");
|
|
6
|
+
const Hook = require("../Hook");
|
|
7
|
+
const Settings = require("../Settings");
|
|
8
|
+
const Property = require("../Property");
|
|
9
|
+
const Error_1 = require("../Error");
|
|
10
|
+
const Utilities = require("../Utilities");
|
|
11
|
+
const _utils_1 = require("./_utils");
|
|
12
|
+
const Model_1 = require("../Model");
|
|
13
|
+
const Helpers = require("../Helpers");
|
|
14
|
+
function noOperation(...args) { }
|
|
15
|
+
;
|
|
16
|
+
function throwErrorIfExtraPropertyNameRepeat(hostModel, extraProps) {
|
|
17
|
+
Object.entries(extraProps).forEach(([eKey]) => {
|
|
18
|
+
if (hostModel.allProperties[eKey]) {
|
|
19
|
+
throw new Error_1.default(`disallow defining same name extra property '${eKey}' with property on model '${hostModel.name}'`, 'BAD_MODEL', { repeatKey: eKey });
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function prepare(Model, assocs, opts) {
|
|
24
|
+
const { many_associations } = assocs;
|
|
25
|
+
const { db } = opts;
|
|
26
|
+
Model.hasMany = function () {
|
|
27
|
+
Utilities.disAllowOpForVModel(Model, 'model.hasMany');
|
|
28
|
+
let name, makeKey, mergeId, mergeAssocId;
|
|
29
|
+
let OtherModel = Model;
|
|
30
|
+
let props = null;
|
|
31
|
+
let assoc_options = {};
|
|
32
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
33
|
+
switch (typeof arguments[i]) {
|
|
34
|
+
case 'string':
|
|
35
|
+
name = arguments[i];
|
|
36
|
+
break;
|
|
37
|
+
case 'function':
|
|
38
|
+
OtherModel = arguments[i];
|
|
39
|
+
break;
|
|
40
|
+
case 'object':
|
|
41
|
+
if (props === null) {
|
|
42
|
+
props = arguments[i];
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
assoc_options = arguments[i];
|
|
46
|
+
}
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
Utilities.disAllowOpForVModel(OtherModel, 'associated by model.hasMany');
|
|
51
|
+
for (let i = 0; i < db.plugins.length; i++) {
|
|
52
|
+
if (typeof db.plugins[i].beforeHasMany === "function") {
|
|
53
|
+
db.plugins[i].beforeHasMany(Model, {
|
|
54
|
+
association_name: name,
|
|
55
|
+
ext_model: OtherModel,
|
|
56
|
+
assoc_props: props,
|
|
57
|
+
assoc_options
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (props === null) {
|
|
62
|
+
props = {};
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
for (let k in props) {
|
|
66
|
+
props[k] = Property.normalize({
|
|
67
|
+
prop: props[k],
|
|
68
|
+
name: k,
|
|
69
|
+
customTypes: db.customTypes,
|
|
70
|
+
settings: Model.settings
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// restrain association.props[any].name not same with Model[any].name
|
|
75
|
+
throwErrorIfExtraPropertyNameRepeat(OtherModel, props);
|
|
76
|
+
makeKey = assoc_options.key || Settings.defaults().hasMany.key;
|
|
77
|
+
mergeId = Utilities.convertPropToJoinKeyProp(Utilities.wrapFieldObject({
|
|
78
|
+
field: assoc_options.mergeId, model: Model, altName: Model.table
|
|
79
|
+
}) ||
|
|
80
|
+
Utilities.formatAssociatedField(Model, Model.table, true, assoc_options.reversed), { makeKey: makeKey, required: true });
|
|
81
|
+
mergeAssocId = Utilities.convertPropToJoinKeyProp(Utilities.wrapFieldObject({
|
|
82
|
+
field: assoc_options.mergeAssocId, model: OtherModel, altName: name
|
|
83
|
+
}) ||
|
|
84
|
+
Utilities.formatAssociatedField(OtherModel, name, true, assoc_options.reversed), { makeKey: makeKey, required: true });
|
|
85
|
+
const associationSemanticNameCore = assoc_options.name || Utilities.formatNameFor("assoc:hasMany", name);
|
|
86
|
+
const fieldhash = Utilities.wrapFieldObject({ field: assoc_options.field, model: OtherModel, altName: Model.table }) || Utilities.formatAssociatedField(Model, name, true, assoc_options.reversed);
|
|
87
|
+
const association = {
|
|
88
|
+
name: name,
|
|
89
|
+
model: OtherModel,
|
|
90
|
+
props: props,
|
|
91
|
+
autoFetch: assoc_options.autoFetch || false,
|
|
92
|
+
autoFetchLimit: assoc_options.autoFetchLimit || 2,
|
|
93
|
+
// I'm not sure the next key is used..
|
|
94
|
+
field: fieldhash,
|
|
95
|
+
mergeTable: assoc_options.mergeTable || (Model.table + "_" + name),
|
|
96
|
+
mergeId: mergeId,
|
|
97
|
+
mergeAssocId: mergeAssocId,
|
|
98
|
+
getAccessor: assoc_options.getAccessor || (_utils_1.ACCESSOR_KEYS.get + associationSemanticNameCore),
|
|
99
|
+
setAccessor: assoc_options.setAccessor || (_utils_1.ACCESSOR_KEYS.set + associationSemanticNameCore),
|
|
100
|
+
hasAccessor: assoc_options.hasAccessor || (_utils_1.ACCESSOR_KEYS.has + associationSemanticNameCore),
|
|
101
|
+
delAccessor: assoc_options.delAccessor || (_utils_1.ACCESSOR_KEYS.del + associationSemanticNameCore),
|
|
102
|
+
addAccessor: assoc_options.addAccessor || (_utils_1.ACCESSOR_KEYS.add + associationSemanticNameCore),
|
|
103
|
+
modelFindByAccessor: assoc_options.modelFindByAccessor || (_utils_1.ACCESSOR_KEYS.modelFindBy + associationSemanticNameCore),
|
|
104
|
+
hooks: Object.assign({}, assoc_options.hooks),
|
|
105
|
+
};
|
|
106
|
+
Utilities.fillSyncVersionAccessorForAssociation(association);
|
|
107
|
+
Utilities.addHookPatchHelperForAssociation(association);
|
|
108
|
+
many_associations.push(association);
|
|
109
|
+
if (assoc_options.reverse) {
|
|
110
|
+
OtherModel.hasMany(assoc_options.reverse, Model, association.props, {
|
|
111
|
+
reversed: true,
|
|
112
|
+
association: assoc_options.reverseAssociation,
|
|
113
|
+
mergeTable: association.mergeTable,
|
|
114
|
+
mergeId: association.mergeAssocId,
|
|
115
|
+
mergeAssocId: association.mergeId,
|
|
116
|
+
field: fieldhash,
|
|
117
|
+
autoFetch: association.autoFetch,
|
|
118
|
+
autoFetchLimit: association.autoFetchLimit,
|
|
119
|
+
hooks: assoc_options.reverseHooks
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
const findByAccessorChainOrRunSync = function (is_sync = false) {
|
|
123
|
+
return function () {
|
|
124
|
+
var cb = null, conditions = null, right_find_opts = null;
|
|
125
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
126
|
+
switch (typeof arguments[i]) {
|
|
127
|
+
case "function":
|
|
128
|
+
cb = arguments[i];
|
|
129
|
+
break;
|
|
130
|
+
case "object":
|
|
131
|
+
if (conditions === null) {
|
|
132
|
+
conditions = Object.assign({}, arguments[i]);
|
|
133
|
+
}
|
|
134
|
+
else if (right_find_opts === null) {
|
|
135
|
+
right_find_opts = arguments[i];
|
|
136
|
+
}
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (conditions === null) {
|
|
141
|
+
throw new Error_1.default(`.${association.modelFindByAccessor}() is missing a conditions object`, 'PARAM_MISMATCH');
|
|
142
|
+
}
|
|
143
|
+
right_find_opts = right_find_opts || {};
|
|
144
|
+
const extraWhere = Utilities.extractHasManyExtraConditions(association, conditions);
|
|
145
|
+
// only set exists conditions when extraWhere is not empty
|
|
146
|
+
if (Object.keys(extraWhere).length > 0) {
|
|
147
|
+
// always set it only 1 element
|
|
148
|
+
right_find_opts.exists = [{
|
|
149
|
+
table: '',
|
|
150
|
+
link: ['', ''],
|
|
151
|
+
conditions: {}
|
|
152
|
+
}];
|
|
153
|
+
right_find_opts.exists.forEach(exists => {
|
|
154
|
+
exists.table = association.mergeTable;
|
|
155
|
+
// TODO: how to link complex associated fields?
|
|
156
|
+
exists.link = [
|
|
157
|
+
(0, _utils_1.getMapsToFromPropertyHash)(association.mergeAssocId)[0],
|
|
158
|
+
association.model.keys[0]
|
|
159
|
+
];
|
|
160
|
+
exists.conditions = extraWhere;
|
|
161
|
+
if (!Object.keys(exists.conditions).length) {
|
|
162
|
+
throw new Error_1.default(`"assocConditions.exists[number].conditions" is required on ".${association.modelFindByAccessor}(conds, assocConditions)"!`, 'PARAM_MISMATCH');
|
|
163
|
+
}
|
|
164
|
+
return exists;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
return (0, Model_1.listFindByChainOrRunSync)(Model, {}, [
|
|
168
|
+
{
|
|
169
|
+
association_name: association.name,
|
|
170
|
+
conditions: conditions
|
|
171
|
+
},
|
|
172
|
+
], right_find_opts, { callback: cb, is_sync });
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
Model[association.modelFindByAccessor] = findByAccessorChainOrRunSync();
|
|
176
|
+
Model[association.modelFindBySyncAccessor] = findByAccessorChainOrRunSync(true);
|
|
177
|
+
(0, _utils_1.addAssociationInfoToModel)(Model, name, {
|
|
178
|
+
type: 'hasMany',
|
|
179
|
+
association: association
|
|
180
|
+
});
|
|
181
|
+
return this;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
exports.prepare = prepare;
|
|
185
|
+
;
|
|
186
|
+
function extend(Model, Instance, Driver, associations, cfg) {
|
|
187
|
+
for (let i = 0; i < associations.length; i++) {
|
|
188
|
+
extendInstance(Model, Instance, Driver, associations[i], cfg);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
exports.extend = extend;
|
|
192
|
+
;
|
|
193
|
+
function autoFetch(Instance, associations, opts, parallel = false) {
|
|
194
|
+
if (associations.length === 0)
|
|
195
|
+
return;
|
|
196
|
+
Utilities.parallelQueryIfPossible(parallel, associations, (item) => autoFetchInstance(Instance, item, opts));
|
|
197
|
+
}
|
|
198
|
+
exports.autoFetch = autoFetch;
|
|
199
|
+
;
|
|
200
|
+
function adjustForMapsTo(properties, field) {
|
|
201
|
+
if (!field)
|
|
202
|
+
return;
|
|
203
|
+
/**
|
|
204
|
+
* Loop through the (cloned) association model id fields ... some of them may've been mapped to different
|
|
205
|
+
* names in the actual database - if so update to the mapped database column name
|
|
206
|
+
*/
|
|
207
|
+
for (let i = 0; i < field.length; i++) {
|
|
208
|
+
var idProp = properties[field[i]];
|
|
209
|
+
if (idProp && idProp.mapsTo) {
|
|
210
|
+
field[i] = idProp.mapsTo;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function mapKeysToString(keys, item) {
|
|
215
|
+
return util.map(keys, function (k) {
|
|
216
|
+
return item[k];
|
|
217
|
+
}).join(',');
|
|
218
|
+
}
|
|
219
|
+
function extendInstance(Model, Instance, Driver, association, cfg) {
|
|
220
|
+
if (Model.settings.get("instance.cascadeRemove")) {
|
|
221
|
+
Instance.on("beforeRemove", function () {
|
|
222
|
+
Instance[association.delAccessor]();
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.hasSyncAccessor, function (...Instances) {
|
|
226
|
+
var conditions = {}, join_conditions = {}, options = {};
|
|
227
|
+
if (Instances.length)
|
|
228
|
+
if (Array.isArray(Instances[0]))
|
|
229
|
+
Instances = Instances[0];
|
|
230
|
+
// if (Driver.hasMany) {
|
|
231
|
+
// return Driver.hasMany(Model, association)
|
|
232
|
+
// .has(Instance, Instances, conditions);
|
|
233
|
+
// }
|
|
234
|
+
options.autoFetchLimit = 0;
|
|
235
|
+
options.__merge = {
|
|
236
|
+
from: { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
|
|
237
|
+
to: { table: association.model.table, field: association.model.id.slice(0) },
|
|
238
|
+
where: [association.mergeTable, join_conditions],
|
|
239
|
+
table: association.model.table,
|
|
240
|
+
select: []
|
|
241
|
+
};
|
|
242
|
+
adjustForMapsTo(association.model.properties, options.__merge.to.field);
|
|
243
|
+
options.extra = association.props;
|
|
244
|
+
options.extra_info = {
|
|
245
|
+
table: association.mergeTable,
|
|
246
|
+
id: Utilities.values(Instance, Model.id),
|
|
247
|
+
id_prop: Object.keys(association.mergeId),
|
|
248
|
+
assoc_prop: Object.keys(association.mergeAssocId)
|
|
249
|
+
};
|
|
250
|
+
Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, options.__merge.where[1]);
|
|
251
|
+
for (let i = 0; i < Instances.length; i++) {
|
|
252
|
+
Utilities.populateModelIdKeysConditions(association.model, Object.keys(association.mergeAssocId), Instances[i], options.__merge.where[1], false);
|
|
253
|
+
}
|
|
254
|
+
const foundItems = association.model.findSync(conditions, options);
|
|
255
|
+
if (util.isEmpty(Instances))
|
|
256
|
+
return foundItems.length > 0;
|
|
257
|
+
var foundItemsIDs = Array.from(new Set(foundItems.map(item => mapKeysToString(association.model.keys, item))));
|
|
258
|
+
var InstancesIDs = Array.from(new Set(Instances.map(item => mapKeysToString(association.model.keys, item))));
|
|
259
|
+
var sameLength = foundItemsIDs.length === InstancesIDs.length;
|
|
260
|
+
var sameContents = sameLength && util.isEmpty(util.difference(foundItemsIDs, InstancesIDs));
|
|
261
|
+
return sameContents;
|
|
262
|
+
});
|
|
263
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.hasAccessor, function (...Instances) {
|
|
264
|
+
let cb;
|
|
265
|
+
if (typeof util.last(Instances) === 'function')
|
|
266
|
+
cb = Instances.pop();
|
|
267
|
+
process.nextTick(() => {
|
|
268
|
+
const syncResponse = Utilities.catchBlocking(Instance[association.hasSyncAccessor], Instances);
|
|
269
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
270
|
+
});
|
|
271
|
+
return this;
|
|
272
|
+
});
|
|
273
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.getAccessor, function () {
|
|
274
|
+
let options = {};
|
|
275
|
+
let conditions = null;
|
|
276
|
+
let order = null;
|
|
277
|
+
let cb = null;
|
|
278
|
+
Helpers.selectArgs(arguments, function (arg_type, arg) {
|
|
279
|
+
switch (arg_type) {
|
|
280
|
+
case "function":
|
|
281
|
+
cb = arg;
|
|
282
|
+
break;
|
|
283
|
+
case "object":
|
|
284
|
+
if (Array.isArray(arg)) {
|
|
285
|
+
order = arg;
|
|
286
|
+
order[0] = [association.model.table, order[0]];
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
if (conditions === null) {
|
|
290
|
+
conditions = Object.assign({}, arg);
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
options = arg;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
break;
|
|
297
|
+
case "string":
|
|
298
|
+
if (arg[0] == "-") {
|
|
299
|
+
order = [[association.model.table, arg.substr(1)], "Z"];
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
order = [[association.model.table, arg]];
|
|
303
|
+
}
|
|
304
|
+
break;
|
|
305
|
+
case "number":
|
|
306
|
+
options.limit = arg;
|
|
307
|
+
break;
|
|
308
|
+
}
|
|
309
|
+
});
|
|
310
|
+
if (order !== null) {
|
|
311
|
+
options.order = order;
|
|
312
|
+
}
|
|
313
|
+
if (conditions === null)
|
|
314
|
+
conditions = {};
|
|
315
|
+
// extract extra info on conditions
|
|
316
|
+
// populate out conditions in extra, not belonging to either one model of associations
|
|
317
|
+
let extraWhere = Utilities.extractHasManyExtraConditions(association, conditions);
|
|
318
|
+
extraWhere = Object.assign(Object.assign({}, options.join_where), extraWhere);
|
|
319
|
+
options.__merge = {
|
|
320
|
+
from: { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
|
|
321
|
+
to: { table: association.model.table, field: association.model.id.slice(0) },
|
|
322
|
+
where: [association.mergeTable, extraWhere],
|
|
323
|
+
table: association.model.table,
|
|
324
|
+
select: []
|
|
325
|
+
};
|
|
326
|
+
adjustForMapsTo(association.model.properties, options.__merge.to.field);
|
|
327
|
+
options.extra = association.props;
|
|
328
|
+
options.extra_info = {
|
|
329
|
+
table: association.mergeTable,
|
|
330
|
+
id: Utilities.values(Instance, Model.id),
|
|
331
|
+
id_prop: Object.keys(association.mergeId),
|
|
332
|
+
assoc_prop: Object.keys(association.mergeAssocId)
|
|
333
|
+
};
|
|
334
|
+
Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, options.__merge.where[1]);
|
|
335
|
+
if (cb === null)
|
|
336
|
+
return association.model.find(conditions, options);
|
|
337
|
+
association.model.find(conditions, options, cb);
|
|
338
|
+
return this;
|
|
339
|
+
});
|
|
340
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.getSyncAccessor, function (...args) {
|
|
341
|
+
args = args.filter(x => !util.isFunction(x));
|
|
342
|
+
const chain = Instance[association.getAccessor].apply(Instance, args);
|
|
343
|
+
return chain.runSync();
|
|
344
|
+
});
|
|
345
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.setSyncAccessor, function () {
|
|
346
|
+
const $ref = {
|
|
347
|
+
instance: Instance,
|
|
348
|
+
associations: _flatten(arguments),
|
|
349
|
+
useChannel: Utilities.reusableChannelGenerator()
|
|
350
|
+
};
|
|
351
|
+
let results = [];
|
|
352
|
+
Hook.wait(Instance, association.hooks[`beforeSet`], Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
|
|
353
|
+
Instance.$emit(`before:set:${association.name}`, $ref.associations);
|
|
354
|
+
Instance.$emit(`before-del-extension:${association.setAccessor}`);
|
|
355
|
+
Instance[association.delSyncAccessor]();
|
|
356
|
+
Instance.$emit(`after-del-extension:${association.setAccessor}`);
|
|
357
|
+
if (!$ref.associations.length)
|
|
358
|
+
return;
|
|
359
|
+
Instance.$emit(`before-add-extension:${association.setAccessor}`, $ref.associations);
|
|
360
|
+
results = Instance[association.addSyncAccessor]($ref.associations);
|
|
361
|
+
Instance.$emit(`after-add-extension:${association.setAccessor}`, $ref.associations);
|
|
362
|
+
Instance.$emit(`after:set:${association.name}`, $ref.associations);
|
|
363
|
+
if (Instance.__instRtd.keys.length === 1) {
|
|
364
|
+
const [key] = Instance.__instRtd.keys;
|
|
365
|
+
$ref.association_ids = $ref.associations.map((x) => x[key]);
|
|
366
|
+
}
|
|
367
|
+
}), Utilities.buildAssocHooksContext('beforeSet', { $ref }));
|
|
368
|
+
Hook.trigger(Instance, association.hooks[`afterSet`], Utilities.buildAssocHooksContext('afterSet', { $ref }));
|
|
369
|
+
return results;
|
|
370
|
+
});
|
|
371
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.setAccessor, function () {
|
|
372
|
+
// TODO: shold allow passing `extra` as 2nd argument
|
|
373
|
+
const items = _flatten(arguments);
|
|
374
|
+
const cb = typeof util.last(items) === 'function' ? items.pop() : noOperation;
|
|
375
|
+
process.nextTick(() => {
|
|
376
|
+
const syncResponse = Utilities.catchBlocking(Instance[association.setSyncAccessor], items);
|
|
377
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
|
|
378
|
+
});
|
|
379
|
+
return this;
|
|
380
|
+
});
|
|
381
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.delAccessor, function (...args) {
|
|
382
|
+
var cb;
|
|
383
|
+
Helpers.selectArgs(args, (arg_type, arg, idx) => {
|
|
384
|
+
switch (arg_type) {
|
|
385
|
+
case "function":
|
|
386
|
+
cb = arg;
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
args = args.filter(x => x !== cb);
|
|
391
|
+
process.nextTick(() => {
|
|
392
|
+
const syncResponse = Utilities.catchBlocking(Instance[association.delSyncAccessor], args);
|
|
393
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
|
|
394
|
+
});
|
|
395
|
+
return this;
|
|
396
|
+
});
|
|
397
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.delSyncAccessor, function (...args) {
|
|
398
|
+
var Associations = [];
|
|
399
|
+
Helpers.selectArgs(args, (arg_type, arg) => {
|
|
400
|
+
switch (arg_type) {
|
|
401
|
+
case "object":
|
|
402
|
+
if (Array.isArray(arg)) {
|
|
403
|
+
Associations = Associations.concat(arg);
|
|
404
|
+
}
|
|
405
|
+
else if (arg.isInstance) {
|
|
406
|
+
Associations.push(arg);
|
|
407
|
+
}
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
});
|
|
411
|
+
const conditions = {};
|
|
412
|
+
Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, conditions);
|
|
413
|
+
if (!this.saved())
|
|
414
|
+
this.saveSync();
|
|
415
|
+
const $ref = {
|
|
416
|
+
instance: Instance,
|
|
417
|
+
associations: Associations,
|
|
418
|
+
removeConditions: conditions
|
|
419
|
+
};
|
|
420
|
+
Hook.wait(Instance, association.hooks[`beforeRemove`], Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
|
|
421
|
+
const Associations = $ref.associations;
|
|
422
|
+
Instance.$emit(`before:del:${association.name}`);
|
|
423
|
+
if (Driver.hasMany) {
|
|
424
|
+
return [
|
|
425
|
+
Driver.hasMany(Model, association).del(Instance, Associations),
|
|
426
|
+
Instance.$emit(`after:del:${association.name}`)
|
|
427
|
+
][0];
|
|
428
|
+
}
|
|
429
|
+
if (Associations.length === 0) {
|
|
430
|
+
return [
|
|
431
|
+
Driver.remove(association.mergeTable, $ref.removeConditions),
|
|
432
|
+
Instance.$emit(`after:del:${association.name}`)
|
|
433
|
+
][0];
|
|
434
|
+
}
|
|
435
|
+
for (let i = 0; i < Associations.length; i++) {
|
|
436
|
+
Utilities.populateModelIdKeysConditions(association.model, Object.keys(association.mergeAssocId), Associations[i], $ref.removeConditions, false);
|
|
437
|
+
}
|
|
438
|
+
Driver.remove(association.mergeTable, $ref.removeConditions);
|
|
439
|
+
Instance.$emit(`after:del:${association.name}`);
|
|
440
|
+
}), Utilities.buildAssocHooksContext('beforeRemove', { $ref }));
|
|
441
|
+
Hook.trigger(Instance, association.hooks[`afterRemove`], Utilities.buildAssocHooksContext('afterRemove', { $ref }));
|
|
442
|
+
return this;
|
|
443
|
+
});
|
|
444
|
+
const isExtraNonEmpty = function () {
|
|
445
|
+
return !!Object.keys(association.props).length;
|
|
446
|
+
};
|
|
447
|
+
const collectParamsForAdd = function (args) {
|
|
448
|
+
var Associations = [];
|
|
449
|
+
var add_opts = {};
|
|
450
|
+
Helpers.selectArgs(args, (arg_type, arg) => {
|
|
451
|
+
switch (arg_type) {
|
|
452
|
+
case "object":
|
|
453
|
+
if (Array.isArray(arg)) {
|
|
454
|
+
Associations = Associations.concat(arg);
|
|
455
|
+
}
|
|
456
|
+
else if (arg.isInstance) {
|
|
457
|
+
Associations.push(arg);
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
add_opts = arg;
|
|
461
|
+
}
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
if (Associations.length === 0) {
|
|
466
|
+
throw new Error_1.default("No associations defined", 'PARAM_MISMATCH', { model: Model.name });
|
|
467
|
+
}
|
|
468
|
+
return { Associations, add_opts };
|
|
469
|
+
};
|
|
470
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.addSyncAccessor, function () {
|
|
471
|
+
let args = Array.prototype.slice.apply(arguments);
|
|
472
|
+
const { Associations, add_opts } = collectParamsForAdd(args);
|
|
473
|
+
const savedAssociations = [];
|
|
474
|
+
const $ref = {
|
|
475
|
+
instance: Instance,
|
|
476
|
+
associations: Associations,
|
|
477
|
+
useChannel: Utilities.reusableChannelGenerator()
|
|
478
|
+
};
|
|
479
|
+
Hook.wait(Instance, association.hooks[`beforeAdd`], Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
|
|
480
|
+
Instance.$emit(`before:add:${association.name}`, $ref.associations);
|
|
481
|
+
Utilities.parallelQueryIfPossible(false, $ref.associations, (Association) => {
|
|
482
|
+
const saveAssociation = function (err) {
|
|
483
|
+
if (err)
|
|
484
|
+
throw err;
|
|
485
|
+
Association.saveSync();
|
|
486
|
+
const data = {};
|
|
487
|
+
for (let k in add_opts) {
|
|
488
|
+
if (association.props[k]) {
|
|
489
|
+
const mapsTo = association.props[k].mapsTo || k;
|
|
490
|
+
data[mapsTo] = Driver.propertyToValue ?
|
|
491
|
+
Driver.propertyToValue(add_opts[k], association.props[k])
|
|
492
|
+
: add_opts[k];
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, data);
|
|
496
|
+
Utilities.populateModelIdKeysConditions(association.model, Object.keys(association.mergeAssocId), Association, data);
|
|
497
|
+
Driver.insert(association.mergeTable, data, null);
|
|
498
|
+
savedAssociations.push(Association);
|
|
499
|
+
};
|
|
500
|
+
Instance.$emit(`before-association-save:${association.addAccessor}`, $ref.associations);
|
|
501
|
+
if (isExtraNonEmpty()) {
|
|
502
|
+
Hook.wait(Association, association.hooks.beforeSave, saveAssociation, add_opts);
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
Hook.wait(Association, association.hooks.beforeSave, saveAssociation);
|
|
506
|
+
}
|
|
507
|
+
$ref.associations = savedAssociations;
|
|
508
|
+
Instance.$emit(`after-association-save:${association.addAccessor}`, savedAssociations);
|
|
509
|
+
});
|
|
510
|
+
if (!this.saved())
|
|
511
|
+
this.saveSync();
|
|
512
|
+
Instance.$emit(`after:add:${association.name}`, savedAssociations);
|
|
513
|
+
}), Utilities.buildAssocHooksContext('beforeAdd', { $ref }));
|
|
514
|
+
Hook.trigger(Instance, association.hooks[`afterAdd`], Utilities.buildAssocHooksContext('afterAdd', { $ref }));
|
|
515
|
+
return savedAssociations;
|
|
516
|
+
});
|
|
517
|
+
Utilities.addHiddenUnwritableMethodToInstance(Instance, association.addAccessor, function () {
|
|
518
|
+
let args = Array.prototype.slice.apply(arguments);
|
|
519
|
+
const withExtraProps = isExtraNonEmpty();
|
|
520
|
+
let cb = null;
|
|
521
|
+
Helpers.selectArgs(args, (arg_type, arg) => {
|
|
522
|
+
switch (arg_type) {
|
|
523
|
+
case "function":
|
|
524
|
+
cb = arg;
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
args = args.filter(x => x !== cb);
|
|
529
|
+
collectParamsForAdd(args);
|
|
530
|
+
const errWaitor = Utilities.getErrWaitor(!cb);
|
|
531
|
+
process.nextTick(() => {
|
|
532
|
+
const syncResponse = Utilities.catchBlocking(Instance[association.addSyncAccessor], args);
|
|
533
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: !withExtraProps, callback: cb });
|
|
534
|
+
errWaitor.err = syncResponse.error;
|
|
535
|
+
if (errWaitor.evt)
|
|
536
|
+
errWaitor.evt.set();
|
|
537
|
+
});
|
|
538
|
+
if (errWaitor.evt)
|
|
539
|
+
errWaitor.evt.wait();
|
|
540
|
+
if (errWaitor.err)
|
|
541
|
+
throw errWaitor.err;
|
|
542
|
+
return this;
|
|
543
|
+
});
|
|
544
|
+
Object.defineProperty(Instance, association.name, {
|
|
545
|
+
get: function () {
|
|
546
|
+
return Instance.__instRtd.associations[association.name].value;
|
|
547
|
+
},
|
|
548
|
+
set: function (val) {
|
|
549
|
+
Instance.__instRtd.associations[association.name].changed = true;
|
|
550
|
+
Instance.__instRtd.associations[association.name].value = val;
|
|
551
|
+
},
|
|
552
|
+
enumerable: true
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
function autoFetchInstance(Instance, association, opts) {
|
|
556
|
+
if (!Instance.saved())
|
|
557
|
+
return;
|
|
558
|
+
if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
|
|
559
|
+
opts.autoFetchLimit = association.autoFetchLimit;
|
|
560
|
+
}
|
|
561
|
+
if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch))
|
|
562
|
+
return;
|
|
563
|
+
try {
|
|
564
|
+
const Assoc = Instance[association.getSyncAccessor]({}, { autoFetchLimit: opts.autoFetchLimit - 1 });
|
|
565
|
+
// Set this way to prevent setting 'changed' status
|
|
566
|
+
Instance.__instRtd.associations[association.name].value = Assoc;
|
|
567
|
+
}
|
|
568
|
+
catch (err) { }
|
|
569
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { FxOrmInstance } from '../Typo/instance';
|
|
2
|
+
import { FxOrmAssociation } from '../Typo/assoc';
|
|
3
|
+
import { FxOrmModel } from '../Typo/model';
|
|
4
|
+
import { FxOrmNS } from '../Typo/ORM';
|
|
5
|
+
import { FxOrmDMLDriver } from '../Typo/DMLDriver';
|
|
6
|
+
export declare function prepare(Model: FxOrmModel.Model, assocs: {
|
|
7
|
+
one_associations: FxOrmAssociation.InstanceAssociationItem_HasOne[];
|
|
8
|
+
many_associations: FxOrmAssociation.InstanceAssociationItem_HasMany[];
|
|
9
|
+
extend_associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[];
|
|
10
|
+
}, opts: {
|
|
11
|
+
db: FxOrmNS.ORM;
|
|
12
|
+
}): void;
|
|
13
|
+
export declare function extend(Model: FxOrmModel.Model, Instance: FxOrmInstance.Instance, Driver: FxOrmDMLDriver.DMLDriver, associations: FxOrmAssociation.InstanceAssociationItem_HasOne[], cfg: {
|
|
14
|
+
assoc_opts: any;
|
|
15
|
+
}): void;
|
|
16
|
+
export declare function autoFetch(Instance: FxOrmInstance.Instance, associations: FxOrmAssociation.InstanceAssociationItem[], opts: FxOrmAssociation.AutoFetchInstanceOptions, parallel: boolean): void;
|