lakutata 2.0.34 → 2.0.35

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 (210) hide show
  1. package/com/cacher.cjs +211 -0
  2. package/com/cacher.d.ts +514 -0
  3. package/com/cacher.mjs +185 -0
  4. package/com/database.cjs +6 -2
  5. package/com/database.mjs +6 -2
  6. package/com/docker.cjs +23 -17
  7. package/com/docker.mjs +19 -13
  8. package/com/entrypoint.cjs +1 -1
  9. package/com/entrypoint.mjs +1 -1
  10. package/com/logger.cjs +6 -2
  11. package/com/logger.mjs +6 -2
  12. package/decorator/ctrl.cjs +1 -1
  13. package/decorator/ctrl.mjs +1 -1
  14. package/decorator/orm.cjs +22 -18
  15. package/decorator/orm.mjs +6 -2
  16. package/helper.cjs +1 -1
  17. package/helper.mjs +1 -1
  18. package/lakutata.cjs +7 -3
  19. package/lakutata.mjs +7 -3
  20. package/orm.cjs +19 -15
  21. package/orm.mjs +15 -11
  22. package/package.json +6 -1
  23. package/provider/database.cjs +6 -2
  24. package/provider/database.mjs +6 -2
  25. package/src/components/Database.cjs +6 -2
  26. package/src/components/Database.mjs +6 -2
  27. package/src/components/Logger.cjs +6 -2
  28. package/src/components/Logger.mjs +6 -2
  29. package/src/components/cacher/Cacher.cjs +193 -0
  30. package/src/components/cacher/Cacher.mjs +183 -0
  31. package/src/components/cacher/options/CacherOptions.cjs +93 -0
  32. package/src/components/cacher/options/CacherOptions.mjs +87 -0
  33. package/src/components/cacher/options/FileCacheOptions.cjs +77 -0
  34. package/src/components/cacher/options/FileCacheOptions.mjs +71 -0
  35. package/src/components/cacher/options/MemcacheCacheOptions.cjs +79 -0
  36. package/src/components/cacher/options/MemcacheCacheOptions.mjs +73 -0
  37. package/src/components/cacher/options/MongoCacheOptions.cjs +83 -0
  38. package/src/components/cacher/options/MongoCacheOptions.mjs +77 -0
  39. package/src/components/cacher/options/MysqlCacheOptions.cjs +83 -0
  40. package/src/components/cacher/options/MysqlCacheOptions.mjs +77 -0
  41. package/src/components/cacher/options/PostgresCacheOptions.cjs +87 -0
  42. package/src/components/cacher/options/PostgresCacheOptions.mjs +81 -0
  43. package/src/components/cacher/options/RedisCacheOptions.cjs +99 -0
  44. package/src/components/cacher/options/RedisCacheOptions.mjs +93 -0
  45. package/src/components/cacher/options/SqliteCacheOptions.cjs +77 -0
  46. package/src/components/cacher/options/SqliteCacheOptions.mjs +71 -0
  47. package/src/components/cacher/types/CacheStoreOptions.cjs +1 -0
  48. package/src/components/cacher/types/CacheStoreOptions.mjs +1 -0
  49. package/src/components/docker/ConnectionOptionsBuilder.cjs +19 -13
  50. package/src/components/docker/ConnectionOptionsBuilder.mjs +19 -13
  51. package/src/components/docker/Docker.cjs +19 -13
  52. package/src/components/docker/Docker.mjs +19 -13
  53. package/src/components/docker/lib/DockerContainer.cjs +26 -20
  54. package/src/components/docker/lib/DockerContainer.mjs +20 -14
  55. package/src/components/docker/lib/DockerContainerTTY.cjs +15 -9
  56. package/src/components/docker/lib/DockerContainerTTY.mjs +15 -9
  57. package/src/components/entrypoint/Entrypoint.cjs +1 -1
  58. package/src/components/entrypoint/Entrypoint.mjs +1 -1
  59. package/src/decorators/ctrl/CLIAction.cjs +1 -1
  60. package/src/decorators/ctrl/CLIAction.mjs +1 -1
  61. package/src/decorators/ctrl/HTTPAction.cjs +1 -1
  62. package/src/decorators/ctrl/HTTPAction.mjs +1 -1
  63. package/src/decorators/ctrl/ServiceAction.cjs +1 -1
  64. package/src/decorators/ctrl/ServiceAction.mjs +1 -1
  65. package/src/decorators/ctrl/http/DELETE.cjs +1 -1
  66. package/src/decorators/ctrl/http/DELETE.mjs +1 -1
  67. package/src/decorators/ctrl/http/GET.cjs +1 -1
  68. package/src/decorators/ctrl/http/GET.mjs +1 -1
  69. package/src/decorators/ctrl/http/HEAD.cjs +1 -1
  70. package/src/decorators/ctrl/http/HEAD.mjs +1 -1
  71. package/src/decorators/ctrl/http/OPTIONS.cjs +1 -1
  72. package/src/decorators/ctrl/http/OPTIONS.mjs +1 -1
  73. package/src/decorators/ctrl/http/PATCH.cjs +1 -1
  74. package/src/decorators/ctrl/http/PATCH.mjs +1 -1
  75. package/src/decorators/ctrl/http/POST.cjs +1 -1
  76. package/src/decorators/ctrl/http/POST.mjs +1 -1
  77. package/src/decorators/ctrl/http/PUT.cjs +1 -1
  78. package/src/decorators/ctrl/http/PUT.mjs +1 -1
  79. package/src/decorators/orm/AfterInsert.cjs +6 -2
  80. package/src/decorators/orm/AfterInsert.mjs +13 -9
  81. package/src/decorators/orm/AfterLoad.cjs +6 -2
  82. package/src/decorators/orm/AfterLoad.mjs +16 -12
  83. package/src/decorators/orm/AfterRecover.cjs +6 -2
  84. package/src/decorators/orm/AfterRecover.mjs +10 -6
  85. package/src/decorators/orm/AfterRemove.cjs +6 -2
  86. package/src/decorators/orm/AfterRemove.mjs +10 -6
  87. package/src/decorators/orm/AfterSoftRemove.cjs +6 -2
  88. package/src/decorators/orm/AfterSoftRemove.mjs +10 -6
  89. package/src/decorators/orm/AfterUpdate.cjs +6 -2
  90. package/src/decorators/orm/AfterUpdate.mjs +14 -10
  91. package/src/decorators/orm/BeforeInsert.cjs +6 -2
  92. package/src/decorators/orm/BeforeInsert.mjs +10 -6
  93. package/src/decorators/orm/BeforeRecover.cjs +13 -9
  94. package/src/decorators/orm/BeforeRecover.mjs +6 -2
  95. package/src/decorators/orm/BeforeRemove.cjs +13 -9
  96. package/src/decorators/orm/BeforeRemove.mjs +6 -2
  97. package/src/decorators/orm/BeforeSoftRemove.cjs +10 -6
  98. package/src/decorators/orm/BeforeSoftRemove.mjs +6 -2
  99. package/src/decorators/orm/BeforeUpdate.cjs +6 -2
  100. package/src/decorators/orm/BeforeUpdate.mjs +11 -7
  101. package/src/decorators/orm/Check.cjs +6 -2
  102. package/src/decorators/orm/Check.mjs +6 -2
  103. package/src/decorators/orm/ChildEntity.cjs +11 -7
  104. package/src/decorators/orm/ChildEntity.mjs +6 -2
  105. package/src/decorators/orm/Column.cjs +23 -19
  106. package/src/decorators/orm/Column.mjs +6 -2
  107. package/src/decorators/orm/CreateDateColumn.cjs +8 -4
  108. package/src/decorators/orm/CreateDateColumn.mjs +6 -2
  109. package/src/decorators/orm/DeleteDateColumn.cjs +6 -2
  110. package/src/decorators/orm/DeleteDateColumn.mjs +6 -2
  111. package/src/decorators/orm/Entity.cjs +6 -2
  112. package/src/decorators/orm/Entity.mjs +12 -8
  113. package/src/decorators/orm/EventSubscriber.cjs +6 -2
  114. package/src/decorators/orm/EventSubscriber.mjs +6 -2
  115. package/src/decorators/orm/Exclusion.cjs +6 -2
  116. package/src/decorators/orm/Exclusion.mjs +12 -8
  117. package/src/decorators/orm/Generated.cjs +10 -6
  118. package/src/decorators/orm/Generated.mjs +12 -8
  119. package/src/decorators/orm/Index.cjs +14 -10
  120. package/src/decorators/orm/Index.mjs +24 -20
  121. package/src/decorators/orm/JoinColumn.cjs +6 -2
  122. package/src/decorators/orm/JoinColumn.mjs +6 -2
  123. package/src/decorators/orm/JoinTable.cjs +6 -2
  124. package/src/decorators/orm/JoinTable.mjs +8 -4
  125. package/src/decorators/orm/ManyToMany.cjs +13 -9
  126. package/src/decorators/orm/ManyToMany.mjs +17 -13
  127. package/src/decorators/orm/ManyToOne.cjs +6 -2
  128. package/src/decorators/orm/ManyToOne.mjs +6 -2
  129. package/src/decorators/orm/ObjectIdColumn.cjs +6 -2
  130. package/src/decorators/orm/ObjectIdColumn.mjs +12 -8
  131. package/src/decorators/orm/OneToMany.cjs +14 -10
  132. package/src/decorators/orm/OneToMany.mjs +6 -2
  133. package/src/decorators/orm/OneToOne.cjs +15 -11
  134. package/src/decorators/orm/OneToOne.mjs +13 -9
  135. package/src/decorators/orm/PrimaryColumn.cjs +6 -2
  136. package/src/decorators/orm/PrimaryColumn.mjs +26 -22
  137. package/src/decorators/orm/PrimaryGeneratedColumn.cjs +6 -2
  138. package/src/decorators/orm/PrimaryGeneratedColumn.mjs +8 -4
  139. package/src/decorators/orm/RelationId.cjs +11 -7
  140. package/src/decorators/orm/RelationId.mjs +12 -8
  141. package/src/decorators/orm/TableInheritance.cjs +6 -2
  142. package/src/decorators/orm/TableInheritance.mjs +10 -6
  143. package/src/decorators/orm/Tree.cjs +6 -2
  144. package/src/decorators/orm/Tree.mjs +6 -2
  145. package/src/decorators/orm/TreeChildren.cjs +6 -2
  146. package/src/decorators/orm/TreeChildren.mjs +17 -13
  147. package/src/decorators/orm/TreeLevelColumn.cjs +6 -2
  148. package/src/decorators/orm/TreeLevelColumn.mjs +6 -2
  149. package/src/decorators/orm/TreeParent.cjs +12 -8
  150. package/src/decorators/orm/TreeParent.mjs +6 -2
  151. package/src/decorators/orm/Unique.cjs +6 -2
  152. package/src/decorators/orm/Unique.mjs +6 -2
  153. package/src/decorators/orm/UpdateDateColumn.cjs +8 -4
  154. package/src/decorators/orm/UpdateDateColumn.mjs +6 -2
  155. package/src/decorators/orm/VersionColumn.cjs +6 -2
  156. package/src/decorators/orm/VersionColumn.mjs +6 -2
  157. package/src/decorators/orm/ViewColumn.cjs +6 -2
  158. package/src/decorators/orm/ViewColumn.mjs +6 -2
  159. package/src/decorators/orm/ViewEntity.cjs +8 -4
  160. package/src/decorators/orm/ViewEntity.mjs +6 -2
  161. package/src/decorators/orm/VirtualColumn.cjs +6 -2
  162. package/src/decorators/orm/VirtualColumn.mjs +10 -6
  163. package/src/lib/base/internal/ControllerEntrypoint.cjs +1 -1
  164. package/src/lib/base/internal/ControllerEntrypoint.mjs +1 -1
  165. package/src/lib/core/Application.cjs +11 -7
  166. package/src/lib/core/Application.mjs +7 -3
  167. package/src/lib/core/Module.cjs +1 -1
  168. package/src/lib/core/Module.mjs +1 -1
  169. package/src/lib/helpers/NonceStr.cjs +1 -1
  170. package/src/lib/helpers/NonceStr.mjs +1 -1
  171. package/src/lib/helpers/ObjectHash.cjs +1 -1
  172. package/src/lib/helpers/ObjectHash.mjs +1 -1
  173. package/src/lib/helpers/RandomString.cjs +1 -1
  174. package/src/lib/helpers/RandomString.mjs +1 -1
  175. package/src/providers/Database.cjs +6 -2
  176. package/src/providers/Database.mjs +6 -2
  177. package/vendor/Package.112.cjs +5498 -5500
  178. package/vendor/Package.112.mjs +5504 -5506
  179. package/vendor/Package.12.cjs +4 -80
  180. package/vendor/Package.12.mjs +4 -74
  181. package/vendor/Package.122.cjs +87 -0
  182. package/vendor/Package.122.mjs +79 -0
  183. package/vendor/Package.13.cjs +1 -1
  184. package/vendor/Package.13.mjs +1 -1
  185. package/vendor/Package.14.cjs +1 -14055
  186. package/vendor/Package.14.mjs +1 -14029
  187. package/vendor/Package.15.cjs +12449 -46635
  188. package/vendor/Package.15.mjs +12435 -46611
  189. package/vendor/Package.16.cjs +3 -0
  190. package/vendor/Package.16.mjs +1 -0
  191. package/vendor/Package.17.cjs +48245 -0
  192. package/vendor/Package.17.mjs +48202 -0
  193. package/vendor/Package.18.cjs +3 -0
  194. package/vendor/Package.18.mjs +1 -0
  195. package/vendor/Package.19.cjs +4466 -0
  196. package/vendor/Package.19.mjs +4442 -0
  197. package/vendor/Package.610.cjs +196 -0
  198. package/vendor/Package.610.mjs +180 -0
  199. package/vendor/Package.62.cjs +1 -1
  200. package/vendor/Package.62.mjs +1 -1
  201. package/vendor/Package.65.cjs +860 -0
  202. package/vendor/Package.65.mjs +832 -0
  203. package/vendor/Package.66.cjs +321 -0
  204. package/vendor/Package.66.mjs +305 -0
  205. package/vendor/Package.67.cjs +341 -0
  206. package/vendor/Package.67.mjs +327 -0
  207. package/vendor/Package.68.cjs +333 -0
  208. package/vendor/Package.68.mjs +315 -0
  209. package/vendor/Package.69.cjs +353 -0
  210. package/vendor/Package.69.mjs +335 -0
@@ -0,0 +1,341 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, {
4
+ value: "Module"
5
+ });
6
+
7
+ const e = require("events");
8
+
9
+ const t = require("buffer");
10
+
11
+ const n = require("mongodb");
12
+
13
+ const s = e => e && e.__esModule ? e : {
14
+ default: e
15
+ };
16
+
17
+ const o = s(e);
18
+
19
+ var a = new Set([ "url", "collection", "namespace", "serialize", "deserialize", "uri", "useGridFS", "dialect", "db" ]);
20
+
21
+ var i = class extends o.default {
22
+ ttlSupport=false;
23
+ opts;
24
+ connect;
25
+ namespace;
26
+ constructor(e, t) {
27
+ super();
28
+ e ??= {};
29
+ if (typeof e === "string") {
30
+ e = {
31
+ url: e
32
+ };
33
+ }
34
+ if (e.uri) {
35
+ e = {
36
+ url: e.uri,
37
+ ...e
38
+ };
39
+ }
40
+ this.opts = {
41
+ url: "mongodb://127.0.0.1:27017",
42
+ collection: "keyv",
43
+ ...e,
44
+ ...t
45
+ };
46
+ delete this.opts.emitErrors;
47
+ const s = Object.fromEntries(Object.entries(this.opts).filter((([e]) => !a.has(e))));
48
+ this.opts = Object.fromEntries(Object.entries(this.opts).filter((([e]) => a.has(e))));
49
+ this.connect = new Promise((async (e, t) => {
50
+ try {
51
+ let t = "";
52
+ if (this.opts.url) {
53
+ t = this.opts.url;
54
+ }
55
+ const o = new n.MongoClient(t, s);
56
+ await o.connect();
57
+ const a = o.db(this.opts.db);
58
+ if (this.opts.useGridFS) {
59
+ const t = new n.GridFSBucket(a, {
60
+ readPreference: this.opts.readPreference,
61
+ bucketName: this.opts.collection
62
+ });
63
+ const s = a.collection(`${this.opts.collection}.files`);
64
+ await s.createIndex({
65
+ uploadDate: -1
66
+ });
67
+ await s.createIndex({
68
+ "metadata.expiresAt": 1
69
+ });
70
+ await s.createIndex({
71
+ "metadata.lastAccessed": 1
72
+ });
73
+ await s.createIndex({
74
+ "metadata.filename": 1
75
+ });
76
+ e({
77
+ bucket: t,
78
+ store: s,
79
+ db: a,
80
+ mongoClient: o
81
+ });
82
+ } else {
83
+ let t = "keyv";
84
+ if (this.opts.collection) {
85
+ t = this.opts.collection;
86
+ }
87
+ const n = a.collection(t);
88
+ await n.createIndex({
89
+ key: 1
90
+ }, {
91
+ unique: true,
92
+ background: true
93
+ });
94
+ await n.createIndex({
95
+ expiresAt: 1
96
+ }, {
97
+ expireAfterSeconds: 0,
98
+ background: true
99
+ });
100
+ e({
101
+ store: n,
102
+ mongoClient: o
103
+ });
104
+ }
105
+ } catch (e) {
106
+ this.emit("error", e);
107
+ }
108
+ }));
109
+ }
110
+ async get(e) {
111
+ const n = await this.connect;
112
+ if (this.opts.useGridFS) {
113
+ await n.store.updateOne({
114
+ filename: e
115
+ }, {
116
+ $set: {
117
+ "metadata.lastAccessed": new Date
118
+ }
119
+ });
120
+ const s = n.bucket.openDownloadStreamByName(e);
121
+ return new Promise((e => {
122
+ const n = [];
123
+ s.on("error", (() => {
124
+ e(void 0);
125
+ }));
126
+ s.on("end", (() => {
127
+ const s = t.Buffer.concat(n).toString("utf8");
128
+ e(s);
129
+ }));
130
+ s.on("data", (e => {
131
+ n.push(e);
132
+ }));
133
+ }));
134
+ }
135
+ const s = await n.store.findOne({
136
+ key: {
137
+ $eq: e
138
+ }
139
+ });
140
+ if (!s) {
141
+ return void 0;
142
+ }
143
+ return s.value;
144
+ }
145
+ async getMany(e) {
146
+ if (this.opts.useGridFS) {
147
+ const t = [];
148
+ for (const n of e) {
149
+ t.push(this.get(n));
150
+ }
151
+ const n = await Promise.allSettled(t);
152
+ const s = [];
153
+ for (const e of n) {
154
+ s.push(e.value);
155
+ }
156
+ return s;
157
+ }
158
+ const t = await this.connect;
159
+ const n = await t.store.s.db.collection(this.opts.collection).find({
160
+ key: {
161
+ $in: e
162
+ }
163
+ }).project({
164
+ _id: 0,
165
+ value: 1,
166
+ key: 1
167
+ }).toArray();
168
+ const s = [ ...e ];
169
+ let o = 0;
170
+ for (const t of e) {
171
+ const e = n.findIndex((e => e.key === t));
172
+ s[o] = e > -1 ? n[e].value : void 0;
173
+ o++;
174
+ }
175
+ return s;
176
+ }
177
+ async set(e, t, n) {
178
+ const s = typeof n === "number" ? new Date(Date.now() + n) : null;
179
+ if (this.opts.useGridFS) {
180
+ const n = await this.connect;
181
+ const o = n.bucket.openUploadStream(e, {
182
+ metadata: {
183
+ expiresAt: s,
184
+ lastAccessed: new Date
185
+ }
186
+ });
187
+ return new Promise((e => {
188
+ o.on("finish", (() => {
189
+ e(o);
190
+ }));
191
+ o.end(t);
192
+ }));
193
+ }
194
+ const o = await this.connect;
195
+ await o.store.updateOne({
196
+ key: {
197
+ $eq: e
198
+ }
199
+ }, {
200
+ $set: {
201
+ key: e,
202
+ value: t,
203
+ expiresAt: s
204
+ }
205
+ }, {
206
+ upsert: true
207
+ });
208
+ }
209
+ async delete(e) {
210
+ if (typeof e !== "string") {
211
+ return false;
212
+ }
213
+ const t = await this.connect;
214
+ if (this.opts.useGridFS) {
215
+ try {
216
+ const s = t.db;
217
+ const o = new n.GridFSBucket(s, {
218
+ bucketName: this.opts.collection
219
+ });
220
+ const a = await o.find({
221
+ filename: e
222
+ }).toArray();
223
+ await t.bucket.delete(a[0]._id);
224
+ return true;
225
+ } catch {
226
+ return false;
227
+ }
228
+ }
229
+ const s = await t.store.deleteOne({
230
+ key: {
231
+ $eq: e
232
+ }
233
+ });
234
+ return s.deletedCount > 0;
235
+ }
236
+ async deleteMany(e) {
237
+ const t = await this.connect;
238
+ if (this.opts.useGridFS) {
239
+ const s = t.db;
240
+ const o = new n.GridFSBucket(s, {
241
+ bucketName: this.opts.collection
242
+ });
243
+ const a = await o.find({
244
+ filename: {
245
+ $in: e
246
+ }
247
+ }).toArray();
248
+ if (a.length === 0) {
249
+ return false;
250
+ }
251
+ await Promise.all(a.map((async e => t.bucket.delete(e._id))));
252
+ return true;
253
+ }
254
+ const s = await t.store.deleteMany({
255
+ key: {
256
+ $in: e
257
+ }
258
+ });
259
+ return s.deletedCount > 0;
260
+ }
261
+ async clear() {
262
+ const e = await this.connect;
263
+ if (this.opts.useGridFS) {
264
+ try {
265
+ await e.bucket.drop();
266
+ } catch (e) {
267
+ if (!(e instanceof n.MongoServerError && e.code === 26)) {
268
+ throw e;
269
+ }
270
+ }
271
+ }
272
+ await e.store.deleteMany({
273
+ key: {
274
+ $regex: this.namespace ? `^${this.namespace}:*` : ""
275
+ }
276
+ });
277
+ }
278
+ async clearExpired() {
279
+ if (!this.opts.useGridFS) {
280
+ return false;
281
+ }
282
+ return this.connect.then((async e => {
283
+ const t = e.db;
284
+ const s = new n.GridFSBucket(t, {
285
+ bucketName: this.opts.collection
286
+ });
287
+ return s.find({
288
+ "metadata.expiresAt": {
289
+ $lte: new Date(Date.now())
290
+ }
291
+ }).toArray().then((async t => Promise.all(t.map((async t => e.bucket.delete(t._id)))).then((() => true))));
292
+ }));
293
+ }
294
+ async clearUnusedFor(e) {
295
+ if (!this.opts.useGridFS) {
296
+ return false;
297
+ }
298
+ const t = await this.connect;
299
+ const s = t.db;
300
+ const o = new n.GridFSBucket(s, {
301
+ bucketName: this.opts.collection
302
+ });
303
+ const a = await o.find({
304
+ "metadata.lastAccessed": {
305
+ $lte: new Date(Date.now() - e * 1e3)
306
+ }
307
+ }).toArray();
308
+ await Promise.all(a.map((async e => t.bucket.delete(e._id))));
309
+ return true;
310
+ }
311
+ async* iterator(e) {
312
+ const t = await this.connect;
313
+ const n = new RegExp(`^${e ? e + ":" : ".*"}`);
314
+ const s = this.opts.useGridFS ? t.store.find({
315
+ filename: n
316
+ }).map((async e => [ e.filename, await this.get(e.filename) ])) : t.store.find({
317
+ key: n
318
+ }).map((e => [ e.key, e.value ]));
319
+ yield* s;
320
+ }
321
+ async has(e) {
322
+ const t = await this.connect;
323
+ const n = {
324
+ [this.opts.useGridFS ? "filename" : "key"]: {
325
+ $eq: e
326
+ }
327
+ };
328
+ const s = await t.store.count(n);
329
+ return s !== 0;
330
+ }
331
+ async disconnect() {
332
+ const e = await this.connect;
333
+ await e.mongoClient.close();
334
+ }
335
+ };
336
+
337
+ var c = i;
338
+
339
+ exports.KeyvMongo = i;
340
+
341
+ exports.default = c;
@@ -0,0 +1,327 @@
1
+ import t from "events";
2
+
3
+ import { Buffer as e } from "buffer";
4
+
5
+ import { MongoClient as n, GridFSBucket as s, MongoServerError as a } from "mongodb";
6
+
7
+ var o = new Set([ "url", "collection", "namespace", "serialize", "deserialize", "uri", "useGridFS", "dialect", "db" ]);
8
+
9
+ var i = class extends t {
10
+ ttlSupport=false;
11
+ opts;
12
+ connect;
13
+ namespace;
14
+ constructor(t, e) {
15
+ super();
16
+ t ??= {};
17
+ if (typeof t === "string") {
18
+ t = {
19
+ url: t
20
+ };
21
+ }
22
+ if (t.uri) {
23
+ t = {
24
+ url: t.uri,
25
+ ...t
26
+ };
27
+ }
28
+ this.opts = {
29
+ url: "mongodb://127.0.0.1:27017",
30
+ collection: "keyv",
31
+ ...t,
32
+ ...e
33
+ };
34
+ delete this.opts.emitErrors;
35
+ const a = Object.fromEntries(Object.entries(this.opts).filter((([t]) => !o.has(t))));
36
+ this.opts = Object.fromEntries(Object.entries(this.opts).filter((([t]) => o.has(t))));
37
+ this.connect = new Promise((async (t, e) => {
38
+ try {
39
+ let e = "";
40
+ if (this.opts.url) {
41
+ e = this.opts.url;
42
+ }
43
+ const o = new n(e, a);
44
+ await o.connect();
45
+ const i = o.db(this.opts.db);
46
+ if (this.opts.useGridFS) {
47
+ const e = new s(i, {
48
+ readPreference: this.opts.readPreference,
49
+ bucketName: this.opts.collection
50
+ });
51
+ const n = i.collection(`${this.opts.collection}.files`);
52
+ await n.createIndex({
53
+ uploadDate: -1
54
+ });
55
+ await n.createIndex({
56
+ "metadata.expiresAt": 1
57
+ });
58
+ await n.createIndex({
59
+ "metadata.lastAccessed": 1
60
+ });
61
+ await n.createIndex({
62
+ "metadata.filename": 1
63
+ });
64
+ t({
65
+ bucket: e,
66
+ store: n,
67
+ db: i,
68
+ mongoClient: o
69
+ });
70
+ } else {
71
+ let e = "keyv";
72
+ if (this.opts.collection) {
73
+ e = this.opts.collection;
74
+ }
75
+ const n = i.collection(e);
76
+ await n.createIndex({
77
+ key: 1
78
+ }, {
79
+ unique: true,
80
+ background: true
81
+ });
82
+ await n.createIndex({
83
+ expiresAt: 1
84
+ }, {
85
+ expireAfterSeconds: 0,
86
+ background: true
87
+ });
88
+ t({
89
+ store: n,
90
+ mongoClient: o
91
+ });
92
+ }
93
+ } catch (t) {
94
+ this.emit("error", t);
95
+ }
96
+ }));
97
+ }
98
+ async get(t) {
99
+ const n = await this.connect;
100
+ if (this.opts.useGridFS) {
101
+ await n.store.updateOne({
102
+ filename: t
103
+ }, {
104
+ $set: {
105
+ "metadata.lastAccessed": new Date
106
+ }
107
+ });
108
+ const s = n.bucket.openDownloadStreamByName(t);
109
+ return new Promise((t => {
110
+ const n = [];
111
+ s.on("error", (() => {
112
+ t(void 0);
113
+ }));
114
+ s.on("end", (() => {
115
+ const s = e.concat(n).toString("utf8");
116
+ t(s);
117
+ }));
118
+ s.on("data", (t => {
119
+ n.push(t);
120
+ }));
121
+ }));
122
+ }
123
+ const s = await n.store.findOne({
124
+ key: {
125
+ $eq: t
126
+ }
127
+ });
128
+ if (!s) {
129
+ return void 0;
130
+ }
131
+ return s.value;
132
+ }
133
+ async getMany(t) {
134
+ if (this.opts.useGridFS) {
135
+ const e = [];
136
+ for (const n of t) {
137
+ e.push(this.get(n));
138
+ }
139
+ const n = await Promise.allSettled(e);
140
+ const s = [];
141
+ for (const t of n) {
142
+ s.push(t.value);
143
+ }
144
+ return s;
145
+ }
146
+ const e = await this.connect;
147
+ const n = await e.store.s.db.collection(this.opts.collection).find({
148
+ key: {
149
+ $in: t
150
+ }
151
+ }).project({
152
+ _id: 0,
153
+ value: 1,
154
+ key: 1
155
+ }).toArray();
156
+ const s = [ ...t ];
157
+ let a = 0;
158
+ for (const e of t) {
159
+ const t = n.findIndex((t => t.key === e));
160
+ s[a] = t > -1 ? n[t].value : void 0;
161
+ a++;
162
+ }
163
+ return s;
164
+ }
165
+ async set(t, e, n) {
166
+ const s = typeof n === "number" ? new Date(Date.now() + n) : null;
167
+ if (this.opts.useGridFS) {
168
+ const n = await this.connect;
169
+ const a = n.bucket.openUploadStream(t, {
170
+ metadata: {
171
+ expiresAt: s,
172
+ lastAccessed: new Date
173
+ }
174
+ });
175
+ return new Promise((t => {
176
+ a.on("finish", (() => {
177
+ t(a);
178
+ }));
179
+ a.end(e);
180
+ }));
181
+ }
182
+ const a = await this.connect;
183
+ await a.store.updateOne({
184
+ key: {
185
+ $eq: t
186
+ }
187
+ }, {
188
+ $set: {
189
+ key: t,
190
+ value: e,
191
+ expiresAt: s
192
+ }
193
+ }, {
194
+ upsert: true
195
+ });
196
+ }
197
+ async delete(t) {
198
+ if (typeof t !== "string") {
199
+ return false;
200
+ }
201
+ const e = await this.connect;
202
+ if (this.opts.useGridFS) {
203
+ try {
204
+ const n = e.db;
205
+ const a = new s(n, {
206
+ bucketName: this.opts.collection
207
+ });
208
+ const o = await a.find({
209
+ filename: t
210
+ }).toArray();
211
+ await e.bucket.delete(o[0]._id);
212
+ return true;
213
+ } catch {
214
+ return false;
215
+ }
216
+ }
217
+ const n = await e.store.deleteOne({
218
+ key: {
219
+ $eq: t
220
+ }
221
+ });
222
+ return n.deletedCount > 0;
223
+ }
224
+ async deleteMany(t) {
225
+ const e = await this.connect;
226
+ if (this.opts.useGridFS) {
227
+ const n = e.db;
228
+ const a = new s(n, {
229
+ bucketName: this.opts.collection
230
+ });
231
+ const o = await a.find({
232
+ filename: {
233
+ $in: t
234
+ }
235
+ }).toArray();
236
+ if (o.length === 0) {
237
+ return false;
238
+ }
239
+ await Promise.all(o.map((async t => e.bucket.delete(t._id))));
240
+ return true;
241
+ }
242
+ const n = await e.store.deleteMany({
243
+ key: {
244
+ $in: t
245
+ }
246
+ });
247
+ return n.deletedCount > 0;
248
+ }
249
+ async clear() {
250
+ const t = await this.connect;
251
+ if (this.opts.useGridFS) {
252
+ try {
253
+ await t.bucket.drop();
254
+ } catch (t) {
255
+ if (!(t instanceof a && t.code === 26)) {
256
+ throw t;
257
+ }
258
+ }
259
+ }
260
+ await t.store.deleteMany({
261
+ key: {
262
+ $regex: this.namespace ? `^${this.namespace}:*` : ""
263
+ }
264
+ });
265
+ }
266
+ async clearExpired() {
267
+ if (!this.opts.useGridFS) {
268
+ return false;
269
+ }
270
+ return this.connect.then((async t => {
271
+ const e = t.db;
272
+ const n = new s(e, {
273
+ bucketName: this.opts.collection
274
+ });
275
+ return n.find({
276
+ "metadata.expiresAt": {
277
+ $lte: new Date(Date.now())
278
+ }
279
+ }).toArray().then((async e => Promise.all(e.map((async e => t.bucket.delete(e._id)))).then((() => true))));
280
+ }));
281
+ }
282
+ async clearUnusedFor(t) {
283
+ if (!this.opts.useGridFS) {
284
+ return false;
285
+ }
286
+ const e = await this.connect;
287
+ const n = e.db;
288
+ const a = new s(n, {
289
+ bucketName: this.opts.collection
290
+ });
291
+ const o = await a.find({
292
+ "metadata.lastAccessed": {
293
+ $lte: new Date(Date.now() - t * 1e3)
294
+ }
295
+ }).toArray();
296
+ await Promise.all(o.map((async t => e.bucket.delete(t._id))));
297
+ return true;
298
+ }
299
+ async* iterator(t) {
300
+ const e = await this.connect;
301
+ const n = new RegExp(`^${t ? t + ":" : ".*"}`);
302
+ const s = this.opts.useGridFS ? e.store.find({
303
+ filename: n
304
+ }).map((async t => [ t.filename, await this.get(t.filename) ])) : e.store.find({
305
+ key: n
306
+ }).map((t => [ t.key, t.value ]));
307
+ yield* s;
308
+ }
309
+ async has(t) {
310
+ const e = await this.connect;
311
+ const n = {
312
+ [this.opts.useGridFS ? "filename" : "key"]: {
313
+ $eq: t
314
+ }
315
+ };
316
+ const s = await e.store.count(n);
317
+ return s !== 0;
318
+ }
319
+ async disconnect() {
320
+ const t = await this.connect;
321
+ await t.mongoClient.close();
322
+ }
323
+ };
324
+
325
+ var c = i;
326
+
327
+ export { i as KeyvMongo, c as default };