fib-orm 1.4.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (295) hide show
  1. package/License +1 -1
  2. package/README.md +53 -0
  3. package/lib/cli/commands/ddlFrom.d.ts +2 -0
  4. package/lib/cli/commands/ddlFrom.js +75 -0
  5. package/lib/cli/commands/dumpModel.d.ts +2 -0
  6. package/lib/cli/commands/dumpModel.js +107 -0
  7. package/lib/cli/commands/escape.d.ts +2 -0
  8. package/lib/cli/commands/escape.js +69 -0
  9. package/lib/cli/commands/spinner.d.ts +2 -0
  10. package/lib/cli/commands/spinner.js +37 -0
  11. package/lib/cli/commands/upgrade.d.ts +2 -0
  12. package/lib/cli/commands/upgrade.js +66 -0
  13. package/lib/cli/helpers/ddl.d.ts +17 -0
  14. package/lib/cli/helpers/ddl.js +55 -0
  15. package/lib/cli/helpers/package.d.ts +25 -0
  16. package/lib/cli/helpers/package.js +56 -0
  17. package/lib/cli/index.d.ts +2 -0
  18. package/lib/cli/index.js +19 -0
  19. package/lib/codepoints-map.json +762 -0
  20. package/lib/db-driver/Typo.d.ts +111 -0
  21. package/lib/db-driver/Typo.js +2 -0
  22. package/lib/db-driver/built-ins/base.class.d.ts +91 -0
  23. package/lib/db-driver/built-ins/base.class.js +187 -0
  24. package/lib/db-driver/built-ins/driver-dm.d.ts +17 -0
  25. package/lib/db-driver/built-ins/driver-dm.js +54 -0
  26. package/lib/db-driver/built-ins/driver-mongodb.d.ts +23 -0
  27. package/lib/db-driver/built-ins/driver-mongodb.js +47 -0
  28. package/lib/db-driver/built-ins/driver-mysql.d.ts +17 -0
  29. package/lib/db-driver/built-ins/driver-mysql.js +38 -0
  30. package/lib/db-driver/built-ins/driver-postgresql.d.ts +21 -0
  31. package/lib/db-driver/built-ins/driver-postgresql.js +77 -0
  32. package/lib/db-driver/built-ins/driver-redis.d.ts +11 -0
  33. package/lib/db-driver/built-ins/driver-redis.js +36 -0
  34. package/lib/db-driver/built-ins/driver-sqlite.d.ts +16 -0
  35. package/lib/db-driver/built-ins/driver-sqlite.js +35 -0
  36. package/lib/db-driver/index.d.ts +3 -0
  37. package/lib/db-driver/index.js +5 -0
  38. package/lib/db-driver/utils.d.ts +14 -0
  39. package/lib/db-driver/utils.js +187 -0
  40. package/lib/knex/deps/tarn/dist/PendingOperation.d.ts +12 -0
  41. package/lib/knex/deps/tarn/dist/PendingOperation.js +48 -0
  42. package/lib/knex/deps/tarn/dist/Pool.d.ts +97 -0
  43. package/lib/knex/deps/tarn/dist/Pool.js +516 -0
  44. package/lib/knex/deps/tarn/dist/PromiseInspection.d.ts +16 -0
  45. package/lib/knex/deps/tarn/dist/PromiseInspection.js +21 -0
  46. package/lib/knex/deps/tarn/dist/Resource.d.ts +9 -0
  47. package/lib/knex/deps/tarn/dist/Resource.js +19 -0
  48. package/lib/knex/deps/tarn/dist/TimeoutError.d.ts +2 -0
  49. package/lib/knex/deps/tarn/dist/TimeoutError.js +5 -0
  50. package/lib/knex/deps/tarn/dist/tarn.d.ts +3 -0
  51. package/lib/knex/deps/tarn/dist/tarn.js +10 -0
  52. package/lib/knex/deps/tarn/dist/utils.d.ts +14 -0
  53. package/lib/knex/deps/tarn/dist/utils.js +60 -0
  54. package/lib/knex/knex.d.ts +2 -0
  55. package/lib/knex/knex.js +4 -0
  56. package/lib/knex/sandbox.d.ts +4 -0
  57. package/lib/knex/sandbox.js +23 -0
  58. package/lib/orm/Adapters.d.ts +3 -0
  59. package/lib/orm/Adapters.js +44 -0
  60. package/lib/orm/AggregateFunctions.d.ts +3 -0
  61. package/lib/orm/AggregateFunctions.js +177 -0
  62. package/lib/orm/Associations/Extend.d.ts +22 -0
  63. package/lib/orm/Associations/Extend.js +274 -0
  64. package/lib/orm/Associations/Many.d.ts +16 -0
  65. package/lib/orm/Associations/Many.js +569 -0
  66. package/lib/orm/Associations/One.d.ts +16 -0
  67. package/lib/orm/Associations/One.js +310 -0
  68. package/lib/orm/Associations/_utils.d.ts +14 -0
  69. package/lib/orm/Associations/_utils.js +53 -0
  70. package/lib/orm/ChainFind.d.ts +3 -0
  71. package/lib/orm/ChainFind.js +328 -0
  72. package/lib/orm/ChainInstance.d.ts +6 -0
  73. package/lib/orm/ChainInstance.js +74 -0
  74. package/lib/orm/Debug.d.ts +2 -0
  75. package/lib/orm/Debug.js +17 -0
  76. package/lib/orm/Drivers/DB/base.d.ts +2 -0
  77. package/lib/orm/Drivers/DB/base.js +4 -0
  78. package/lib/orm/Drivers/DB/dm.d.ts +9 -0
  79. package/lib/orm/Drivers/DB/dm.js +24 -0
  80. package/lib/orm/Drivers/DB/mysql.d.ts +9 -0
  81. package/lib/orm/Drivers/DB/mysql.js +24 -0
  82. package/lib/orm/Drivers/DB/postgres.d.ts +9 -0
  83. package/lib/orm/Drivers/DB/postgres.js +23 -0
  84. package/lib/orm/Drivers/DB/sqlite3.d.ts +14 -0
  85. package/lib/orm/Drivers/DB/sqlite3.js +40 -0
  86. package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
  87. package/lib/orm/Drivers/DML/_ddl-sql.js +52 -0
  88. package/lib/orm/Drivers/DML/_dml-helpers.d.ts +19 -0
  89. package/lib/orm/Drivers/DML/_dml-helpers.js +51 -0
  90. package/lib/orm/Drivers/DML/_shared.d.ts +9 -0
  91. package/lib/orm/Drivers/DML/_shared.js +39 -0
  92. package/lib/orm/Drivers/DML/_utils.d.ts +8 -0
  93. package/lib/orm/Drivers/DML/_utils.js +67 -0
  94. package/lib/orm/Drivers/DML/constants.d.ts +3 -0
  95. package/lib/orm/Drivers/DML/constants.js +3 -0
  96. package/lib/orm/Drivers/DML/dm.d.ts +2 -0
  97. package/lib/orm/Drivers/DML/dm.js +294 -0
  98. package/lib/orm/Drivers/DML/index.d.ts +4 -0
  99. package/lib/orm/Drivers/DML/index.js +7 -0
  100. package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
  101. package/lib/orm/Drivers/DML/mysql.js +252 -0
  102. package/lib/orm/Drivers/DML/postgres.d.ts +2 -0
  103. package/lib/orm/Drivers/DML/postgres.js +277 -0
  104. package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
  105. package/lib/orm/Drivers/DML/sqlite.js +267 -0
  106. package/lib/orm/Error.d.ts +11 -0
  107. package/lib/orm/Error.js +41 -0
  108. package/lib/orm/Helpers.d.ts +35 -0
  109. package/lib/orm/Helpers.js +234 -0
  110. package/lib/orm/Hook.d.ts +13 -0
  111. package/lib/orm/Hook.js +100 -0
  112. package/lib/orm/Instance.d.ts +2 -0
  113. package/lib/orm/Instance.js +698 -0
  114. package/lib/orm/LazyLoad.d.ts +4 -0
  115. package/lib/orm/LazyLoad.js +76 -0
  116. package/lib/orm/Model.d.ts +6 -0
  117. package/lib/orm/Model.js +974 -0
  118. package/lib/orm/ORM.d.ts +37 -0
  119. package/lib/orm/ORM.js +242 -0
  120. package/lib/orm/Patch/fib-cache.d.ts +39 -0
  121. package/lib/orm/Patch/fib-cache.js +2 -0
  122. package/lib/orm/Patch/plugin.d.ts +4 -0
  123. package/lib/orm/Patch/plugin.js +14 -0
  124. package/lib/orm/Property.d.ts +9 -0
  125. package/lib/orm/Property.js +71 -0
  126. package/lib/orm/Settings.d.ts +42 -0
  127. package/lib/orm/Settings.js +120 -0
  128. package/lib/orm/Singleton.d.ts +5 -0
  129. package/lib/orm/Singleton.js +73 -0
  130. package/lib/orm/Typo/Adapter.d.ts +5 -0
  131. package/lib/orm/Typo/Adapter.js +2 -0
  132. package/lib/orm/Typo/DMLDriver.d.ts +209 -0
  133. package/lib/orm/Typo/DMLDriver.js +2 -0
  134. package/lib/orm/Typo/Db.d.ts +35 -0
  135. package/lib/orm/Typo/Db.js +2 -0
  136. package/lib/orm/Typo/Error.d.ts +29 -0
  137. package/lib/orm/Typo/Error.js +2 -0
  138. package/lib/orm/Typo/ORM.d.ts +231 -0
  139. package/lib/orm/Typo/ORM.js +4 -0
  140. package/lib/orm/Typo/Validators.d.ts +31 -0
  141. package/lib/orm/Typo/Validators.js +3 -0
  142. package/lib/orm/Typo/_common.d.ts +37 -0
  143. package/lib/orm/Typo/_common.js +2 -0
  144. package/lib/orm/Typo/assoc.d.ts +221 -0
  145. package/lib/orm/Typo/assoc.js +2 -0
  146. package/lib/orm/Typo/hook.d.ts +42 -0
  147. package/lib/orm/Typo/hook.js +2 -0
  148. package/lib/orm/Typo/instance.d.ts +156 -0
  149. package/lib/orm/Typo/instance.js +2 -0
  150. package/lib/orm/Typo/model.d.ts +311 -0
  151. package/lib/orm/Typo/model.js +7 -0
  152. package/lib/orm/Typo/patch.d.ts +31 -0
  153. package/lib/orm/Typo/patch.js +2 -0
  154. package/lib/orm/Typo/property.d.ts +57 -0
  155. package/lib/orm/Typo/property.js +2 -0
  156. package/lib/orm/Typo/query.d.ts +216 -0
  157. package/lib/orm/Typo/query.js +2 -0
  158. package/lib/orm/Typo/settings.d.ts +35 -0
  159. package/lib/orm/Typo/settings.js +6 -0
  160. package/lib/orm/Typo/synchronous.d.ts +47 -0
  161. package/lib/orm/Typo/synchronous.js +2 -0
  162. package/lib/orm/Utilities.d.ts +178 -0
  163. package/lib/orm/Utilities.js +975 -0
  164. package/lib/orm/Validators.d.ts +33 -0
  165. package/lib/orm/Validators.js +101 -0
  166. package/lib/orm/Where/common.d.ts +1 -0
  167. package/lib/orm/Where/common.js +7 -0
  168. package/lib/orm/Where/filterDate.d.ts +11 -0
  169. package/lib/orm/Where/filterDate.js +58 -0
  170. package/lib/orm/index.d.ts +67 -0
  171. package/lib/orm/index.js +158 -0
  172. package/lib/orm-core/callback.d.ts +12 -0
  173. package/lib/orm-core/callback.js +2 -0
  174. package/lib/orm-core/dialect.d.ts +80 -0
  175. package/lib/orm-core/dialect.js +2 -0
  176. package/lib/orm-core/error.d.ts +16 -0
  177. package/lib/orm-core/error.js +29 -0
  178. package/lib/orm-core/index.d.ts +6 -0
  179. package/lib/orm-core/index.js +7 -0
  180. package/lib/orm-core/sync.d.ts +12 -0
  181. package/lib/orm-core/sync.js +2 -0
  182. package/lib/orm-core/utils.d.ts +1 -0
  183. package/lib/orm-core/utils.js +6 -0
  184. package/lib/orm-property/Property.d.ts +78 -0
  185. package/lib/orm-property/Property.js +2 -0
  186. package/lib/orm-property/Utils.d.ts +9 -0
  187. package/lib/orm-property/Utils.js +13 -0
  188. package/lib/orm-property/customTypes.d.ts +2 -0
  189. package/lib/orm-property/customTypes.js +13 -0
  190. package/lib/orm-property/index.d.ts +10 -0
  191. package/lib/orm-property/index.js +23 -0
  192. package/lib/orm-property/transformer.d.ts +5 -0
  193. package/lib/orm-property/transformer.js +10 -0
  194. package/lib/orm-property/transformers/dm.d.ts +29 -0
  195. package/lib/orm-property/transformers/dm.js +238 -0
  196. package/lib/orm-property/transformers/index.d.ts +4 -0
  197. package/lib/orm-property/transformers/index.js +6 -0
  198. package/lib/orm-property/transformers/mysql.d.ts +101 -0
  199. package/lib/orm-property/transformers/mysql.js +262 -0
  200. package/lib/orm-property/transformers/postgresql.d.ts +54 -0
  201. package/lib/orm-property/transformers/postgresql.js +190 -0
  202. package/lib/orm-property/transformers/sqlite.d.ts +19 -0
  203. package/lib/orm-property/transformers/sqlite.js +172 -0
  204. package/lib/sql-ddl-sync/Dialects/dm.d.ts +39 -0
  205. package/lib/sql-ddl-sync/Dialects/dm.js +297 -0
  206. package/lib/sql-ddl-sync/Dialects/index.d.ts +6 -0
  207. package/lib/sql-ddl-sync/Dialects/index.js +8 -0
  208. package/lib/sql-ddl-sync/Dialects/mysql.d.ts +38 -0
  209. package/lib/sql-ddl-sync/Dialects/mysql.js +241 -0
  210. package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +39 -0
  211. package/lib/sql-ddl-sync/Dialects/postgresql.js +287 -0
  212. package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +43 -0
  213. package/lib/sql-ddl-sync/Dialects/sqlite.js +257 -0
  214. package/lib/sql-ddl-sync/SQL.d.ts +12 -0
  215. package/lib/sql-ddl-sync/SQL.js +108 -0
  216. package/lib/sql-ddl-sync/Typo/Collection.d.ts +13 -0
  217. package/lib/sql-ddl-sync/Typo/Collection.js +2 -0
  218. package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
  219. package/lib/sql-ddl-sync/Typo/DbIndex.js +2 -0
  220. package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
  221. package/lib/sql-ddl-sync/Typo/Dialect.js +2 -0
  222. package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
  223. package/lib/sql-ddl-sync/Typo/Driver.js +2 -0
  224. package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
  225. package/lib/sql-ddl-sync/Typo/Query.js +2 -0
  226. package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
  227. package/lib/sql-ddl-sync/Typo/SQL.js +2 -0
  228. package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
  229. package/lib/sql-ddl-sync/Typo/_common.js +2 -0
  230. package/lib/sql-ddl-sync/Utils.d.ts +39 -0
  231. package/lib/sql-ddl-sync/Utils.js +217 -0
  232. package/lib/sql-ddl-sync/index.d.ts +87 -0
  233. package/lib/sql-ddl-sync/index.js +372 -0
  234. package/lib/sql-query/Comparators.d.ts +3 -0
  235. package/lib/sql-query/Comparators.js +95 -0
  236. package/lib/sql-query/Create.d.ts +35 -0
  237. package/lib/sql-query/Create.js +78 -0
  238. package/lib/sql-query/Dialects/dm.d.ts +3 -0
  239. package/lib/sql-query/Dialects/dm.js +75 -0
  240. package/lib/sql-query/Dialects/index.d.ts +10 -0
  241. package/lib/sql-query/Dialects/index.js +13 -0
  242. package/lib/sql-query/Dialects/mssql.d.ts +3 -0
  243. package/lib/sql-query/Dialects/mssql.js +74 -0
  244. package/lib/sql-query/Dialects/mysql.d.ts +3 -0
  245. package/lib/sql-query/Dialects/mysql.js +101 -0
  246. package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
  247. package/lib/sql-query/Dialects/postgresql.js +77 -0
  248. package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
  249. package/lib/sql-query/Dialects/sqlite.js +62 -0
  250. package/lib/sql-query/Helpers.d.ts +37 -0
  251. package/lib/sql-query/Helpers.js +260 -0
  252. package/lib/sql-query/Insert.d.ts +12 -0
  253. package/lib/sql-query/Insert.js +34 -0
  254. package/lib/sql-query/Query.d.ts +28 -0
  255. package/lib/sql-query/Query.js +120 -0
  256. package/lib/sql-query/Remove.d.ts +16 -0
  257. package/lib/sql-query/Remove.js +94 -0
  258. package/lib/sql-query/Select.d.ts +62 -0
  259. package/lib/sql-query/Select.js +534 -0
  260. package/lib/sql-query/Set.d.ts +4 -0
  261. package/lib/sql-query/Set.js +16 -0
  262. package/lib/sql-query/Typo/Aggregation.d.ts +32 -0
  263. package/lib/sql-query/Typo/Aggregation.js +2 -0
  264. package/lib/sql-query/Typo/Comparators.d.ts +140 -0
  265. package/lib/sql-query/Typo/Comparators.js +2 -0
  266. package/lib/sql-query/Typo/Dialect.d.ts +39 -0
  267. package/lib/sql-query/Typo/Dialect.js +2 -0
  268. package/lib/sql-query/Typo/Field.d.ts +8 -0
  269. package/lib/sql-query/Typo/Field.js +2 -0
  270. package/lib/sql-query/Typo/Helper.d.ts +4 -0
  271. package/lib/sql-query/Typo/Helper.js +2 -0
  272. package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
  273. package/lib/sql-query/Typo/Query-ChainBuilder.js +2 -0
  274. package/lib/sql-query/Typo/Query.d.ts +34 -0
  275. package/lib/sql-query/Typo/Query.js +17 -0
  276. package/lib/sql-query/Typo/Sql.d.ts +143 -0
  277. package/lib/sql-query/Typo/Sql.js +2 -0
  278. package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
  279. package/lib/sql-query/Typo/SubQuery.js +2 -0
  280. package/lib/sql-query/Typo/index.d.ts +9 -0
  281. package/lib/sql-query/Typo/index.js +2 -0
  282. package/lib/sql-query/Update.d.ts +15 -0
  283. package/lib/sql-query/Update.js +39 -0
  284. package/lib/sql-query/Where.d.ts +4 -0
  285. package/lib/sql-query/Where.js +280 -0
  286. package/package.json +71 -36
  287. package/.travis.yml +0 -14
  288. package/@types/index.d.ts +0 -254
  289. package/@types/orm_mirror/orm.d.ts +0 -275
  290. package/@types/orm_mirror/sql-query.d.ts +0 -79
  291. package/Readme.md +0 -843
  292. package/lib/index.js +0 -13
  293. package/lib/modules/mysql.js +0 -35
  294. package/lib/modules/sqlite3.js +0 -27
  295. package/lib/patch.js +0 -271
@@ -0,0 +1,310 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.prepare = prepare;
4
+ exports.extend = extend;
5
+ exports.autoFetch = autoFetch;
6
+ const util = require("util");
7
+ const Hook = require("../Hook");
8
+ const Utilities = require("../Utilities");
9
+ const Error_1 = require("../Error");
10
+ const _utils_1 = require("./_utils");
11
+ const Model_1 = require("../Model");
12
+ const Helpers = require("../Helpers");
13
+ function noOperation(...args) { }
14
+ ;
15
+ function prepare(Model, assocs, opts) {
16
+ const { one_associations } = assocs;
17
+ const { db } = opts;
18
+ Model.hasOne = function (assoc_name, ext_model, assoc_options) {
19
+ Utilities.disAllowOpForVModel(Model, 'model.hasOne');
20
+ if (arguments[1] && !arguments[1].table) {
21
+ assoc_options = arguments[1];
22
+ ext_model = arguments[1] = null;
23
+ }
24
+ ext_model = ext_model || Model;
25
+ assoc_name = assoc_name || ext_model.table;
26
+ assoc_options = Object.assign({}, assoc_options);
27
+ if (!assoc_name) {
28
+ throw new Error(`[hasOne] association's name is required!`);
29
+ }
30
+ else if (typeof assoc_name !== 'string') {
31
+ throw new Error(`[hasOne] association's name must be string!`);
32
+ }
33
+ Utilities.disAllowOpForVModel(ext_model, 'associated by model.hasOne');
34
+ for (let i = 0; i < db.plugins.length; i++) {
35
+ if (typeof db.plugins[i].beforeHasOne === "function") {
36
+ db.plugins[i].beforeHasOne(Model, {
37
+ association_name: assoc_name,
38
+ ext_model: ext_model,
39
+ assoc_options
40
+ });
41
+ }
42
+ }
43
+ const associationSemanticNameCore = Utilities.formatNameFor("assoc:hasOne", assoc_name);
44
+ const association = Object.assign(Object.assign({ name: assoc_name, model: ext_model, field: null, reversed: false, __for_extension: false, autoFetch: false, autoFetchLimit: 2, required: false, setAccessor: _utils_1.ACCESSOR_KEYS.set + associationSemanticNameCore, getAccessor: _utils_1.ACCESSOR_KEYS.get + associationSemanticNameCore, hasAccessor: _utils_1.ACCESSOR_KEYS.has + associationSemanticNameCore, delAccessor: null, modelFindByAccessor: assoc_options.modelFindByAccessor || (_utils_1.ACCESSOR_KEYS.modelFindBy + associationSemanticNameCore) }, assoc_options), { hooks: Object.assign({}, assoc_options.hooks) });
45
+ if (!association.reversed)
46
+ association.delAccessor = _utils_1.ACCESSOR_KEYS['del'] + associationSemanticNameCore;
47
+ Utilities.fillSyncVersionAccessorForAssociation(association);
48
+ Utilities.addHookPatchHelperForAssociation(association);
49
+ if (!association.field) {
50
+ association.field = Utilities.formatAssociatedField(association.model, association.name, association.required, association.reversed);
51
+ }
52
+ else if (!association.__for_extension) {
53
+ association.field = Utilities.wrapFieldObject({
54
+ field: association.field, model: Model, altName: Model.table,
55
+ mapsTo: association.mapsTo
56
+ });
57
+ }
58
+ const normalizedField = association.field;
59
+ Utilities.convertPropToJoinKeyProp(normalizedField, { makeKey: false, required: association.required });
60
+ one_associations.push(association);
61
+ for (let k in normalizedField) {
62
+ if (!normalizedField.hasOwnProperty(k)) {
63
+ continue;
64
+ }
65
+ if (!association.reversed && !association.__for_extension) {
66
+ Model.addProperty(util.extend({}, normalizedField[k], { klass: 'hasOne' }), false);
67
+ }
68
+ }
69
+ if (association.reverse) {
70
+ association.model.hasOne(association.reverse, Model, {
71
+ reversed: true,
72
+ accessor: association.reverseAccessor,
73
+ reverseAccessor: undefined,
74
+ field: normalizedField,
75
+ autoFetch: association.autoFetch,
76
+ autoFetchLimit: association.autoFetchLimit,
77
+ hooks: assoc_options.reverseHooks
78
+ });
79
+ }
80
+ const findByAccessorChainOrRunSync = function (is_sync = false) {
81
+ return function () {
82
+ var cb = null, conditions = null, options = {};
83
+ Helpers.selectArgs(arguments, (arg_type, arg) => {
84
+ switch (arg_type) {
85
+ case "function":
86
+ cb = arg;
87
+ break;
88
+ case "object":
89
+ if (conditions === null) {
90
+ conditions = arg;
91
+ }
92
+ else {
93
+ options = arg;
94
+ }
95
+ break;
96
+ }
97
+ });
98
+ if (conditions === null) {
99
+ throw new Error_1.default(`.${association.modelFindByAccessor}() is missing a conditions object`, 'PARAM_MISMATCH');
100
+ }
101
+ return (0, Model_1.listFindByChainOrRunSync)(Model, {}, [
102
+ {
103
+ association_name: association.name,
104
+ conditions: conditions
105
+ },
106
+ ], options, { callback: cb, is_sync });
107
+ };
108
+ };
109
+ Model[association.modelFindByAccessor] = findByAccessorChainOrRunSync();
110
+ Model[association.modelFindBySyncAccessor] = findByAccessorChainOrRunSync(true);
111
+ (0, _utils_1.addAssociationInfoToModel)(Model, assoc_name, {
112
+ type: 'hasOne',
113
+ association
114
+ });
115
+ return this;
116
+ };
117
+ }
118
+ ;
119
+ function extend(Model, Instance, Driver,
120
+ // extend target
121
+ associations, cfg) {
122
+ for (let i = 0; i < associations.length; i++) {
123
+ extendInstance(Model, Instance, Driver, associations[i], cfg);
124
+ }
125
+ }
126
+ ;
127
+ function autoFetch(Instance, associations, opts, parallel) {
128
+ if (associations.length === 0)
129
+ return;
130
+ Utilities.parallelQueryIfPossible(parallel, associations, (item) => autoFetchInstance(Instance, item, opts));
131
+ }
132
+ ;
133
+ function extendInstance(Model, Instance, Driver,
134
+ // extend target
135
+ association, cfg) {
136
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.hasSyncAccessor, function (_has_opts) {
137
+ if (!Utilities.hasValues(Instance, Object.keys(association.field)))
138
+ return false;
139
+ try {
140
+ const instance = association.model.getSync(Utilities.values(Instance, Object.keys(association.field)), _has_opts);
141
+ return !!instance;
142
+ }
143
+ catch (error) {
144
+ return false;
145
+ }
146
+ });
147
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.hasAccessor, function (_has_opts, cb) {
148
+ if (typeof _has_opts === "function") {
149
+ cb = _has_opts;
150
+ _has_opts = {};
151
+ }
152
+ process.nextTick(() => {
153
+ const syncResponse = Utilities.catchBlocking(Instance[association.hasSyncAccessor], [_has_opts]);
154
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
155
+ });
156
+ return this;
157
+ });
158
+ const getAccessorChainOrRunSync = function (opts = {}, withOutCallbackWhenNonSync) {
159
+ const saveAndReturn = function (Assoc) {
160
+ Instance[association.name] = Assoc;
161
+ return Assoc;
162
+ };
163
+ let result;
164
+ // process get reversed instance's original instance
165
+ if (association.reversed) {
166
+ let reverseHostInstances = null;
167
+ if (Utilities.hasValues(Instance, Model.id)) {
168
+ const query_conds = Utilities.getConditions(Model, Object.keys(association.field), Instance);
169
+ if (withOutCallbackWhenNonSync) {
170
+ return association.model.find.call(association.model, query_conds, opts);
171
+ }
172
+ reverseHostInstances = association.model.findSync(query_conds, opts);
173
+ result = saveAndReturn(reverseHostInstances);
174
+ }
175
+ return result;
176
+ }
177
+ let assocInstance = null;
178
+ if (Instance.isShell()) {
179
+ Instance = Model.getSync(Utilities.values(Instance, Model.id));
180
+ if (!Utilities.hasValues(Instance, Object.keys(association.field)))
181
+ return;
182
+ assocInstance = association.model.getSync(Utilities.values(Instance, Object.keys(association.field)), opts);
183
+ return saveAndReturn(assocInstance);
184
+ }
185
+ if (Utilities.hasValues(Instance, Object.keys(association.field))) {
186
+ assocInstance = association.model.getSync(Utilities.values(Instance, Object.keys(association.field)), opts);
187
+ return saveAndReturn(assocInstance);
188
+ }
189
+ };
190
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.getSyncAccessor, function (opts = {}) {
191
+ return getAccessorChainOrRunSync(opts, false);
192
+ });
193
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.getAccessor, function (opts, cb) {
194
+ if (typeof opts === "function") {
195
+ cb = opts;
196
+ opts = {};
197
+ }
198
+ const withCallback = typeof cb === 'function';
199
+ if (withCallback) {
200
+ process.nextTick(() => {
201
+ const syncResponse = Utilities.catchBlocking(getAccessorChainOrRunSync, [opts, !withCallback]);
202
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
203
+ });
204
+ return this;
205
+ }
206
+ return getAccessorChainOrRunSync(opts, !withCallback);
207
+ });
208
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.setSyncAccessor, function (OtherInstance) {
209
+ const inst_arr = Array.isArray(OtherInstance) ? Array.from(OtherInstance) : [OtherInstance];
210
+ let results = [];
211
+ let hookHandlr = null;
212
+ const $ref = {
213
+ instance: Instance,
214
+ association: association.reversed ? null : OtherInstance,
215
+ associations: association.reversed ? inst_arr : null,
216
+ useChannel: Utilities.reusableChannelGenerator()
217
+ };
218
+ if (association.reversed) {
219
+ hookHandlr = Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
220
+ Instance.saveSync();
221
+ const runReversed = function (other) {
222
+ Instance.$emit(`before:set:${association.name}`, other);
223
+ Utilities.populateModelIdKeysConditions(Model, Object.keys(association.field), Instance, other, true);
224
+ // link
225
+ other.saveSync({}, { saveAssociations: false });
226
+ Instance.$emit(`after:set:${association.name}`, other);
227
+ return other;
228
+ };
229
+ results = Utilities.parallelQueryIfPossible(false, $ref.associations, runReversed);
230
+ });
231
+ }
232
+ else {
233
+ hookHandlr = Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
234
+ const runNonReversed = function (oinst) {
235
+ Instance.$emit(`before:set:${association.name}`, oinst);
236
+ oinst.saveSync({}, { saveAssociations: false });
237
+ Instance[association.name] = oinst;
238
+ // TODO: on pre normalize stage, record linked associated key property name on build association
239
+ // const idInHostSide = Object.keys(association.field)[0];
240
+ // const idInOtherSide = association.model.keys[0];
241
+ // Instance[idInHostSide] = oinst[idInOtherSide];
242
+ Utilities.populateModelIdKeysConditions(association.model, Object.keys(association.field), oinst, Instance);
243
+ Instance.$emit(`after:set:${association.name}`, oinst);
244
+ return oinst;
245
+ };
246
+ results = runNonReversed($ref.association);
247
+ // link
248
+ Instance.saveSync({}, { saveAssociations: false });
249
+ });
250
+ }
251
+ Hook.wait(Instance, association.hooks[`beforeSet`], hookHandlr, Utilities.buildAssocHooksContext('beforeSet', { $ref }));
252
+ Hook.trigger(Instance, association.hooks['afterSet'], Utilities.buildAssocHooksContext('afterSet', { $ref }));
253
+ return results;
254
+ });
255
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.setAccessor, function (OtherInstance, cb) {
256
+ process.nextTick(() => {
257
+ const syncResponse = Utilities.catchBlocking(Instance[association.setSyncAccessor], [OtherInstance]);
258
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
259
+ });
260
+ return this;
261
+ });
262
+ // only non-reversed could delete associated instance
263
+ if (!association.reversed && !association.__for_extension) {
264
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.delSyncAccessor, function () {
265
+ const $ref = {
266
+ instance: Instance,
267
+ useChannel: Utilities.reusableChannelGenerator()
268
+ };
269
+ Hook.wait(Instance, association.hooks[`beforeRemove`], Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
270
+ Instance.$emit(`before:del:${association.name}`);
271
+ for (let k in association.field) {
272
+ if (association.field.hasOwnProperty(k)) {
273
+ Instance[k] = null;
274
+ }
275
+ }
276
+ Instance.saveSync({}, { saveAssociations: false });
277
+ delete Instance[association.name];
278
+ Instance.$emit(`after:del:${association.name}`);
279
+ }), Utilities.buildAssocHooksContext('beforeRemove', { $ref }));
280
+ Hook.trigger(Instance, association.hooks['afterRemove'], Utilities.buildAssocHooksContext('afterRemove', { $ref }));
281
+ return this;
282
+ }, {});
283
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.delAccessor, function (cb) {
284
+ process.nextTick(() => {
285
+ const syncResponse = Utilities.catchBlocking(Instance[association.delSyncAccessor]);
286
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb });
287
+ });
288
+ return this;
289
+ });
290
+ }
291
+ }
292
+ function autoFetchInstance(Instance, association, opts) {
293
+ if (!Instance.saved())
294
+ return;
295
+ if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit === "undefined") {
296
+ opts.autoFetchLimit = association.autoFetchLimit;
297
+ }
298
+ if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch))
299
+ return;
300
+ /**
301
+ * When we have a new non persisted instance for which the association field (eg owner_id)
302
+ * is set, we don't want to auto fetch anything, since `new Model(owner_id: 12)` takes no
303
+ * callback, and hence this lookup would complete at an arbitrary point in the future.
304
+ * The associated entity should probably be fetched when the instance is persisted.
305
+ */
306
+ if (Instance.isPersisted()) {
307
+ Instance[association.getSyncAccessor]({ autoFetchLimit: opts.autoFetchLimit - 1 });
308
+ }
309
+ return;
310
+ }
@@ -0,0 +1,14 @@
1
+ import type { FxOrmAssociation } from "../Typo/assoc";
2
+ import type { FxOrmModel } from "../Typo/model";
3
+ import type { FxOrmProperty } from "../Typo/property";
4
+ export declare function defineDefaultExtendsToTableName(modelTableName: string, assocName: string): string;
5
+ type ACCESSOR_PREFIX = 'add' | 'get' | 'set' | 'has' | 'remove' | 'findBy';
6
+ export declare function defineAssociationAccessorMethodName(prefixer: ACCESSOR_PREFIX, assocName: string): string;
7
+ export declare const ACCESSOR_KEYS: {
8
+ [k: string]: ACCESSOR_PREFIX;
9
+ };
10
+ export declare function getMapsToFromProperty(property: FxOrmProperty.NormalizedProperty, k: string): string;
11
+ export declare function getMapsToFromPropertyHash(hash: Record<string, FxOrmProperty.NormalizedProperty>): string[];
12
+ export declare function cutOffAssociatedModelFindOptions(findby_options: FxOrmAssociation.ModelAssociationMethod__FindByOptions, association_name: string): any;
13
+ export declare function addAssociationInfoToModel(Model: FxOrmModel.Model, association_name: string, opts: FxOrmModel.Model['associations'][any]): FxOrmModel.Model['associations'][string];
14
+ export {};
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ /// <reference lib="es6" />
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ACCESSOR_KEYS = void 0;
5
+ exports.defineDefaultExtendsToTableName = defineDefaultExtendsToTableName;
6
+ exports.defineAssociationAccessorMethodName = defineAssociationAccessorMethodName;
7
+ exports.getMapsToFromProperty = getMapsToFromProperty;
8
+ exports.getMapsToFromPropertyHash = getMapsToFromPropertyHash;
9
+ exports.cutOffAssociatedModelFindOptions = cutOffAssociatedModelFindOptions;
10
+ exports.addAssociationInfoToModel = addAssociationInfoToModel;
11
+ function defineDefaultExtendsToTableName(modelTableName, assocName) {
12
+ return `${modelTableName}_${assocName}`;
13
+ }
14
+ function defineAssociationAccessorMethodName(prefixer, assocName) {
15
+ return `${prefixer}${assocName}`;
16
+ }
17
+ exports.ACCESSOR_KEYS = {
18
+ /* common :start */
19
+ "get": "get",
20
+ "set": "set",
21
+ "has": "has",
22
+ "del": "remove",
23
+ "modelFindBy": "findBy",
24
+ /* common :end */
25
+ // useful for association `hasMany`
26
+ "add": "add",
27
+ };
28
+ function getMapsToFromProperty(property, k) {
29
+ return property.mapsTo || k;
30
+ }
31
+ function getMapsToFromPropertyHash(hash) {
32
+ return Object.keys(hash).map(k => {
33
+ const item = hash[k];
34
+ return item.mapsTo || k;
35
+ });
36
+ }
37
+ function cutOffAssociatedModelFindOptions(findby_options, association_name) {
38
+ let opts = null;
39
+ const k = `find_options:${association_name}`;
40
+ if (findby_options.hasOwnProperty(k)) {
41
+ opts = findby_options[k];
42
+ delete findby_options[k];
43
+ }
44
+ return;
45
+ return opts;
46
+ }
47
+ function addAssociationInfoToModel(Model, association_name, opts) {
48
+ Model.associations[association_name] = {
49
+ type: opts.type,
50
+ association: opts.association
51
+ };
52
+ return Model.associations[association_name];
53
+ }
@@ -0,0 +1,3 @@
1
+ import type { FxOrmQuery } from './Typo/query';
2
+ declare const ChainFind: FxOrmQuery.ChainFindGenerator;
3
+ export = ChainFind;