@daiso-tech/core 0.26.0 → 0.27.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 (275) hide show
  1. package/README.md +10 -0
  2. package/dist/async/utilities/delay/delay.d.ts +1 -12
  3. package/dist/async/utilities/delay/delay.js +1 -12
  4. package/dist/async/utilities/delay/delay.js.map +1 -1
  5. package/dist/async/utilities/lazy-promise/lazy-promise.d.ts +99 -65
  6. package/dist/async/utilities/lazy-promise/lazy-promise.js +151 -111
  7. package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
  8. package/dist/cache/contracts/_module-exports.d.ts +1 -0
  9. package/dist/cache/contracts/_module-exports.js +1 -0
  10. package/dist/cache/contracts/_module-exports.js.map +1 -1
  11. package/dist/cache/contracts/cache-adapter.contract.d.ts +19 -21
  12. package/dist/cache/contracts/cache-adapter.contract.js +1 -4
  13. package/dist/cache/contracts/cache-adapter.contract.js.map +1 -1
  14. package/dist/cache/contracts/cache-factory.contract.d.ts +1 -1
  15. package/dist/cache/contracts/cache.contract.d.ts +52 -88
  16. package/dist/cache/contracts/cache.contract.js +1 -2
  17. package/dist/cache/contracts/cache.contract.js.map +1 -1
  18. package/dist/cache/contracts/cache.errors.js +2 -5
  19. package/dist/cache/contracts/cache.errors.js.map +1 -1
  20. package/dist/cache/contracts/cache.events.d.ts +10 -10
  21. package/dist/cache/contracts/cache.events.js +2 -5
  22. package/dist/cache/contracts/cache.events.js.map +1 -1
  23. package/dist/cache/contracts/database-cache-adapter.contract.d.ts +89 -0
  24. package/dist/cache/contracts/database-cache-adapter.contract.js +5 -0
  25. package/dist/cache/contracts/database-cache-adapter.contract.js.map +1 -0
  26. package/dist/cache/implementations/adapters/_module-exports.d.ts +0 -1
  27. package/dist/cache/implementations/adapters/_module-exports.js +0 -1
  28. package/dist/cache/implementations/adapters/_module-exports.js.map +1 -1
  29. package/dist/cache/implementations/adapters/{kysely-sqlite-cache-adapter → kysely-cache-adapter}/_module.d.ts +1 -1
  30. package/dist/cache/implementations/adapters/kysely-cache-adapter/_module.js +2 -0
  31. package/dist/cache/implementations/adapters/kysely-cache-adapter/_module.js.map +1 -0
  32. package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.d.ts +73 -0
  33. package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js +224 -0
  34. package/dist/cache/implementations/adapters/kysely-cache-adapter/kysely-cache-adapter.js.map +1 -0
  35. package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.d.ts +29 -39
  36. package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.js +45 -54
  37. package/dist/cache/implementations/adapters/libsql-cache-adapter/libsql-cache-adapter.js.map +1 -1
  38. package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.d.ts +17 -33
  39. package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js +43 -52
  40. package/dist/cache/implementations/adapters/memory-cache-adapter/memory-cache-adapter.js.map +1 -1
  41. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.d.ts +13 -0
  42. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js +38 -0
  43. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js.map +1 -0
  44. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.d.ts +27 -33
  45. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js +145 -197
  46. package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js.map +1 -1
  47. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.d.ts +13 -0
  48. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js +39 -0
  49. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js.map +1 -0
  50. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.d.ts +16 -22
  51. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js +51 -132
  52. package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter.js.map +1 -1
  53. package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.d.ts +13 -0
  54. package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js +69 -0
  55. package/dist/cache/implementations/adapters/redis-cache-adapter/utilities.js.map +1 -0
  56. package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.d.ts +29 -40
  57. package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.js +46 -55
  58. package/dist/cache/implementations/adapters/sqlite-cache-adapter/sqlite-cache-adapter.js.map +1 -1
  59. package/dist/cache/implementations/derivables/_module-exports.d.ts +1 -1
  60. package/dist/cache/implementations/derivables/_module-exports.js +1 -1
  61. package/dist/cache/implementations/derivables/_module-exports.js.map +1 -1
  62. package/dist/cache/implementations/derivables/cache/_module.d.ts +1 -0
  63. package/dist/cache/implementations/derivables/cache/_module.js +1 -0
  64. package/dist/cache/implementations/derivables/cache/_module.js.map +1 -1
  65. package/dist/cache/implementations/derivables/cache/cache.d.ts +122 -565
  66. package/dist/cache/implementations/derivables/cache/cache.js +342 -854
  67. package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
  68. package/dist/cache/implementations/derivables/cache/database-cache-adapter.d.ts +21 -0
  69. package/dist/cache/implementations/derivables/cache/database-cache-adapter.js +110 -0
  70. package/dist/cache/implementations/derivables/cache/database-cache-adapter.js.map +1 -0
  71. package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.d.ts +9 -0
  72. package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.js +20 -0
  73. package/dist/cache/implementations/derivables/cache/is-database-cache-adapter.js.map +1 -0
  74. package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +65 -104
  75. package/dist/cache/implementations/derivables/cache-factory/cache-factory.js +101 -85
  76. package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
  77. package/dist/cache/implementations/test-utilities/_module-exports.d.ts +1 -0
  78. package/dist/cache/implementations/test-utilities/_module-exports.js +1 -0
  79. package/dist/cache/implementations/test-utilities/_module-exports.js.map +1 -1
  80. package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.d.ts +9 -11
  81. package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js +246 -247
  82. package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js.map +1 -1
  83. package/dist/cache/implementations/test-utilities/cache.test-suite.d.ts +16 -30
  84. package/dist/cache/implementations/test-utilities/cache.test-suite.js +271 -996
  85. package/dist/cache/implementations/test-utilities/cache.test-suite.js.map +1 -1
  86. package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.d.ts +60 -0
  87. package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.js +698 -0
  88. package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.js.map +1 -0
  89. package/dist/collection/contracts/async-collection.contract.d.ts +2 -3
  90. package/dist/collection/contracts/async-collection.contract.js +1 -12
  91. package/dist/collection/contracts/async-collection.contract.js.map +1 -1
  92. package/dist/collection/contracts/collection.contract.d.ts +1 -1
  93. package/dist/collection/contracts/collection.errors.d.ts +1 -1
  94. package/dist/collection/contracts/collection.errors.js +3 -6
  95. package/dist/collection/contracts/collection.errors.js.map +1 -1
  96. package/dist/collection/implementations/_shared.d.ts +4 -4
  97. package/dist/collection/implementations/_shared.js +6 -9
  98. package/dist/collection/implementations/_shared.js.map +1 -1
  99. package/dist/collection/implementations/async-iterable-collection/_shared/async-delay-iterable.js +2 -2
  100. package/dist/collection/implementations/async-iterable-collection/_shared/async-delay-iterable.js.map +1 -1
  101. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.d.ts +1 -1
  102. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js +6 -5
  103. package/dist/collection/implementations/async-iterable-collection/async-iterable-collection.js.map +1 -1
  104. package/dist/collection/implementations/iterable-collection/iterable-collection.d.ts +1 -1
  105. package/dist/collection/implementations/iterable-collection/iterable-collection.js +1 -1
  106. package/dist/collection/implementations/list-collection/list-collection.d.ts +1 -1
  107. package/dist/collection/implementations/list-collection/list-collection.js +1 -1
  108. package/dist/event-bus/contracts/event-bus-adapter.contract.d.ts +0 -9
  109. package/dist/event-bus/contracts/event-bus.contract.d.ts +5 -5
  110. package/dist/event-bus/contracts/event-bus.contract.js.map +1 -1
  111. package/dist/event-bus/contracts/event-bus.errors.js +2 -5
  112. package/dist/event-bus/contracts/event-bus.errors.js.map +1 -1
  113. package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.d.ts +9 -29
  114. package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js +14 -35
  115. package/dist/event-bus/implementations/adapters/memory-event-bus-adapter/memory-event-bus-adapter.js.map +1 -1
  116. package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.d.ts +1 -3
  117. package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.js +1 -7
  118. package/dist/event-bus/implementations/adapters/no-op-event-bus-adapter/no-op-event-bus-adapter.js.map +1 -1
  119. package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.d.ts +8 -15
  120. package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js +15 -34
  121. package/dist/event-bus/implementations/adapters/redis-pub-sub-event-bus-adapter/redis-pub-sub-event-bus-adapter.js.map +1 -1
  122. package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +78 -267
  123. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +119 -323
  124. package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
  125. package/dist/event-bus/implementations/derivables/event-bus/listener-store.d.ts +13 -0
  126. package/dist/event-bus/implementations/derivables/event-bus/listener-store.js +42 -0
  127. package/dist/event-bus/implementations/derivables/event-bus/listener-store.js.map +1 -0
  128. package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.d.ts +70 -96
  129. package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js +100 -90
  130. package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js.map +1 -1
  131. package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.d.ts +2 -48
  132. package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js +31 -122
  133. package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js.map +1 -1
  134. package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.d.ts +2 -50
  135. package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +41 -89
  136. package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
  137. package/dist/lock/contracts/database-lock-adapter.contract.d.ts +0 -9
  138. package/dist/lock/contracts/lock-adapter.contract.d.ts +0 -9
  139. package/dist/lock/contracts/lock-provider.contract.d.ts +1 -1
  140. package/dist/lock/contracts/lock.errors.js +2 -5
  141. package/dist/lock/contracts/lock.errors.js.map +1 -1
  142. package/dist/lock/contracts/lock.events.js +2 -5
  143. package/dist/lock/contracts/lock.events.js.map +1 -1
  144. package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.d.ts +0 -5
  145. package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js +2 -21
  146. package/dist/lock/implementations/adapters/kysely-lock-adapter/kysely-lock-adapter.js.map +1 -1
  147. package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.d.ts +11 -16
  148. package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js +11 -20
  149. package/dist/lock/implementations/adapters/libsql-lock-adapter/libsql-lock-adapter.js.map +1 -1
  150. package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.d.ts +7 -26
  151. package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js +7 -35
  152. package/dist/lock/implementations/adapters/memory-lock-adapter/memory-lock-adapter.js.map +1 -1
  153. package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.d.ts +13 -20
  154. package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js +13 -37
  155. package/dist/lock/implementations/adapters/mongodb-lock-adapter/mongodb-lock-adapter.js.map +1 -1
  156. package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.d.ts +4 -22
  157. package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js +4 -31
  158. package/dist/lock/implementations/adapters/redis-lock-adapter/redis-lock-adapter.js.map +1 -1
  159. package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.d.ts +12 -17
  160. package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js +12 -21
  161. package/dist/lock/implementations/adapters/sqlite-lock-adapter/sqlite-lock-adapter.js.map +1 -1
  162. package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.d.ts +0 -2
  163. package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js +2 -8
  164. package/dist/lock/implementations/derivables/lock-provider/database-lock-adapter.js.map +1 -1
  165. package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.d.ts +9 -0
  166. package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js +17 -0
  167. package/dist/lock/implementations/derivables/lock-provider/is-database-lock-adapter.js.map +1 -0
  168. package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +115 -372
  169. package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +197 -455
  170. package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
  171. package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +15 -17
  172. package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js +42 -40
  173. package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
  174. package/dist/lock/implementations/derivables/lock-provider/lock-state.d.ts +3 -3
  175. package/dist/lock/implementations/derivables/lock-provider/lock-state.js.map +1 -1
  176. package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +30 -754
  177. package/dist/lock/implementations/derivables/lock-provider/lock.js +94 -812
  178. package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
  179. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +58 -161
  180. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js +116 -122
  181. package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
  182. package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.d.ts +4 -5
  183. package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js +172 -247
  184. package/dist/lock/implementations/test-utilities/database-lock-adapter.test-suite.js.map +1 -1
  185. package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.d.ts +5 -8
  186. package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js +93 -98
  187. package/dist/lock/implementations/test-utilities/lock-adapter.test-suite.js.map +1 -1
  188. package/dist/lock/implementations/test-utilities/lock-provider.test-suite.d.ts +15 -20
  189. package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +133 -137
  190. package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
  191. package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js +1 -2
  192. package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js.map +1 -1
  193. package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.d.ts +2 -2
  194. package/dist/serde/implementations/adapters/no-op-serde-adapter/no-op-serde-adapter.js +1 -1
  195. package/dist/serde/implementations/adapters/redis-serde/redis-serde.js +1 -2
  196. package/dist/serde/implementations/adapters/redis-serde/redis-serde.js.map +1 -1
  197. package/dist/serde/implementations/adapters/sql-serde/sql-serde.js +1 -2
  198. package/dist/serde/implementations/adapters/sql-serde/sql-serde.js.map +1 -1
  199. package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.d.ts +2 -2
  200. package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js +2 -2
  201. package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js.map +1 -1
  202. package/dist/serde/implementations/derivables/serde.d.ts +14 -14
  203. package/dist/serde/implementations/derivables/serde.js +14 -15
  204. package/dist/serde/implementations/derivables/serde.js.map +1 -1
  205. package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.d.ts +4 -4
  206. package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.js +3 -3
  207. package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.d.ts +5 -5
  208. package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.js +4 -4
  209. package/dist/utilities/_module-exports.d.ts +1 -2
  210. package/dist/utilities/_module-exports.js +1 -2
  211. package/dist/utilities/_module-exports.js.map +1 -1
  212. package/dist/utilities/classes/_module.d.ts +4 -0
  213. package/dist/utilities/classes/_module.js +5 -0
  214. package/dist/utilities/classes/_module.js.map +1 -0
  215. package/dist/utilities/classes/key-prefixer/_module.d.ts +2 -0
  216. package/dist/utilities/classes/key-prefixer/_module.js +3 -0
  217. package/dist/utilities/classes/key-prefixer/_module.js.map +1 -0
  218. package/dist/utilities/classes/key-prefixer/key-prefixer.contract.d.ts +28 -0
  219. package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js +5 -0
  220. package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js.map +1 -0
  221. package/dist/utilities/classes/key-prefixer/key-prefixer.d.ts +73 -0
  222. package/dist/utilities/classes/key-prefixer/key-prefixer.js +135 -0
  223. package/dist/utilities/classes/key-prefixer/key-prefixer.js.map +1 -0
  224. package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.d.ts +1 -0
  225. package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.js +2 -0
  226. package/dist/utilities/classes/kysely-table-name-transformer-plugin/_module.js.map +1 -0
  227. package/dist/utilities/classes/kysely-table-name-transformer-plugin/kysely-table-name-transformer-plugin.js.map +1 -0
  228. package/dist/utilities/classes/pipeline/_module.d.ts +1 -0
  229. package/dist/utilities/classes/pipeline/_module.js +2 -0
  230. package/dist/utilities/classes/pipeline/_module.js.map +1 -0
  231. package/dist/utilities/{pipeline → classes/pipeline}/pipeline.d.ts +3 -2
  232. package/dist/utilities/{pipeline → classes/pipeline}/pipeline.js +3 -2
  233. package/dist/utilities/classes/pipeline/pipeline.js.map +1 -0
  234. package/dist/utilities/classes/time-span/_module.d.ts +1 -0
  235. package/dist/utilities/classes/time-span/_module.js +2 -0
  236. package/dist/utilities/classes/time-span/_module.js.map +1 -0
  237. package/dist/utilities/{time-span → classes/time-span}/time-span.d.ts +2 -1
  238. package/dist/utilities/{time-span → classes/time-span}/time-span.js +1 -0
  239. package/dist/utilities/classes/time-span/time-span.js.map +1 -0
  240. package/dist/utilities/contracts/_module.d.ts +2 -1
  241. package/dist/utilities/contracts/_module.js +2 -1
  242. package/dist/utilities/contracts/_module.js.map +1 -1
  243. package/dist/utilities/contracts/prunable.contract.d.ts +11 -0
  244. package/dist/utilities/contracts/prunable.contract.js +5 -0
  245. package/dist/utilities/contracts/prunable.contract.js.map +1 -0
  246. package/dist/utilities/functions.d.ts +30 -2
  247. package/dist/utilities/functions.js +66 -4
  248. package/dist/utilities/functions.js.map +1 -1
  249. package/dist/utilities/types.d.ts +47 -14
  250. package/package.json +14 -13
  251. package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/_module.js +0 -2
  252. package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/_module.js.map +0 -1
  253. package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.d.ts +0 -61
  254. package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js +0 -318
  255. package/dist/cache/implementations/adapters/kysely-sqlite-cache-adapter/kysely-sqlite-cache-adapter.js.map +0 -1
  256. package/dist/cache/implementations/adapters/no-op-cache-adapter/_module.d.ts +0 -1
  257. package/dist/cache/implementations/adapters/no-op-cache-adapter/_module.js +0 -2
  258. package/dist/cache/implementations/adapters/no-op-cache-adapter/_module.js.map +0 -1
  259. package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.d.ts +0 -22
  260. package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js +0 -39
  261. package/dist/cache/implementations/adapters/no-op-cache-adapter/no-op-cache-adapter.js.map +0 -1
  262. package/dist/utilities/kysely/_module.d.ts +0 -1
  263. package/dist/utilities/kysely/_module.js +0 -2
  264. package/dist/utilities/kysely/_module.js.map +0 -1
  265. package/dist/utilities/kysely/kysely-table-name-transformer-plugin.js.map +0 -1
  266. package/dist/utilities/pipeline/_module.d.ts +0 -1
  267. package/dist/utilities/pipeline/_module.js +0 -2
  268. package/dist/utilities/pipeline/_module.js.map +0 -1
  269. package/dist/utilities/pipeline/pipeline.js.map +0 -1
  270. package/dist/utilities/time-span/_module.d.ts +0 -1
  271. package/dist/utilities/time-span/_module.js +0 -2
  272. package/dist/utilities/time-span/_module.js.map +0 -1
  273. package/dist/utilities/time-span/time-span.js.map +0 -1
  274. /package/dist/utilities/{kysely → classes/kysely-table-name-transformer-plugin}/kysely-table-name-transformer-plugin.d.ts +0 -0
  275. /package/dist/utilities/{kysely → classes/kysely-table-name-transformer-plugin}/kysely-table-name-transformer-plugin.js +0 -0
@@ -2,349 +2,210 @@
2
2
  * @module Cache
3
3
  */
4
4
  import { KeyFoundCacheEvent, KeyNotFoundCacheEvent, KeyAddedCacheEvent, KeyUpdatedCacheEvent, KeyRemovedCacheEvent, KeyIncrementedCacheEvent, KeyDecrementedCacheEvent, KeysClearedCacheEvent, UnexpectedCacheErrorEvent, } from "../../../../cache/contracts/_module-exports.js";
5
- import { KeyNotFoundCacheError, TypeCacheError, UnexpectedCacheError, } from "../../../../cache/contracts/_module-exports.js";
5
+ import { KeyNotFoundCacheError, TypeCacheError, } from "../../../../cache/contracts/_module-exports.js";
6
6
  import {} from "../../../../cache/contracts/_module-exports.js";
7
- import { isObjectEmpty, resolveAsyncLazyable, resolveOneOrMoreStr, } from "../../../../utilities/_module-exports.js";
7
+ import { isFactory, resolveAsyncLazyable, resolveFactoryable, } from "../../../../utilities/_module-exports.js";
8
+ import {} from "../../../../utilities/_module-exports.js";
9
+ import { KeyPrefixer, } from "../../../../utilities/_module-exports.js";
8
10
  import { LazyPromise } from "../../../../async/_module-exports.js";
9
11
  import { EventBus } from "../../../../event-bus/implementations/derivables/_module-exports.js";
10
12
  import { MemoryEventBusAdapter } from "../../../../event-bus/implementations/adapters/_module-exports.js";
13
+ import { isDatabaseCacheAdapter } from "../../../../cache/implementations/derivables/cache/is-database-cache-adapter.js";
14
+ import { DatabaseCacheAdapter } from "../../../../cache/implementations/derivables/cache/database-cache-adapter.js";
11
15
  /**
12
- * <i>Cache</i> class can be derived from any <i>{@link ICacheAdapter}</i>.
13
16
  *
14
- * IMPORT_PATH: ```"@daiso-tech/core/cache/implementations/derivables"```
17
+ * IMPORT_PATH: ```"@daiso-tech/core/cache"```
15
18
  * @group Derivables
16
19
  */
17
20
  export class Cache {
21
+ static resolveCacheAdapter(adapter) {
22
+ if (isDatabaseCacheAdapter(adapter)) {
23
+ return new DatabaseCacheAdapter(adapter);
24
+ }
25
+ return adapter;
26
+ }
27
+ static async resolveCacheAdapterFactoryable(factoryable, rootPrefix) {
28
+ const adapter = await resolveFactoryable(factoryable, rootPrefix);
29
+ return Cache.resolveCacheAdapter(adapter);
30
+ }
18
31
  static defaultRetryPolicy = (error) => {
19
32
  return !(error instanceof TypeCacheError ||
20
33
  error instanceof KeyNotFoundCacheError);
21
34
  };
22
35
  groupdEventBus;
23
36
  eventBus;
24
- adapter;
37
+ adapterFactoryable;
38
+ adapterPromise;
25
39
  defaultTtl;
26
40
  retryAttempts;
27
41
  backoffPolicy;
28
42
  retryPolicy;
29
43
  timeout;
44
+ keyPrefixer;
30
45
  /**
31
- * @example
32
- * ```ts
33
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
34
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
35
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
36
- *
37
- * const cache: IGroupableCache = new Cache({
38
- * adapter: new MemoryCacheAdapter({
39
- * rootGroup: "@global"
40
- * }),
41
- * });
42
- * ```
43
- */
44
- constructor(settings) {
45
- const { adapter, eventBus: groupdEventBus = new EventBus({
46
- adapter: new MemoryEventBusAdapter({
47
- rootGroup: "@global",
48
- }),
49
- }), defaultTtl = null, retryAttempts = null, backoffPolicy = null, retryPolicy = Cache.defaultRetryPolicy, timeout = null, } = settings;
50
- this.groupdEventBus = groupdEventBus;
51
- this.eventBus = groupdEventBus.withGroup(adapter.getGroup());
52
- this.adapter = adapter;
53
- this.defaultTtl = defaultTtl;
54
- this.retryAttempts = retryAttempts;
55
- this.backoffPolicy = backoffPolicy;
56
- this.retryPolicy = retryPolicy;
57
- this.timeout = timeout;
58
- }
59
- createLayPromise(asyncFn) {
60
- return new LazyPromise(asyncFn)
61
- .setRetryAttempts(this.retryAttempts)
62
- .setBackoffPolicy(this.backoffPolicy)
63
- .setRetryPolicy(this.retryPolicy)
64
- .setTimeout(this.timeout);
65
- }
66
- /**
67
- * You can listen to different events of <i>Cache</i> class instance.
68
46
  *
69
- * Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
70
- * Refer to <i>{@link IEventListenable}</i> for details on how the method works.
71
47
  * @example
72
48
  * ```ts
73
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
74
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
75
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
76
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
77
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
78
- * import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
79
- *
80
- * const cache: IGroupableCache = new Cache({
81
- * adapter: new MemoryCacheAdapter({
82
- * rootGroup: "@global"
83
- * }),
49
+ * import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
50
+ * import { Serde } from "@daiso-tech/core/serde";
51
+ * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
52
+ * import Sqlite from "better-sqlite3";
53
+ * import { Cache } from "@daiso-tech/core/cache";
54
+ * import { KeyPrefixer } from "@daiso-tech/core/utilities";
55
+ *
56
+ * const database = new Sqlite("local.db");
57
+ * const serde = new Serde(new SuperJsonSerdeAdapter());
58
+ * const cacheAdapter = new SqliteCacheAdapter({
59
+ * database,
60
+ * serde,
84
61
  * });
62
+ * // You need initialize the adapter once before using it.
63
+ * await cacheAdapter.init();
85
64
  *
86
- * const listener: Invokable<CacheEvents> = event => {
87
- * console.log(event);
88
- * };
89
- * await cache.addListener(KeyAddedCacheEvent, listener);
90
- * await cache.add("a", 1);
91
- * ```
92
- */
93
- addListener(eventName, listener) {
94
- return this.eventBus.addListener(eventName, listener);
95
- }
96
- /**
97
- * You can listen to different events of <i>Cache</i> class instance.
98
- *
99
- * Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
100
- * Refer to <i>{@link IEventListenable}</i> for details on how the method works.
101
- * @example
102
- * ```ts
103
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
104
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
105
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
106
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
107
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
108
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
109
- * import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
110
- *
111
- * const cache: IGroupableCache = new Cache({
112
- * adapter: new MemoryCacheAdapter({
113
- * rootGroup: "@global"
114
- * }),
65
+ * const cache = new Cache({
66
+ * keyPrefixer: new KeyPrefixer("cache"),
67
+ * adapter: cacheAdapter,
115
68
  * });
116
- *
117
- * const listener: Invokable<CacheEvents> = event => {
118
- * console.log(event);
119
- * };
120
- * await cache.addListenerMany([KeyAddedCacheEvent], listener);
121
- * await cache.add("a", 1);
122
69
  * ```
123
- */
124
- addListenerMany(eventNames, listener) {
125
- return this.eventBus.addListenerMany(eventNames, listener);
126
- }
127
- /**
128
- * You can listen to different events of <i>Cache</i> class instance.
129
70
  *
130
- * Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
131
- * Refer to <i>{@link IEventListenable}</i> for details on how the method works.
71
+ * You can pass factory function that will create an adapter for every group.
132
72
  * @example
133
73
  * ```ts
134
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
135
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
136
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
137
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
138
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
139
- * import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
140
- *
141
- * const cache: IGroupableCache = new Cache({
142
- * adapter: new MemoryCacheAdapter({
143
- * rootGroup: "@global"
144
- * }),
74
+ * import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
75
+ * import type { ICacheAdapter } from "@daiso-tech/core/cache/contracts";
76
+ * import { Serde } from "@daiso-tech/core/serde";
77
+ * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
78
+ * import Sqlite from "better-sqlite3";
79
+ * import { Cache } from "@daiso-tech/core/cache";
80
+ * import { KeyPrefixer, type Promiseable } from "@daiso-tech/core/utilities";
81
+ *
82
+ *
83
+ * async function cahceAdapterFactory(prefix: string): Promiseable<ICacheAdapter> {
84
+ * const database = new Sqlite("local.db");
85
+ * const serde = new Serde(new SuperJsonSerdeAdapter());
86
+ * const cacheAdapter = new SqliteCacheAdapter({
87
+ * database,
88
+ * serde,
89
+ * tableName: `cache_${prefix}`
90
+ * });
91
+ * await cacheAdapter.init();
92
+ * return cacheAdapter;
93
+ * }
94
+ *
95
+ * const cache = new Cache({
96
+ * keyPrefixer: new KeyPrefixer("cache"),
97
+ * adapter: cahceAdapterFactory,
145
98
  * });
146
- *
147
- * const listener: Invokable<CacheEvents> = event => {
148
- * console.log(event);
149
- * };
150
- * await cache.addListener(KeyAddedCacheEvent, listener);
151
- * await cache.removeListener(KeyAddedCacheEvent, listener);
152
- * await cache.add("a", 1);
153
99
  * ```
154
- */
155
- removeListener(eventName, listener) {
156
- return this.eventBus.removeListener(eventName, listener);
157
- }
158
- /**
159
- * You can listen to different events of <i>Cache</i> class instance.
160
100
  *
161
- * Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
162
- * Refer to <i>{@link IEventListenable}</i> for details on how the method works.
101
+ * You can also pass factory object that implements <i>{@link IFactoryObject}</i> contract. This useful for depedency injection libraries.
163
102
  * @example
164
103
  * ```ts
165
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
166
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
167
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
168
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
169
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
170
- * import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
171
- *
172
- * const cache: IGroupableCache = new Cache({
173
- * adapter: new MemoryCacheAdapter({
174
- * rootGroup: "@global"
175
- * }),
104
+ * import { SqliteCacheAdapter } from "@daiso-tech/core/cache/adapters";
105
+ * import type { ICacheAdapter } from "@daiso-tech/core/cache/contracts";
106
+ * import { Serde } from "@daiso-tech/core/serde";
107
+ * import { SuperJsonSerdeAdapter } from "@daiso-tech/core/serde/adapters"
108
+ * import Sqlite from "better-sqlite3";
109
+ * import { Cache } from "@daiso-tech/core/cache";
110
+ * import { KeyPrefixer, type IFactoryObject, type Promiseable } from "@daiso-tech/core/utilities";
111
+ *
112
+ * class CahceAdapterFactory implements IFactoryObject<string, ICacheAdapter> {
113
+ * async use(prefix: string): Promiseable<ICacheAdapter> {
114
+ * const database = new Sqlite("local.db");
115
+ * const serde = new Serde(new SuperJsonSerdeAdapter());
116
+ * const cacheAdapter = new SqliteCacheAdapter({
117
+ * database,
118
+ * serde,
119
+ * tableName: `cache_${prefix}`
120
+ * });
121
+ * await cacheAdapter.init();
122
+ * return cacheAdapter;
123
+ * }
124
+ * }
125
+ * const cahceAdapterFactory = new CahceAdapterFactory();
126
+ * const cache = new Cache({
127
+ * keyPrefixer: new KeyPrefixer("cache"),
128
+ * adapter: cahceAdapterFactory,
176
129
  * });
177
- *
178
- * const listener: Invokable<CacheEvents> = event => {
179
- * console.log(event);
180
- * };
181
- * await cache.addListenerMany([KeyAddedCacheEvent], listener);
182
- * await cache.removeListenerMany(KeyAddedCacheEvent, listener);
183
- * await cache.add("a", 1);
184
130
  * ```
185
131
  */
186
- removeListenerMany(eventNames, listener) {
187
- return this.eventBus.removeListenerMany(eventNames, listener);
132
+ constructor(settings) {
133
+ const { keyPrefixer, adapter, eventBus: groupdEventBus = new EventBus({
134
+ keyPrefixer: new KeyPrefixer("event-bus"),
135
+ adapter: new MemoryEventBusAdapter(),
136
+ }), defaultTtl = null, retryAttempts = null, backoffPolicy = null, retryPolicy = Cache.defaultRetryPolicy, timeout = null, } = settings;
137
+ this.keyPrefixer = keyPrefixer;
138
+ this.groupdEventBus = groupdEventBus;
139
+ this.adapterFactoryable = adapter;
140
+ this.defaultTtl = defaultTtl;
141
+ this.retryAttempts = retryAttempts;
142
+ this.backoffPolicy = backoffPolicy;
143
+ this.retryPolicy = retryPolicy;
144
+ this.timeout = timeout;
145
+ this.eventBus = this.groupdEventBus.withGroup(this.keyPrefixer.resolvedRootPrefix);
146
+ if (this.keyPrefixer.resolvedGroup) {
147
+ this.eventBus = this.groupdEventBus.withGroup([
148
+ this.keyPrefixer.resolvedRootPrefix,
149
+ this.keyPrefixer.resolvedGroup,
150
+ ]);
151
+ }
152
+ this.adapterPromise = new LazyPromise(() => Cache.resolveCacheAdapterFactoryable(this.adapterFactoryable, this.keyPrefixer.keyPrefix));
188
153
  }
189
- /**
190
- * You can listen to different events of <i>Cache</i> class instance.
191
- *
192
- * Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
193
- * Refer to <i>{@link IEventListenable}</i> for details on how the method works.
194
- * @example
195
- * ```ts
196
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
197
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
198
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
199
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
200
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
201
- * import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
202
- *
203
- * const cache: IGroupableCache = new Cache({
204
- * adapter: new MemoryCacheAdapter({
205
- * rootGroup: "@global"
206
- * }),
207
- * });
208
- *
209
- * const listener: Invokable<CacheEvents> = event => {
210
- * console.log(event);
211
- * };
212
- * await cache.listenOnce(KeyAddedCacheEvent, listener);
213
- * await cache.add("a", 1);
214
- * ```
215
- */
216
- listenOnce(eventName, listener) {
217
- return this.eventBus.listenOnce(eventName, listener);
154
+ addListener(event, listener) {
155
+ return this.eventBus.addListener(event, listener);
156
+ }
157
+ addListenerMany(events, listener) {
158
+ return this.eventBus.addListenerMany(events, listener);
159
+ }
160
+ removeListener(event, listener) {
161
+ return this.eventBus.removeListener(event, listener);
162
+ }
163
+ removeListenerMany(events, listener) {
164
+ return this.eventBus.removeListenerMany(events, listener);
165
+ }
166
+ listenOnce(event, listener) {
167
+ return this.eventBus.listenOnce(event, listener);
218
168
  }
219
169
  asPromise(event) {
220
170
  return this.eventBus.asPromise(event);
221
171
  }
222
- /**
223
- * You can listen to different events of <i>Cache</i> class instance.
224
- *
225
- * Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
226
- * Refer to <i>{@link IEventListenable}</i> for details on how the method works.
227
- * @example
228
- * ```ts
229
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
230
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
231
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
232
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
233
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
234
- * import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
235
- *
236
- * const cache: IGroupableCache = new Cache({
237
- * adapter: new MemoryCacheAdapter({
238
- * rootGroup: "@global"
239
- * }),
240
- * });
241
- *
242
- * const listener: Invokable<CacheEvents> = event => {
243
- * console.log(event);
244
- * };
245
- * const unsubscribe = await cache.subscribe(KeyAddedCacheEvent, listener);
246
- * await cache.add("a", 1);
247
- * await unsubscribe();
248
- * ```
249
- */
250
- subscribe(eventName, listener) {
251
- return this.eventBus.subscribe(eventName, listener);
172
+ subscribe(event, listener) {
173
+ return this.eventBus.subscribe(event, listener);
252
174
  }
253
- /**
254
- * You can listen to different events of <i>Cache</i> class instance.
255
- *
256
- * Refer to <i>{@link CacheEvents}</i>, to se all events dispatched by <i>Cache</i> class instance.
257
- * Refer to <i>{@link IEventListenable}</i> for details on how the method works.
258
- * @example
259
- * ```ts
260
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
261
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
262
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
263
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
264
- * import type { Invokable} from "@daiso-tech/core/event-bus/contracts";
265
- * import { KeyAddedCacheEvent, type CacheEvents } from "@daiso-tech/core/cache/contracts";
266
- *
267
- * const cache: IGroupableCache = new Cache({
268
- * adapter: new MemoryCacheAdapter({
269
- * rootGroup: "@global"
270
- * }),
271
- * });
272
- *
273
- * const listener: Invokable<CacheEvents> = event => {
274
- * console.log(event);
275
- * };
276
- * const unsubscribe = await cache.subscribeMany([KeyAddedCacheEvent], listener);
277
- * await cache.add("a", 1);
278
- * await unsubscribe();
279
- * ```
280
- */
281
- subscribeMany(eventNames, listener) {
282
- return this.eventBus.subscribeMany(eventNames, listener);
175
+ subscribeMany(events, listener) {
176
+ return this.eventBus.subscribeMany(events, listener);
283
177
  }
284
- /**
285
- * @example
286
- * ```ts
287
- * import type { IGroupableCache, ICache } from "@daiso-tech/core/cache/contracts";
288
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
289
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
290
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
291
- *
292
- * const cache: IGroupableCache = new Cache({
293
- * adapter: new MemoryCacheAdapter({
294
- * rootGroup: "@global"
295
- * }),
296
- * });
297
- *
298
- * // Will print "@global"
299
- * console.log(cache.getGroup());
300
- *
301
- * const groupedCache: ICache = cache.withGroup("company-1");
302
- *
303
- * // Will print "@global/company-1"
304
- * console.log(groupedCache.getGroup());
305
- * ```
306
- */
307
- withGroup(group) {
308
- return new Cache({
309
- adapter: this.adapter.withGroup(resolveOneOrMoreStr(group)),
310
- defaultTtl: this.defaultTtl,
311
- eventBus: this.groupdEventBus,
178
+ createLazyPromise(asyncFn) {
179
+ return new LazyPromise(asyncFn, {
312
180
  retryAttempts: this.retryAttempts,
313
181
  backoffPolicy: this.backoffPolicy,
314
182
  retryPolicy: this.retryPolicy,
315
183
  timeout: this.timeout,
316
184
  });
317
185
  }
318
- /**
319
- * @example
320
- * ```ts
321
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
322
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
323
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
324
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
325
- *
326
- * const cache: IGroupableCache = new Cache({
327
- * adapter: new MemoryCacheAdapter({
328
- * rootGroup: "@global"
329
- * }),
330
- * });
331
- *
332
- * // Will print "@global"
333
- * console.log(cache.getGroup());
334
- * ```
335
- */
336
- getGroup() {
337
- return this.adapter.getGroup();
186
+ exists(key) {
187
+ return new LazyPromise(async () => {
188
+ const value = await this.get(key);
189
+ return value !== null;
190
+ });
191
+ }
192
+ missing(key) {
193
+ return this.createLazyPromise(async () => {
194
+ const hasKey = await this.exists(key);
195
+ return !hasKey;
196
+ });
338
197
  }
339
198
  get(key) {
340
- return this.createLayPromise(async () => {
199
+ return this.createLazyPromise(async () => {
200
+ const keyObj = this.keyPrefixer.create(key);
341
201
  try {
342
- const value = await this.adapter.get(key);
202
+ const adapter = await this.adapterPromise;
203
+ const value = await adapter.get(keyObj.prefixed);
343
204
  if (value === null) {
344
205
  this.eventBus
345
206
  .dispatch(new KeyNotFoundCacheEvent({
346
207
  group: this.getGroup(),
347
- key,
208
+ key: keyObj.resolved,
348
209
  }))
349
210
  .defer();
350
211
  }
@@ -352,7 +213,7 @@ export class Cache {
352
213
  this.eventBus
353
214
  .dispatch(new KeyFoundCacheEvent({
354
215
  group: this.getGroup(),
355
- key,
216
+ key: keyObj.resolved,
356
217
  value,
357
218
  }))
358
219
  .defer();
@@ -363,7 +224,7 @@ export class Cache {
363
224
  this.eventBus
364
225
  .dispatch(new UnexpectedCacheErrorEvent({
365
226
  group: this.getGroup(),
366
- key,
227
+ keys: [keyObj.resolved],
367
228
  method: this.get.name,
368
229
  error,
369
230
  }))
@@ -372,33 +233,52 @@ export class Cache {
372
233
  }
373
234
  });
374
235
  }
375
- add(key, value,
376
- /**
377
- * @default null
378
- */
379
- ttl = this.defaultTtl) {
380
- return this.createLayPromise(async () => {
236
+ getOrFail(key) {
237
+ return this.createLazyPromise(async () => {
238
+ const value = await this.get(key);
239
+ if (value === null) {
240
+ throw new KeyNotFoundCacheError(`Key "${this.keyPrefixer.create(key).resolved}" is not found`);
241
+ }
242
+ return value;
243
+ });
244
+ }
245
+ getAndRemove(key) {
246
+ return this.createLazyPromise(async () => {
247
+ const keyObj = this.keyPrefixer.create(key);
381
248
  try {
382
- const hasAdded = await this.adapter.add(key, value, ttl);
383
- if (hasAdded) {
249
+ const adapter = await this.adapterPromise;
250
+ const value = await adapter.getAndRemove(keyObj.prefixed);
251
+ if (value === null) {
384
252
  this.eventBus
385
- .dispatch(new KeyAddedCacheEvent({
253
+ .dispatch(new KeyNotFoundCacheEvent({
254
+ group: this.getGroup(),
255
+ key: keyObj.resolved,
256
+ }))
257
+ .defer();
258
+ }
259
+ else {
260
+ this.eventBus
261
+ .dispatch(new KeyFoundCacheEvent({
386
262
  group: this.getGroup(),
387
- key,
263
+ key: keyObj.resolved,
388
264
  value,
389
- ttl,
265
+ }))
266
+ .defer();
267
+ this.eventBus
268
+ .dispatch(new KeyRemovedCacheEvent({
269
+ group: this.getGroup(),
270
+ key: keyObj.resolved,
390
271
  }))
391
272
  .defer();
392
273
  }
393
- return hasAdded;
274
+ return value;
394
275
  }
395
276
  catch (error) {
396
277
  this.eventBus
397
278
  .dispatch(new UnexpectedCacheErrorEvent({
398
279
  group: this.getGroup(),
399
- key,
400
- value,
401
- method: this.add.name,
280
+ keys: [keyObj.resolved],
281
+ method: this.get.name,
402
282
  error,
403
283
  }))
404
284
  .defer();
@@ -406,36 +286,52 @@ export class Cache {
406
286
  }
407
287
  });
408
288
  }
409
- update(key, value) {
410
- return this.createLayPromise(async () => {
289
+ getOr(key, defaultValue) {
290
+ return this.createLazyPromise(async () => {
291
+ const value = await this.get(key);
292
+ if (value === null) {
293
+ const simplifiedValueToAdd = await resolveAsyncLazyable(defaultValue);
294
+ return simplifiedValueToAdd;
295
+ }
296
+ return value;
297
+ });
298
+ }
299
+ getOrAdd(key, valueToAdd, ttl) {
300
+ return this.createLazyPromise(async () => {
301
+ const value = await this.get(key);
302
+ if (value === null) {
303
+ const simplifiedValueToAdd = await resolveAsyncLazyable(valueToAdd);
304
+ await this.add(key, simplifiedValueToAdd, ttl);
305
+ return simplifiedValueToAdd;
306
+ }
307
+ return value;
308
+ });
309
+ }
310
+ add(key, value, ttl = this.defaultTtl) {
311
+ return this.createLazyPromise(async () => {
312
+ const keyObj = this.keyPrefixer.create(key);
411
313
  try {
412
- const hasUpdated = await this.adapter.update(key, value);
413
- if (hasUpdated) {
314
+ const adapter = await this.adapterPromise;
315
+ const hasAdded = await adapter.add(keyObj.prefixed, value, ttl);
316
+ if (hasAdded) {
414
317
  this.eventBus
415
- .dispatch(new KeyUpdatedCacheEvent({
318
+ .dispatch(new KeyAddedCacheEvent({
416
319
  group: this.getGroup(),
417
- key,
320
+ key: keyObj.resolved,
418
321
  value,
322
+ ttl,
419
323
  }))
420
324
  .defer();
421
325
  }
422
- else {
423
- this.eventBus
424
- .dispatch(new KeyNotFoundCacheEvent({
425
- group: this.getGroup(),
426
- key,
427
- }))
428
- .defer();
429
- }
430
- return hasUpdated;
326
+ return hasAdded;
431
327
  }
432
328
  catch (error) {
433
329
  this.eventBus
434
330
  .dispatch(new UnexpectedCacheErrorEvent({
435
331
  group: this.getGroup(),
436
- key,
332
+ keys: [keyObj.resolved],
437
333
  value,
438
- method: this.update.name,
334
+ method: this.add.name,
439
335
  error,
440
336
  }))
441
337
  .defer();
@@ -443,19 +339,17 @@ export class Cache {
443
339
  }
444
340
  });
445
341
  }
446
- put(key, value,
447
- /**
448
- * @default null
449
- */
450
- ttl = this.defaultTtl) {
451
- return this.createLayPromise(async () => {
342
+ put(key, value, ttl = this.defaultTtl) {
343
+ return this.createLazyPromise(async () => {
344
+ const keyObj = this.keyPrefixer.create(key);
452
345
  try {
453
- const hasUpdated = await this.adapter.put(key, value, ttl);
346
+ const adapter = await this.adapterPromise;
347
+ const hasUpdated = await adapter.put(keyObj.prefixed, value, ttl);
454
348
  if (hasUpdated) {
455
349
  this.eventBus
456
350
  .dispatch(new KeyUpdatedCacheEvent({
457
351
  group: this.getGroup(),
458
- key,
352
+ key: keyObj.resolved,
459
353
  value,
460
354
  }))
461
355
  .defer();
@@ -464,7 +358,7 @@ export class Cache {
464
358
  this.eventBus
465
359
  .dispatch(new KeyAddedCacheEvent({
466
360
  group: this.getGroup(),
467
- key,
361
+ key: keyObj.resolved,
468
362
  value,
469
363
  ttl,
470
364
  }))
@@ -476,7 +370,7 @@ export class Cache {
476
370
  this.eventBus
477
371
  .dispatch(new UnexpectedCacheErrorEvent({
478
372
  group: this.getGroup(),
479
- key,
373
+ keys: [keyObj.resolved],
480
374
  value,
481
375
  method: this.put.name,
482
376
  error,
@@ -486,15 +380,18 @@ export class Cache {
486
380
  }
487
381
  });
488
382
  }
489
- remove(key) {
490
- return this.createLayPromise(async () => {
383
+ update(key, value) {
384
+ return this.createLazyPromise(async () => {
385
+ const keyObj = this.keyPrefixer.create(key);
491
386
  try {
492
- const hasRemoved = await this.adapter.remove(key);
493
- if (hasRemoved) {
387
+ const adapter = await this.adapterPromise;
388
+ const hasUpdated = await adapter.update(keyObj.prefixed, value);
389
+ if (hasUpdated) {
494
390
  this.eventBus
495
- .dispatch(new KeyRemovedCacheEvent({
391
+ .dispatch(new KeyUpdatedCacheEvent({
496
392
  group: this.getGroup(),
497
- key,
393
+ key: keyObj.resolved,
394
+ value,
498
395
  }))
499
396
  .defer();
500
397
  }
@@ -502,18 +399,19 @@ export class Cache {
502
399
  this.eventBus
503
400
  .dispatch(new KeyNotFoundCacheEvent({
504
401
  group: this.getGroup(),
505
- key,
402
+ key: keyObj.resolved,
506
403
  }))
507
404
  .defer();
508
405
  }
509
- return hasRemoved;
406
+ return hasUpdated;
510
407
  }
511
408
  catch (error) {
512
409
  this.eventBus
513
410
  .dispatch(new UnexpectedCacheErrorEvent({
514
411
  group: this.getGroup(),
515
- key,
516
- method: this.remove.name,
412
+ keys: [keyObj.resolved],
413
+ value,
414
+ method: this.update.name,
517
415
  error,
518
416
  }))
519
417
  .defer();
@@ -521,15 +419,17 @@ export class Cache {
521
419
  }
522
420
  });
523
421
  }
524
- increment(key, value = 1) {
525
- return this.createLayPromise(async () => {
422
+ increment(key, value = 0) {
423
+ return this.createLazyPromise(async () => {
424
+ const keyObj = this.keyPrefixer.create(key);
526
425
  try {
527
- const hasUpdated = await this.adapter.increment(key, value);
426
+ const adapter = await this.adapterPromise;
427
+ const hasUpdated = await adapter.increment(keyObj.prefixed, value);
528
428
  if (hasUpdated && value > 0) {
529
429
  this.eventBus
530
430
  .dispatch(new KeyIncrementedCacheEvent({
531
431
  group: this.getGroup(),
532
- key,
432
+ key: keyObj.resolved,
533
433
  value,
534
434
  }))
535
435
  .defer();
@@ -538,7 +438,7 @@ export class Cache {
538
438
  this.eventBus
539
439
  .dispatch(new KeyDecrementedCacheEvent({
540
440
  group: this.getGroup(),
541
- key,
441
+ key: keyObj.resolved,
542
442
  value: -value,
543
443
  }))
544
444
  .defer();
@@ -547,7 +447,7 @@ export class Cache {
547
447
  this.eventBus
548
448
  .dispatch(new KeyNotFoundCacheEvent({
549
449
  group: this.getGroup(),
550
- key,
450
+ key: keyObj.resolved,
551
451
  }))
552
452
  .defer();
553
453
  }
@@ -557,7 +457,7 @@ export class Cache {
557
457
  this.eventBus
558
458
  .dispatch(new UnexpectedCacheErrorEvent({
559
459
  group: this.getGroup(),
560
- key,
460
+ keys: [keyObj.resolved],
561
461
  value,
562
462
  method: this.increment.name,
563
463
  error,
@@ -567,21 +467,41 @@ export class Cache {
567
467
  }
568
468
  });
569
469
  }
570
- clear() {
571
- return this.createLayPromise(async () => {
470
+ decrement(key, value = 0) {
471
+ return this.createLazyPromise(async () => {
472
+ return await this.increment(key, -value);
473
+ });
474
+ }
475
+ remove(key) {
476
+ return this.createLazyPromise(async () => {
477
+ const keyObj = this.keyPrefixer.create(key);
572
478
  try {
573
- await this.adapter.clear();
574
- this.eventBus
575
- .dispatch(new KeysClearedCacheEvent({
576
- group: this.getGroup(),
577
- }))
578
- .defer();
479
+ const adapter = await this.adapterPromise;
480
+ const hasRemoved = await adapter.removeMany([keyObj.prefixed]);
481
+ if (hasRemoved) {
482
+ this.eventBus
483
+ .dispatch(new KeyRemovedCacheEvent({
484
+ group: this.getGroup(),
485
+ key: keyObj.resolved,
486
+ }))
487
+ .defer();
488
+ }
489
+ else {
490
+ this.eventBus
491
+ .dispatch(new KeyNotFoundCacheEvent({
492
+ group: this.getGroup(),
493
+ key: keyObj.resolved,
494
+ }))
495
+ .defer();
496
+ }
497
+ return hasRemoved;
579
498
  }
580
499
  catch (error) {
581
500
  this.eventBus
582
501
  .dispatch(new UnexpectedCacheErrorEvent({
583
502
  group: this.getGroup(),
584
- method: this.clear.name,
503
+ keys: [keyObj.resolved],
504
+ method: this.remove.name,
585
505
  error,
586
506
  }))
587
507
  .defer();
@@ -589,521 +509,89 @@ export class Cache {
589
509
  }
590
510
  });
591
511
  }
592
- exists(key) {
593
- return this.createLayPromise(async () => {
594
- const value = await this.get(key);
595
- return value !== null;
596
- });
597
- }
598
- existsMany(keys) {
599
- return this.createLayPromise(async () => {
600
- if (keys.length === 0) {
601
- return {};
602
- }
603
- const valuePromises = [];
604
- for (const key of keys) {
605
- valuePromises.push(this.exists(key));
606
- }
607
- const values = await Promise.all(valuePromises);
608
- const result = {};
609
- for (const [index, key] of keys.entries()) {
610
- const value = values[index];
611
- if (value === undefined) {
612
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
613
- }
614
- result[key] = value;
615
- }
616
- return result;
617
- });
618
- }
619
- missing(key) {
620
- return this.createLayPromise(async () => {
621
- const value = await this.get(key);
622
- return value === null;
623
- });
624
- }
625
- missingMany(keys) {
626
- return this.createLayPromise(async () => {
627
- if (keys.length === 0) {
628
- return {};
629
- }
630
- const valuePromises = [];
631
- for (const key of keys) {
632
- valuePromises.push(this.missing(key));
633
- }
634
- const values = await Promise.all(valuePromises);
635
- const result = {};
636
- for (const [index, key] of keys.entries()) {
637
- const value = values[index];
638
- if (value === undefined) {
639
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
640
- }
641
- result[key] = value;
642
- }
643
- return result;
644
- });
645
- }
646
- getMany(keys) {
647
- return this.createLayPromise(async () => {
648
- if (keys.length === 0) {
649
- return {};
650
- }
651
- const valuePromises = [];
652
- for (const key of keys) {
653
- valuePromises.push(this.get(key));
654
- }
655
- const values = await Promise.all(valuePromises);
656
- const result = {};
657
- for (const [index, key] of keys.entries()) {
658
- const value = values[index];
659
- if (value === undefined) {
660
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
661
- }
662
- result[key] = value;
663
- }
664
- return result;
665
- });
666
- }
667
- /**
668
- * @example
669
- * ```ts
670
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
671
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
672
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
673
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
674
- *
675
- * const cache: IGroupableCache = new Cache({
676
- * adapter: new MemoryCacheAdapter({
677
- * rootGroup: "@global"
678
- * }),
679
- * });
680
- *
681
- * const result = await cache.getOr("a", -1);
682
- * // -1
683
- * console.log(result);
684
- * ```
685
- *
686
- * You can pass function as default value.
687
- * @example
688
- * ```ts
689
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
690
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
691
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
692
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
693
- *
694
- * const cache: IGroupableCache = new Cache({
695
- * adapter: new MemoryCacheAdapter({
696
- * rootGroup: "@global"
697
- * }),
698
- * });
699
- *
700
- * const result = await cache.getOr("a", () => -1);
701
- * // -1
702
- * console.log(result);
703
- * ```
704
- *
705
- * You can pass async function as default value.
706
- * @example
707
- * ```ts
708
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
709
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
710
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
711
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
712
- *
713
- * const cache: IGroupableCache = new Cache({
714
- * adapter: new MemoryCacheAdapter({
715
- * rootGroup: "@global"
716
- * }),
717
- * });
718
- *
719
- * const result = await cache.getOr("a", async () => -1);
720
- * // -1
721
- * console.log(result);
722
- * ```
723
- *
724
- * You can pass <i>{@link LazyPromise}</i> as default value.
725
- * @example
726
- * ```ts
727
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
728
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
729
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
730
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
731
- *
732
- * const cache: IGroupableCache = new Cache({
733
- * adapter: new MemoryCacheAdapter({
734
- * rootGroup: "@global"
735
- * }),
736
- * });
737
- *
738
- * const result = await cache.getOr("a", new LazyPromise(async () => -1));
739
- * // -1
740
- * console.log(result);
741
- * ```
742
- */
743
- getOr(key, defaultValue) {
744
- return this.createLayPromise(async () => {
745
- const value = await this.get(key);
746
- if (value === null) {
747
- return await resolveAsyncLazyable(defaultValue);
748
- }
749
- return value;
750
- });
751
- }
752
- /**
753
- * @example
754
- * ```ts
755
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
756
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
757
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
758
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
759
- *
760
- * const cache: IGroupableCache = new Cache({
761
- * adapter: new MemoryCacheAdapter({
762
- * rootGroup: "@global"
763
- * }),
764
- * });
765
- *
766
- * const result = await cache.getOrMany({ a: -1 });
767
- * // { a: -1 }
768
- * console.log(result);
769
- * ```
770
- *
771
- * You can pass function as default value.
772
- * @example
773
- * ```ts
774
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
775
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
776
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
777
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
778
- *
779
- * const cache: IGroupableCache = new Cache({
780
- * adapter: new MemoryCacheAdapter({
781
- * rootGroup: "@global"
782
- * }),
783
- * });
784
- *
785
- * const result = await cache.getOrMany({ a: () => -1 });
786
- * // { a: -1 }
787
- * console.log(result);
788
- * ```
789
- *
790
- * You can pass async function as default value.
791
- * @example
792
- * ```ts
793
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
794
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
795
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
796
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
797
- *
798
- * const cache: IGroupableCache = new Cache({
799
- * adapter: new MemoryCacheAdapter({
800
- * rootGroup: "@global"
801
- * }),
802
- * });
803
- *
804
- * const result = await cache.getOrMany({ a: async () => -1 });
805
- * // { a: -1 }
806
- * console.log(result);
807
- * ```
808
- *
809
- * You can pass <i>{@link LazyPromise}</i> as default value.
810
- * @example
811
- * ```ts
812
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
813
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
814
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
815
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
816
- *
817
- * const cache: IGroupableCache = new Cache({
818
- * adapter: new MemoryCacheAdapter({
819
- * rootGroup: "@global"
820
- * }),
821
- * });
822
- *
823
- * const result = await cache.getOrMany({ a: new LazyPromise(async () => - 1) });
824
- * // { a: -1 }
825
- * console.log(result);
826
- * ```
827
- */
828
- getOrMany(keysWithDefaults) {
829
- return this.createLayPromise(async () => {
830
- const keys = Object.keys(keysWithDefaults);
512
+ removeMany(keys) {
513
+ return this.createLazyPromise(async () => {
831
514
  if (keys.length === 0) {
832
- return {};
833
- }
834
- const valuePromises = [];
835
- for (const key of keys) {
836
- const defaultValue = keysWithDefaults[key];
837
- valuePromises.push(this.getOr(key, defaultValue));
838
- }
839
- const values = await Promise.all(valuePromises);
840
- const result = {};
841
- for (const [index, key] of keys.entries()) {
842
- const value = values[index];
843
- if (value === undefined) {
844
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
845
- }
846
- result[key] = value;
847
- }
848
- return result;
849
- });
850
- }
851
- /**
852
- * @example
853
- * ```ts
854
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
855
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
856
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
857
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
858
- *
859
- * const cache: IGroupableCache = new Cache({
860
- * adapter: new MemoryCacheAdapter({
861
- * rootGroup: "@global"
862
- * }),
863
- * });
864
- *
865
- * await cache.add("a", 1);
866
- *
867
- * const result1 = await cache.getOrFail("a");
868
- * // Will print 1
869
- * console.log(result1);
870
- *
871
- * await cache.remove("a");
872
- *
873
- * // Will throw an error
874
- * await cache.getOrFail("a");
875
- * ```
876
- */
877
- getOrFail(key) {
878
- return this.createLayPromise(async () => {
879
- const value = await this.get(key);
880
- if (value === null) {
881
- throw new KeyNotFoundCacheError(`Key "${key}" is not found`);
515
+ return true;
882
516
  }
883
- return value;
884
- });
885
- }
886
- addMany(values) {
887
- return this.createLayPromise(async () => {
888
- if (isObjectEmpty(values)) {
889
- return {};
890
- }
891
- const valuePromises = [];
892
- for (const key in values) {
893
- const { value, ttl } = values[key];
894
- valuePromises.push(this.add(key, value, ttl));
895
- }
896
- const returnValues = await Promise.all(valuePromises);
897
- const result = {};
898
- for (const [index, key] of Object.keys(values).entries()) {
899
- const value = returnValues[index];
900
- if (value === undefined) {
901
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
517
+ const keyObjArr = keys.map((key) => this.keyPrefixer.create(key));
518
+ try {
519
+ const adapter = await this.adapterPromise;
520
+ const hasRemovedAtLeastOne = await adapter.removeMany(keyObjArr.map((keyObj) => keyObj.prefixed));
521
+ if (hasRemovedAtLeastOne) {
522
+ const events = keyObjArr.map((keyObj) => new KeyRemovedCacheEvent({
523
+ group: this.getGroup(),
524
+ key: keyObj.resolved,
525
+ }));
526
+ this.eventBus.dispatchMany(events).defer();
902
527
  }
903
- result[key] = value;
904
- }
905
- return result;
906
- });
907
- }
908
- updateMany(values) {
909
- return this.createLayPromise(async () => {
910
- if (isObjectEmpty(values)) {
911
- return {};
912
- }
913
- const valuePromises = [];
914
- for (const key in values) {
915
- const value = values[key];
916
- valuePromises.push(this.update(key, value));
917
- }
918
- const returnValues = await Promise.all(valuePromises);
919
- const result = {};
920
- for (const [index, key] of Object.keys(values).entries()) {
921
- const value = returnValues[index];
922
- if (value === undefined) {
923
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
528
+ else {
529
+ const events = keyObjArr.map((keyObj) => new KeyNotFoundCacheEvent({
530
+ group: this.getGroup(),
531
+ key: keyObj.resolved,
532
+ }));
533
+ this.eventBus.dispatchMany(events).defer();
924
534
  }
925
- result[key] = value;
535
+ return hasRemovedAtLeastOne;
926
536
  }
927
- return result;
928
- });
929
- }
930
- putMany(values) {
931
- return this.createLayPromise(async () => {
932
- if (isObjectEmpty(values)) {
933
- return {};
934
- }
935
- const valuePromises = [];
936
- for (const key in values) {
937
- const { value, ttl } = values[key];
938
- valuePromises.push(this.put(key, value, ttl));
939
- }
940
- const returnValues = await Promise.all(valuePromises);
941
- const result = {};
942
- for (const [index, key] of Object.keys(values).entries()) {
943
- const value = returnValues[index];
944
- if (value === undefined) {
945
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
946
- }
947
- result[key] = value;
537
+ catch (error) {
538
+ this.eventBus
539
+ .dispatch(new UnexpectedCacheErrorEvent({
540
+ group: this.getGroup(),
541
+ keys: keyObjArr.map((keyObj) => keyObj.resolved),
542
+ method: this.remove.name,
543
+ error,
544
+ }))
545
+ .defer();
546
+ throw error;
948
547
  }
949
- return result;
950
548
  });
951
549
  }
952
- removeMany(keys) {
953
- return this.createLayPromise(async () => {
954
- if (keys.length === 0) {
955
- return {};
956
- }
957
- const valuePromises = [];
958
- for (const key of keys) {
959
- valuePromises.push(this.remove(key));
960
- }
961
- const values = await Promise.all(valuePromises);
962
- const result = {};
963
- for (const [index, key] of keys.entries()) {
964
- const value = values[index];
965
- if (value === undefined) {
966
- throw new UnexpectedCacheError(`Item "values[${String(index)}]" is undefined`);
550
+ clear() {
551
+ return this.createLazyPromise(async () => {
552
+ try {
553
+ const adapter = await this.adapterPromise;
554
+ const promise = this.eventBus.dispatch(new KeysClearedCacheEvent({
555
+ group: this.getGroup(),
556
+ }));
557
+ if (isFactory(this.adapterFactoryable)) {
558
+ await adapter.removeAll();
559
+ promise.defer();
560
+ return;
967
561
  }
968
- result[key] = value;
562
+ await adapter.removeByKeyPrefix(this.keyPrefixer.keyPrefix);
563
+ promise.defer();
969
564
  }
970
- return result;
971
- });
972
- }
973
- /**
974
- * @example
975
- * ```ts
976
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
977
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
978
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
979
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
980
- *
981
- * const cache: IGroupableCache = new Cache({
982
- * adapter: new MemoryCacheAdapter({
983
- * rootGroup: "@global"
984
- * }),
985
- * });
986
- *
987
- * const result1 = await cache.getAndRemove("a");
988
- * // Will print null
989
- * console.log(result1);
990
- *
991
- * await cache.add("a", 2)
992
- *
993
- * const result2 = await cache.getAndRemove("a");
994
- * // Will print 2
995
- * console.log(result2);
996
- *
997
- * const result3 = await cache.get("a");
998
- * // Will print null
999
- * console.log(result3);
1000
- * ```
1001
- */
1002
- getAndRemove(key) {
1003
- return this.createLayPromise(async () => {
1004
- const value = await this.get(key);
1005
- if (value === null) {
1006
- return null;
565
+ catch (error) {
566
+ this.eventBus
567
+ .dispatch(new UnexpectedCacheErrorEvent({
568
+ group: this.getGroup(),
569
+ method: this.clear.name,
570
+ error,
571
+ }))
572
+ .defer();
573
+ throw error;
1007
574
  }
1008
- await this.remove(key);
1009
- return value;
1010
575
  });
1011
576
  }
1012
- /**
1013
- * @example
1014
- * ```ts
1015
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
1016
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
1017
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
1018
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
1019
- *
1020
- * const cache: IGroupableCache = new Cache({
1021
- * adapter: new MemoryCacheAdapter({
1022
- * rootGroup: "@global"
1023
- * }),
1024
- * });
1025
- *
1026
- * const result = await cache.getOrAdd("a", -1);
1027
- * // -1
1028
- * console.log(result);
1029
- * ```
1030
- *
1031
- * You can pass function as default value.
1032
- * @example
1033
- * ```ts
1034
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
1035
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
1036
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
1037
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
1038
- *
1039
- * const cache: IGroupableCache = new Cache({
1040
- * adapter: new MemoryCacheAdapter({
1041
- * rootGroup: "@global"
1042
- * }),
1043
- * });
1044
- *
1045
- * const result = await cache.getOrAdd("a", () => -1);
1046
- * // -1
1047
- * console.log(result);
1048
- * ```
1049
- *
1050
- * You can pass async function as default value.
1051
- * @example
1052
- * ```ts
1053
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
1054
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
1055
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
1056
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
1057
- *
1058
- * const cache: IGroupableCache = new Cache({
1059
- * adapter: new MemoryCacheAdapter({
1060
- * rootGroup: "@global"
1061
- * }),
1062
- * });
1063
- *
1064
- * const result = await cache.getOrAdd("a", async () => -1);
1065
- * // -1
1066
- * console.log(result);
1067
- * ```
1068
- *
1069
- * You can pass <i>{@link LazyPromise}</i> as default value.
1070
- * @example
1071
- * ```ts
1072
- * import type { IGroupableCache } from "@daiso-tech/core/cache/contracts";
1073
- * import type { IGroupableEventBus } from "@daiso-tech/core/event-bus/contracts";
1074
- * import { Cache } from "@daiso-tech/core/cache/implementations/derivables";
1075
- * import { MemoryCacheAdapter } from "@daiso-tech/core/cache/implementations/adapters";
1076
- *
1077
- * const cache: IGroupableCache = new Cache({
1078
- * adapter: new MemoryCacheAdapter({
1079
- * rootGroup: "@global"
1080
- * }),
1081
- * });
1082
- *
1083
- * const result = await cache.getOrAdd("a", new LazyPromise(async () => -1));
1084
- * // -1
1085
- * console.log(result);
1086
- * ```
1087
- */
1088
- getOrAdd(key, valueToAdd,
1089
- /**
1090
- * @default {null}
1091
- */
1092
- ttl) {
1093
- return this.createLayPromise(async () => {
1094
- const value = await this.get(key);
1095
- if (value === null) {
1096
- const simplifiedValueToAdd = await resolveAsyncLazyable(valueToAdd);
1097
- await this.add(key, simplifiedValueToAdd, ttl);
1098
- return simplifiedValueToAdd;
1099
- }
1100
- return value;
1101
- });
577
+ getGroup() {
578
+ if (this.keyPrefixer.resolvedGroup) {
579
+ return this.keyPrefixer.resolvedGroup;
580
+ }
581
+ return null;
1102
582
  }
1103
- decrement(key, value = 1) {
1104
- return this.createLayPromise(async () => {
1105
- return await this.increment(key, -value);
583
+ withGroup(group) {
584
+ const cache = new Cache({
585
+ keyPrefixer: this.keyPrefixer.withGroup(group),
586
+ adapter: this.adapterFactoryable,
587
+ eventBus: this.groupdEventBus,
588
+ defaultTtl: this.defaultTtl,
589
+ retryAttempts: this.retryAttempts,
590
+ backoffPolicy: this.backoffPolicy,
591
+ retryPolicy: this.retryPolicy,
592
+ timeout: this.timeout,
1106
593
  });
594
+ return cache;
1107
595
  }
1108
596
  }
1109
597
  //# sourceMappingURL=cache.js.map