fib-orm 2.1.0 → 3.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 (427) hide show
  1. package/README.md +42 -6
  2. package/index.d.ts +11 -0
  3. package/index.js +15 -0
  4. package/package.json +37 -34
  5. package/{lib/cli/commands/ddlFrom.js → src/cli/commands/ddlFrom.ts} +40 -27
  6. package/{lib/cli/commands/dumpModel.js → src/cli/commands/dumpModel.ts} +78 -32
  7. package/{lib/cli/commands/escape.js → src/cli/commands/escape.ts} +51 -29
  8. package/{lib/cli/commands/spinner.js → src/cli/commands/spinner.ts} +20 -11
  9. package/{lib/cli/commands/upgrade.js → src/cli/commands/upgrade.ts} +53 -34
  10. package/src/cli/helpers/ddl.ts +84 -0
  11. package/src/cli/helpers/package.ts +81 -0
  12. package/src/cli/index.ts +24 -0
  13. package/src/cli/wrapSubcommand.ts +66 -0
  14. package/src/db-driver/Typo.d.ts +141 -0
  15. package/src/db-driver/built-ins/base.class.ts +260 -0
  16. package/src/db-driver/built-ins/driver-dm.ts +62 -0
  17. package/src/db-driver/built-ins/driver-mssql.ts +67 -0
  18. package/src/db-driver/built-ins/driver-mysql.ts +44 -0
  19. package/src/db-driver/built-ins/driver-postgresql.ts +94 -0
  20. package/src/db-driver/built-ins/driver-redis.ts +45 -0
  21. package/src/db-driver/built-ins/driver-sqlite.ts +41 -0
  22. package/src/db-driver/index.ts +6 -0
  23. package/src/db-driver/parseQueryString.ts +76 -0
  24. package/src/db-driver/utils.ts +204 -0
  25. package/src/knex/deps/tarn/PendingOperation.ts +56 -0
  26. package/src/knex/deps/tarn/Pool.ts +610 -0
  27. package/src/knex/deps/tarn/PromiseInspection.ts +29 -0
  28. package/src/knex/deps/tarn/Resource.ts +22 -0
  29. package/src/knex/deps/tarn/TimeoutError.ts +1 -0
  30. package/src/knex/deps/tarn/tarn.ts +7 -0
  31. package/src/knex/deps/tarn/utils.ts +62 -0
  32. package/src/knex/knex.ts +11 -0
  33. package/src/knex/result.d.ts +27 -0
  34. package/src/knex/sandbox.ts +45 -0
  35. package/src/knex/tables.d.ts +4 -0
  36. package/src/knex/types.d.ts +3228 -0
  37. package/src/orm/Adapters.ts +53 -0
  38. package/src/orm/AggregateFunctions.ts +254 -0
  39. package/src/orm/Associations/Extend.ts +405 -0
  40. package/src/orm/Associations/Many.ts +784 -0
  41. package/src/orm/Associations/One.ts +494 -0
  42. package/src/orm/Associations/_utils.ts +72 -0
  43. package/src/orm/ChainFind.ts +427 -0
  44. package/src/orm/ChainInstance.ts +97 -0
  45. package/src/orm/Debug.ts +21 -0
  46. package/src/orm/Drivers/DB/base.ts +7 -0
  47. package/src/orm/Drivers/DB/dm.ts +35 -0
  48. package/src/orm/Drivers/DB/mssql.ts +35 -0
  49. package/src/orm/Drivers/DB/mysql.ts +35 -0
  50. package/src/orm/Drivers/DB/postgres.ts +35 -0
  51. package/src/orm/Drivers/DB/sqlite3.ts +60 -0
  52. package/src/orm/Drivers/DML/_ddl-sql.ts +85 -0
  53. package/src/orm/Drivers/DML/_dml-helpers.ts +66 -0
  54. package/src/orm/Drivers/DML/_shared.ts +61 -0
  55. package/src/orm/Drivers/DML/_utils.ts +112 -0
  56. package/{lib/orm/Drivers/DML/dm.js → src/orm/Drivers/DML/dm.ts} +178 -86
  57. package/src/orm/Drivers/DML/index.ts +10 -0
  58. package/src/orm/Drivers/DML/mssql.ts +366 -0
  59. package/src/orm/Drivers/DML/mysql.ts +347 -0
  60. package/src/orm/Drivers/DML/postgres.ts +382 -0
  61. package/src/orm/Drivers/DML/sqlite.ts +361 -0
  62. package/src/orm/Error.ts +49 -0
  63. package/src/orm/Helpers.ts +297 -0
  64. package/src/orm/Hook.ts +125 -0
  65. package/src/orm/Instance.ts +944 -0
  66. package/src/orm/LazyLoad.ts +117 -0
  67. package/src/orm/Model.ts +1235 -0
  68. package/src/orm/ORM.ts +301 -0
  69. package/src/orm/Patch/fib-cache.ts +7 -0
  70. package/src/orm/Patch/plugin.ts +25 -0
  71. package/src/orm/Property.ts +86 -0
  72. package/src/orm/Settings.ts +134 -0
  73. package/src/orm/Singleton.ts +97 -0
  74. package/src/orm/Typo/Adapter.d.ts +5 -0
  75. package/src/orm/Typo/DMLDriver.d.ts +260 -0
  76. package/src/orm/Typo/Db.d.ts +100 -0
  77. package/src/orm/Typo/Error.d.ts +36 -0
  78. package/src/orm/Typo/ORM.d.ts +315 -0
  79. package/src/orm/Typo/Validators.d.ts +39 -0
  80. package/src/orm/Typo/_common.d.ts +44 -0
  81. package/src/orm/Typo/assoc.d.ts +280 -0
  82. package/src/orm/Typo/hook.d.ts +68 -0
  83. package/src/orm/Typo/instance.d.ts +214 -0
  84. package/{lib → src}/orm/Typo/model.d.ts +297 -174
  85. package/src/orm/Typo/patch.d.ts +39 -0
  86. package/src/orm/Typo/property.d.ts +79 -0
  87. package/src/orm/Typo/query.d.ts +266 -0
  88. package/{lib → src}/orm/Typo/settings.d.ts +14 -7
  89. package/{lib → src}/orm/Typo/synchronous.d.ts +51 -26
  90. package/src/orm/Utilities.ts +1313 -0
  91. package/src/orm/Validators.ts +127 -0
  92. package/src/orm/Where/common.ts +8 -0
  93. package/src/orm/Where/filterDate.ts +74 -0
  94. package/src/orm/cloneDeep.ts +45 -0
  95. package/src/orm/index.ts +231 -0
  96. package/src/orm-core/callback.d.ts +15 -0
  97. package/{lib → src}/orm-core/dialect.d.ts +75 -52
  98. package/src/orm-core/error.ts +71 -0
  99. package/{lib/orm-core/index.d.ts → src/orm-core/index.ts} +8 -2
  100. package/src/orm-core/sync.d.ts +14 -0
  101. package/src/orm-core/utils.ts +4 -0
  102. package/src/orm-property/Property.ts +110 -0
  103. package/src/orm-property/Utils.ts +26 -0
  104. package/src/orm-property/customTypes.ts +13 -0
  105. package/src/orm-property/index.ts +34 -0
  106. package/src/orm-property/transformer.ts +16 -0
  107. package/{lib/orm-property/transformers/dm.js → src/orm-property/transformers/dm.ts} +128 -69
  108. package/src/orm-property/transformers/index.ts +10 -0
  109. package/src/orm-property/transformers/mssql.ts +285 -0
  110. package/src/orm-property/transformers/mysql.ts +395 -0
  111. package/src/orm-property/transformers/postgresql.ts +267 -0
  112. package/src/orm-property/transformers/sqlite.ts +217 -0
  113. package/src/plugins/orm-plugin-uacl/Typo.d.ts +173 -0
  114. package/src/plugins/orm-plugin-uacl/_utils.ts +237 -0
  115. package/src/plugins/orm-plugin-uacl/acl-tree.ts +440 -0
  116. package/src/plugins/orm-plugin-uacl/config-acl-tree.ts +592 -0
  117. package/src/plugins/orm-plugin-uacl/index.ts +180 -0
  118. package/src/plugins/orm-plugin-uacl/tree.ts +331 -0
  119. package/src/sql-ddl-sync/Dialects/dm.ts +493 -0
  120. package/{lib/sql-query/Dialects/index.d.ts → src/sql-ddl-sync/Dialects/index.ts} +3 -3
  121. package/src/sql-ddl-sync/Dialects/mssql.ts +441 -0
  122. package/src/sql-ddl-sync/Dialects/mysql.ts +414 -0
  123. package/src/sql-ddl-sync/Dialects/postgresql.ts +474 -0
  124. package/src/sql-ddl-sync/Dialects/sqlite.ts +439 -0
  125. package/src/sql-ddl-sync/SQL.ts +157 -0
  126. package/src/sql-ddl-sync/Typo/Collection.d.ts +19 -0
  127. package/src/sql-ddl-sync/Typo/DbIndex.d.ts +11 -0
  128. package/{lib → src}/sql-ddl-sync/Typo/Dialect.d.ts +67 -52
  129. package/src/sql-ddl-sync/Typo/Driver.d.ts +67 -0
  130. package/src/sql-ddl-sync/Typo/Query.d.ts +8 -0
  131. package/src/sql-ddl-sync/Typo/SQL.d.ts +45 -0
  132. package/src/sql-ddl-sync/Typo/_common.d.ts +59 -0
  133. package/src/sql-ddl-sync/Utils.ts +291 -0
  134. package/src/sql-ddl-sync/index.ts +599 -0
  135. package/src/sql-query/Comparators.ts +115 -0
  136. package/src/sql-query/Create.ts +87 -0
  137. package/{lib/sql-query/Dialects/dm.js → src/sql-query/Dialects/dm.ts} +42 -22
  138. package/src/sql-query/Dialects/index.ts +14 -0
  139. package/src/sql-query/Dialects/mssql.ts +91 -0
  140. package/src/sql-query/Dialects/mysql.ts +120 -0
  141. package/src/sql-query/Dialects/postgresql.ts +99 -0
  142. package/src/sql-query/Dialects/sqlite.ts +80 -0
  143. package/src/sql-query/Helpers.ts +303 -0
  144. package/src/sql-query/Insert.ts +41 -0
  145. package/src/sql-query/Query.ts +140 -0
  146. package/src/sql-query/Remove.ts +111 -0
  147. package/src/sql-query/Select.ts +719 -0
  148. package/src/sql-query/Set.ts +25 -0
  149. package/src/sql-query/Typo/Aggregation.d.ts +34 -0
  150. package/src/sql-query/Typo/Comparators.d.ts +156 -0
  151. package/src/sql-query/Typo/Dialect.d.ts +51 -0
  152. package/src/sql-query/Typo/Field.d.ts +10 -0
  153. package/src/sql-query/Typo/Helper.d.ts +4 -0
  154. package/src/sql-query/Typo/Query-ChainBuilder.d.ts +110 -0
  155. package/src/sql-query/Typo/Query.d.ts +65 -0
  156. package/src/sql-query/Typo/Sql.d.ts +192 -0
  157. package/src/sql-query/Typo/SubQuery.d.ts +52 -0
  158. package/{lib/sql-query/Typo/index.d.ts → src/sql-query/Typo/index.ts} +5 -2
  159. package/src/sql-query/Update.ts +50 -0
  160. package/src/sql-query/Where.ts +435 -0
  161. package/{lib/orm/Patch → src/typings}/fib-cache.d.ts +4 -2
  162. package/lib/cli/commands/ddlFrom.d.ts +0 -2
  163. package/lib/cli/commands/dumpModel.d.ts +0 -2
  164. package/lib/cli/commands/escape.d.ts +0 -2
  165. package/lib/cli/commands/spinner.d.ts +0 -2
  166. package/lib/cli/commands/upgrade.d.ts +0 -2
  167. package/lib/cli/helpers/ddl.d.ts +0 -17
  168. package/lib/cli/helpers/ddl.js +0 -55
  169. package/lib/cli/helpers/package.d.ts +0 -25
  170. package/lib/cli/helpers/package.js +0 -56
  171. package/lib/cli/index.d.ts +0 -2
  172. package/lib/cli/index.js +0 -19
  173. package/lib/db-driver/Typo.d.ts +0 -111
  174. package/lib/db-driver/Typo.js +0 -2
  175. package/lib/db-driver/built-ins/base.class.d.ts +0 -91
  176. package/lib/db-driver/built-ins/base.class.js +0 -187
  177. package/lib/db-driver/built-ins/driver-dm.d.ts +0 -17
  178. package/lib/db-driver/built-ins/driver-dm.js +0 -54
  179. package/lib/db-driver/built-ins/driver-mongodb.d.ts +0 -23
  180. package/lib/db-driver/built-ins/driver-mongodb.js +0 -47
  181. package/lib/db-driver/built-ins/driver-mysql.d.ts +0 -17
  182. package/lib/db-driver/built-ins/driver-mysql.js +0 -38
  183. package/lib/db-driver/built-ins/driver-postgresql.d.ts +0 -21
  184. package/lib/db-driver/built-ins/driver-postgresql.js +0 -77
  185. package/lib/db-driver/built-ins/driver-redis.d.ts +0 -11
  186. package/lib/db-driver/built-ins/driver-redis.js +0 -36
  187. package/lib/db-driver/built-ins/driver-sqlite.d.ts +0 -16
  188. package/lib/db-driver/built-ins/driver-sqlite.js +0 -35
  189. package/lib/db-driver/index.d.ts +0 -3
  190. package/lib/db-driver/index.js +0 -5
  191. package/lib/db-driver/utils.d.ts +0 -14
  192. package/lib/db-driver/utils.js +0 -187
  193. package/lib/knex/deps/tarn/dist/PendingOperation.d.ts +0 -12
  194. package/lib/knex/deps/tarn/dist/PendingOperation.js +0 -48
  195. package/lib/knex/deps/tarn/dist/Pool.d.ts +0 -97
  196. package/lib/knex/deps/tarn/dist/Pool.js +0 -516
  197. package/lib/knex/deps/tarn/dist/PromiseInspection.d.ts +0 -16
  198. package/lib/knex/deps/tarn/dist/PromiseInspection.js +0 -21
  199. package/lib/knex/deps/tarn/dist/Resource.d.ts +0 -9
  200. package/lib/knex/deps/tarn/dist/Resource.js +0 -19
  201. package/lib/knex/deps/tarn/dist/TimeoutError.d.ts +0 -2
  202. package/lib/knex/deps/tarn/dist/TimeoutError.js +0 -5
  203. package/lib/knex/deps/tarn/dist/tarn.d.ts +0 -3
  204. package/lib/knex/deps/tarn/dist/tarn.js +0 -10
  205. package/lib/knex/deps/tarn/dist/utils.d.ts +0 -14
  206. package/lib/knex/deps/tarn/dist/utils.js +0 -60
  207. package/lib/knex/knex.d.ts +0 -2
  208. package/lib/knex/knex.js +0 -4
  209. package/lib/knex/sandbox.d.ts +0 -4
  210. package/lib/knex/sandbox.js +0 -23
  211. package/lib/orm/Adapters.d.ts +0 -3
  212. package/lib/orm/Adapters.js +0 -44
  213. package/lib/orm/AggregateFunctions.d.ts +0 -3
  214. package/lib/orm/AggregateFunctions.js +0 -177
  215. package/lib/orm/Associations/Extend.d.ts +0 -22
  216. package/lib/orm/Associations/Extend.js +0 -274
  217. package/lib/orm/Associations/Many.d.ts +0 -16
  218. package/lib/orm/Associations/Many.js +0 -569
  219. package/lib/orm/Associations/One.d.ts +0 -16
  220. package/lib/orm/Associations/One.js +0 -310
  221. package/lib/orm/Associations/_utils.d.ts +0 -14
  222. package/lib/orm/Associations/_utils.js +0 -53
  223. package/lib/orm/ChainFind.d.ts +0 -3
  224. package/lib/orm/ChainFind.js +0 -328
  225. package/lib/orm/ChainInstance.d.ts +0 -6
  226. package/lib/orm/ChainInstance.js +0 -74
  227. package/lib/orm/Debug.d.ts +0 -2
  228. package/lib/orm/Debug.js +0 -17
  229. package/lib/orm/Drivers/DB/base.d.ts +0 -2
  230. package/lib/orm/Drivers/DB/base.js +0 -4
  231. package/lib/orm/Drivers/DB/dm.d.ts +0 -9
  232. package/lib/orm/Drivers/DB/dm.js +0 -24
  233. package/lib/orm/Drivers/DB/mysql.d.ts +0 -9
  234. package/lib/orm/Drivers/DB/mysql.js +0 -24
  235. package/lib/orm/Drivers/DB/postgres.d.ts +0 -9
  236. package/lib/orm/Drivers/DB/postgres.js +0 -23
  237. package/lib/orm/Drivers/DB/sqlite3.d.ts +0 -14
  238. package/lib/orm/Drivers/DB/sqlite3.js +0 -40
  239. package/lib/orm/Drivers/DML/_ddl-sql.d.ts +0 -3
  240. package/lib/orm/Drivers/DML/_ddl-sql.js +0 -52
  241. package/lib/orm/Drivers/DML/_dml-helpers.d.ts +0 -19
  242. package/lib/orm/Drivers/DML/_dml-helpers.js +0 -51
  243. package/lib/orm/Drivers/DML/_shared.d.ts +0 -9
  244. package/lib/orm/Drivers/DML/_shared.js +0 -39
  245. package/lib/orm/Drivers/DML/_utils.d.ts +0 -8
  246. package/lib/orm/Drivers/DML/_utils.js +0 -67
  247. package/lib/orm/Drivers/DML/constants.d.ts +0 -3
  248. package/lib/orm/Drivers/DML/constants.js +0 -3
  249. package/lib/orm/Drivers/DML/dm.d.ts +0 -2
  250. package/lib/orm/Drivers/DML/index.d.ts +0 -4
  251. package/lib/orm/Drivers/DML/index.js +0 -7
  252. package/lib/orm/Drivers/DML/mysql.d.ts +0 -2
  253. package/lib/orm/Drivers/DML/mysql.js +0 -252
  254. package/lib/orm/Drivers/DML/postgres.d.ts +0 -2
  255. package/lib/orm/Drivers/DML/postgres.js +0 -277
  256. package/lib/orm/Drivers/DML/sqlite.d.ts +0 -2
  257. package/lib/orm/Drivers/DML/sqlite.js +0 -267
  258. package/lib/orm/Error.d.ts +0 -11
  259. package/lib/orm/Error.js +0 -41
  260. package/lib/orm/Helpers.d.ts +0 -35
  261. package/lib/orm/Helpers.js +0 -234
  262. package/lib/orm/Hook.d.ts +0 -13
  263. package/lib/orm/Hook.js +0 -100
  264. package/lib/orm/Instance.d.ts +0 -2
  265. package/lib/orm/Instance.js +0 -698
  266. package/lib/orm/LazyLoad.d.ts +0 -4
  267. package/lib/orm/LazyLoad.js +0 -76
  268. package/lib/orm/Model.d.ts +0 -6
  269. package/lib/orm/Model.js +0 -974
  270. package/lib/orm/ORM.d.ts +0 -37
  271. package/lib/orm/ORM.js +0 -242
  272. package/lib/orm/Patch/fib-cache.js +0 -2
  273. package/lib/orm/Patch/plugin.d.ts +0 -4
  274. package/lib/orm/Patch/plugin.js +0 -14
  275. package/lib/orm/Property.d.ts +0 -9
  276. package/lib/orm/Property.js +0 -71
  277. package/lib/orm/Settings.d.ts +0 -42
  278. package/lib/orm/Settings.js +0 -120
  279. package/lib/orm/Singleton.d.ts +0 -5
  280. package/lib/orm/Singleton.js +0 -73
  281. package/lib/orm/Typo/Adapter.d.ts +0 -5
  282. package/lib/orm/Typo/Adapter.js +0 -2
  283. package/lib/orm/Typo/DMLDriver.d.ts +0 -209
  284. package/lib/orm/Typo/DMLDriver.js +0 -2
  285. package/lib/orm/Typo/Db.d.ts +0 -35
  286. package/lib/orm/Typo/Db.js +0 -2
  287. package/lib/orm/Typo/Error.d.ts +0 -29
  288. package/lib/orm/Typo/Error.js +0 -2
  289. package/lib/orm/Typo/ORM.d.ts +0 -231
  290. package/lib/orm/Typo/ORM.js +0 -4
  291. package/lib/orm/Typo/Validators.d.ts +0 -31
  292. package/lib/orm/Typo/Validators.js +0 -3
  293. package/lib/orm/Typo/_common.d.ts +0 -37
  294. package/lib/orm/Typo/_common.js +0 -2
  295. package/lib/orm/Typo/assoc.d.ts +0 -221
  296. package/lib/orm/Typo/assoc.js +0 -2
  297. package/lib/orm/Typo/hook.d.ts +0 -42
  298. package/lib/orm/Typo/hook.js +0 -2
  299. package/lib/orm/Typo/instance.d.ts +0 -156
  300. package/lib/orm/Typo/instance.js +0 -2
  301. package/lib/orm/Typo/model.js +0 -7
  302. package/lib/orm/Typo/patch.d.ts +0 -31
  303. package/lib/orm/Typo/patch.js +0 -2
  304. package/lib/orm/Typo/property.d.ts +0 -57
  305. package/lib/orm/Typo/property.js +0 -2
  306. package/lib/orm/Typo/query.d.ts +0 -216
  307. package/lib/orm/Typo/query.js +0 -2
  308. package/lib/orm/Typo/settings.js +0 -6
  309. package/lib/orm/Typo/synchronous.js +0 -2
  310. package/lib/orm/Utilities.d.ts +0 -178
  311. package/lib/orm/Utilities.js +0 -975
  312. package/lib/orm/Validators.d.ts +0 -33
  313. package/lib/orm/Validators.js +0 -101
  314. package/lib/orm/Where/common.d.ts +0 -1
  315. package/lib/orm/Where/common.js +0 -7
  316. package/lib/orm/Where/filterDate.d.ts +0 -11
  317. package/lib/orm/Where/filterDate.js +0 -58
  318. package/lib/orm/index.d.ts +0 -67
  319. package/lib/orm/index.js +0 -158
  320. package/lib/orm-core/callback.d.ts +0 -12
  321. package/lib/orm-core/callback.js +0 -2
  322. package/lib/orm-core/dialect.js +0 -2
  323. package/lib/orm-core/error.d.ts +0 -16
  324. package/lib/orm-core/error.js +0 -29
  325. package/lib/orm-core/index.js +0 -7
  326. package/lib/orm-core/sync.d.ts +0 -12
  327. package/lib/orm-core/sync.js +0 -2
  328. package/lib/orm-core/utils.d.ts +0 -1
  329. package/lib/orm-core/utils.js +0 -6
  330. package/lib/orm-property/Property.d.ts +0 -78
  331. package/lib/orm-property/Property.js +0 -2
  332. package/lib/orm-property/Utils.d.ts +0 -9
  333. package/lib/orm-property/Utils.js +0 -13
  334. package/lib/orm-property/customTypes.d.ts +0 -2
  335. package/lib/orm-property/customTypes.js +0 -13
  336. package/lib/orm-property/index.d.ts +0 -10
  337. package/lib/orm-property/index.js +0 -23
  338. package/lib/orm-property/transformer.d.ts +0 -5
  339. package/lib/orm-property/transformer.js +0 -10
  340. package/lib/orm-property/transformers/dm.d.ts +0 -29
  341. package/lib/orm-property/transformers/index.d.ts +0 -4
  342. package/lib/orm-property/transformers/index.js +0 -6
  343. package/lib/orm-property/transformers/mysql.d.ts +0 -101
  344. package/lib/orm-property/transformers/mysql.js +0 -262
  345. package/lib/orm-property/transformers/postgresql.d.ts +0 -54
  346. package/lib/orm-property/transformers/postgresql.js +0 -190
  347. package/lib/orm-property/transformers/sqlite.d.ts +0 -19
  348. package/lib/orm-property/transformers/sqlite.js +0 -172
  349. package/lib/sql-ddl-sync/Dialects/dm.d.ts +0 -39
  350. package/lib/sql-ddl-sync/Dialects/dm.js +0 -297
  351. package/lib/sql-ddl-sync/Dialects/index.d.ts +0 -6
  352. package/lib/sql-ddl-sync/Dialects/index.js +0 -8
  353. package/lib/sql-ddl-sync/Dialects/mysql.d.ts +0 -38
  354. package/lib/sql-ddl-sync/Dialects/mysql.js +0 -241
  355. package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +0 -39
  356. package/lib/sql-ddl-sync/Dialects/postgresql.js +0 -287
  357. package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +0 -43
  358. package/lib/sql-ddl-sync/Dialects/sqlite.js +0 -257
  359. package/lib/sql-ddl-sync/SQL.d.ts +0 -12
  360. package/lib/sql-ddl-sync/SQL.js +0 -108
  361. package/lib/sql-ddl-sync/Typo/Collection.d.ts +0 -13
  362. package/lib/sql-ddl-sync/Typo/Collection.js +0 -2
  363. package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +0 -13
  364. package/lib/sql-ddl-sync/Typo/DbIndex.js +0 -2
  365. package/lib/sql-ddl-sync/Typo/Dialect.js +0 -2
  366. package/lib/sql-ddl-sync/Typo/Driver.d.ts +0 -53
  367. package/lib/sql-ddl-sync/Typo/Driver.js +0 -2
  368. package/lib/sql-ddl-sync/Typo/Query.d.ts +0 -7
  369. package/lib/sql-ddl-sync/Typo/Query.js +0 -2
  370. package/lib/sql-ddl-sync/Typo/SQL.d.ts +0 -31
  371. package/lib/sql-ddl-sync/Typo/SQL.js +0 -2
  372. package/lib/sql-ddl-sync/Typo/_common.d.ts +0 -29
  373. package/lib/sql-ddl-sync/Typo/_common.js +0 -2
  374. package/lib/sql-ddl-sync/Utils.d.ts +0 -39
  375. package/lib/sql-ddl-sync/Utils.js +0 -217
  376. package/lib/sql-ddl-sync/index.d.ts +0 -87
  377. package/lib/sql-ddl-sync/index.js +0 -372
  378. package/lib/sql-query/Comparators.d.ts +0 -3
  379. package/lib/sql-query/Comparators.js +0 -95
  380. package/lib/sql-query/Create.d.ts +0 -35
  381. package/lib/sql-query/Create.js +0 -78
  382. package/lib/sql-query/Dialects/dm.d.ts +0 -3
  383. package/lib/sql-query/Dialects/index.js +0 -13
  384. package/lib/sql-query/Dialects/mssql.d.ts +0 -3
  385. package/lib/sql-query/Dialects/mssql.js +0 -74
  386. package/lib/sql-query/Dialects/mysql.d.ts +0 -3
  387. package/lib/sql-query/Dialects/mysql.js +0 -101
  388. package/lib/sql-query/Dialects/postgresql.d.ts +0 -3
  389. package/lib/sql-query/Dialects/postgresql.js +0 -77
  390. package/lib/sql-query/Dialects/sqlite.d.ts +0 -3
  391. package/lib/sql-query/Dialects/sqlite.js +0 -62
  392. package/lib/sql-query/Helpers.d.ts +0 -37
  393. package/lib/sql-query/Helpers.js +0 -260
  394. package/lib/sql-query/Insert.d.ts +0 -12
  395. package/lib/sql-query/Insert.js +0 -34
  396. package/lib/sql-query/Query.d.ts +0 -28
  397. package/lib/sql-query/Query.js +0 -120
  398. package/lib/sql-query/Remove.d.ts +0 -16
  399. package/lib/sql-query/Remove.js +0 -94
  400. package/lib/sql-query/Select.d.ts +0 -62
  401. package/lib/sql-query/Select.js +0 -534
  402. package/lib/sql-query/Set.d.ts +0 -4
  403. package/lib/sql-query/Set.js +0 -16
  404. package/lib/sql-query/Typo/Aggregation.d.ts +0 -32
  405. package/lib/sql-query/Typo/Aggregation.js +0 -2
  406. package/lib/sql-query/Typo/Comparators.d.ts +0 -140
  407. package/lib/sql-query/Typo/Comparators.js +0 -2
  408. package/lib/sql-query/Typo/Dialect.d.ts +0 -39
  409. package/lib/sql-query/Typo/Dialect.js +0 -2
  410. package/lib/sql-query/Typo/Field.d.ts +0 -8
  411. package/lib/sql-query/Typo/Field.js +0 -2
  412. package/lib/sql-query/Typo/Helper.d.ts +0 -4
  413. package/lib/sql-query/Typo/Helper.js +0 -2
  414. package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +0 -80
  415. package/lib/sql-query/Typo/Query-ChainBuilder.js +0 -2
  416. package/lib/sql-query/Typo/Query.d.ts +0 -34
  417. package/lib/sql-query/Typo/Query.js +0 -17
  418. package/lib/sql-query/Typo/Sql.d.ts +0 -143
  419. package/lib/sql-query/Typo/Sql.js +0 -2
  420. package/lib/sql-query/Typo/SubQuery.d.ts +0 -39
  421. package/lib/sql-query/Typo/SubQuery.js +0 -2
  422. package/lib/sql-query/Typo/index.js +0 -2
  423. package/lib/sql-query/Update.d.ts +0 -15
  424. package/lib/sql-query/Update.js +0 -39
  425. package/lib/sql-query/Where.d.ts +0 -4
  426. package/lib/sql-query/Where.js +0 -280
  427. /package/{lib → src}/codepoints-map.json +0 -0
@@ -0,0 +1,944 @@
1
+ import { createRequire } from 'node:module';
2
+ const require = createRequire(import.meta.url);
3
+ const util = require('util');
4
+ import { EventEmitter } from 'events';
5
+
6
+ import * as FxORMCore from '../orm-core/index';
7
+
8
+ import * as Utilities from './Utilities';
9
+ import * as Hook from './Hook';
10
+ import ORMError from "./Error";
11
+ const enforce = require("@fibjs/enforce");
12
+ import * as Helpers from './Helpers';
13
+
14
+ import type { FxOrmNS } from './Typo/ORM';
15
+ import type { FxOrmInstance } from './Typo/instance';
16
+ import type { FxOrmModel } from './Typo/model';
17
+ import type { FxOrmError } from './Typo/Error';
18
+ import type { FxOrmProperty } from './Typo/property';
19
+ import type { FxOrmCommon } from './Typo/_common';
20
+ import type { FxOrmAssociation } from './Typo/assoc';
21
+
22
+ import type {
23
+ FxSqlQuerySql,
24
+ FxSqlQuerySubQuery
25
+ } from '../sql-query/Query';
26
+
27
+ interface EmitEventFunctionInInstance {
28
+ (state: string, err?: Error | Error[], _instance?: any): void
29
+ (state: string, _instance?: any): void
30
+ }
31
+
32
+ export const Instance = function (
33
+ this: FxOrmInstance.Instance,
34
+ Model: FxOrmModel.Model, _opts: FxOrmInstance.InstanceConstructorOptions
35
+ ) {
36
+ const instRtd: FxOrmInstance.NormalizedInnerInstanceRuntimeData = util.extend({}, _opts);
37
+ instRtd.data = instRtd.data || {};
38
+ instRtd.extra = instRtd.extra || {};
39
+ if (!instRtd.keys && !Utilities.isVirtualViewModel(Model)) {
40
+ instRtd.keys = ['id'];
41
+ }
42
+
43
+ instRtd.changes = (instRtd.isNew ? Object.keys(instRtd.data) : []);
44
+ instRtd.extrachanges = [];
45
+ instRtd.associations = {};
46
+ instRtd.events = util.extend({}, instRtd.events);
47
+ instRtd.originalKeyValues = {};
48
+ instRtd.__validationData = instRtd.__validationData || {};
49
+
50
+ const eventor = new EventEmitter();
51
+
52
+ var instance_saving = false;
53
+ var instance: FxOrmInstance.Instance = {} as FxOrmInstance.Instance;
54
+
55
+ var emitCallbackStyleEvent: EmitEventFunctionInInstance = function (event: string, ...args: any[]) {
56
+ eventor.emit(event, ...args);
57
+ };
58
+ var rememberKeys = function () {
59
+ for(let i = 0; i < instRtd.keyProperties.length; i++) {
60
+ const prop = instRtd.keyProperties[i];
61
+ instRtd.originalKeyValues[prop.name] = instRtd.data[prop.name];
62
+ }
63
+ };
64
+ var shouldSaveAssocs = function (
65
+ saveOptions: {
66
+ saveAssociations?: boolean
67
+ }
68
+ ) {
69
+ if (Model.settings.get("instance.saveAssociationsByDefault")) {
70
+ return saveOptions.saveAssociations !== false;
71
+ } else {
72
+ return !!saveOptions.saveAssociations;
73
+ }
74
+ };
75
+
76
+ const returnAllErrors = Model.settings.get("instance.returnAllErrors");
77
+
78
+ const handleValidationsSync = function (): FxOrmError.ExtendedError | FxOrmError.ExtendedError[] {
79
+ let required: boolean,
80
+ alwaysValidate: boolean;
81
+
82
+ let validationErr: FxOrmError.ExtendedError | FxOrmError.ExtendedError[] | undefined
83
+ Hook.wait(instance, instRtd.hooks.beforeValidation, function (err: FxOrmError.ExtendedError) {
84
+ if (err) {
85
+ validationErr = err;
86
+ // saveError(err);
87
+ return ;
88
+ }
89
+
90
+ const checks = new enforce.Enforce({ returnAllErrors });
91
+
92
+ for (let k in instRtd.validations) {
93
+ required = false;
94
+
95
+ if (Model.__propertiesByName[k]) {
96
+ required = !!Model.__propertiesByName[k].required;
97
+ alwaysValidate = !!Model.__propertiesByName[k].alwaysValidate;
98
+ } else {
99
+ for (let i = 0; i < instRtd.one_associations.length; i++) {
100
+ /* non-normalized `field` maybe string now */
101
+ if ((instRtd.one_associations[i].field as any) === k) {
102
+ required = !!instRtd.one_associations[i].required;
103
+ break;
104
+ }
105
+ }
106
+ }
107
+ if (!alwaysValidate && !required && instance[k] == null) {
108
+ continue; // avoid validating if property is not required and is "empty"
109
+ }
110
+
111
+ for (const validator of instRtd.validations[k] as FibjsEnforce.IValidator[]) {
112
+ checks.add(k, validator);
113
+ }
114
+ }
115
+
116
+ checks.context("instance", instance);
117
+ checks.context("model", Model);
118
+ checks.context("driver", instRtd.driver);
119
+
120
+ const errors = checks.checkSync(instance);
121
+ if (errors && errors.length)
122
+ validationErr = returnAllErrors ? errors : errors[0]
123
+ });
124
+
125
+ return validationErr as FxOrmError.ExtendedError | FxOrmError.ExtendedError[];
126
+ };
127
+
128
+ const saveError = function (
129
+ err: FxOrmError.ExtendedError | FxOrmError.ExtendedError[],
130
+ ) {
131
+ instance_saving = false;
132
+
133
+ emitCallbackStyleEvent("save", err, instance);
134
+
135
+ Hook.trigger(instance, instRtd.hooks.afterSave, false);
136
+
137
+ return instance;
138
+ };
139
+
140
+ const throwErrorIfExistsOnSave = function (errors: FxOrmError.ExtendedError | FxOrmError.ExtendedError[], forceList = false) {
141
+ const origErrorIsList = Array.isArray(errors);
142
+
143
+ const validationId = Utilities.getUUID();
144
+ instRtd.__validationData[validationId] = Utilities.arraify(errors).filter(Boolean);
145
+
146
+ const setErrors = (errors: FxOrmError.ExtendedError | FxOrmError.ExtendedError[]) => {
147
+ instRtd.__validationData[validationId] = Utilities.arraify(errors).filter(Boolean);
148
+ };
149
+
150
+ Hook.trigger(instance, instRtd.hooks.afterValidation, {
151
+ errors: instRtd.__validationData[validationId].slice(0),
152
+ setErrors
153
+ });
154
+
155
+ const tmp = Utilities.arraify(instRtd.__validationData[validationId]);
156
+ delete instRtd.__validationData[validationId];
157
+
158
+ const firstErr = Utilities.firstEl(tmp);
159
+ if (!firstErr) return ;
160
+
161
+ if (origErrorIsList) {
162
+ saveError(tmp);
163
+ throw tmp;
164
+ } else {
165
+ saveError(firstErr);
166
+ throw firstErr;
167
+ }
168
+ }
169
+
170
+ const saveInstanceSync = function (
171
+ saveOptions: FxOrmInstance.SaveOptions,
172
+ ) {
173
+ // what this condition means:
174
+ // - If the instance is in state mode
175
+ // - AND it's not an association that is asking it to save
176
+ // -> return has already saved
177
+ if (instance_saving && saveOptions.saveAssociations !== false)
178
+ return instance;
179
+
180
+ instance_saving = true;
181
+
182
+ throwErrorIfExistsOnSave(handleValidationsSync(), returnAllErrors);
183
+
184
+ if (instRtd.isNew) {
185
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'create', {});
186
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {});
187
+ Hook.wait(instance, [ instRtd.hooks['beforeCreate'], instRtd.hooks['beforeSave'] ], function (err: FxOrmError.ExtendedError) {
188
+ if (err) {
189
+ saveError(err);
190
+ throw err;
191
+ }
192
+
193
+ saveNewSync(saveOptions, getInstanceData());
194
+ });
195
+ } else {
196
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {});
197
+ Hook.wait(instance, [ instRtd.hooks['beforeSave'] ], function (err: FxOrmError.ExtendedError) {
198
+ if (err) {
199
+ saveError(err);
200
+ throw err;
201
+ }
202
+
203
+ savePersistedSync(saveOptions, getInstanceData());
204
+ });
205
+ }
206
+ };
207
+
208
+ const runSyncAfterSaveActions = function (is_create?: boolean, err?: Error) {
209
+ instance_saving = false;
210
+
211
+ emitCallbackStyleEvent("save", err, instance);
212
+
213
+ if (is_create)
214
+ Hook.trigger(instance, instRtd.hooks.afterCreate, !err);
215
+
216
+ Hook.trigger(instance, instRtd.hooks.afterSave, !err);
217
+ };
218
+ const getInstanceData = function () {
219
+ const data: FxOrmInstance.InstanceDataPayload = {};
220
+ let prop: FxOrmProperty.NormalizedProperty;
221
+
222
+ for (let k in instRtd.data) {
223
+ if (!instRtd.data.hasOwnProperty(k)) continue;
224
+ prop = Model.allProperties[k];
225
+
226
+ if (prop) {
227
+ if (instRtd.data[k] == null && (prop.type == 'serial' || typeof prop.defaultValue == 'function')) {
228
+ continue;
229
+ }
230
+
231
+ if (instRtd.driver.propertyToValue) {
232
+ data[k] = instRtd.driver.propertyToValue(instRtd.data[k], prop);
233
+ } else {
234
+ data[k] = instRtd.data[k];
235
+ }
236
+ } else {
237
+ data[k] = instRtd.data[k];
238
+ }
239
+ }
240
+
241
+ return data;
242
+ };
243
+
244
+ const resetChanges = function () {
245
+ instRtd.changes.length = 0;
246
+ }
247
+
248
+ const saveNewSync = function (
249
+ saveOptions: FxOrmInstance.SaveOptions,
250
+ data: FxOrmInstance.InstanceDataPayload,
251
+ ) {
252
+ data = util.omit(data, Object.keys(Model.virtualProperties));
253
+ data = Utilities.transformPropertyNames(data, Model.allProperties);
254
+
255
+ const info = instRtd.driver.insert(instRtd.table, data, instRtd.keyProperties);
256
+
257
+ resetChanges();
258
+ for (let i = 0, prop; i < instRtd.keyProperties.length; i++) {
259
+ prop = instRtd.keyProperties[i];
260
+ instRtd.data[prop.name] = info.hasOwnProperty(prop.name) ? info[prop.name] : data[prop.name];
261
+ }
262
+ instRtd.isNew = false;
263
+ rememberKeys();
264
+
265
+ let err: FxOrmError.ExtendedError | undefined;
266
+
267
+ if (shouldSaveAssocs(saveOptions)) {
268
+ const syncReponse = FxORMCore.catchBlocking<boolean>(saveAssociationsSync)
269
+ err = syncReponse.error;
270
+ }
271
+
272
+ runSyncAfterSaveActions(true, err);
273
+
274
+ if (err)
275
+ throw err;
276
+
277
+ saveInstanceExtraSync();
278
+ };
279
+
280
+ const oneOneAssocs = Object.values(Model.associations).filter(assoc => assoc.type === 'hasOne');
281
+
282
+ const savePersistedSync = function (
283
+ saveOptions: FxOrmInstance.SaveOptions,
284
+ data: FxOrmInstance.InstanceDataPayload,
285
+ ) {
286
+ let changes = {} as FxSqlQuerySql.DataToSet,
287
+ conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
288
+
289
+ const savedCheckSync = function (saved: boolean) {
290
+ const _shouldSaveAssocs = shouldSaveAssocs(saveOptions)
291
+ if (!saved && !_shouldSaveAssocs)
292
+ return saveInstanceExtraSync();
293
+
294
+ if (!_shouldSaveAssocs) {
295
+ runSyncAfterSaveActions(false);
296
+ return saveInstanceExtraSync();
297
+ }
298
+
299
+ const { error: err, result: assocSaved } = FxORMCore.catchBlocking<boolean>(saveAssociationsSync)
300
+
301
+ if (saved || assocSaved) {
302
+ runSyncAfterSaveActions(false, err);
303
+ if (err)
304
+ throw err;
305
+ }
306
+
307
+ return saveInstanceExtraSync();
308
+ }
309
+
310
+ if (instance.saved())
311
+ return savedCheckSync(false);
312
+
313
+ for (let i = 0; i < instRtd.changes.length; i++) {
314
+ changes[instRtd.changes[i]] = data[instRtd.changes[i]];
315
+ }
316
+
317
+ for (let i = 0; i < instRtd.keyProperties.length; i++) {
318
+ const prop = instRtd.keyProperties[i];
319
+ conditions[prop.mapsTo!] = instRtd.originalKeyValues[prop.name];
320
+ }
321
+ changes = util.omit(changes, Object.keys(Model.virtualProperties));
322
+ changes = Utilities.transformPropertyNames(changes, Model.allProperties);
323
+
324
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
325
+
326
+ const syncResponse = FxORMCore.catchBlocking(() => instRtd.driver.update(instRtd.table, changes, conditions));
327
+ fillBackAssociatedFieldsAfterPersist: {
328
+ const couldParallel = false;
329
+
330
+ Utilities.parallelQueryIfPossible(couldParallel, oneOneAssocs, ({ association }) => {
331
+ Utilities.parallelQueryIfPossible(couldParallel, Object.keys(association.field), propName => {
332
+ if (!changes.hasOwnProperty(propName)) {
333
+ return ;
334
+ }
335
+ instance[propName] = changes[propName];
336
+ if (instance.hasOwnProperty(association.name)) {
337
+ instance[association.getSyncAccessor]();
338
+ }
339
+ })
340
+ })
341
+ }
342
+
343
+ if (syncResponse.error) {
344
+ saveError(syncResponse.error)
345
+ throw syncResponse.error;
346
+ }
347
+ resetChanges();
348
+ rememberKeys();
349
+
350
+ savedCheckSync(true);
351
+ };
352
+
353
+ const saveAssociationsSync = function (cb?: FxOrmCommon.ExecutionCallback<boolean>): boolean {
354
+ let pending = 1,
355
+ // to check if error passed by cb if cb exists
356
+ error_passed = false,
357
+ assocSaved = false;
358
+
359
+ const saveAssociationItemSync = function (syncVersionAccessor: string, instances: FxOrmInstance.InstanceDataPayload) {
360
+ pending += 1;
361
+
362
+ let error: FxOrmError.ExtendedError | null | undefined = null;
363
+
364
+ const syncResponse = FxORMCore.catchBlocking(() => {
365
+ instance[syncVersionAccessor](instances);
366
+ });
367
+
368
+ error = syncResponse.error;
369
+
370
+ if (syncResponse.error) {
371
+ if (error_passed) return ;
372
+
373
+ error_passed = true;
374
+ assocSaved = true;
375
+ }
376
+
377
+ if (--pending === 0) {
378
+ assocSaved = true;
379
+ }
380
+
381
+ FxORMCore.takeAwayResult({ error: error || undefined, result: assocSaved }, { callback: cb || undefined });
382
+ };
383
+
384
+ const _saveOneAssociation = function (assoc: FxOrmAssociation.InstanceAssociationItem) {
385
+ if (!instance[assoc.name] || typeof instance[assoc.name] !== "object") return;
386
+ if (assoc.reversed) {
387
+ // reversed hasOne associations should behave like hasMany
388
+ if (!Array.isArray(instance[assoc.name])) {
389
+ instance[assoc.name] = [ instance[assoc.name] ];
390
+ }
391
+
392
+ const instances = instance[assoc.name] as FxOrmInstance.Instance[];
393
+ Utilities.parallelQueryIfPossible(
394
+ false,
395
+ instances,
396
+ (item) => {
397
+ if (!item.isInstance) {
398
+ item = new assoc.model(item);
399
+ }
400
+ saveAssociationItemSync(assoc.setSyncAccessor, item);
401
+ }
402
+ );
403
+
404
+ return;
405
+ }
406
+
407
+ if (!instance[assoc.name].isInstance) {
408
+ instance[assoc.name] = new assoc.model(instance[assoc.name]);
409
+ }
410
+
411
+ saveAssociationItemSync(assoc.setSyncAccessor, instance[assoc.name]);
412
+ };
413
+
414
+ for (let i = 0; i < instRtd.one_associations.length; i++) {
415
+ _saveOneAssociation(instRtd.one_associations[i]);
416
+ }
417
+
418
+ const _saveManyAssociation = function (assoc: FxOrmAssociation.InstanceAssociationItem) {
419
+ var assocVal = instance[assoc.name];
420
+
421
+ if (!Array.isArray(assocVal)) return;
422
+ if (!instRtd.associations[assoc.name].changed) return;
423
+
424
+ for (let j = 0; j < assocVal.length; j++) {
425
+ if (!assocVal[j].isInstance) {
426
+ assocVal[j] = new assoc.model(assocVal[j]);
427
+ }
428
+ }
429
+
430
+ saveAssociationItemSync(assoc.setSyncAccessor, assocVal);
431
+ };
432
+
433
+ for (let i = 0; i < instRtd.many_associations.length; i++) {
434
+ _saveManyAssociation(instRtd.many_associations[i]);
435
+ }
436
+
437
+ if (--pending === 0)
438
+ cb && cb(null, false);
439
+
440
+ return assocSaved;
441
+ };
442
+ var getNormalizedExtraDataAtPropertyTime = function () {
443
+ return instRtd.extra as Record<string, FxOrmProperty.NormalizedProperty>
444
+ };
445
+
446
+ const saveInstanceExtraSync = function (): FxOrmInstance.Instance {
447
+ if (instRtd.extrachanges.length === 0)
448
+ return instance;
449
+
450
+ const extra_info = instRtd.extra_info;
451
+ if (!extra_info)
452
+ return instance;
453
+
454
+ var data: FxOrmInstance.InstanceDataPayload = {};
455
+ var conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
456
+
457
+ for (let i = 0; i < instRtd.extrachanges.length; i++) {
458
+ if (!instRtd.data.hasOwnProperty(instRtd.extrachanges[i])) continue;
459
+
460
+ if (getNormalizedExtraDataAtPropertyTime()[instRtd.extrachanges[i]]) {
461
+ data[instRtd.extrachanges[i]] = instRtd.data[instRtd.extrachanges[i]];
462
+ if (instRtd.driver.propertyToValue) {
463
+ data[instRtd.extrachanges[i]] = instRtd.driver.propertyToValue(data[instRtd.extrachanges[i]], getNormalizedExtraDataAtPropertyTime()[instRtd.extrachanges[i]]);
464
+ }
465
+ } else {
466
+ data[instRtd.extrachanges[i]] = instRtd.data[instRtd.extrachanges[i]];
467
+ }
468
+ }
469
+
470
+ for (let i = 0; i < extra_info.id.length; i++) {
471
+ conditions[extra_info.id_prop[i]] = extra_info.id[i];
472
+ conditions[extra_info.assoc_prop[i]] = instRtd.data[instRtd.keys[i]];
473
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
474
+ }
475
+
476
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
477
+
478
+ instRtd.driver.update(extra_info.table, data, conditions);
479
+
480
+ return instance;
481
+ };
482
+
483
+ const saveInstanceProperty = function (key: string, value: any) {
484
+ const changes: FxOrmInstance.InstanceDataPayload = {},
485
+ conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
486
+ changes[key] = value;
487
+
488
+ if (Model.properties[key])
489
+ if (instRtd.driver.propertyToValue)
490
+ changes[key] = instRtd.driver.propertyToValue(changes[key], Model.properties[key]);
491
+
492
+ for (let i = 0; i < instRtd.keys.length; i++) {
493
+ conditions[instRtd.keys[i]] = instRtd.data[instRtd.keys[i]];
494
+ }
495
+
496
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'save', {});
497
+ Hook.wait(instance, instRtd.hooks.beforeSave, function (err: FxOrmError.ExtendedError) {
498
+ if (err) {
499
+ Hook.trigger(instance, instRtd.hooks.afterSave, false);
500
+ emitCallbackStyleEvent("save", err, instance);
501
+ return;
502
+ }
503
+
504
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
505
+ const syncReponse = FxORMCore.catchBlocking(() => instRtd.driver.update(instRtd.table, changes, conditions))
506
+ if (!syncReponse.error)
507
+ instRtd.data[key] = value;
508
+
509
+ Hook.trigger(instance, instRtd.hooks.afterSave, !syncReponse.error);
510
+ emitCallbackStyleEvent("save", syncReponse.error, instance);
511
+ });
512
+ };
513
+ const setInstanceProperty = function (key: string, value: any) {
514
+ const prop = Model.__propertiesByName[key] || getNormalizedExtraDataAtPropertyTime()[key];
515
+
516
+ if (prop) {
517
+ if ('valueToProperty' in instRtd.driver) {
518
+ value = instRtd.driver.valueToProperty(value, prop);
519
+ }
520
+ if (instRtd.data[key] !== value) {
521
+ instRtd.data[key] = value;
522
+ return true;
523
+ }
524
+ }
525
+ return false;
526
+ }
527
+
528
+ // ('data.a.b', 5) => instRtd.data.a.b = 5
529
+ const setPropertyByPath: FxOrmInstance.Instance['set'] = function (path, value) {
530
+ if (typeof path == 'string') {
531
+ path = path.split('.');
532
+ } else if (!Array.isArray(path)) {
533
+ return;
534
+ }
535
+
536
+ var propName = path.shift()!;
537
+ var prop = Model.__propertiesByName[propName] || getNormalizedExtraDataAtPropertyTime()[propName];
538
+ var currKey: string, currObj: any;
539
+
540
+ if (!prop) {
541
+ return;
542
+ }
543
+ if (path.length == 0) {
544
+ instance[propName] = value;
545
+ return;
546
+ }
547
+ currObj = instance[propName];
548
+
549
+ while(currObj && path.length > 0 ) {
550
+ currKey = path.shift()!;
551
+
552
+ if (path.length > 0) {
553
+ currObj = currObj[currKey];
554
+ } else if (currObj[currKey] !== value) {
555
+ currObj[currKey] = value;
556
+ instRtd.changes.push(propName);
557
+ }
558
+ }
559
+ }
560
+
561
+ var addInstanceProperty = function (key: string) {
562
+ var defaultValue = null;
563
+ var prop = Model.__propertiesByName[key];
564
+
565
+ // This code was first added, and then commented out in a later commit.
566
+ // Its presence doesn't affect tests, so I'm just gonna log if it ever gets called.
567
+ // If someone complains about noise, we know it does something, and figure it out then.
568
+ if (instance.hasOwnProperty(key))
569
+ console.log("Overwriting instance property");
570
+
571
+ if (key in instRtd.data) {
572
+ defaultValue = instRtd.data[key];
573
+ } else if (prop && 'defaultValue' in prop) {
574
+ defaultValue = prop.defaultValue;
575
+ }
576
+
577
+ setInstanceProperty(key, defaultValue);
578
+
579
+ Object.defineProperty(instance, key, {
580
+ get: function () {
581
+ return instRtd.data[key];
582
+ },
583
+ set: function (val) {
584
+ if (prop.key === true) {
585
+ if (prop.type == 'serial' && instRtd.data[key] != null) {
586
+ if (!(val === null || val === undefined)) {
587
+ return ;
588
+ }
589
+ } else {
590
+ instRtd.originalKeyValues[prop.name] = instRtd.data[prop.name];
591
+ }
592
+ }
593
+
594
+ if (!setInstanceProperty(key, val)) {
595
+ return;
596
+ }
597
+
598
+ if (instRtd.autoSave) {
599
+ saveInstanceProperty(key, val);
600
+ } else if (instRtd.changes.indexOf(key) === -1) {
601
+ instRtd.changes.push(key);
602
+ }
603
+ },
604
+ enumerable: !(prop && !prop.enumerable)
605
+ });
606
+ };
607
+ var addInstanceExtraProperty = function (key: string) {
608
+ if (!instance.hasOwnProperty("extra") || instance.extra === "") {
609
+ instance.extra = {};
610
+ }
611
+ Object.defineProperty(instance.extra, key, {
612
+ get: function () {
613
+ return instRtd.data[key];
614
+ },
615
+ set: function (val) {
616
+ setInstanceProperty(key, val);
617
+
618
+ /*if (instRtd.autoSave) {
619
+ saveInstanceProperty(key, val);
620
+ }*/
621
+ if (instRtd.extrachanges.indexOf(key) === -1) {
622
+ instRtd.extrachanges.push(key);
623
+ }
624
+ },
625
+ enumerable: true
626
+ });
627
+ };
628
+
629
+ for (let k of Object.keys(Model.__propertiesByName)) {
630
+ addInstanceProperty(k);
631
+ }
632
+ for (let k in instRtd.extra) {
633
+ addInstanceProperty(k);
634
+ }
635
+
636
+ for (let k in instRtd.methods) {
637
+ Utilities.addHiddenPropertyToInstance(instance, k, instRtd.methods[k].bind(instance), { writable: true });
638
+ }
639
+
640
+ for (let k in instRtd.extra) {
641
+ addInstanceExtraProperty(k);
642
+ }
643
+
644
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "on", function (this: any, event: string, cb: FxOrmCommon.VoidCallback) {
645
+ cb = Utilities.bindInstance(instance, cb);
646
+ eventor.on(event, cb);
647
+ return this;
648
+ });
649
+
650
+ const eventEmitterArgsToMap = function (restArgs: any[]): {[k: string]: (...args: any[]) => any} | string {
651
+ let map: any = restArgs[0]
652
+ if (typeof map === 'string' && typeof restArgs[1] === 'function')
653
+ map = {[map]: restArgs[1]}
654
+
655
+ return map;
656
+ }
657
+
658
+ // Keep the bound callbacks so $off(event, listener) can remove the exact
659
+ // listener that was registered by $on(). Binding on every call would create
660
+ // a new function identity and silently leak the listener.
661
+ const boundEventCallbacks: { [event: string]: Map<Function, (...args: any[]) => void> } = Object.create(null);
662
+
663
+ const getBoundEventCallback = function (event: string, cb: FxOrmCommon.VoidCallback): (...args: any[]) => void {
664
+ let bindings = boundEventCallbacks[event];
665
+ if (!bindings) {
666
+ bindings = new Map<Function, (...args: any[]) => void>();
667
+ boundEventCallbacks[event] = bindings;
668
+ }
669
+
670
+ let bound = bindings.get(cb);
671
+ if (!bound) {
672
+ bound = Utilities.bindInstance(instance, cb);
673
+ bindings.set(cb, bound);
674
+ }
675
+
676
+ return bound;
677
+ };
678
+
679
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "$on", function (...args: any) {
680
+ const mapOrString = eventEmitterArgsToMap(args);
681
+ if (typeof mapOrString === 'string') {
682
+ if (typeof args[1] !== 'function')
683
+ return ;
684
+
685
+ return eventor.on(mapOrString, getBoundEventCallback(mapOrString, args[1]));
686
+ }
687
+
688
+ Object.keys(mapOrString).forEach(event => {
689
+ eventor.on(event, getBoundEventCallback(event, mapOrString[event]));
690
+ });
691
+ });
692
+
693
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "$off", function (...args: any) {
694
+ const mapOrString = eventEmitterArgsToMap(args);
695
+ if (typeof mapOrString === 'string') {
696
+ const bindings = boundEventCallbacks[mapOrString];
697
+ const bound = bindings && bindings.get(args[1]);
698
+
699
+ if (bound)
700
+ bindings.delete(args[1]);
701
+
702
+ if (typeof args[1] !== 'function')
703
+ return eventor.off(mapOrString);
704
+
705
+ return eventor.off(mapOrString, bound || args[1]);
706
+ }
707
+
708
+ Object.keys(mapOrString).forEach(event => {
709
+ const bindings = boundEventCallbacks[event];
710
+ const bound = bindings && bindings.get(mapOrString[event]);
711
+
712
+ if (bound)
713
+ bindings.delete(mapOrString[event]);
714
+
715
+ eventor.off(event, bound || mapOrString[event]);
716
+ });
717
+ });
718
+
719
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "$emit", function (event: string, ...args: any[]) {
720
+ return eventor.emit(event, ...args);
721
+ });
722
+
723
+ const collectParamsForSave = function (args: any[]) {
724
+ var objCount = 0;
725
+ var data: FxOrmInstance.InstanceDataPayload = {},
726
+ saveOptions = {};
727
+
728
+ Helpers.selectArgs(args, function (arg_type, arg) {
729
+ switch (arg_type) {
730
+ case 'object':
731
+ switch (objCount) {
732
+ case 0:
733
+ data = arg;
734
+ break;
735
+ case 1:
736
+ saveOptions = arg;
737
+ break;
738
+ }
739
+ objCount++;
740
+ break;
741
+ default:
742
+ const err: FxOrmNS.ExtensibleError = new Error("Unknown parameter type '" + (typeof arg) + "' in Instance.save()");
743
+ err.model = Model.table;
744
+ throw err;
745
+ }
746
+ });
747
+
748
+ return { saveOptions, data }
749
+ }
750
+
751
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "saveSync", function (this: typeof instance) {
752
+ Utilities.disAllowOpForVModel(Model, 'instance.save');
753
+
754
+ const args = Array.prototype.slice.apply(arguments);
755
+ const { saveOptions, data } = collectParamsForSave(args);
756
+
757
+ for (let k in data) {
758
+ if (data.hasOwnProperty(k)) {
759
+ this[k] = data[k];
760
+ }
761
+ }
762
+
763
+ saveInstanceSync(saveOptions);
764
+
765
+ return instance;
766
+ });
767
+
768
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "save", function (this: typeof instance) {
769
+ var cb: FxOrmCommon.ExecutionCallback<FxOrmInstance.Instance> | null = null;
770
+
771
+ let args: any[] = Array.prototype.slice.apply(arguments);
772
+ Helpers.selectArgs(args, function (arg_type, arg) {
773
+ switch (arg_type) {
774
+ case 'function':
775
+ cb = arg;
776
+ break;
777
+ }
778
+ });
779
+ args = args.filter(x => x !== cb);
780
+ collectParamsForSave(args);
781
+
782
+ process.nextTick(() => {
783
+ const syncResponse = FxORMCore.catchBlocking(instance.saveSync, args);
784
+ FxORMCore.takeAwayResult({ error: syncResponse.error, result: instance }, { no_throw: !!cb, callback: cb || undefined });
785
+ });
786
+
787
+ return this;
788
+ })
789
+
790
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "saved", function (this: typeof instance) {
791
+ return instRtd.changes.length === 0;
792
+ });
793
+
794
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "removeSync", function () {
795
+ Utilities.disAllowOpForVModel(Model, 'instance.remove');
796
+
797
+ if (instRtd.isNew)
798
+ return ;
799
+
800
+ var conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
801
+ for (let i = 0; i < instRtd.keys.length; i++) {
802
+ conditions[instRtd.keys[i]] = instRtd.data[instRtd.keys[i]];
803
+ }
804
+
805
+ let removeErr: FxOrmError.ExtendedError | null | undefined = null;
806
+
807
+ Utilities.attachOnceTypedHookRefToInstance(instance, 'remove', {});
808
+ Hook.wait(instance, instRtd.hooks.beforeRemove, function (err: FxOrmError.ExtendedError) {
809
+ if (err) {
810
+ emitCallbackStyleEvent("remove", err, instance);
811
+
812
+ removeErr = err;
813
+ return ;
814
+ }
815
+
816
+ emitCallbackStyleEvent("beforeRemove", instance);
817
+
818
+ Utilities.filterWhereConditionsInput(conditions, instance.model());
819
+ const syncResponse = FxORMCore.catchBlocking(() => instRtd.driver.remove(instRtd.table, conditions));
820
+
821
+ Hook.trigger(instance, instRtd.hooks.afterRemove, !syncResponse.error);
822
+
823
+ emitCallbackStyleEvent("remove", syncResponse.error, instance);
824
+
825
+ removeErr = syncResponse.error;
826
+
827
+ instance = undefined as any;
828
+ });
829
+
830
+ if (removeErr)
831
+ throw removeErr;
832
+ })
833
+
834
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "remove", function (this: any, cb: FxOrmCommon.ExecutionCallback<FxOrmInstance.Instance>) {
835
+ const syncReponse = FxORMCore.catchBlocking(() => instance.removeSync());
836
+ FxORMCore.takeAwayResult(syncReponse, { callback: cb });
837
+
838
+ return this;
839
+ });
840
+
841
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "set", setPropertyByPath);
842
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "markAsDirty", function (propName: string) {
843
+ if (propName != undefined) {
844
+ instRtd.changes.push(propName);
845
+ }
846
+ });
847
+
848
+ Utilities.addHiddenReadonlyPropertyToInstance(instance, "dirtyProperties", function () { return instRtd.changes; });
849
+
850
+ Utilities.addHiddenPropertyToInstance(instance, "isInstance", true);
851
+
852
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "isPersisted", function (this: typeof instance) {
853
+ return !instRtd.isNew;
854
+ });
855
+
856
+ Utilities.addHiddenPropertyToInstance(instance, "isShell", function () {
857
+ return instRtd.isShell;
858
+ });
859
+
860
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "validateSync", function () {
861
+ Utilities.disAllowOpForVModel(Model, 'instance.validate');
862
+
863
+ return handleValidationsSync() || false;
864
+ });
865
+
866
+ Utilities.addHiddenUnwritableMethodToInstance(instance, "validate", function (cb: FxOrmCommon.GenericCallback<FxOrmError.ExtendedError | FxOrmError.ExtendedError[] |false>) {
867
+ cb(null, instance.validateSync());
868
+ });
869
+
870
+ Utilities.addHiddenPropertyToInstance(instance, "__singleton_uid", function (this: typeof instance) {
871
+ return instRtd.uid;
872
+ });
873
+
874
+ Utilities.addHiddenPropertyToInstance(instance, "__instRtd", instRtd);
875
+ /* just for compat, use __instRtd plz */
876
+ Utilities.addHiddenPropertyToInstance(instance, "__opts", instRtd);
877
+ Utilities.addHiddenPropertyToInstance(instance, "model", function (this: typeof instance) {
878
+ return Model;
879
+ });
880
+
881
+ for (let i = 0; i < instRtd.keyProperties.length; i++) {
882
+ var prop = instRtd.keyProperties[i];
883
+
884
+ if (!(prop.name in instRtd.data)) {
885
+ instRtd.changes = Object.keys(instRtd.data);
886
+ break;
887
+ }
888
+ }
889
+ rememberKeys();
890
+
891
+ instRtd.__setupAssociations(instance);
892
+
893
+ for (let i = 0; i < instRtd.one_associations.length; i++) {
894
+ var asc = instRtd.one_associations[i];
895
+
896
+ if (!asc.reversed && !asc.__for_extension) {
897
+ for (let k in asc.field as Record<string, FxOrmProperty.NormalizedProperty>) {
898
+ if (!instRtd.data.hasOwnProperty(k)) {
899
+ addInstanceProperty(k);
900
+ }
901
+ }
902
+ }
903
+
904
+ if (asc.name in instRtd.data) {
905
+ var d = instRtd.data[asc.name];
906
+ var mapper = function (obj: FxOrmInstance.Instance | FxOrmInstance.InstanceDataPayload) {
907
+ return obj.isInstance ? obj : new asc.model(obj);
908
+ };
909
+
910
+ if (Array.isArray(d)) {
911
+ instance[asc.name] = d.map(mapper);
912
+ } else {
913
+ instance[asc.name] = mapper(d);
914
+ }
915
+ delete instRtd.data[asc.name];
916
+ }
917
+ }
918
+ for (let i = 0; i < instRtd.many_associations.length; i++) {
919
+ var aName = instRtd.many_associations[i].name;
920
+ instRtd.associations[aName] = {
921
+ changed: false, data: instRtd.many_associations[i]
922
+ };
923
+
924
+ if (Array.isArray(instRtd.data[aName])) {
925
+ instance[aName] = instRtd.data[aName];
926
+ delete instRtd.data[aName];
927
+ }
928
+ }
929
+
930
+ Object.keys(instRtd.events).forEach((evtName) => {
931
+ if (typeof instRtd.events[evtName] !== 'function')
932
+ throw new ORMError("INVALID_EVENT_HANDLER", 'PARAM_MISMATCH');
933
+
934
+ instance.on(evtName as FxOrmInstance.InstanceEventType, instRtd.events[evtName]);
935
+ });
936
+
937
+ Hook.wait(instance, instRtd.hooks.afterLoad, function (err: Error) {
938
+ process.nextTick(() => {
939
+ emitCallbackStyleEvent("ready", err, instance);
940
+ });
941
+ });
942
+
943
+ return instance;
944
+ } as any as FxOrmInstance.InstanceConstructor