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