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,698 @@
1
+ "use strict";
2
+ /// <reference types="@fibjs/types" />
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Instance = void 0;
5
+ const util = require("util");
6
+ const events_1 = require("events");
7
+ const FxORMCore = require("../orm-core/index");
8
+ const Utilities = require("./Utilities");
9
+ const Hook = require("./Hook");
10
+ const Error_1 = require("./Error");
11
+ const enforce = require("@fibjs/enforce");
12
+ const Helpers = require("./Helpers");
13
+ exports.Instance = function (Model, _opts) {
14
+ const instRtd = util.extend({}, _opts);
15
+ instRtd.data = instRtd.data || {};
16
+ instRtd.extra = instRtd.extra || {};
17
+ if (!instRtd.keys && !Utilities.isVirtualViewModel(Model)) {
18
+ instRtd.keys = ['id'];
19
+ }
20
+ instRtd.changes = (instRtd.isNew ? Object.keys(instRtd.data) : []);
21
+ instRtd.extrachanges = [];
22
+ instRtd.associations = {};
23
+ instRtd.events = util.extend({}, instRtd.events);
24
+ instRtd.originalKeyValues = {};
25
+ instRtd.__validationData = instRtd.__validationData || {};
26
+ const eventor = new events_1.EventEmitter();
27
+ var instance_saving = false;
28
+ var instance = {};
29
+ var emitCallbackStyleEvent = function (event, ...args) {
30
+ eventor.emit(event, ...args);
31
+ };
32
+ var rememberKeys = function () {
33
+ for (let i = 0; i < instRtd.keyProperties.length; i++) {
34
+ const prop = instRtd.keyProperties[i];
35
+ instRtd.originalKeyValues[prop.name] = instRtd.data[prop.name];
36
+ }
37
+ };
38
+ var shouldSaveAssocs = function (saveOptions) {
39
+ if (Model.settings.get("instance.saveAssociationsByDefault")) {
40
+ return saveOptions.saveAssociations !== false;
41
+ }
42
+ else {
43
+ return !!saveOptions.saveAssociations;
44
+ }
45
+ };
46
+ const returnAllErrors = Model.settings.get("instance.returnAllErrors");
47
+ const handleValidationsSync = function () {
48
+ let required, alwaysValidate;
49
+ let validationErr;
50
+ Hook.wait(instance, instRtd.hooks.beforeValidation, function (err) {
51
+ if (err) {
52
+ validationErr = err;
53
+ // saveError(err);
54
+ return;
55
+ }
56
+ const checks = new enforce.Enforce({ returnAllErrors });
57
+ for (let k in instRtd.validations) {
58
+ required = false;
59
+ if (Model.__propertiesByName[k]) {
60
+ required = Model.__propertiesByName[k].required;
61
+ alwaysValidate = Model.__propertiesByName[k].alwaysValidate;
62
+ }
63
+ else {
64
+ for (let i = 0; i < instRtd.one_associations.length; i++) {
65
+ /* non-normalized `field` maybe string now */
66
+ if (instRtd.one_associations[i].field === k) {
67
+ required = instRtd.one_associations[i].required;
68
+ break;
69
+ }
70
+ }
71
+ }
72
+ if (!alwaysValidate && !required && instance[k] == null) {
73
+ continue; // avoid validating if property is not required and is "empty"
74
+ }
75
+ for (const validator of instRtd.validations[k]) {
76
+ checks.add(k, validator);
77
+ }
78
+ }
79
+ checks.context("instance", instance);
80
+ checks.context("model", Model);
81
+ checks.context("driver", instRtd.driver);
82
+ const errors = checks.checkSync(instance);
83
+ if (errors && errors.length)
84
+ validationErr = returnAllErrors ? errors : errors[0];
85
+ });
86
+ return validationErr;
87
+ };
88
+ const saveError = function (err) {
89
+ instance_saving = false;
90
+ emitCallbackStyleEvent("save", err, instance);
91
+ Hook.trigger(instance, instRtd.hooks.afterSave, false);
92
+ return instance;
93
+ };
94
+ const throwErrorIfExistsOnSave = function (errors, forceList = false) {
95
+ const origErrorIsList = Array.isArray(errors);
96
+ const validationId = Utilities.getUUID();
97
+ instRtd.__validationData[validationId] = Utilities.arraify(errors).filter(Boolean);
98
+ const setErrors = (errors) => {
99
+ instRtd.__validationData[validationId] = Utilities.arraify(errors).filter(Boolean);
100
+ };
101
+ Hook.trigger(instance, instRtd.hooks.afterValidation, {
102
+ errors: instRtd.__validationData[validationId].slice(0),
103
+ setErrors
104
+ });
105
+ const tmp = Utilities.arraify(instRtd.__validationData[validationId]);
106
+ delete instRtd.__validationData[validationId];
107
+ const firstErr = Utilities.firstEl(tmp);
108
+ if (!firstErr)
109
+ return;
110
+ if (origErrorIsList) {
111
+ saveError(tmp);
112
+ throw tmp;
113
+ }
114
+ else {
115
+ saveError(firstErr);
116
+ throw firstErr;
117
+ }
118
+ };
119
+ const saveInstanceSync = function (saveOptions) {
120
+ // what this condition means:
121
+ // - If the instance is in state mode
122
+ // - AND it's not an association that is asking it to save
123
+ // -> return has already saved
124
+ if (instance_saving && saveOptions.saveAssociations !== false)
125
+ return instance;
126
+ instance_saving = true;
127
+ throwErrorIfExistsOnSave(handleValidationsSync(), returnAllErrors);
128
+ if (instRtd.isNew) {
129
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'create', {});
130
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {});
131
+ Hook.wait(instance, [instRtd.hooks['beforeCreate'], instRtd.hooks['beforeSave']], function (err) {
132
+ if (err) {
133
+ saveError(err);
134
+ throw err;
135
+ }
136
+ saveNewSync(saveOptions, getInstanceData());
137
+ });
138
+ }
139
+ else {
140
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {});
141
+ Hook.wait(instance, [instRtd.hooks['beforeSave']], function (err) {
142
+ if (err) {
143
+ saveError(err);
144
+ throw err;
145
+ }
146
+ savePersistedSync(saveOptions, getInstanceData());
147
+ });
148
+ }
149
+ };
150
+ const runSyncAfterSaveActions = function (is_create, err) {
151
+ instance_saving = false;
152
+ emitCallbackStyleEvent("save", err, instance);
153
+ if (is_create)
154
+ Hook.trigger(instance, instRtd.hooks.afterCreate, !err);
155
+ Hook.trigger(instance, instRtd.hooks.afterSave, !err);
156
+ };
157
+ const getInstanceData = function () {
158
+ const data = {};
159
+ let prop;
160
+ for (let k in instRtd.data) {
161
+ if (!instRtd.data.hasOwnProperty(k))
162
+ continue;
163
+ prop = Model.allProperties[k];
164
+ if (prop) {
165
+ if (instRtd.data[k] == null && (prop.type == 'serial' || typeof prop.defaultValue == 'function')) {
166
+ continue;
167
+ }
168
+ if (instRtd.driver.propertyToValue) {
169
+ data[k] = instRtd.driver.propertyToValue(instRtd.data[k], prop);
170
+ }
171
+ else {
172
+ data[k] = instRtd.data[k];
173
+ }
174
+ }
175
+ else {
176
+ data[k] = instRtd.data[k];
177
+ }
178
+ }
179
+ return data;
180
+ };
181
+ const resetChanges = function () {
182
+ instRtd.changes.length = 0;
183
+ };
184
+ const saveNewSync = function (saveOptions, data) {
185
+ data = util.omit(data, Object.keys(Model.virtualProperties));
186
+ data = Utilities.transformPropertyNames(data, Model.allProperties);
187
+ const info = instRtd.driver.insert(instRtd.table, data, instRtd.keyProperties);
188
+ resetChanges();
189
+ for (let i = 0, prop; i < instRtd.keyProperties.length; i++) {
190
+ prop = instRtd.keyProperties[i];
191
+ instRtd.data[prop.name] = info.hasOwnProperty(prop.name) ? info[prop.name] : data[prop.name];
192
+ }
193
+ instRtd.isNew = false;
194
+ rememberKeys();
195
+ let err;
196
+ if (shouldSaveAssocs(saveOptions)) {
197
+ const syncReponse = FxORMCore.catchBlocking(saveAssociationsSync);
198
+ err = syncReponse.error;
199
+ }
200
+ runSyncAfterSaveActions(true, err);
201
+ if (err)
202
+ throw err;
203
+ saveInstanceExtraSync();
204
+ };
205
+ const oneOneAssocs = Object.values(Model.associations).filter(assoc => assoc.type === 'hasOne');
206
+ const savePersistedSync = function (saveOptions, data) {
207
+ let changes = {}, conditions = {};
208
+ const savedCheckSync = function (saved) {
209
+ const _shouldSaveAssocs = shouldSaveAssocs(saveOptions);
210
+ if (!saved && !_shouldSaveAssocs)
211
+ return saveInstanceExtraSync();
212
+ if (!_shouldSaveAssocs) {
213
+ runSyncAfterSaveActions(false);
214
+ return saveInstanceExtraSync();
215
+ }
216
+ const { error: err, result: assocSaved } = FxORMCore.catchBlocking(saveAssociationsSync);
217
+ if (saved || assocSaved) {
218
+ runSyncAfterSaveActions(false, err);
219
+ if (err)
220
+ throw err;
221
+ }
222
+ return saveInstanceExtraSync();
223
+ };
224
+ if (instance.saved())
225
+ return savedCheckSync(false);
226
+ for (let i = 0; i < instRtd.changes.length; i++) {
227
+ changes[instRtd.changes[i]] = data[instRtd.changes[i]];
228
+ }
229
+ for (let i = 0; i < instRtd.keyProperties.length; i++) {
230
+ const prop = instRtd.keyProperties[i];
231
+ conditions[prop.mapsTo] = instRtd.originalKeyValues[prop.name];
232
+ }
233
+ changes = util.omit(changes, Object.keys(Model.virtualProperties));
234
+ changes = Utilities.transformPropertyNames(changes, Model.allProperties);
235
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
236
+ const syncResponse = FxORMCore.catchBlocking(() => instRtd.driver.update(instRtd.table, changes, conditions));
237
+ fillBackAssociatedFieldsAfterPersist: {
238
+ const couldParallel = false;
239
+ Utilities.parallelQueryIfPossible(couldParallel, oneOneAssocs, ({ association }) => {
240
+ Utilities.parallelQueryIfPossible(couldParallel, Object.keys(association.field), propName => {
241
+ if (!changes.hasOwnProperty(propName)) {
242
+ return;
243
+ }
244
+ instance[propName] = changes[propName];
245
+ if (instance.hasOwnProperty(association.name)) {
246
+ instance[association.getSyncAccessor]();
247
+ }
248
+ });
249
+ });
250
+ }
251
+ if (syncResponse.error) {
252
+ saveError(syncResponse.error);
253
+ throw syncResponse.error;
254
+ }
255
+ resetChanges();
256
+ rememberKeys();
257
+ savedCheckSync(true);
258
+ };
259
+ const saveAssociationsSync = function (cb) {
260
+ let pending = 1,
261
+ // to check if error passed by cb if cb exists
262
+ error_passed = false, assocSaved;
263
+ const saveAssociationItemSync = function (syncVersionAccessor, instances) {
264
+ pending += 1;
265
+ let error = null;
266
+ const syncResponse = FxORMCore.catchBlocking(() => {
267
+ instance[syncVersionAccessor](instances);
268
+ });
269
+ error = syncResponse.error;
270
+ if (syncResponse.error) {
271
+ if (error_passed)
272
+ return;
273
+ error_passed = true;
274
+ assocSaved = true;
275
+ }
276
+ if (--pending === 0) {
277
+ assocSaved = true;
278
+ }
279
+ FxORMCore.takeAwayResult({ error: error, result: assocSaved }, { callback: cb });
280
+ };
281
+ const _saveOneAssociation = function (assoc) {
282
+ if (!instance[assoc.name] || typeof instance[assoc.name] !== "object")
283
+ return;
284
+ if (assoc.reversed) {
285
+ // reversed hasOne associations should behave like hasMany
286
+ if (!Array.isArray(instance[assoc.name])) {
287
+ instance[assoc.name] = [instance[assoc.name]];
288
+ }
289
+ const instances = instance[assoc.name];
290
+ Utilities.parallelQueryIfPossible(false, instances, (item) => {
291
+ if (!item.isInstance) {
292
+ item = new assoc.model(item);
293
+ }
294
+ saveAssociationItemSync(assoc.setSyncAccessor, item);
295
+ });
296
+ return;
297
+ }
298
+ if (!instance[assoc.name].isInstance) {
299
+ instance[assoc.name] = new assoc.model(instance[assoc.name]);
300
+ }
301
+ saveAssociationItemSync(assoc.setSyncAccessor, instance[assoc.name]);
302
+ };
303
+ for (let i = 0; i < instRtd.one_associations.length; i++) {
304
+ _saveOneAssociation(instRtd.one_associations[i]);
305
+ }
306
+ const _saveManyAssociation = function (assoc) {
307
+ var assocVal = instance[assoc.name];
308
+ if (!Array.isArray(assocVal))
309
+ return;
310
+ if (!instRtd.associations[assoc.name].changed)
311
+ return;
312
+ for (let j = 0; j < assocVal.length; j++) {
313
+ if (!assocVal[j].isInstance) {
314
+ assocVal[j] = new assoc.model(assocVal[j]);
315
+ }
316
+ }
317
+ saveAssociationItemSync(assoc.setSyncAccessor, assocVal);
318
+ };
319
+ for (let i = 0; i < instRtd.many_associations.length; i++) {
320
+ _saveManyAssociation(instRtd.many_associations[i]);
321
+ }
322
+ if (--pending === 0)
323
+ cb && cb(null, false);
324
+ return assocSaved;
325
+ };
326
+ var getNormalizedExtraDataAtPropertyTime = function () {
327
+ return instRtd.extra;
328
+ };
329
+ const saveInstanceExtraSync = function () {
330
+ if (instRtd.extrachanges.length === 0)
331
+ return instance;
332
+ var data = {};
333
+ var conditions = {};
334
+ for (let i = 0; i < instRtd.extrachanges.length; i++) {
335
+ if (!instRtd.data.hasOwnProperty(instRtd.extrachanges[i]))
336
+ continue;
337
+ if (getNormalizedExtraDataAtPropertyTime()[instRtd.extrachanges[i]]) {
338
+ data[instRtd.extrachanges[i]] = instRtd.data[instRtd.extrachanges[i]];
339
+ if (instRtd.driver.propertyToValue) {
340
+ data[instRtd.extrachanges[i]] = instRtd.driver.propertyToValue(data[instRtd.extrachanges[i]], getNormalizedExtraDataAtPropertyTime()[instRtd.extrachanges[i]]);
341
+ }
342
+ }
343
+ else {
344
+ data[instRtd.extrachanges[i]] = instRtd.data[instRtd.extrachanges[i]];
345
+ }
346
+ }
347
+ for (let i = 0; i < instRtd.extra_info.id.length; i++) {
348
+ conditions[instRtd.extra_info.id_prop[i]] = instRtd.extra_info.id[i];
349
+ conditions[instRtd.extra_info.assoc_prop[i]] = instRtd.data[instRtd.keys[i]];
350
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
351
+ }
352
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
353
+ instRtd.driver.update(instRtd.extra_info.table, data, conditions);
354
+ return instance;
355
+ };
356
+ const saveInstanceProperty = function (key, value) {
357
+ const changes = {}, conditions = {};
358
+ changes[key] = value;
359
+ if (Model.properties[key])
360
+ if (instRtd.driver.propertyToValue)
361
+ changes[key] = instRtd.driver.propertyToValue(changes[key], Model.properties[key]);
362
+ for (let i = 0; i < instRtd.keys.length; i++) {
363
+ conditions[instRtd.keys[i]] = instRtd.data[instRtd.keys[i]];
364
+ }
365
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {});
366
+ Hook.wait(instance, instRtd.hooks.beforeSave, function (err) {
367
+ if (err) {
368
+ Hook.trigger(instance, instRtd.hooks.afterSave, false);
369
+ emitCallbackStyleEvent("save", err, instance);
370
+ return;
371
+ }
372
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
373
+ const syncReponse = FxORMCore.catchBlocking(() => instRtd.driver.update(instRtd.table, changes, conditions));
374
+ if (!syncReponse.error)
375
+ instRtd.data[key] = value;
376
+ Hook.trigger(instance, instRtd.hooks.afterSave, !syncReponse.error);
377
+ emitCallbackStyleEvent("save", syncReponse.error, instance);
378
+ });
379
+ };
380
+ const setInstanceProperty = function (key, value) {
381
+ const prop = Model.__propertiesByName[key] || getNormalizedExtraDataAtPropertyTime()[key];
382
+ if (prop) {
383
+ if ('valueToProperty' in instRtd.driver) {
384
+ value = instRtd.driver.valueToProperty(value, prop);
385
+ }
386
+ if (instRtd.data[key] !== value) {
387
+ instRtd.data[key] = value;
388
+ return true;
389
+ }
390
+ }
391
+ return false;
392
+ };
393
+ // ('data.a.b', 5) => instRtd.data.a.b = 5
394
+ const setPropertyByPath = function (path, value) {
395
+ if (typeof path == 'string') {
396
+ path = path.split('.');
397
+ }
398
+ else if (!Array.isArray(path)) {
399
+ return;
400
+ }
401
+ var propName = path.shift();
402
+ var prop = Model.__propertiesByName[propName] || getNormalizedExtraDataAtPropertyTime()[propName];
403
+ var currKey, currObj;
404
+ if (!prop) {
405
+ return;
406
+ }
407
+ if (path.length == 0) {
408
+ instance[propName] = value;
409
+ return;
410
+ }
411
+ currObj = instance[propName];
412
+ while (currObj && path.length > 0) {
413
+ currKey = path.shift();
414
+ if (path.length > 0) {
415
+ currObj = currObj[currKey];
416
+ }
417
+ else if (currObj[currKey] !== value) {
418
+ currObj[currKey] = value;
419
+ instRtd.changes.push(propName);
420
+ }
421
+ }
422
+ };
423
+ var addInstanceProperty = function (key) {
424
+ var defaultValue = null;
425
+ var prop = Model.__propertiesByName[key];
426
+ // This code was first added, and then commented out in a later commit.
427
+ // Its presence doesn't affect tests, so I'm just gonna log if it ever gets called.
428
+ // If someone complains about noise, we know it does something, and figure it out then.
429
+ if (instance.hasOwnProperty(key))
430
+ console.log("Overwriting instance property");
431
+ if (key in instRtd.data) {
432
+ defaultValue = instRtd.data[key];
433
+ }
434
+ else if (prop && 'defaultValue' in prop) {
435
+ defaultValue = prop.defaultValue;
436
+ }
437
+ setInstanceProperty(key, defaultValue);
438
+ Object.defineProperty(instance, key, {
439
+ get: function () {
440
+ return instRtd.data[key];
441
+ },
442
+ set: function (val) {
443
+ if (prop.key === true) {
444
+ if (prop.type == 'serial' && instRtd.data[key] != null) {
445
+ if (!(val === null || val === undefined)) {
446
+ return;
447
+ }
448
+ }
449
+ else {
450
+ instRtd.originalKeyValues[prop.name] = instRtd.data[prop.name];
451
+ }
452
+ }
453
+ if (!setInstanceProperty(key, val)) {
454
+ return;
455
+ }
456
+ if (instRtd.autoSave) {
457
+ saveInstanceProperty(key, val);
458
+ }
459
+ else if (instRtd.changes.indexOf(key) === -1) {
460
+ instRtd.changes.push(key);
461
+ }
462
+ },
463
+ enumerable: !(prop && !prop.enumerable)
464
+ });
465
+ };
466
+ var addInstanceExtraProperty = function (key) {
467
+ if (!instance.hasOwnProperty("extra") || instance.extra === "") {
468
+ instance.extra = {};
469
+ }
470
+ Object.defineProperty(instance.extra, key, {
471
+ get: function () {
472
+ return instRtd.data[key];
473
+ },
474
+ set: function (val) {
475
+ setInstanceProperty(key, val);
476
+ /*if (instRtd.autoSave) {
477
+ saveInstanceProperty(key, val);
478
+ }*/
479
+ if (instRtd.extrachanges.indexOf(key) === -1) {
480
+ instRtd.extrachanges.push(key);
481
+ }
482
+ },
483
+ enumerable: true
484
+ });
485
+ };
486
+ for (let k of Object.keys(Model.__propertiesByName)) {
487
+ addInstanceProperty(k);
488
+ }
489
+ for (let k in instRtd.extra) {
490
+ addInstanceProperty(k);
491
+ }
492
+ for (let k in instRtd.methods) {
493
+ Utilities.addHiddenPropertyToInstance(instance, k, instRtd.methods[k].bind(instance), { writable: true });
494
+ }
495
+ for (let k in instRtd.extra) {
496
+ addInstanceExtraProperty(k);
497
+ }
498
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "on", function (event, cb) {
499
+ cb = Utilities.bindInstance(instance, cb);
500
+ eventor.on(event, cb);
501
+ return this;
502
+ });
503
+ const eventEmitterArgsToMap = function (restArgs) {
504
+ let map = restArgs[0];
505
+ if (typeof map === 'string' && typeof restArgs[1] === 'function')
506
+ map = { [map]: restArgs[1] };
507
+ if (typeof map === 'object')
508
+ Object.keys(map).forEach(evt => map[evt] = Utilities.bindInstance(instance, map[evt]));
509
+ return map;
510
+ };
511
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "$on", function (...args) {
512
+ const mapOrString = eventEmitterArgsToMap(args);
513
+ if (typeof mapOrString === 'string')
514
+ return;
515
+ return eventor.on(mapOrString);
516
+ });
517
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "$off", function (...args) {
518
+ const mapOrString = eventEmitterArgsToMap(args);
519
+ if (typeof mapOrString === 'string')
520
+ return eventor.off(mapOrString);
521
+ return eventor.off(mapOrString);
522
+ });
523
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "$emit", function (event, ...args) {
524
+ return eventor.emit(event, ...args);
525
+ });
526
+ const collectParamsForSave = function (args) {
527
+ var objCount = 0;
528
+ var data = {}, saveOptions = {};
529
+ Helpers.selectArgs(args, function (arg_type, arg) {
530
+ switch (arg_type) {
531
+ case 'object':
532
+ switch (objCount) {
533
+ case 0:
534
+ data = arg;
535
+ break;
536
+ case 1:
537
+ saveOptions = arg;
538
+ break;
539
+ }
540
+ objCount++;
541
+ break;
542
+ default:
543
+ const err = new Error("Unknown parameter type '" + (typeof arg) + "' in Instance.save()");
544
+ err.model = Model.table;
545
+ throw err;
546
+ }
547
+ });
548
+ return { saveOptions, data };
549
+ };
550
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "saveSync", function () {
551
+ Utilities.disAllowOpForVModel(Model, 'instance.save');
552
+ const args = Array.prototype.slice.apply(arguments);
553
+ const { saveOptions, data } = collectParamsForSave(args);
554
+ for (let k in data) {
555
+ if (data.hasOwnProperty(k)) {
556
+ this[k] = data[k];
557
+ }
558
+ }
559
+ saveInstanceSync(saveOptions);
560
+ return instance;
561
+ });
562
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "save", function () {
563
+ var cb = null;
564
+ let args = Array.prototype.slice.apply(arguments);
565
+ Helpers.selectArgs(args, function (arg_type, arg) {
566
+ switch (arg_type) {
567
+ case 'function':
568
+ cb = arg;
569
+ break;
570
+ }
571
+ });
572
+ args = args.filter(x => x !== cb);
573
+ collectParamsForSave(args);
574
+ process.nextTick(() => {
575
+ const syncResponse = FxORMCore.catchBlocking(instance.saveSync, args);
576
+ FxORMCore.takeAwayResult({ error: syncResponse.error, result: instance }, { no_throw: !!cb, callback: cb });
577
+ });
578
+ return this;
579
+ });
580
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "saved", function () {
581
+ return instRtd.changes.length === 0;
582
+ });
583
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "removeSync", function () {
584
+ Utilities.disAllowOpForVModel(Model, 'instance.remove');
585
+ if (instRtd.isNew)
586
+ return;
587
+ var conditions = {};
588
+ for (let i = 0; i < instRtd.keys.length; i++) {
589
+ conditions[instRtd.keys[i]] = instRtd.data[instRtd.keys[i]];
590
+ }
591
+ let removeErr = null;
592
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'remove', {});
593
+ Hook.wait(instance, instRtd.hooks.beforeRemove, function (err) {
594
+ if (err) {
595
+ emitCallbackStyleEvent("remove", err, instance);
596
+ removeErr = err;
597
+ return;
598
+ }
599
+ emitCallbackStyleEvent("beforeRemove", instance);
600
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
601
+ const syncResponse = FxORMCore.catchBlocking(() => instRtd.driver.remove(instRtd.table, conditions));
602
+ Hook.trigger(instance, instRtd.hooks.afterRemove, !syncResponse.error);
603
+ emitCallbackStyleEvent("remove", syncResponse.error, instance);
604
+ removeErr = syncResponse.error;
605
+ instance = undefined;
606
+ });
607
+ if (removeErr)
608
+ throw removeErr;
609
+ });
610
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "remove", function (cb) {
611
+ const syncReponse = FxORMCore.catchBlocking(() => instance.removeSync());
612
+ FxORMCore.takeAwayResult(syncReponse, { callback: cb });
613
+ return this;
614
+ });
615
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "set", setPropertyByPath);
616
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "markAsDirty", function (propName) {
617
+ if (propName != undefined) {
618
+ instRtd.changes.push(propName);
619
+ }
620
+ });
621
+ Utilities.addHiddenReadonlyPropertyToInstance(instance, "dirtyProperties", function () { return instRtd.changes; });
622
+ Utilities.addHiddenPropertyToInstance(instance, "isInstance", true);
623
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "isPersisted", function () {
624
+ return !instRtd.isNew;
625
+ });
626
+ Utilities.addHiddenPropertyToInstance(instance, "isShell", function () {
627
+ return instRtd.isShell;
628
+ });
629
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "validateSync", function () {
630
+ Utilities.disAllowOpForVModel(Model, 'instance.validate');
631
+ return handleValidationsSync() || false;
632
+ });
633
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "validate", function (cb) {
634
+ cb(null, instance.validateSync());
635
+ });
636
+ Utilities.addHiddenPropertyToInstance(instance, "__singleton_uid", function () {
637
+ return instRtd.uid;
638
+ });
639
+ Utilities.addHiddenPropertyToInstance(instance, "__instRtd", instRtd);
640
+ /* just for compat, use __instRtd plz */
641
+ Utilities.addHiddenPropertyToInstance(instance, "__opts", instRtd);
642
+ Utilities.addHiddenPropertyToInstance(instance, "model", function () {
643
+ return Model;
644
+ });
645
+ for (let i = 0; i < instRtd.keyProperties.length; i++) {
646
+ var prop = instRtd.keyProperties[i];
647
+ if (!(prop.name in instRtd.data)) {
648
+ instRtd.changes = Object.keys(instRtd.data);
649
+ break;
650
+ }
651
+ }
652
+ rememberKeys();
653
+ instRtd.__setupAssociations(instance);
654
+ for (let i = 0; i < instRtd.one_associations.length; i++) {
655
+ var asc = instRtd.one_associations[i];
656
+ if (!asc.reversed && !asc.__for_extension) {
657
+ for (let k in asc.field) {
658
+ if (!instRtd.data.hasOwnProperty(k)) {
659
+ addInstanceProperty(k);
660
+ }
661
+ }
662
+ }
663
+ if (asc.name in instRtd.data) {
664
+ var d = instRtd.data[asc.name];
665
+ var mapper = function (obj) {
666
+ return obj.isInstance ? obj : new asc.model(obj);
667
+ };
668
+ if (Array.isArray(d)) {
669
+ instance[asc.name] = d.map(mapper);
670
+ }
671
+ else {
672
+ instance[asc.name] = mapper(d);
673
+ }
674
+ delete instRtd.data[asc.name];
675
+ }
676
+ }
677
+ for (let i = 0; i < instRtd.many_associations.length; i++) {
678
+ var aName = instRtd.many_associations[i].name;
679
+ instRtd.associations[aName] = {
680
+ changed: false, data: instRtd.many_associations[i]
681
+ };
682
+ if (Array.isArray(instRtd.data[aName])) {
683
+ instance[aName] = instRtd.data[aName];
684
+ delete instRtd.data[aName];
685
+ }
686
+ }
687
+ Object.keys(instRtd.events).forEach((evtName) => {
688
+ if (typeof instRtd.events[evtName] !== 'function')
689
+ throw new Error_1.default("INVALID_EVENT_HANDLER", 'PARAM_MISMATCH');
690
+ instance.on(evtName, instRtd.events[evtName]);
691
+ });
692
+ Hook.wait(instance, instRtd.hooks.afterLoad, function (err) {
693
+ process.nextTick(() => {
694
+ emitCallbackStyleEvent("ready", err, instance);
695
+ });
696
+ });
697
+ return instance;
698
+ };