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,361 @@
1
+ import { createRequire } from 'node:module';
2
+ const require = createRequire(import.meta.url);
3
+ import * as Debug from '../../Debug';
4
+ const util = require('util');
5
+ import { Database } from "../DB/sqlite3";
6
+ import * as shared from './_shared';
7
+ import * as DDL from './_ddl-sql';
8
+ import * as Sync from '../../../sql-ddl-sync/index';
9
+ import * as utils from './_utils';
10
+ import * as Utilities from '../../Utilities';
11
+
12
+ import { Query } from "../../../sql-query/Query";
13
+ import type { FxSqlQuery } from "../../../sql-query/Query";
14
+ import type { FxOrmDMLDriver } from '../../Typo/DMLDriver';
15
+ import type { FxDbDriverNS, IDbDriver } from '../../../db-driver/index';
16
+ import type { FxOrmDb } from '../../Typo/Db';
17
+ import type { FxOrmCommon } from '../../Typo/_common';
18
+ import type { FxOrmQuery } from '../../Typo/query';
19
+ import { safeParseJson, unwrapQuote } from './_dml-helpers';
20
+
21
+ export const Driver: FxOrmDMLDriver.DMLDriverConstructor_SQLite = function (
22
+ this: FxOrmDMLDriver.DMLDriver_SQLite,
23
+ config: FxDbDriverNS.DBConnectionConfig,
24
+ connection: FxOrmDb.DatabaseBase_SQLite,
25
+ opts: FxOrmDMLDriver.DMLDriverOptions
26
+ ) {
27
+ this.dialect = 'sqlite';
28
+ this.opts = opts || <FxOrmDMLDriver.DMLDriverOptions>{};
29
+
30
+ this.customTypes = {};
31
+
32
+ Object.defineProperty(this, 'db', {
33
+ value: connection || new Database(config),
34
+ writable: false
35
+ });
36
+
37
+ Object.defineProperty(this, 'sqlDriver', {
38
+ value: this.db,
39
+ writable: false
40
+ });
41
+
42
+ Object.defineProperty(this, 'config', {
43
+ value: this.db.config,
44
+ writable: false
45
+ });
46
+ if (!this.config.timezone) this.config.timezone = "local";
47
+
48
+ Object.defineProperty(this, 'ddlSync', {
49
+ value: Sync.dialect(this.dialect),
50
+ writable: false
51
+ });
52
+
53
+ Object.defineProperty(this, 'query', {
54
+ value: new Query({ dialect: this.dialect, timezone: this.config.timezone }),
55
+ writable: false
56
+ });
57
+
58
+ utils.getKnexInstance(this);
59
+
60
+ this.aggregate_functions = ["ABS", "ROUND",
61
+ "AVG", "MIN", "MAX",
62
+ "RANDOM",
63
+ "SUM", "COUNT",
64
+ "DISTINCT"];
65
+ } as any as FxOrmDMLDriver.DMLDriverConstructor_SQLite;
66
+
67
+ util.extend(Driver.prototype, shared, DDL);
68
+
69
+ Driver.prototype.ping = function (
70
+ this: FxOrmDMLDriver.DMLDriver_SQLite, cb?
71
+ ) {
72
+ Utilities.takeAwayResult({ error: undefined }, { callback: cb, use_tick: true });
73
+ return this;
74
+ };
75
+
76
+ Driver.prototype.on = function (this: FxOrmDMLDriver.DMLDriver_SQLite,
77
+ ev, cb?
78
+ ) {
79
+ if (ev == "error") {
80
+ if (cb) this.db.eventor.on("error", cb);
81
+ }
82
+ return this;
83
+ };
84
+
85
+ Driver.prototype.connect = function (
86
+ this: FxOrmDMLDriver.DMLDriver_SQLite, cb?: FxOrmCommon.GenericCallback<IDbDriver>
87
+ ) {
88
+ const errResults = Utilities.catchBlocking(
89
+ () => this.db.connect()
90
+ )
91
+
92
+ Utilities.takeAwayResult(errResults, { no_throw: !!cb, callback: cb });
93
+ return errResults.result;
94
+ };
95
+
96
+ Driver.prototype.close = function (
97
+ this: FxOrmDMLDriver.DMLDriver_SQLite, cb?
98
+ ) {
99
+ const errResults = Utilities.catchBlocking(
100
+ () => this.db.close()
101
+ )
102
+
103
+ Utilities.takeAwayResult(errResults, { no_throw: !!cb, callback: cb });
104
+ return errResults.result;
105
+ };
106
+
107
+ Driver.prototype.getQuery = function (
108
+ this: FxOrmDMLDriver.DMLDriver_SQLite,
109
+ ) {
110
+ return this.query;
111
+ };
112
+
113
+ Driver.prototype.execSimpleQuery = function (
114
+ this: FxOrmDMLDriver.DMLDriver_SQLite, query, cb?
115
+ ) {
116
+ if (this.opts.debug) {
117
+ Debug.sql('sqlite', query);
118
+ }
119
+ return this.db.all(query, cb);
120
+ };
121
+
122
+ Driver.prototype.find = function (
123
+ this: FxOrmDMLDriver.DMLDriver_SQLite, selectFields, table, conditions, opts
124
+ ) {
125
+ const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
126
+
127
+ const __pointTypeMapsTo = opts.__pointTypeMapsTo || [];
128
+
129
+ const ctx = {
130
+ table,
131
+ fromTuple: from_tuple,
132
+ selectFields,
133
+ selectVirtualFields: opts.selectVirtualFields || []
134
+ };
135
+ let q = this.query.select();
136
+ q = (((opts?.generateSqlSelect || Utilities.DEFAULT_GENERATE_SQL_QUERY_SELECT) as any).call(this, ctx, q) || q) as any;
137
+
138
+ if (opts.offset) {
139
+ q.offset(opts.offset);
140
+ }
141
+ if (typeof opts.limit == "number") {
142
+ q.limit(opts.limit);
143
+ } else if (opts.offset) {
144
+ // OFFSET cannot be used without LIMIT so we use the biggest INTEGER number possible
145
+ q.limit('9223372036854775807');
146
+ }
147
+
148
+ (utils.buildBaseConditionsToQuery as any).apply(this, [q, pure_table, conditions, opts.topConditions]);
149
+ (utils.buildOrderToQuery as any).apply(this, [q, opts.order]);
150
+ (utils.buildMergeToQuery as any).apply(this, [q, opts.merge]);
151
+ (utils.buildExistsToQuery as any).apply(this, [q, alias, opts.exists]);
152
+
153
+ const results = this.execSimpleQuery(q.build());
154
+
155
+ if (__pointTypeMapsTo.length > 0 && Array.isArray(results)) {
156
+ results.forEach(item => {
157
+ __pointTypeMapsTo.forEach(field => {
158
+ item[field] = safeParseJson(unwrapQuote(item[field]));
159
+ });
160
+ })
161
+ }
162
+
163
+ return results;
164
+ };
165
+
166
+ Driver.prototype.count = function (
167
+ this: FxOrmDMLDriver.DMLDriver_SQLite, table, conditions, opts, cb?
168
+ ) {
169
+ const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
170
+ const q = this.query.select()
171
+ .from(from_tuple)
172
+ .count(null, 'c');
173
+
174
+ (utils.buildBaseConditionsToQuery as any).apply(this, [q, pure_table, conditions]);
175
+ (utils.buildMergeToQuery as any).apply(this, [q, opts.merge, conditions]);
176
+ (utils.buildExistsToQuery as any).apply(this, [q, alias, opts.exists]);
177
+
178
+ return this.execSimpleQuery(q.build(), cb);
179
+ };
180
+
181
+ Driver.prototype.insert = function (
182
+ this: FxOrmDMLDriver.DMLDriver_SQLite, table, data, keyProperties, cb?
183
+ ) {
184
+ var q = this.query.insert()
185
+ .into(table)
186
+ .set(data)
187
+ .build();
188
+
189
+ const syncResponse = Utilities.catchBlocking(() => {
190
+ const info = this.execSimpleQuery<FxOrmQuery.InsertResult>(q);
191
+
192
+ if (!keyProperties) return null;
193
+
194
+ var ids: { [k: string]: any } = {},
195
+ prop;
196
+
197
+ if (keyProperties.length == 1 && keyProperties[0].type == 'serial') {
198
+ ids[keyProperties[0].name] = info.insertId;
199
+ } else {
200
+ for (let i = 0; i < keyProperties.length; i++) {
201
+ prop = keyProperties[i];
202
+ // Zero is a valid value for an ID column
203
+ ids[prop.name] = data[prop.mapsTo!] !== undefined ? data[prop.mapsTo!] : null;
204
+ }
205
+ }
206
+
207
+ return ids;
208
+ });
209
+ Utilities.takeAwayResult(syncResponse, { callback: cb });
210
+
211
+ return syncResponse.result
212
+ };
213
+
214
+ Driver.prototype.update = function (
215
+ this: FxOrmDMLDriver.DMLDriver_SQLite, table, changes, conditions, cb?
216
+ ) {
217
+ var q = this.query.update()
218
+ .into(table)
219
+ .set(changes)
220
+ .where(conditions)
221
+ .build();
222
+
223
+ return this.execSimpleQuery(q, cb);
224
+ };
225
+
226
+ Driver.prototype.remove = function (
227
+ this: FxOrmDMLDriver.DMLDriver_SQLite, table, conditions, cb?
228
+ ) {
229
+ var q = this.query.remove()
230
+ .from(table)
231
+ .where(conditions)
232
+ .build();
233
+
234
+ return this.execSimpleQuery(q, cb);
235
+ };
236
+
237
+ Driver.prototype.clear = function (
238
+ this: FxOrmDMLDriver.DMLDriver_SQLite, table, cb?
239
+ ) {
240
+ const syncResponse = Utilities.catchBlocking(() => {
241
+ this.execQuery(
242
+ this.query.remove()
243
+ .from(table)
244
+ .build()
245
+ );
246
+
247
+ this.execQuery(
248
+ this.query.remove()
249
+ .from(table)
250
+ .where({ name: 'sqlite_sequence' })
251
+ .build()
252
+ );
253
+ })
254
+ Utilities.takeAwayResult(syncResponse, { callback: cb });
255
+
256
+ return syncResponse.result;
257
+ };
258
+
259
+ Driver.prototype.valueToProperty = function (
260
+ this: FxOrmDMLDriver.DMLDriver_SQLite, value, property
261
+ ) {
262
+ var v, customType;
263
+
264
+ switch (property.type) {
265
+ case "boolean":
266
+ value = !!value;
267
+ break;
268
+ case "object":
269
+ if (typeof value == "object" && !Buffer.isBuffer(value)) {
270
+ break;
271
+ }
272
+ try {
273
+ value = JSON.parse(value);
274
+ } catch (e) {
275
+ value = null;
276
+ }
277
+ break;
278
+ case "binary": {
279
+ // compat with standard behavior of `JSON.stringify`/`JSON.parse`
280
+ if (value && value.type === 'Buffer' && Array.isArray(value.data)) {
281
+ value = Buffer.from(value.data);
282
+ }
283
+ break;
284
+ }
285
+ case "number":
286
+ if (typeof value == 'string') {
287
+ switch (value.trim()) {
288
+ case 'Infinity':
289
+ case '-Infinity':
290
+ case 'NaN':
291
+ value = Number(value);
292
+ break;
293
+ default:
294
+ v = parseFloat(value);
295
+ if (Number.isFinite(v)) {
296
+ value = v;
297
+ }
298
+ }
299
+ }
300
+ break;
301
+ case "integer":
302
+ if (typeof value == 'string') {
303
+ v = parseInt(value);
304
+
305
+ if (Number.isFinite(v)) {
306
+ value = v;
307
+ }
308
+ }
309
+ break;
310
+ case "date":
311
+ if (util.isNumber(value) || util.isString(value))
312
+ value = new Date(value);
313
+
314
+ break;
315
+ default:
316
+ customType = this.customTypes[property.type];
317
+ if (customType && 'valueToProperty' in customType) {
318
+ value = customType.valueToProperty!(value, property);
319
+ }
320
+ }
321
+ return value;
322
+ };
323
+
324
+ Driver.prototype.propertyToValue = function (
325
+ this: FxOrmDMLDriver.DMLDriver_SQLite, value, property
326
+ ) {
327
+ switch (property.type) {
328
+ case "boolean":
329
+ value = (value) ? 1 : 0;
330
+ break;
331
+ case "object":
332
+ if (value !== null) {
333
+ value = JSON.stringify(value);
334
+ }
335
+ break;
336
+ case "date":
337
+ if (util.isNumber(value) || util.isString(value))
338
+ value = new Date(value);
339
+ break;
340
+ default:
341
+ const customType = this.customTypes[property.type];
342
+ if (customType && 'propertyToValue' in customType) {
343
+ value = customType.propertyToValue!(value);
344
+ }
345
+ }
346
+ return value;
347
+ };
348
+
349
+ Object.defineProperty(Driver.prototype, "isSql", {
350
+ value: true
351
+ });
352
+
353
+ function convertTimezone(tz: FxSqlQuery.FxSqlQueryTimezone) {
354
+ if (tz == "Z") return 0;
355
+
356
+ var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
357
+ if (m) {
358
+ return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
359
+ }
360
+ return false;
361
+ }
@@ -0,0 +1,49 @@
1
+ import type { FxOrmError } from "./Typo/Error";
2
+
3
+ const codes: FxOrmError.PredefineErrorCodes = {
4
+ QUERY_ERROR : 1,
5
+ NOT_FOUND : 2,
6
+ NOT_DEFINED : 3,
7
+ NO_SUPPORT : 4,
8
+ MISSING_CALLBACK : 5,
9
+ PARAM_MISMATCH : 6,
10
+ CONNECTION_LOST : 10,
11
+ BAD_MODEL : 15
12
+ };
13
+
14
+ export default class ORMError extends Error {
15
+ static codes = codes;
16
+
17
+ name: string = 'ORMError';
18
+
19
+ message: string = '';
20
+ code: number | string = 0;
21
+ literalCode: string = '';
22
+
23
+ [k: string]: any;
24
+
25
+ constructor (message: string, code?: keyof FxOrmError.PredefineErrorCodes, extras?: any) {
26
+ super();
27
+
28
+ Error.call(this);
29
+ (Error as any).captureStackTrace(this, this.constructor);
30
+
31
+ this.message = message;
32
+ if (code) {
33
+ this.code = codes[code];
34
+ this.literalCode = code;
35
+ if (!this.code) {
36
+ throw new Error("Invalid error code: " + code);
37
+ }
38
+ }
39
+ if (extras) {
40
+ for(let k in extras) {
41
+ this[k] = extras[k];
42
+ }
43
+ }
44
+ }
45
+
46
+ toString () {
47
+ return '[ORMError ' + this.literalCode + ': ' + this.message + ']';
48
+ }
49
+ }
@@ -0,0 +1,297 @@
1
+ import { createRequire } from 'node:module';
2
+ const require = createRequire(import.meta.url);
3
+ import { Driver as DbDriver, type FxDbDriverNS, type IDbDriver } from "../db-driver/index";
4
+ import * as Hook from './Hook';
5
+ import * as Utilities from './Utilities';
6
+
7
+ import ORMError from "./Error";
8
+
9
+ import type { FxOrmNS } from './Typo/ORM';
10
+ import type { FxOrmInstance } from './Typo/instance';
11
+ import type { FxOrmAssociation } from './Typo/assoc';
12
+ import type { FxOrmModel } from './Typo/model';
13
+ import type { FxOrmHook } from './Typo/hook';
14
+ import type { FxOrmProperty } from "./Typo/property";
15
+
16
+ export function buildDbDriver (opts: string | FxDbDriverNS.DBConnectionConfig): FxOrmNS.ORMLike | IDbDriver {
17
+ if (!opts) {
18
+ return Utilities.ORM_Error(new ORMError("CONNECTION_URL_EMPTY", 'PARAM_MISMATCH'));
19
+ } else if (typeof opts === 'string') {
20
+ if ((opts as string).trim().length === 0) {
21
+ return Utilities.ORM_Error(new ORMError("CONNECTION_URL_EMPTY", 'PARAM_MISMATCH'));
22
+ }
23
+ }
24
+ let driver: IDbDriver
25
+ try {
26
+ driver = DbDriver.create(opts)
27
+ } catch (error) {
28
+ if ((error as Error).message === '[driver.config] invalid protocol')
29
+ return Utilities.ORM_Error(new ORMError("CONNECTION_URL_NO_PROTOCOL", 'PARAM_MISMATCH'));
30
+ throw error;
31
+ }
32
+ const config = driver.config
33
+
34
+ const isSqlite = config.protocol === 'sqlite:';
35
+
36
+ if (isSqlite && config.timezone === undefined)
37
+ driver.extend_config.timezone = 'UTC';
38
+
39
+ return driver;
40
+ }
41
+
42
+ /* model helpers :start */
43
+ // TODO: add test case
44
+ export function pickProperties<T extends FxOrmModel.Model = FxOrmModel.Model>(
45
+ m: T,
46
+ picker: ((p: FxOrmProperty.NormalizedProperty, k: string, m: T) => boolean)
47
+ ) {
48
+ return Object.entries(m.__propertiesByName).reduce((accu, [k, prop]) => {
49
+ if (picker(prop, k, m)) {
50
+ accu[k] = prop
51
+ };
52
+ return accu;
53
+ }, {} as Record<string, FxOrmProperty.NormalizedProperty>);
54
+ }
55
+ /* model helpers :end */
56
+
57
+ function manyAssocsFromInst (instance: FxOrmInstance.Instance): FxOrmAssociation.InstanceAssociationItem_HasMany[] {
58
+ return instance.__instRtd.many_associations
59
+ }
60
+ function oneAssocsFromInst (instance: FxOrmInstance.Instance): FxOrmAssociation.InstanceAssociationItem_HasOne[] {
61
+ return instance.__instRtd.one_associations
62
+ }
63
+ function extendsToAssocsFromInst (instance: FxOrmInstance.Instance): FxOrmAssociation.InstanceAssociationItem_ExtendTos[] {
64
+ return instance.__instRtd.extend_associations
65
+ }
66
+
67
+ /* by instance extname :start */
68
+ export function getManyAssociationItemFromInstanceByExtname (instance: FxOrmInstance.Instance, extend_name: string): FxOrmAssociation.InstanceAssociationItem_HasMany {
69
+ const many_assocs = manyAssocsFromInst(instance)
70
+
71
+ return many_assocs.find(a => a.name === extend_name)!
72
+ }
73
+
74
+ export function getOneAssociationItemFromInstanceByExtname (instance: FxOrmInstance.Instance, extend_name: string): FxOrmAssociation.InstanceAssociationItem_HasOne {
75
+ const one_assocs = oneAssocsFromInst(instance)
76
+
77
+ return one_assocs.find(a => a.name === extend_name)!
78
+ }
79
+
80
+ export function getExtendsToAssociationItemFromInstanceByExtname (instance: FxOrmInstance.Instance, extend_name: string): FxOrmAssociation.InstanceAssociationItem_ExtendTos {
81
+ const extendsto_assocs = extendsToAssocsFromInst(instance)
82
+
83
+ return extendsto_assocs.find(a => a.name === extend_name)!
84
+ }
85
+
86
+ export function getAssociationItemFromInstanceByExtname (reltype: string, inst: FxOrmInstance.Instance, extend_name: string): FxOrmAssociation.InstanceAssociationItem {
87
+ switch (reltype) {
88
+ default:
89
+ throw 'invalid association reltype!'
90
+ case 'extendsTo':
91
+ return getExtendsToAssociationItemFromInstanceByExtname(inst, extend_name);
92
+
93
+ case 'hasOne':
94
+ return getOneAssociationItemFromInstanceByExtname(inst, extend_name);
95
+
96
+ case 'hasMany':
97
+ return getManyAssociationItemFromInstanceByExtname(inst, extend_name);
98
+ }
99
+ }
100
+ /* by instance extname :end */
101
+
102
+ /* by instance x assoc_model :start */
103
+ export function getManyAssociationItemFromInstanceByAssocModel (instance: FxOrmInstance.Instance, assoc_model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem_HasMany {
104
+ const many_assocs = manyAssocsFromInst(instance)
105
+
106
+ return many_assocs.find(a => a.model === assoc_model)!
107
+ }
108
+ export function getOneAssociationItemFromInstanceByAssocModel (instance: FxOrmInstance.Instance, assoc_model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem_HasOne {
109
+ const one_assocs = oneAssocsFromInst(instance)
110
+
111
+ return one_assocs.find(a => a.model === assoc_model)!
112
+ }
113
+ export function getExtendsToAssociationItemFromInstanceByAssocModel (instance: FxOrmInstance.Instance, assoc_model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem_ExtendTos {
114
+ const extendsto_assocs = extendsToAssocsFromInst(instance)
115
+
116
+ return extendsto_assocs.find(a => a.model === assoc_model)!
117
+ }
118
+ export function getAssociationItemFromInstanceByAssocModel (reltype: string, inst: FxOrmInstance.Instance, assoc_model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem {
119
+ switch (reltype) {
120
+ default:
121
+ throw 'invalid association reltype!'
122
+ case 'extendsTo':
123
+ return getManyAssociationItemFromInstanceByAssocModel(inst, assoc_model);
124
+
125
+ case 'hasOne':
126
+ return getOneAssociationItemFromInstanceByAssocModel(inst, assoc_model);
127
+
128
+ case 'hasMany':
129
+ return getExtendsToAssociationItemFromInstanceByAssocModel(inst, assoc_model);
130
+ }
131
+ }
132
+ /* by instance x assoc_model :end */
133
+
134
+ /* by ext_name x _model :start */
135
+ export function getManyAssociationItemFromModel (ext_name: string, _model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem_HasMany {
136
+ const rel_info = _model.associations[ext_name]
137
+
138
+ if (!rel_info || rel_info.type !== 'hasMany')
139
+ return null as any;
140
+
141
+ return rel_info.association
142
+ }
143
+ export function getOneAssociationItemFromModel (ext_name: string, _model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem_HasOne {
144
+ const rel_info = _model.associations[ext_name]
145
+
146
+ if (!rel_info || rel_info.type !== 'hasOne')
147
+ return null as any;
148
+
149
+ return rel_info.association
150
+ }
151
+ export function getExtendsToAssociationItemFromModel (ext_name: string, _model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem_ExtendTos {
152
+ const rel_info = _model.associations[ext_name]
153
+
154
+ if (!rel_info || rel_info.type !== 'extendsTo')
155
+ return null as any;
156
+
157
+ return rel_info.association
158
+ }
159
+ export function getAssociationItemFromModel (reltype: string, extend_name: string, _model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem {
160
+ switch (reltype) {
161
+ default:
162
+ throw 'invalid association reltype!'
163
+ case 'extendsTo':
164
+ return getManyAssociationItemFromModel(extend_name, _model);
165
+
166
+ case 'hasOne':
167
+ return getOneAssociationItemFromModel(extend_name, _model);
168
+
169
+ case 'hasMany':
170
+ return getExtendsToAssociationItemFromModel(extend_name, _model);
171
+ }
172
+ }
173
+ export function tryGetAssociationItemFromModel (extend_name: string, _model: FxOrmModel.Model): FxOrmAssociation.InstanceAssociationItem {
174
+ return getManyAssociationItemFromModel(extend_name, _model) || getOneAssociationItemFromModel(extend_name, _model) || getExtendsToAssociationItemFromModel(extend_name, _model)
175
+ }
176
+ /* by ext_name x assoc_model :end */
177
+
178
+ /* hooks :start */
179
+ export function prependHook (hooks: FxOrmModel.Hooks, hookName: keyof FxOrmModel.Hooks, preLogic: FxOrmHook.HookActionCallback | FxOrmHook.HookResultCallback): void {
180
+ if (typeof hooks[hookName] === 'function') {
181
+ var oldHook = hooks[hookName];
182
+
183
+ const callOldHook = function (this: any, next: ((...args: any[]) => any)|boolean) {
184
+ if (typeof oldHook === 'function') {
185
+ if (oldHook.length > 0)
186
+ return (oldHook as any).call(this, next)
187
+
188
+ (oldHook as any).call(this)
189
+ }
190
+
191
+ if (typeof next === 'function')
192
+ next()
193
+ }
194
+
195
+ hooks[hookName] = function (this: any, next: any) {
196
+ if (preLogic.length > 0) {
197
+ var self = this
198
+ return (preLogic as any).call(this, function () {
199
+ callOldHook.call(self, next)
200
+ })
201
+ }
202
+
203
+ preLogic.call(this)
204
+ callOldHook.call(this, next)
205
+ };
206
+ } else {
207
+ hooks[hookName] = preLogic as any;
208
+ }
209
+ }
210
+
211
+ export function preReplaceHook (
212
+ m: FxOrmModel.Model,
213
+ opts: FxOrmModel.ModelDefineOptions,
214
+ hookName: keyof FxOrmModel.Hooks,
215
+ cb: (this: FxOrmInstance.Instance, inst: FxOrmInstance.Instance) => void
216
+ ): void {
217
+ var _oldHook: FxOrmHook.HookActionCallback | FxOrmHook.HookResultCallback;
218
+ if (opts !== undefined && opts.hooks)
219
+ _oldHook = opts.hooks[hookName] as typeof _oldHook;
220
+
221
+ (m[hookName] as any)(function (this: FxOrmInstance.Instance, next: any) {
222
+ cb.call(this, this);
223
+
224
+ if (_oldHook) {
225
+ if (_oldHook.length > 0) {
226
+ if (typeof next === 'boolean')
227
+ return (_oldHook as FxOrmHook.HookResultCallback).call(this, next);
228
+ else
229
+ return (_oldHook as FxOrmHook.HookActionCallback).call(this, next);
230
+ }
231
+ _oldHook!.call(this);
232
+ }
233
+
234
+ if (typeof next === 'function')
235
+ next.call(this);
236
+ });
237
+ }
238
+
239
+ export const hookTrigger = Hook.trigger
240
+ export const hookWait = Hook.wait
241
+ /* hooks: end */
242
+
243
+ /* arguments input :start */
244
+ export function selectArgs (
245
+ args: ArrayLike<any>,
246
+ callback: {
247
+ (
248
+ arg_type: "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function",
249
+ arg: any,
250
+ idx: number
251
+ ): void
252
+ }
253
+ ): void {
254
+ // copy slice to ensure loop idx is correct
255
+ args = Array.prototype.slice.apply(args);
256
+
257
+ for (let i = 0, arg: any = null; i < args.length; i++) {
258
+ arg = args[i]
259
+ callback(typeof arg, arg, i);
260
+ }
261
+ }
262
+ /* arguments input :end */
263
+
264
+ /* singleton helpers: start */
265
+ export function parseDriverUidAndTableNameFromUID (uid: string) {
266
+ uid = uid || ''
267
+
268
+ const arr = uid.split('/');
269
+
270
+ return {
271
+ driver_uid: arr[0],
272
+ table: arr[1],
273
+ driver_table_uid: Utilities.makeIdForDriverTable(arr[0], arr[1])
274
+ }
275
+ }
276
+ /* singleton helpers: end */
277
+ export function valueOrComputeFunction <T = Exclude<any, Function>> (
278
+ input: T | ((...args: any[]) => T),
279
+ args?: any[],
280
+ thisArg?: any
281
+ ): T {
282
+ if (typeof input === 'function')
283
+ return (input as (...args: any[]) => T).apply(thisArg, args || [])
284
+
285
+ return input
286
+ }
287
+
288
+ /* callback helpers :start */
289
+ export function pickCallback<T extends (...args: any[]) => any> (args: ArrayLike<any>): T | null {
290
+ for (let i = 0; i < args.length; i++) {
291
+ if (typeof args[i] === 'function')
292
+ return args[i] as T;
293
+ }
294
+
295
+ return null;
296
+ }
297
+ /* callback helpers :end */