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,610 @@
1
+ import { EventEmitter } from 'events';
2
+ import { PendingOperation } from './PendingOperation';
3
+ import { Resource } from './Resource';
4
+ import { checkOptionalTime, delay, duration, now, reflect, tryPromise } from './utils';
5
+
6
+ export type Callback<T> = (err: Error | null, resource?: T) => any;
7
+ export type CallbackOrPromise<T> = (cb: Callback<T>) => T | void | PromiseLike<T>;
8
+
9
+ export interface PoolOptions<T> {
10
+ create: CallbackOrPromise<T>;
11
+ destroy: (resource: T) => any;
12
+ min: number;
13
+ max: number;
14
+ acquireTimeoutMillis?: number;
15
+ createTimeoutMillis?: number;
16
+ destroyTimeoutMillis?: number;
17
+ idleTimeoutMillis?: number;
18
+ createRetryIntervalMillis?: number;
19
+ reapIntervalMillis?: number;
20
+ log?: (msg: string, level?: 'warn') => any;
21
+ validate?: (resource: T) => boolean;
22
+ propagateCreateError?: boolean;
23
+ }
24
+
25
+ export class Pool<T> {
26
+ protected min: number;
27
+ protected max: number;
28
+ protected used: Resource<T>[] = [];
29
+ protected free: Resource<T>[] = [];
30
+ protected pendingCreates: PendingOperation<T>[] = [];
31
+ protected pendingAcquires: PendingOperation<T>[] = [];
32
+ protected pendingDestroys: PendingOperation<T>[] = [];
33
+ protected pendingValidations: PendingOperation<T>[] = [];
34
+ protected interval: ReturnType<typeof setInterval> | null = null;
35
+ protected destroyed = false;
36
+ protected propagateCreateError: boolean;
37
+ protected idleTimeoutMillis: number;
38
+ protected createRetryIntervalMillis: number;
39
+ protected reapIntervalMillis: number;
40
+ protected createTimeoutMillis: number;
41
+ protected destroyTimeoutMillis: number;
42
+ protected acquireTimeoutMillis: number;
43
+ protected log: (msg: string, level?: 'warn') => any;
44
+ protected creator: CallbackOrPromise<T>;
45
+ protected destroyer: (resource: T) => any;
46
+ protected validate: (resource: T) => boolean;
47
+ protected eventId = 1;
48
+ protected emitter: EventEmitter = new EventEmitter();
49
+
50
+ constructor(opt: PoolOptions<T>) {
51
+ opt = opt || {} as PoolOptions<T>;
52
+
53
+ if (!opt.create) {
54
+ throw new Error('Tarn: opt.create function most be provided');
55
+ }
56
+
57
+ if (!opt.destroy) {
58
+ throw new Error('Tarn: opt.destroy function most be provided');
59
+ }
60
+
61
+ if (typeof opt.min !== 'number' || opt.min < 0 || opt.min !== Math.round(opt.min)) {
62
+ throw new Error('Tarn: opt.min must be an integer >= 0');
63
+ }
64
+
65
+ if (typeof opt.max !== 'number' || opt.max <= 0 || opt.max !== Math.round(opt.max)) {
66
+ throw new Error('Tarn: opt.max must be an integer > 0');
67
+ }
68
+
69
+ if (opt.min > opt.max) {
70
+ throw new Error('Tarn: opt.max is smaller than opt.min');
71
+ }
72
+
73
+ if (!checkOptionalTime(opt.acquireTimeoutMillis)) {
74
+ throw new Error('Tarn: invalid opt.acquireTimeoutMillis ' + JSON.stringify(opt.acquireTimeoutMillis));
75
+ }
76
+
77
+ if (!checkOptionalTime(opt.createTimeoutMillis)) {
78
+ throw new Error('Tarn: invalid opt.createTimeoutMillis ' + JSON.stringify(opt.createTimeoutMillis));
79
+ }
80
+
81
+ if (!checkOptionalTime(opt.destroyTimeoutMillis)) {
82
+ throw new Error('Tarn: invalid opt.destroyTimeoutMillis ' + JSON.stringify(opt.destroyTimeoutMillis));
83
+ }
84
+
85
+ if (!checkOptionalTime(opt.idleTimeoutMillis)) {
86
+ throw new Error('Tarn: invalid opt.idleTimeoutMillis ' + JSON.stringify(opt.idleTimeoutMillis));
87
+ }
88
+
89
+ if (!checkOptionalTime(opt.reapIntervalMillis)) {
90
+ throw new Error('Tarn: invalid opt.reapIntervalMillis ' + JSON.stringify(opt.reapIntervalMillis));
91
+ }
92
+
93
+ if (!checkOptionalTime(opt.createRetryIntervalMillis)) {
94
+ throw new Error('Tarn: invalid opt.createRetryIntervalMillis ' +
95
+ JSON.stringify(opt.createRetryIntervalMillis));
96
+ }
97
+
98
+ const allowedKeys: Record<string, boolean> = {
99
+ create: true,
100
+ validate: true,
101
+ destroy: true,
102
+ log: true,
103
+ min: true,
104
+ max: true,
105
+ acquireTimeoutMillis: true,
106
+ createTimeoutMillis: true,
107
+ destroyTimeoutMillis: true,
108
+ idleTimeoutMillis: true,
109
+ reapIntervalMillis: true,
110
+ createRetryIntervalMillis: true,
111
+ propagateCreateError: true
112
+ };
113
+
114
+ for (const key of Object.keys(opt)) {
115
+ if (!allowedKeys[key]) {
116
+ throw new Error(`Tarn: unsupported option opt.${key}`);
117
+ }
118
+ }
119
+
120
+ this.creator = opt.create;
121
+ this.destroyer = opt.destroy;
122
+ this.validate = typeof opt.validate === 'function' ? opt.validate : () => true;
123
+ this.log = opt.log || (() => { });
124
+ this.acquireTimeoutMillis = opt.acquireTimeoutMillis || 30000;
125
+ this.createTimeoutMillis = opt.createTimeoutMillis || 30000;
126
+ this.destroyTimeoutMillis = opt.destroyTimeoutMillis || 5000;
127
+ this.idleTimeoutMillis = opt.idleTimeoutMillis || 30000;
128
+ this.reapIntervalMillis = opt.reapIntervalMillis || 1000;
129
+ this.createRetryIntervalMillis = opt.createRetryIntervalMillis || 200;
130
+ this.propagateCreateError = !!opt.propagateCreateError;
131
+ this.min = opt.min;
132
+ this.max = opt.max;
133
+ }
134
+
135
+ numUsed(): number {
136
+ return this.used.length;
137
+ }
138
+
139
+ numFree(): number {
140
+ return this.free.length;
141
+ }
142
+
143
+ numPendingAcquires(): number {
144
+ return this.pendingAcquires.length;
145
+ }
146
+
147
+ numPendingValidations(): number {
148
+ return this.pendingValidations.length;
149
+ }
150
+
151
+ numPendingCreates(): number {
152
+ return this.pendingCreates.length;
153
+ }
154
+
155
+ acquire(): PendingOperation<T> {
156
+ const eventId = this.eventId++;
157
+ this._executeEventHandlers('acquireRequest', eventId);
158
+ const pendingAcquire = new PendingOperation<T>(this.acquireTimeoutMillis);
159
+ this.pendingAcquires.push(pendingAcquire);
160
+
161
+ // If the acquire fails for whatever reason
162
+ // remove it from the pending queue.
163
+ pendingAcquire.promise = pendingAcquire.promise
164
+ .then(resource => {
165
+ this._executeEventHandlers('acquireSuccess', eventId, resource);
166
+ return resource;
167
+ })
168
+ .catch(err => {
169
+ this._executeEventHandlers('acquireFail', eventId, err);
170
+ remove(this.pendingAcquires, pendingAcquire);
171
+ return Promise.reject(err);
172
+ });
173
+
174
+ this._tryAcquireOrCreate();
175
+ return pendingAcquire;
176
+ }
177
+
178
+ release(resource: T): boolean {
179
+ this._executeEventHandlers('release', resource);
180
+
181
+ for (let i = 0, l = this.used.length; i < l; ++i) {
182
+ const used = this.used[i];
183
+
184
+ if (used.resource === resource) {
185
+ this.used.splice(i, 1);
186
+ this.free.push(used.resolve());
187
+ this._tryAcquireOrCreate();
188
+ return true;
189
+ }
190
+ }
191
+
192
+ return false;
193
+ }
194
+
195
+ isEmpty(): boolean {
196
+ return ([
197
+ this.numFree(),
198
+ this.numUsed(),
199
+ this.numPendingAcquires(),
200
+ this.numPendingValidations(),
201
+ this.numPendingCreates()
202
+ ].reduce((total, value) => total + value) === 0);
203
+ }
204
+
205
+ /**
206
+ * Reaping cycle.
207
+ */
208
+ check(): void {
209
+ const timestamp = now();
210
+ const newFree: Resource<T>[] = [];
211
+ const minKeep = this.min - this.used.length;
212
+ const maxDestroy = this.free.length - minKeep;
213
+ let numDestroyed = 0;
214
+
215
+ this.free.forEach(free => {
216
+ if (duration(timestamp, free.timestamp) >= this.idleTimeoutMillis &&
217
+ numDestroyed < maxDestroy) {
218
+ numDestroyed++;
219
+ this._destroy(free.resource);
220
+ } else {
221
+ newFree.push(free);
222
+ }
223
+ });
224
+
225
+ this.free = newFree;
226
+
227
+ // Pool is completely empty, stop reaping.
228
+ // Next .acquire will start reaping interval again.
229
+ if (this.isEmpty()) {
230
+ this._stopReaping();
231
+ }
232
+ }
233
+
234
+ destroy(): Promise<unknown> {
235
+ const eventId = this.eventId++;
236
+ this._executeEventHandlers('poolDestroyRequest', eventId);
237
+ this._stopReaping();
238
+ this.destroyed = true;
239
+
240
+ // First wait for all the pending creates get ready.
241
+ return reflect(Promise.all(this.pendingCreates.map(create => reflect(create.promise)))
242
+ .then(() => {
243
+ // eslint-disable-next-line
244
+ return new Promise<void>((resolve, reject) => {
245
+ // poll every 100ms and wait that all validations are ready
246
+ if (this.numPendingValidations() === 0) {
247
+ resolve();
248
+ return;
249
+ }
250
+
251
+ const interval = setInterval(() => {
252
+ if (this.numPendingValidations() === 0) {
253
+ clearInterval(interval);
254
+ resolve();
255
+ }
256
+ }, 100);
257
+ });
258
+ })
259
+ .then(() => {
260
+ // Wait for all the used resources to be freed.
261
+ return Promise.all(this.used.map(used => reflect(used.promise)));
262
+ })
263
+ .then(() => {
264
+ // Abort all pending acquires.
265
+ return Promise.all(this.pendingAcquires.map(acquire => {
266
+ acquire.abort();
267
+ return reflect(acquire.promise);
268
+ }));
269
+ })
270
+ .then(() => {
271
+ // Now we can destroy all the freed resources.
272
+ return Promise.all(this.free.map(free => reflect(this._destroy(free.resource))));
273
+ })
274
+ .then(() => {
275
+ // Also wait rest of the pending destroys to finish
276
+ return Promise.all(this.pendingDestroys.map(pd => pd.promise));
277
+ })
278
+ .then(() => {
279
+ this.free = [];
280
+ this.pendingAcquires = [];
281
+ })).then(res => {
282
+ this._executeEventHandlers('poolDestroySuccess', eventId);
283
+ this.emitter.removeAllListeners();
284
+ return res;
285
+ });
286
+ }
287
+
288
+ on(eventName: string, handler: (...args: any[]) => void): void {
289
+ this.emitter.on(eventName, handler);
290
+ }
291
+
292
+ removeListener(event: string | symbol, listener: (...args: any[]) => void): void {
293
+ this.emitter.removeListener(event, listener);
294
+ }
295
+
296
+ removeAllListeners(event?: string | symbol): void {
297
+ this.emitter.removeAllListeners(event);
298
+ }
299
+
300
+ /**
301
+ * The most important method that is called always when resources
302
+ * are created / destroyed / acquired / released. In other words
303
+ * every time when resources are moved from used to free or vice
304
+ * versa.
305
+ *
306
+ * Either assigns free resources to pendingAcquires or creates new
307
+ * resources if there is room for it in the pool.
308
+ */
309
+ _tryAcquireOrCreate(): void {
310
+ if (this.destroyed) {
311
+ return;
312
+ }
313
+
314
+ if (this._hasFreeResources()) {
315
+ this._doAcquire();
316
+ } else if (this._shouldCreateMoreResources()) {
317
+ this._doCreate();
318
+ }
319
+ }
320
+
321
+ _hasFreeResources(): boolean {
322
+ return this.free.length > 0;
323
+ }
324
+
325
+ _doAcquire(): void {
326
+ // Acquire as many pending acquires as possible concurrently
327
+ while (this._canAcquire()) {
328
+ // To allow async validation, we actually need to move free resource
329
+ // and pending acquire temporary from their respective arrays and depending
330
+ // on validation result to either leave the free resource to used resources array
331
+ // or destroy the free resource if validation did fail.
332
+ const pendingAcquire = this.pendingAcquires.shift();
333
+ const free = this.free.pop();
334
+
335
+ if (free === undefined || pendingAcquire === undefined) {
336
+ const errMessage = 'this.free was empty while trying to acquire resource';
337
+ this.log(`Tarn: ${errMessage}`, 'warn');
338
+ throw new Error(`Internal error, should never happen. ${errMessage}`);
339
+ }
340
+
341
+ // Make sure that pendingAcquire that is being validated is not lost and
342
+ // can be freed when pool is destroyed.
343
+ this.pendingValidations.push(pendingAcquire);
344
+ // Must be added here pre-emptively to prevent logic that decides
345
+ // if new resources are created will keep on working correctly.
346
+ this.used.push(free);
347
+
348
+ // if acquire fails also pending validation, must be aborted so that pre reserved
349
+ // resource will be returned to free resources immediately
350
+ const abortAbleValidation = new PendingOperation<boolean>(this.acquireTimeoutMillis);
351
+
352
+ // eslint-disable-next-line
353
+ pendingAcquire.promise.catch(err => {
354
+ abortAbleValidation.abort();
355
+ });
356
+
357
+ abortAbleValidation.promise
358
+ .catch(err => {
359
+ // There's nothing we can do here but log the error. This would otherwise
360
+ // leak out as an unhandled exception.
361
+ this.log('Tarn: resource validator threw an exception ' + (err as Error).stack, 'warn');
362
+ return false;
363
+ })
364
+ .then(validationSuccess => {
365
+ try {
366
+ if (validationSuccess && !pendingAcquire.isRejected) {
367
+ // At least one active resource exist, start reaping.
368
+ this._startReaping();
369
+ pendingAcquire.resolve(free.resource);
370
+ } else {
371
+ remove(this.used, free);
372
+
373
+ // Only destroy the resource if the validation has failed
374
+ if (!validationSuccess) {
375
+ this._destroy(free.resource);
376
+ // Since we destroyed an invalid resource and were not able to fulfill
377
+ // all the pending acquires, we may need to create new ones or at
378
+ // least run this acquire loop again to verify it. But not immediately
379
+ // to prevent starving event loop.
380
+ setTimeout(() => {
381
+ this._tryAcquireOrCreate();
382
+ }, 0);
383
+ } else {
384
+ this.free.push(free);
385
+ }
386
+
387
+ // is acquire was canceled, failed or timed out already
388
+ // no need to return it to pending queries
389
+ if (!pendingAcquire.isRejected) {
390
+ this.pendingAcquires.unshift(pendingAcquire);
391
+ }
392
+ }
393
+ } finally {
394
+ remove(this.pendingValidations, pendingAcquire);
395
+ }
396
+ });
397
+
398
+ // try to validate
399
+ this._validateResource(free.resource)
400
+ .then(validationSuccess => {
401
+ abortAbleValidation.resolve(validationSuccess);
402
+ })
403
+ .catch(err => {
404
+ abortAbleValidation.reject(err);
405
+ });
406
+ }
407
+ }
408
+
409
+ _canAcquire(): boolean {
410
+ return this.free.length > 0 && this.pendingAcquires.length > 0;
411
+ }
412
+
413
+ _validateResource(resource: T): Promise<boolean> {
414
+ try {
415
+ return Promise.resolve(this.validate(resource));
416
+ } catch (err) {
417
+ // prevent leaking of sync exception
418
+ return Promise.reject(err);
419
+ }
420
+ }
421
+
422
+ _shouldCreateMoreResources(): boolean {
423
+ return (
424
+ this.used.length + this.pendingCreates.length < this.max &&
425
+ this.pendingCreates.length < this.pendingAcquires.length
426
+ );
427
+ }
428
+
429
+ _doCreate(): void {
430
+ const pendingAcquiresBeforeCreate = this.pendingAcquires.slice();
431
+ const pendingCreate = this._create();
432
+ pendingCreate.promise
433
+ .then((): void => {
434
+ // Not returned on purpose.
435
+ this._tryAcquireOrCreate();
436
+ })
437
+ .catch(err => {
438
+ if (this.propagateCreateError && this.pendingAcquires.length !== 0) {
439
+ // If propagateCreateError is true, we don't retry the create
440
+ // but reject the first pending acquire immediately. Intentionally
441
+ // use `this.pendingAcquires` instead of `pendingAcquiresBeforeCreate`
442
+ // in case some acquires in pendingAcquiresBeforeCreate have already
443
+ // been resolved.
444
+ this.pendingAcquires[0].reject(err);
445
+ }
446
+
447
+ // Save the create error to all pending acquires so that we can use it
448
+ // as the error to reject the acquire if it times out.
449
+ pendingAcquiresBeforeCreate.forEach(pendingAcquire => {
450
+ pendingAcquire.possibleTimeoutCause = err;
451
+ });
452
+
453
+ // Not returned on purpose.
454
+ delay(this.createRetryIntervalMillis).then(() => this._tryAcquireOrCreate());
455
+ });
456
+ }
457
+
458
+ _create(): PendingOperation<T> {
459
+ const eventId = this.eventId++;
460
+ this._executeEventHandlers('createRequest', eventId);
461
+ const pendingCreate = new PendingOperation<T>(this.createTimeoutMillis);
462
+
463
+ // If an error occurs (likely a create timeout) remove this creation from
464
+ // the list of pending creations so we try to create a new one.
465
+ pendingCreate.promise = pendingCreate.promise.catch(err => {
466
+ if (remove(this.pendingCreates, pendingCreate)) {
467
+ // TODO: figure out more consistent way for different error handlers in next rewrite
468
+ this._executeEventHandlers('createFail', eventId, err);
469
+ }
470
+
471
+ throw err;
472
+ });
473
+
474
+ this.pendingCreates.push(pendingCreate);
475
+
476
+ callbackOrPromise(this.creator)
477
+ .then((resource): void => {
478
+ if (pendingCreate.isRejected) {
479
+ this.destroyer(resource);
480
+ return;
481
+ }
482
+
483
+ remove(this.pendingCreates, pendingCreate);
484
+ this.free.push(new Resource(resource));
485
+
486
+ // Not returned on purpose.
487
+ pendingCreate.resolve(resource);
488
+ this._executeEventHandlers('createSuccess', eventId, resource);
489
+ })
490
+ .catch((err): void => {
491
+ if (pendingCreate.isRejected) {
492
+ return;
493
+ }
494
+
495
+ if (remove(this.pendingCreates, pendingCreate)) {
496
+ this._executeEventHandlers('createFail', eventId, err);
497
+ }
498
+
499
+ // Not returned on purpose.
500
+ pendingCreate.reject(err);
501
+ });
502
+
503
+ return pendingCreate;
504
+ }
505
+
506
+ _destroy(resource: T): Promise<T | void> {
507
+ const eventId = this.eventId++;
508
+ this._executeEventHandlers('destroyRequest', eventId, resource);
509
+
510
+ // this.destroyer can be both synchronous and asynchronous.
511
+ // so we wrap it to promise to get all exceptions through same pipeline
512
+ const pendingDestroy = new PendingOperation<T>(this.destroyTimeoutMillis);
513
+ const retVal = Promise.resolve().then(() => this.destroyer(resource));
514
+
515
+ retVal
516
+ .then(() => {
517
+ pendingDestroy.resolve(resource);
518
+ })
519
+ .catch((err) => {
520
+ pendingDestroy.reject(err);
521
+ });
522
+
523
+ this.pendingDestroys.push(pendingDestroy);
524
+
525
+ // In case of an error there's nothing we can do here but log it.
526
+ return pendingDestroy.promise
527
+ .then(res => {
528
+ this._executeEventHandlers('destroySuccess', eventId, resource);
529
+ return res;
530
+ })
531
+ .catch(err => this._logDestroyerError(eventId, resource, err))
532
+ .then(res => {
533
+ const index = this.pendingDestroys.findIndex(pd => pd === pendingDestroy);
534
+ this.pendingDestroys.splice(index, 1);
535
+ return res;
536
+ });
537
+ }
538
+
539
+ _logDestroyerError(eventId: number, resource: T, err: Error): void {
540
+ this._executeEventHandlers('destroyFail', eventId, resource, err);
541
+ this.log('Tarn: resource destroyer threw an exception ' + (err as Error).stack, 'warn');
542
+ }
543
+
544
+ _startReaping(): void {
545
+ if (!this.interval) {
546
+ this._executeEventHandlers('startReaping');
547
+ this.interval = setInterval(() => this.check(), this.reapIntervalMillis);
548
+ }
549
+ }
550
+
551
+ _stopReaping(): void {
552
+ if (this.interval !== null) {
553
+ this._executeEventHandlers('stopReaping');
554
+ clearInterval(this.interval);
555
+ }
556
+
557
+ this.interval = null;
558
+ }
559
+
560
+ _executeEventHandlers(eventName: string, ...args: any[]): void {
561
+ const listeners = this.emitter.listeners(eventName);
562
+
563
+ // just calling .emit() would stop running rest of the listeners if one them fails
564
+ listeners.forEach(listener => {
565
+ try {
566
+ (listener as (...listenerArgs: any[]) => void)(...args);
567
+ } catch (err) {
568
+ // There's nothing we can do here but log the error. This would otherwise
569
+ // leak out as an unhandled exception.
570
+ this.log(`Tarn: event handler "${eventName}" threw an exception ${(err as Error).stack}`, 'warn');
571
+ }
572
+ });
573
+ }
574
+ }
575
+
576
+ function remove<T>(arr: T[], item: T): boolean {
577
+ const idx = arr.indexOf(item);
578
+
579
+ if (idx === -1) {
580
+ return false;
581
+ } else {
582
+ arr.splice(idx, 1);
583
+ return true;
584
+ }
585
+ }
586
+
587
+ function callbackOrPromise<T>(func: CallbackOrPromise<T>): Promise<T> {
588
+ return new Promise<T>((resolve, reject) => {
589
+ const callback: Callback<T> = (err, resource) => {
590
+ if (err) {
591
+ reject(err);
592
+ } else {
593
+ resolve(resource as T);
594
+ }
595
+ };
596
+
597
+ tryPromise(() => func(callback))
598
+ .then(res => {
599
+ // If the result is falsy, we assume that the callback will
600
+ // be called instead of interpreting the falsy value as a
601
+ // result value.
602
+ if (res) {
603
+ resolve(res as T);
604
+ }
605
+ })
606
+ .catch(err => {
607
+ reject(err);
608
+ });
609
+ });
610
+ }
@@ -0,0 +1,29 @@
1
+ export type PromiseInspectionArgs<T> =
2
+ | { value: T; error?: Error }
3
+ | { value?: T; error: Error };
4
+
5
+ export class PromiseInspection<T> {
6
+ _value: T | void;
7
+ _error: Error | void;
8
+
9
+ constructor(args: PromiseInspectionArgs<T>) {
10
+ this._value = args.value;
11
+ this._error = args.error;
12
+ }
13
+
14
+ value(): T | void {
15
+ return this._value;
16
+ }
17
+
18
+ reason(): Error | void {
19
+ return this._error;
20
+ }
21
+
22
+ isRejected(): boolean {
23
+ return !!this._error;
24
+ }
25
+
26
+ isFulfilled(): boolean {
27
+ return !!this._value;
28
+ }
29
+ }
@@ -0,0 +1,22 @@
1
+ import { defer } from './utils';
2
+ import type { Deferred } from './utils';
3
+
4
+ export class Resource<T> {
5
+ readonly timestamp: number;
6
+ protected deferred: Deferred<void>;
7
+
8
+ constructor(public resource: T) {
9
+ this.resource = resource;
10
+ this.timestamp = Date.now();
11
+ this.deferred = defer<void>();
12
+ }
13
+
14
+ get promise(): Promise<void> {
15
+ return this.deferred.promise;
16
+ }
17
+
18
+ resolve(): Resource<T> {
19
+ this.deferred.resolve(undefined);
20
+ return new Resource(this.resource);
21
+ }
22
+ }
@@ -0,0 +1 @@
1
+ export class TimeoutError extends Error {}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * TypeScript port of tarn.js (MIT), vendored for knex's sandbox runtime.
3
+ * Knex only requires `Pool`/`TimeoutError`; the implementation is kept
4
+ * locally so `src` contains no JavaScript.
5
+ */
6
+ export { Pool } from './Pool';
7
+ export { TimeoutError } from './TimeoutError';