fib-orm 2.0.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 +36 -34
  5. package/{lib/cli/commands/ddlFrom.js → src/cli/commands/ddlFrom.ts} +40 -26
  6. package/{lib/cli/commands/dumpModel.js → src/cli/commands/dumpModel.ts} +78 -31
  7. package/{lib/cli/commands/escape.js → src/cli/commands/escape.ts} +51 -28
  8. package/{lib/cli/commands/spinner.js → src/cli/commands/spinner.ts} +20 -10
  9. package/{lib/cli/commands/upgrade.js → src/cli/commands/upgrade.ts} +53 -33
  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 -85
  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 -176
  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 -68
  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 -21
  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 -3
  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 -26
  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 -18
  173. package/lib/db-driver/Typo.d.ts +0 -112
  174. package/lib/db-driver/Typo.js +0 -1
  175. package/lib/db-driver/built-ins/base.class.d.ts +0 -96
  176. package/lib/db-driver/built-ins/base.class.js +0 -186
  177. package/lib/db-driver/built-ins/driver-dm.d.ts +0 -18
  178. package/lib/db-driver/built-ins/driver-dm.js +0 -53
  179. package/lib/db-driver/built-ins/driver-mongodb.d.ts +0 -24
  180. package/lib/db-driver/built-ins/driver-mongodb.js +0 -46
  181. package/lib/db-driver/built-ins/driver-mysql.d.ts +0 -18
  182. package/lib/db-driver/built-ins/driver-mysql.js +0 -37
  183. package/lib/db-driver/built-ins/driver-postgresql.d.ts +0 -22
  184. package/lib/db-driver/built-ins/driver-postgresql.js +0 -76
  185. package/lib/db-driver/built-ins/driver-redis.d.ts +0 -12
  186. package/lib/db-driver/built-ins/driver-redis.js +0 -35
  187. package/lib/db-driver/built-ins/driver-sqlite.d.ts +0 -17
  188. package/lib/db-driver/built-ins/driver-sqlite.js +0 -34
  189. package/lib/db-driver/index.d.ts +0 -3
  190. package/lib/db-driver/index.js +0 -4
  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 -3
  209. package/lib/knex/sandbox.d.ts +0 -4
  210. package/lib/knex/sandbox.js +0 -22
  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 -176
  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 -17
  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 -15
  222. package/lib/orm/Associations/_utils.js +0 -52
  223. package/lib/orm/ChainFind.d.ts +0 -3
  224. package/lib/orm/ChainFind.js +0 -327
  225. package/lib/orm/ChainInstance.d.ts +0 -6
  226. package/lib/orm/ChainInstance.js +0 -73
  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 -3
  230. package/lib/orm/Drivers/DB/base.js +0 -3
  231. package/lib/orm/Drivers/DB/dm.d.ts +0 -11
  232. package/lib/orm/Drivers/DB/dm.js +0 -23
  233. package/lib/orm/Drivers/DB/mysql.d.ts +0 -11
  234. package/lib/orm/Drivers/DB/mysql.js +0 -23
  235. package/lib/orm/Drivers/DB/postgres.d.ts +0 -11
  236. package/lib/orm/Drivers/DB/postgres.js +0 -22
  237. package/lib/orm/Drivers/DB/sqlite3.d.ts +0 -16
  238. package/lib/orm/Drivers/DB/sqlite3.js +0 -39
  239. package/lib/orm/Drivers/DML/_ddl-sql.d.ts +0 -3
  240. package/lib/orm/Drivers/DML/_ddl-sql.js +0 -51
  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 -38
  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 -2
  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 -6
  252. package/lib/orm/Drivers/DML/mysql.d.ts +0 -2
  253. package/lib/orm/Drivers/DML/mysql.js +0 -251
  254. package/lib/orm/Drivers/DML/postgres.d.ts +0 -3
  255. package/lib/orm/Drivers/DML/postgres.js +0 -276
  256. package/lib/orm/Drivers/DML/sqlite.d.ts +0 -2
  257. package/lib/orm/Drivers/DML/sqlite.js +0 -266
  258. package/lib/orm/Error.d.ts +0 -11
  259. package/lib/orm/Error.js +0 -40
  260. package/lib/orm/Helpers.d.ts +0 -35
  261. package/lib/orm/Helpers.js +0 -233
  262. package/lib/orm/Hook.d.ts +0 -13
  263. package/lib/orm/Hook.js +0 -99
  264. package/lib/orm/Instance.d.ts +0 -2
  265. package/lib/orm/Instance.js +0 -697
  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 -7
  269. package/lib/orm/Model.js +0 -971
  270. package/lib/orm/ORM.d.ts +0 -37
  271. package/lib/orm/ORM.js +0 -241
  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 -13
  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 -119
  279. package/lib/orm/Singleton.d.ts +0 -5
  280. package/lib/orm/Singleton.js +0 -72
  281. package/lib/orm/Typo/Adapter.d.ts +0 -5
  282. package/lib/orm/Typo/Adapter.js +0 -1
  283. package/lib/orm/Typo/DMLDriver.d.ts +0 -210
  284. package/lib/orm/Typo/DMLDriver.js +0 -1
  285. package/lib/orm/Typo/Db.d.ts +0 -38
  286. package/lib/orm/Typo/Db.js +0 -1
  287. package/lib/orm/Typo/Error.d.ts +0 -31
  288. package/lib/orm/Typo/Error.js +0 -1
  289. package/lib/orm/Typo/ORM.d.ts +0 -233
  290. package/lib/orm/Typo/ORM.js +0 -3
  291. package/lib/orm/Typo/Validators.d.ts +0 -32
  292. package/lib/orm/Typo/Validators.js +0 -2
  293. package/lib/orm/Typo/_common.d.ts +0 -38
  294. package/lib/orm/Typo/_common.js +0 -1
  295. package/lib/orm/Typo/assoc.d.ts +0 -221
  296. package/lib/orm/Typo/assoc.js +0 -1
  297. package/lib/orm/Typo/hook.d.ts +0 -42
  298. package/lib/orm/Typo/hook.js +0 -1
  299. package/lib/orm/Typo/instance.d.ts +0 -157
  300. package/lib/orm/Typo/instance.js +0 -1
  301. package/lib/orm/Typo/model.js +0 -6
  302. package/lib/orm/Typo/patch.d.ts +0 -31
  303. package/lib/orm/Typo/patch.js +0 -1
  304. package/lib/orm/Typo/property.d.ts +0 -57
  305. package/lib/orm/Typo/property.js +0 -1
  306. package/lib/orm/Typo/query.d.ts +0 -216
  307. package/lib/orm/Typo/query.js +0 -1
  308. package/lib/orm/Typo/settings.js +0 -5
  309. package/lib/orm/Typo/synchronous.js +0 -1
  310. package/lib/orm/Utilities.d.ts +0 -179
  311. package/lib/orm/Utilities.js +0 -975
  312. package/lib/orm/Validators.d.ts +0 -34
  313. package/lib/orm/Validators.js +0 -100
  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 -68
  319. package/lib/orm/index.js +0 -157
  320. package/lib/orm-core/callback.d.ts +0 -12
  321. package/lib/orm-core/callback.js +0 -1
  322. package/lib/orm-core/dialect.js +0 -1
  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 -6
  326. package/lib/orm-core/sync.d.ts +0 -12
  327. package/lib/orm-core/sync.js +0 -1
  328. package/lib/orm-core/utils.d.ts +0 -1
  329. package/lib/orm-core/utils.js +0 -5
  330. package/lib/orm-property/Property.d.ts +0 -78
  331. package/lib/orm-property/Property.js +0 -1
  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 -22
  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 -5
  343. package/lib/orm-property/transformers/mysql.d.ts +0 -102
  344. package/lib/orm-property/transformers/mysql.js +0 -261
  345. package/lib/orm-property/transformers/postgresql.d.ts +0 -54
  346. package/lib/orm-property/transformers/postgresql.js +0 -189
  347. package/lib/orm-property/transformers/sqlite.d.ts +0 -19
  348. package/lib/orm-property/transformers/sqlite.js +0 -171
  349. package/lib/sql-ddl-sync/Dialects/dm.d.ts +0 -40
  350. package/lib/sql-ddl-sync/Dialects/dm.js +0 -296
  351. package/lib/sql-ddl-sync/Dialects/index.d.ts +0 -7
  352. package/lib/sql-ddl-sync/Dialects/index.js +0 -7
  353. package/lib/sql-ddl-sync/Dialects/mysql.d.ts +0 -39
  354. package/lib/sql-ddl-sync/Dialects/mysql.js +0 -240
  355. package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +0 -40
  356. package/lib/sql-ddl-sync/Dialects/postgresql.js +0 -286
  357. package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +0 -44
  358. package/lib/sql-ddl-sync/Dialects/sqlite.js +0 -256
  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 -1
  363. package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +0 -13
  364. package/lib/sql-ddl-sync/Typo/DbIndex.js +0 -1
  365. package/lib/sql-ddl-sync/Typo/Dialect.js +0 -1
  366. package/lib/sql-ddl-sync/Typo/Driver.d.ts +0 -53
  367. package/lib/sql-ddl-sync/Typo/Driver.js +0 -1
  368. package/lib/sql-ddl-sync/Typo/Query.d.ts +0 -7
  369. package/lib/sql-ddl-sync/Typo/Query.js +0 -1
  370. package/lib/sql-ddl-sync/Typo/SQL.d.ts +0 -31
  371. package/lib/sql-ddl-sync/Typo/SQL.js +0 -1
  372. package/lib/sql-ddl-sync/Typo/_common.d.ts +0 -29
  373. package/lib/sql-ddl-sync/Typo/_common.js +0 -1
  374. package/lib/sql-ddl-sync/Utils.d.ts +0 -40
  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 -371
  378. package/lib/sql-query/Comparators.d.ts +0 -3
  379. package/lib/sql-query/Comparators.js +0 -94
  380. package/lib/sql-query/Create.d.ts +0 -35
  381. package/lib/sql-query/Create.js +0 -77
  382. package/lib/sql-query/Dialects/dm.d.ts +0 -3
  383. package/lib/sql-query/Dialects/index.js +0 -12
  384. package/lib/sql-query/Dialects/mssql.d.ts +0 -3
  385. package/lib/sql-query/Dialects/mssql.js +0 -73
  386. package/lib/sql-query/Dialects/mysql.d.ts +0 -3
  387. package/lib/sql-query/Dialects/mysql.js +0 -100
  388. package/lib/sql-query/Dialects/postgresql.d.ts +0 -3
  389. package/lib/sql-query/Dialects/postgresql.js +0 -76
  390. package/lib/sql-query/Dialects/sqlite.d.ts +0 -3
  391. package/lib/sql-query/Dialects/sqlite.js +0 -61
  392. package/lib/sql-query/Helpers.d.ts +0 -38
  393. package/lib/sql-query/Helpers.js +0 -259
  394. package/lib/sql-query/Insert.d.ts +0 -12
  395. package/lib/sql-query/Insert.js +0 -33
  396. package/lib/sql-query/Query.d.ts +0 -29
  397. package/lib/sql-query/Query.js +0 -119
  398. package/lib/sql-query/Remove.d.ts +0 -16
  399. package/lib/sql-query/Remove.js +0 -93
  400. package/lib/sql-query/Select.d.ts +0 -62
  401. package/lib/sql-query/Select.js +0 -533
  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 -1
  406. package/lib/sql-query/Typo/Comparators.d.ts +0 -140
  407. package/lib/sql-query/Typo/Comparators.js +0 -1
  408. package/lib/sql-query/Typo/Dialect.d.ts +0 -39
  409. package/lib/sql-query/Typo/Dialect.js +0 -1
  410. package/lib/sql-query/Typo/Field.d.ts +0 -8
  411. package/lib/sql-query/Typo/Field.js +0 -1
  412. package/lib/sql-query/Typo/Helper.d.ts +0 -4
  413. package/lib/sql-query/Typo/Helper.js +0 -1
  414. package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +0 -80
  415. package/lib/sql-query/Typo/Query-ChainBuilder.js +0 -1
  416. package/lib/sql-query/Typo/Query.d.ts +0 -34
  417. package/lib/sql-query/Typo/Query.js +0 -16
  418. package/lib/sql-query/Typo/Sql.d.ts +0 -143
  419. package/lib/sql-query/Typo/Sql.js +0 -1
  420. package/lib/sql-query/Typo/SubQuery.d.ts +0 -39
  421. package/lib/sql-query/Typo/SubQuery.js +0 -1
  422. package/lib/sql-query/Typo/index.js +0 -1
  423. package/lib/sql-query/Update.d.ts +0 -15
  424. package/lib/sql-query/Update.js +0 -38
  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,784 @@
1
+ import { createRequire } from 'node:module';
2
+ const require = createRequire(import.meta.url);
3
+ /// <reference lib="es5" />
4
+
5
+ const util = require('util');
6
+
7
+ import * as Hook from '../Hook';
8
+ import * as Settings from '../Settings';
9
+ import * as Property from '../Property';
10
+ import ORMError from "../Error";
11
+ import * as Utilities from '../Utilities';
12
+ import { ACCESSOR_KEYS, addAssociationInfoToModel, getMapsToFromPropertyHash } from './_utils';
13
+ import { listFindByChainOrRunSync } from '../Model';
14
+ import * as Helpers from '../Helpers';
15
+
16
+ import type { FxOrmInstance } from '../Typo/instance';
17
+ import type { FxOrmAssociation } from '../Typo/assoc';
18
+ import type { FxOrmError } from '../Typo/Error';
19
+ import type { FxOrmCommon } from '../Typo/_common';
20
+ import type { FxOrmModel } from '../Typo/model';
21
+ import type { FxOrmDMLDriver } from '../Typo/DMLDriver';
22
+ import type { FxOrmProperty } from '../Typo/property';
23
+ import type { FxOrmNS } from '../Typo/ORM';
24
+ import type { FxOrmQuery } from '../Typo/query';
25
+
26
+ import type {
27
+ FxSqlQuerySubQuery
28
+ } from '../../sql-query/Query';
29
+
30
+ function noOperation (...args: any[]) {};
31
+
32
+ function throwErrorIfExtraPropertyNameRepeat(
33
+ hostModel: FxOrmModel.Model,
34
+ extraProps: FxOrmAssociation.InstanceAssociationItem_HasMany['props']
35
+ ) {
36
+ Object.entries(extraProps).forEach(([eKey]) => {
37
+ if (hostModel.allProperties[eKey]) {
38
+ throw new ORMError(
39
+ `disallow defining same name extra property '${eKey}' with property on model '${hostModel.name}'`,
40
+ 'BAD_MODEL',
41
+ { repeatKey: eKey }
42
+ )
43
+ }
44
+ });
45
+ }
46
+
47
+ export function prepare(
48
+ Model: FxOrmModel.Model,
49
+ assocs: {
50
+ one_associations: FxOrmAssociation.InstanceAssociationItem_HasOne[],
51
+ many_associations: FxOrmAssociation.InstanceAssociationItem_HasMany[],
52
+ extend_associations: FxOrmAssociation.InstanceAssociationItem_ExtendTos[],
53
+ },
54
+ opts: {
55
+ db: FxOrmNS.ORM,
56
+ }
57
+ ) {
58
+ const { many_associations } = assocs;
59
+ const { db } = opts
60
+
61
+ Model.hasMany = function () {
62
+ Utilities.disAllowOpForVModel(Model, 'model.hasMany');
63
+
64
+ let name!: string,
65
+ makeKey!: boolean,
66
+ mergeId!: Record<string, FxOrmProperty.NormalizedProperty>,
67
+ mergeAssocId!: Record<string, FxOrmProperty.NormalizedProperty>;
68
+
69
+ let OtherModel: FxOrmModel.Model = Model;
70
+ let props: Record<string, FxOrmProperty.NormalizedProperty> = null as any;
71
+ let assoc_options: FxOrmAssociation.AssociationDefinitionOptions_HasMany = {};
72
+
73
+ for (let i = 0; i < arguments.length; i++) {
74
+ switch (typeof arguments[i]) {
75
+ case 'string':
76
+ name = arguments[i];
77
+ break;
78
+ case 'function':
79
+ OtherModel = arguments[i];
80
+ break;
81
+ case 'object':
82
+ if (props === null) {
83
+ props = arguments[i] as Record<string, FxOrmProperty.NormalizedProperty>;
84
+ } else {
85
+ assoc_options = arguments[i];
86
+ }
87
+ break;
88
+ }
89
+ }
90
+
91
+ Utilities.disAllowOpForVModel(OtherModel, 'associated by model.hasMany');
92
+
93
+ for (let i = 0; i < db.plugins.length; i++) {
94
+ if (typeof db.plugins[i].beforeHasMany === "function") {
95
+ (db.plugins[i].beforeHasMany as (...args: any[]) => any)(Model, {
96
+ association_name: name,
97
+ ext_model: OtherModel,
98
+ assoc_props: props,
99
+ assoc_options
100
+ });
101
+ }
102
+ }
103
+
104
+ if (props === null) {
105
+ props = {};
106
+ } else {
107
+ for (let k in props) {
108
+ props[k] = Property.normalize({
109
+ prop: props[k] as FxOrmModel.ComplexModelPropertyDefinition,
110
+ name: k,
111
+ customTypes: db.customTypes,
112
+ settings: Model.settings
113
+ });
114
+ }
115
+ }
116
+ // restrain association.props[any].name not same with Model[any].name
117
+ throwErrorIfExtraPropertyNameRepeat(OtherModel, props);
118
+
119
+ makeKey = !!(assoc_options.key || Settings.defaults().hasMany.key);
120
+
121
+ mergeId = Utilities.convertPropToJoinKeyProp(
122
+ Utilities.wrapFieldObject({
123
+ field: assoc_options.mergeId as any, model: Model, altName: Model.table
124
+ }) ||
125
+ Utilities.formatAssociatedField(Model, Model.table, true, !!assoc_options.reversed),
126
+ { makeKey: makeKey, required: true }
127
+ );
128
+
129
+ mergeAssocId = Utilities.convertPropToJoinKeyProp(
130
+ Utilities.wrapFieldObject({
131
+ field: assoc_options.mergeAssocId as any, model: OtherModel, altName: name
132
+ }) ||
133
+ Utilities.formatAssociatedField(OtherModel, name, true, !!assoc_options.reversed),
134
+ { makeKey: makeKey, required: true }
135
+ )
136
+
137
+ const associationSemanticNameCore = assoc_options.name || Utilities.formatNameFor("assoc:hasMany", name);
138
+
139
+ const fieldhash = Utilities.wrapFieldObject({ field: assoc_options.field as any, model: OtherModel, altName: Model.table }) || Utilities.formatAssociatedField(Model, name, true, !!assoc_options.reversed)
140
+ const association = {
141
+ name: name,
142
+ model: OtherModel,
143
+ props: props,
144
+ autoFetch: assoc_options.autoFetch || false,
145
+ autoFetchLimit: assoc_options.autoFetchLimit || 2,
146
+ // I'm not sure the next key is used..
147
+ field: fieldhash,
148
+ mergeTable: assoc_options.mergeTable || (Model.table + "_" + name),
149
+ mergeId: mergeId,
150
+ mergeAssocId: mergeAssocId,
151
+ getAccessor: assoc_options.getAccessor || (ACCESSOR_KEYS.get + associationSemanticNameCore),
152
+ setAccessor: assoc_options.setAccessor || (ACCESSOR_KEYS.set + associationSemanticNameCore),
153
+ hasAccessor: assoc_options.hasAccessor || (ACCESSOR_KEYS.has + associationSemanticNameCore),
154
+ delAccessor: assoc_options.delAccessor || (ACCESSOR_KEYS.del + associationSemanticNameCore),
155
+ addAccessor: assoc_options.addAccessor || (ACCESSOR_KEYS.add + associationSemanticNameCore),
156
+
157
+ modelFindByAccessor: assoc_options.modelFindByAccessor || (ACCESSOR_KEYS.modelFindBy + associationSemanticNameCore),
158
+
159
+ hooks: {...assoc_options.hooks},
160
+ } as FxOrmAssociation.InstanceAssociationItem_HasMany;
161
+ Utilities.fillSyncVersionAccessorForAssociation(association);
162
+ Utilities.addHookPatchHelperForAssociation(association);
163
+
164
+ many_associations.push(association);
165
+
166
+ if (assoc_options.reverse) {
167
+ OtherModel.hasMany(assoc_options.reverse, Model, association.props, {
168
+ reversed: true,
169
+ association: assoc_options.reverseAssociation,
170
+ mergeTable: association.mergeTable,
171
+ mergeId: association.mergeAssocId,
172
+ mergeAssocId: association.mergeId,
173
+ field: fieldhash,
174
+ autoFetch: association.autoFetch,
175
+ autoFetchLimit: association.autoFetchLimit,
176
+ hooks: assoc_options.reverseHooks
177
+ });
178
+ }
179
+
180
+ const findByAccessorChainOrRunSync = function (is_sync: boolean = false) {
181
+ return function () {
182
+ var cb: FxOrmModel.ModelMethodCallback__Find | null = null,
183
+ conditions: FxOrmModel.ModelQueryConditions__Find | null = null,
184
+ right_find_opts: FxOrmAssociation.ModelAssociationMethod__FindByOptions | null = null;
185
+
186
+ for (let i = 0; i < arguments.length; i++) {
187
+ switch (typeof arguments[i]) {
188
+ case "function":
189
+ cb = arguments[i];
190
+ break;
191
+ case "object":
192
+ if (conditions === null) {
193
+ conditions = { ...arguments[i] };
194
+ } else if (right_find_opts === null) {
195
+ right_find_opts = arguments[i] as FxOrmAssociation.ModelAssociationMethod__FindByOptions;
196
+ }
197
+ break;
198
+ }
199
+ }
200
+
201
+ if (conditions === null) {
202
+ throw new ORMError(`.${association.modelFindByAccessor}() is missing a conditions object`, 'PARAM_MISMATCH');
203
+ }
204
+
205
+ right_find_opts = right_find_opts || {};
206
+
207
+ const extraWhere = Utilities.extractHasManyExtraConditions(association, conditions) || {}
208
+ // only set exists conditions when extraWhere is not empty
209
+ if (Object.keys(extraWhere).length > 0) {
210
+ // always set it only 1 element
211
+ right_find_opts.exists = [{
212
+ table: '',
213
+ link: ['', ''],
214
+ conditions: {}
215
+ }];
216
+
217
+ right_find_opts.exists.forEach(exists => {
218
+ exists.table = association.mergeTable;
219
+
220
+ // TODO: how to link complex associated fields?
221
+ exists.link = [
222
+ getMapsToFromPropertyHash(association.mergeAssocId)[0],
223
+ association.model.keys[0]
224
+ ]
225
+
226
+ exists.conditions = extraWhere;
227
+
228
+ if (!Object.keys(exists.conditions).length) {
229
+ throw new ORMError(`"assocConditions.exists[number].conditions" is required on ".${association.modelFindByAccessor}(conds, assocConditions)"!`, 'PARAM_MISMATCH');
230
+ }
231
+
232
+ return exists;
233
+ })
234
+ }
235
+
236
+ return listFindByChainOrRunSync(Model,
237
+ {},
238
+ [
239
+ {
240
+ association_name: association.name,
241
+ conditions: conditions!
242
+ },
243
+ ],
244
+ right_find_opts,
245
+ { callback: cb || undefined, is_sync }
246
+ );
247
+ }
248
+ }
249
+
250
+ Model[association.modelFindByAccessor!] = findByAccessorChainOrRunSync();
251
+ Model[association.modelFindBySyncAccessor!] = findByAccessorChainOrRunSync(true);
252
+
253
+ addAssociationInfoToModel(Model, name, {
254
+ type: 'hasMany',
255
+ association: association
256
+ });
257
+
258
+ return this;
259
+ };
260
+ };
261
+
262
+ export function extend(
263
+ Model: FxOrmModel.Model,
264
+ Instance: FxOrmInstance.Instance,
265
+ Driver: FxOrmDMLDriver.DMLDriver,
266
+ associations: FxOrmAssociation.InstanceAssociationItem_HasMany[],
267
+ cfg: {
268
+ assoc_opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany,
269
+ }
270
+ ) {
271
+ for (let i = 0; i < associations.length; i++) {
272
+ extendInstance(Model, Instance, Driver, associations[i], cfg);
273
+ }
274
+ };
275
+
276
+ export function autoFetch(
277
+ Instance: FxOrmInstance.Instance,
278
+ associations: FxOrmAssociation.InstanceAssociationItem_HasMany[],
279
+ opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany,
280
+ parallel: boolean = false
281
+ ) {
282
+ if (associations.length === 0)
283
+ return ;
284
+
285
+ Utilities.parallelQueryIfPossible(
286
+ parallel,
287
+ associations,
288
+ (item) => autoFetchInstance(Instance, item, opts)
289
+ )
290
+ };
291
+
292
+ function adjustForMapsTo(properties: Record<string, FxOrmProperty.NormalizedProperty>, field: string[]) {
293
+ if (!field)
294
+ return ;
295
+
296
+ /**
297
+ * Loop through the (cloned) association model id fields ... some of them may've been mapped to different
298
+ * names in the actual database - if so update to the mapped database column name
299
+ */
300
+ for (let i = 0; i < field.length; i++) {
301
+ var idProp = properties[field[i]];
302
+ if (idProp && idProp.mapsTo) {
303
+ field[i] = idProp.mapsTo;
304
+ }
305
+ }
306
+ }
307
+
308
+ function mapKeysToString (keys: string[], item: FxOrmInstance.Instance) {
309
+ return util.map(keys, function (k: string) {
310
+ return item[k];
311
+ }).join(',')
312
+ }
313
+
314
+ function extendInstance(
315
+ Model: FxOrmModel.Model,
316
+ Instance: FxOrmInstance.Instance,
317
+ Driver: FxOrmDMLDriver.DMLDriver,
318
+ association: FxOrmAssociation.InstanceAssociationItem_HasMany,
319
+ cfg: {
320
+ assoc_opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany
321
+ }
322
+ ) {
323
+ if (Model.settings.get("instance.cascadeRemove")) {
324
+ Instance.on("beforeRemove", function () {
325
+ Instance[association.delAccessor]();
326
+ });
327
+ }
328
+
329
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.hasSyncAccessor, function (...Instances: (FxOrmInstance.Instance | FxOrmInstance.Instance)[]) {
330
+ var conditions = {},
331
+ join_conditions = {},
332
+ options: FxOrmAssociation.ModelAssociationMethod__FindOptions = {};
333
+
334
+ if (Instances.length)
335
+ if (Array.isArray(Instances[0]))
336
+ Instances = Instances[0] as any;
337
+
338
+ // if (Driver.hasMany) {
339
+ // return Driver.hasMany(Model, association)
340
+ // .has(Instance, Instances, conditions);
341
+ // }
342
+
343
+ options.autoFetchLimit = 0;
344
+ options.__merge = {
345
+ from: { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
346
+ to: { table: association.model.table, field: association.model.id.slice(0) }, // clone model id
347
+ where: [association.mergeTable, join_conditions],
348
+ table: association.model.table,
349
+ select: []
350
+ };
351
+
352
+ adjustForMapsTo(association.model.properties, options.__merge.to.field);
353
+
354
+ options.extra = association.props;
355
+ options.extra_info = {
356
+ table: association.mergeTable,
357
+ id: Utilities.values(Instance, Model.id),
358
+ id_prop: Object.keys(association.mergeId),
359
+ assoc_prop: Object.keys(association.mergeAssocId)
360
+ };
361
+
362
+ Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, options.__merge.where[1]);
363
+
364
+ for (let i = 0; i < Instances.length; i++) {
365
+ Utilities.populateModelIdKeysConditions(association.model, Object.keys(association.mergeAssocId), Instances[i], options.__merge.where[1], false);
366
+ }
367
+
368
+ const foundItems = association.model.findSync(conditions, options);
369
+
370
+ if (util.isEmpty(Instances)) return foundItems.length > 0;
371
+
372
+ var foundItemsIDs = Array.from( new Set (
373
+ foundItems.map(item => mapKeysToString(association.model.keys, item))
374
+ ));
375
+ var InstancesIDs = Array.from( new Set (
376
+ Instances.map(item => mapKeysToString(association.model.keys, item))
377
+ ));
378
+
379
+ var sameLength = foundItemsIDs.length === InstancesIDs.length;
380
+ var sameContents = sameLength && util.isEmpty(util.difference(foundItemsIDs, InstancesIDs));
381
+
382
+ return sameContents;
383
+ });
384
+
385
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.hasAccessor, function (this: any, ...Instances: FxOrmInstance.Instance[]) {
386
+ let cb: FxOrmCommon.GenericCallback<boolean>;
387
+ if (typeof util.last(Instances) === 'function')
388
+ cb = Instances.pop() as any;
389
+
390
+ process.nextTick(() => {
391
+ const syncResponse = Utilities.catchBlocking<boolean>(Instance[association.hasSyncAccessor], Instances);
392
+ Utilities.takeAwayResult(syncResponse, { callback: cb })
393
+ });
394
+
395
+ return this;
396
+ });
397
+
398
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.getAccessor, function (
399
+ this: typeof Instance
400
+ ): typeof Instance | FxOrmQuery.IChainFind {
401
+ let options = {} as FxOrmAssociation.ModelAssociationMethod__GetOptions;
402
+ let conditions: FxOrmModel.ModelOptions__Find | null = null;
403
+ let order = null;
404
+ let cb = null;
405
+
406
+ Helpers.selectArgs(arguments, function (arg_type, arg) {
407
+ switch (arg_type) {
408
+ case "function":
409
+ cb = arg;
410
+ break;
411
+ case "object":
412
+ if (Array.isArray(arg)) {
413
+ order = arg;
414
+ order[0] = [association.model.table, order[0]];
415
+ } else {
416
+ if (conditions === null) {
417
+ conditions = { ...arg };
418
+ } else {
419
+ options = arg;
420
+ }
421
+ }
422
+ break;
423
+ case "string":
424
+ if (arg[0] == "-") {
425
+ order = [[association.model.table, arg.substr(1)], "Z"];
426
+ } else {
427
+ order = [[association.model.table, arg]];
428
+ }
429
+ break;
430
+ case "number":
431
+ options.limit = arg;
432
+ break;
433
+ }
434
+ });
435
+
436
+ if (order !== null) {
437
+ options.order = order;
438
+ }
439
+
440
+ if (conditions === null) conditions = {};
441
+
442
+ // extract extra info on conditions
443
+ // populate out conditions in extra, not belonging to either one model of associations
444
+ let extraWhere = Utilities.extractHasManyExtraConditions(association, conditions)
445
+ extraWhere = { ...options.join_where, ...extraWhere };
446
+
447
+ options.__merge = {
448
+ from: { table: association.mergeTable, field: Object.keys(association.mergeAssocId) },
449
+ to: { table: association.model.table, field: association.model.id.slice(0) }, // clone model id
450
+ where: [association.mergeTable, extraWhere],
451
+ table: association.model.table,
452
+ select: []
453
+ };
454
+
455
+ adjustForMapsTo(association.model.properties, options.__merge.to.field);
456
+
457
+ options.extra = association.props;
458
+ options.extra_info = {
459
+ table: association.mergeTable,
460
+ id: Utilities.values(Instance, Model.id),
461
+ id_prop: Object.keys(association.mergeId),
462
+ assoc_prop: Object.keys(association.mergeAssocId)
463
+ };
464
+
465
+ Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, options.__merge.where[1]);
466
+
467
+ if (cb === null)
468
+ return association.model.find(conditions, options);
469
+
470
+ association.model.find(conditions, options, cb);
471
+
472
+ return this;
473
+ });
474
+
475
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.getSyncAccessor, function (this: typeof Instance, ...args: any[]): FxOrmInstance.Instance[] {
476
+ args = args.filter(x => !util.isFunction(x));
477
+
478
+ const chain = Instance[association.getAccessor].apply(Instance, args);
479
+
480
+ return chain.runSync();
481
+ });
482
+
483
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.setSyncAccessor, function (this: typeof Instance) {
484
+ const $ref = {
485
+ instance: Instance,
486
+ associations: Utilities.flattenArray(arguments),
487
+ useChannel: Utilities.reusableChannelGenerator()
488
+ } as FxOrmAssociation.__AssocHooksCtx;
489
+
490
+ let results: FxOrmInstance.Instance[] = [];
491
+
492
+ Hook.wait(
493
+ Instance,
494
+ association.hooks[`beforeSet`],
495
+ Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
496
+ Instance.$emit(`before:set:${association.name}`, $ref.associations!)
497
+
498
+ Instance.$emit(`before-del-extension:${association.setAccessor}`)
499
+ Instance[association.delSyncAccessor]();
500
+ Instance.$emit(`after-del-extension:${association.setAccessor}`)
501
+
502
+ if (!$ref.associations!.length)
503
+ return ;
504
+
505
+ Instance.$emit(`before-add-extension:${association.setAccessor}`, $ref.associations!)
506
+ results = Instance[association.addSyncAccessor!]($ref.associations!);
507
+ Instance.$emit(`after-add-extension:${association.setAccessor}`, $ref.associations!)
508
+
509
+ Instance.$emit(`after:set:${association.name}`, $ref.associations!)
510
+
511
+ if (Instance.__instRtd.keys.length === 1) {
512
+ const [key] = Instance.__instRtd.keys
513
+ $ref.association_ids = $ref.associations!.map((x: FxOrmInstance.InstanceDataPayload) => x[key])
514
+ }
515
+ }),
516
+ Utilities.buildAssocHooksContext('beforeSet', { $ref })
517
+ );
518
+
519
+ Hook.trigger(Instance, association.hooks[`afterSet`], Utilities.buildAssocHooksContext('afterSet', { $ref }))
520
+
521
+ return results;
522
+ });
523
+
524
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.setAccessor, function (this: typeof Instance) {
525
+ // TODO: shold allow passing `extra` as 2nd argument
526
+ const items = Utilities.flattenArray(arguments);
527
+ const cb = typeof util.last(items) === 'function' ? items.pop() : noOperation;
528
+
529
+ process.nextTick(() => {
530
+ const syncResponse = Utilities.catchBlocking<boolean>(Instance[association.setSyncAccessor], items);
531
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb })
532
+ });
533
+
534
+ return this;
535
+ });
536
+
537
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.delAccessor, function (this: typeof Instance, ...args: any[]) {
538
+ var cb: FxOrmCommon.ExecutionCallback<typeof this>;
539
+
540
+ Helpers.selectArgs(args, (arg_type, arg, idx) => {
541
+ switch (arg_type) {
542
+ case "function":
543
+ cb = arg;
544
+ break;
545
+ }
546
+ });
547
+ args = args.filter(x => x !== cb);
548
+
549
+ process.nextTick(() => {
550
+ const syncResponse = Utilities.catchBlocking<FxOrmInstance.Instance>(Instance[association.delSyncAccessor], args);
551
+ Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: cb })
552
+ });
553
+
554
+ return this;
555
+ });
556
+
557
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.delSyncAccessor, function (this: typeof Instance, ...args: any[]) {
558
+ var Associations: FxOrmAssociation.AssociationDefinitionOptions_HasMany[] = [];
559
+
560
+ Helpers.selectArgs(args, (arg_type, arg) => {
561
+ switch (arg_type) {
562
+ case "object":
563
+ if (Array.isArray(arg)) {
564
+ Associations = Associations.concat(arg);
565
+ } else if (arg.isInstance) {
566
+ Associations.push(arg);
567
+ }
568
+ break;
569
+ }
570
+ });
571
+
572
+ const conditions = {} as FxSqlQuerySubQuery.SubQueryConditions;
573
+
574
+ Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, conditions);
575
+
576
+ if (!this.saved())
577
+ this.saveSync();
578
+
579
+ const $ref = {
580
+ instance: Instance,
581
+ associations: Associations,
582
+ removeConditions: conditions
583
+ } as Record<string, any>;
584
+ Hook.wait(
585
+ Instance,
586
+ association.hooks[`beforeRemove`],
587
+ Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
588
+ const Associations = $ref.associations
589
+ Instance.$emit(`before:del:${association.name}`)
590
+ if (Driver.hasMany) {
591
+ return [
592
+ Driver.hasMany(Model, association).del(Instance, Associations),
593
+ Instance.$emit(`after:del:${association.name}`)
594
+ ][0];
595
+ }
596
+
597
+ if (Associations.length === 0) {
598
+ return [
599
+ Driver.remove(association.mergeTable, $ref.removeConditions),
600
+ Instance.$emit(`after:del:${association.name}`)
601
+ ][0];
602
+ }
603
+
604
+ for (let i = 0; i < Associations.length; i++) {
605
+ Utilities.populateModelIdKeysConditions(association.model, Object.keys(association.mergeAssocId), Associations[i], $ref.removeConditions, false);
606
+ }
607
+
608
+ Driver.remove(association.mergeTable, $ref.removeConditions);
609
+ Instance.$emit(`after:del:${association.name}`)
610
+ }),
611
+ Utilities.buildAssocHooksContext('beforeRemove', { $ref })
612
+ );
613
+
614
+ Hook.trigger(Instance, association.hooks[`afterRemove`], Utilities.buildAssocHooksContext('afterRemove', { $ref }))
615
+
616
+ return this;
617
+ });
618
+
619
+ const isExtraNonEmpty = function () {
620
+ return !!Object.keys(association.props).length;
621
+ }
622
+
623
+ const collectParamsForAdd = function (args: any[]) {
624
+ var Associations: FxOrmAssociation.InstanceAssociatedInstance[] = [];
625
+ var add_opts: {[k: string]: any} = {};
626
+
627
+ Helpers.selectArgs(args, (arg_type, arg) => {
628
+ switch (arg_type) {
629
+ case "object":
630
+ if (Array.isArray(arg)) {
631
+ Associations = Associations.concat(arg);
632
+ } else if (arg.isInstance) {
633
+ Associations.push(arg);
634
+ } else {
635
+ add_opts = arg;
636
+ }
637
+ break;
638
+ }
639
+ });
640
+
641
+ if (Associations.length === 0) {
642
+ throw new ORMError("No associations defined", 'PARAM_MISMATCH', { model: Model.name });
643
+ }
644
+
645
+ return { Associations, add_opts }
646
+ }
647
+
648
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.addSyncAccessor!, function (this: typeof Instance) {
649
+ let args: any[]= Array.prototype.slice.apply(arguments);
650
+
651
+ const { Associations, add_opts } = collectParamsForAdd(args);
652
+
653
+ const savedAssociations: FxOrmAssociation.InstanceAssociatedInstance[] = [];
654
+
655
+ const $ref = {
656
+ instance: Instance,
657
+ associations: Associations,
658
+ useChannel: Utilities.reusableChannelGenerator()
659
+ } as Record<string, any>;
660
+ Hook.wait(
661
+ Instance,
662
+ association.hooks[`beforeAdd`],
663
+ Utilities.makeHandlerDecorator({ thisArg: Instance }, () => {
664
+ Instance.$emit(`before:add:${association.name}`, $ref.associations!);
665
+ Utilities.parallelQueryIfPossible(
666
+ false,
667
+ $ref.associations,
668
+ (Association) => {
669
+ const saveAssociation = function (err: FxOrmError.ExtendedError) {
670
+ if (err)
671
+ throw err;
672
+
673
+ Association.saveSync();
674
+
675
+ const data: {[k: string]: any} = {};
676
+
677
+ for (let k in add_opts) {
678
+ if (association.props[k]) {
679
+ const mapsTo = association.props[k].mapsTo || k;
680
+ data[mapsTo] = Driver.propertyToValue ?
681
+ Driver.propertyToValue(add_opts[k], association.props[k])
682
+ : add_opts[k];
683
+ }
684
+ }
685
+
686
+ Utilities.populateModelIdKeysConditions(Model, Object.keys(association.mergeId), Instance, data);
687
+ Utilities.populateModelIdKeysConditions(association.model, Object.keys(association.mergeAssocId), Association, data);
688
+
689
+ Driver.insert(association.mergeTable, data, null as any);
690
+ savedAssociations.push(Association);
691
+ };
692
+
693
+ Instance.$emit(`before-association-save:${association.addAccessor}`, $ref.associations!)
694
+
695
+ if (isExtraNonEmpty()) {
696
+ Hook.wait(Association, association.hooks.beforeSave, saveAssociation, add_opts);
697
+ } else {
698
+ Hook.wait(Association, association.hooks.beforeSave, saveAssociation);
699
+ }
700
+
701
+ $ref.associations = savedAssociations;
702
+ Instance.$emit(`after-association-save:${association.addAccessor}`, savedAssociations)
703
+ }
704
+ )
705
+
706
+ if (!this.saved())
707
+ this.saveSync();
708
+
709
+ Instance.$emit(`after:add:${association.name}`, savedAssociations)
710
+ }),
711
+ Utilities.buildAssocHooksContext('beforeAdd', { $ref })
712
+ );
713
+
714
+ Hook.trigger(Instance, association.hooks[`afterAdd`], Utilities.buildAssocHooksContext('afterAdd', { $ref }))
715
+
716
+ return savedAssociations;
717
+ });
718
+
719
+ Utilities.addHiddenUnwritableMethodToInstance(Instance, association.addAccessor, function (this: typeof Instance) {
720
+ let args: any[] = Array.prototype.slice.apply(arguments);
721
+
722
+ const withExtraProps = isExtraNonEmpty();
723
+ let cb: FxOrmCommon.ExecutionCallback<any> | null = null;
724
+ Helpers.selectArgs(args, (arg_type, arg) => {
725
+ switch (arg_type) {
726
+ case "function":
727
+ cb = arg;
728
+ break;
729
+ }
730
+ });
731
+ args = args.filter(x => x !== cb);
732
+ collectParamsForAdd(args);
733
+
734
+ const errWaitor = Utilities.getErrWaitor(!cb);
735
+
736
+ process.nextTick(() => {
737
+ const syncResponse = Utilities.catchBlocking<void>(Instance[association.addSyncAccessor!], args);
738
+ Utilities.takeAwayResult(syncResponse, { no_throw: !withExtraProps, callback: cb || undefined });
739
+
740
+ errWaitor.err = syncResponse.error || null;
741
+ if (errWaitor.evt) errWaitor.evt.set();
742
+ });
743
+
744
+ if (errWaitor.evt) errWaitor.evt.wait();
745
+
746
+ if (errWaitor.err) throw errWaitor.err;
747
+
748
+ return this;
749
+ });
750
+
751
+ Object.defineProperty(Instance, association.name, {
752
+ get: function () {
753
+ return Instance.__instRtd.associations[association.name].value;
754
+ },
755
+ set: function (val) {
756
+ Instance.__instRtd.associations[association.name].changed = true;
757
+ Instance.__instRtd.associations[association.name].value = val;
758
+ },
759
+ enumerable: true
760
+ });
761
+ }
762
+
763
+ function autoFetchInstance(
764
+ Instance: FxOrmInstance.Instance,
765
+ association: FxOrmAssociation.InstanceAssociationItem_HasMany,
766
+ opts: FxOrmAssociation.AssociationDefinitionOptions_HasMany,
767
+ // cb: FxOrmNS.GenericCallback<void>
768
+ ) {
769
+ if (!Instance.saved())
770
+ return ;
771
+
772
+ if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
773
+ opts.autoFetchLimit = association.autoFetchLimit;
774
+ }
775
+
776
+ if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch))
777
+ return ;
778
+
779
+ try {
780
+ const Assoc = Instance[association.getSyncAccessor]({}, { autoFetchLimit: opts.autoFetchLimit - 1 });
781
+ // Set this way to prevent setting 'changed' status
782
+ Instance.__instRtd.associations[association.name].value = Assoc;
783
+ } catch (err) {}
784
+ }