@furystack/mongodb-store 10.1.7 → 11.0.1

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 (36) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +21 -8
  3. package/esm/define-mongodb-store.d.ts +37 -0
  4. package/esm/define-mongodb-store.d.ts.map +1 -0
  5. package/esm/define-mongodb-store.js +39 -0
  6. package/esm/define-mongodb-store.js.map +1 -0
  7. package/esm/define-mongodb-store.spec.d.ts +2 -0
  8. package/esm/define-mongodb-store.spec.d.ts.map +1 -0
  9. package/esm/define-mongodb-store.spec.js +93 -0
  10. package/esm/define-mongodb-store.spec.js.map +1 -0
  11. package/esm/index.d.ts +2 -1
  12. package/esm/index.d.ts.map +1 -1
  13. package/esm/index.js +2 -1
  14. package/esm/index.js.map +1 -1
  15. package/esm/mongo-client-factory.d.ts +13 -15
  16. package/esm/mongo-client-factory.d.ts.map +1 -1
  17. package/esm/mongo-client-factory.js +25 -32
  18. package/esm/mongo-client-factory.js.map +1 -1
  19. package/esm/mongodb-store.d.ts +14 -3
  20. package/esm/mongodb-store.d.ts.map +1 -1
  21. package/esm/mongodb-store.js +15 -2
  22. package/esm/mongodb-store.js.map +1 -1
  23. package/esm/mongodb-store.spec.js +25 -49
  24. package/esm/mongodb-store.spec.js.map +1 -1
  25. package/package.json +7 -7
  26. package/src/define-mongodb-store.spec.ts +108 -0
  27. package/src/define-mongodb-store.ts +58 -0
  28. package/src/index.ts +2 -1
  29. package/src/mongo-client-factory.ts +35 -30
  30. package/src/mongodb-store.spec.ts +27 -53
  31. package/src/mongodb-store.ts +19 -5
  32. package/esm/store-manager-helpers.d.ts +0 -33
  33. package/esm/store-manager-helpers.d.ts.map +0 -1
  34. package/esm/store-manager-helpers.js +0 -15
  35. package/esm/store-manager-helpers.js.map +0 -1
  36. package/src/store-manager-helpers.ts +0 -55
@@ -1,4 +1,5 @@
1
1
  import type {
2
+ Constructable,
2
3
  CreateResult,
3
4
  FilterType,
4
5
  FindOptions,
@@ -6,10 +7,11 @@ import type {
6
7
  PhysicalStore,
7
8
  WithOptionalId,
8
9
  } from '@furystack/core'
9
- import type { Constructable } from '@furystack/inject'
10
+ import { NotFoundError } from '@furystack/core'
10
11
  import { EventHub } from '@furystack/utils'
11
- import type { Collection, Filter, MongoClient, OptionalUnlessRequiredId, Sort, UpdateFilter } from 'mongodb'
12
+ import type { Collection, Filter, MongoClient, OptionalUnlessRequiredId, Sort } from 'mongodb'
12
13
  import { ObjectId } from 'mongodb'
14
+ import type { MongoClientFactory } from './mongo-client-factory.js'
13
15
 
14
16
  // Improved type safety for hasObjectId
15
17
  const hasObjectId = <T extends { _id?: unknown }>(value: T): value is T & { _id: ObjectId } => {
@@ -17,7 +19,19 @@ const hasObjectId = <T extends { _id?: unknown }>(value: T): value is T & { _id:
17
19
  }
18
20
 
19
21
  /**
20
- * MongoDB Store implementation for FuryStack
22
+ * {@link PhysicalStore} backed by a MongoDB collection.
23
+ *
24
+ * The collection handle is resolved lazily on the first call via
25
+ * {@link getCollection}; if the primary key is not `_id` a unique index is
26
+ * created on it during init. Init failure clears the cached promise so the
27
+ * next call retries.
28
+ *
29
+ * When `primaryKey` is `_id`, string keys are transparently coerced to
30
+ * `ObjectId` for filters and back to hex strings on results so callers can
31
+ * stay in plain string-key territory.
32
+ *
33
+ * The store has no `[Symbol.asyncDispose]` — client lifetime belongs to
34
+ * {@link MongoClientFactory}, which closes pooled clients on injector teardown.
21
35
  */
22
36
  export class MongodbStore<
23
37
  T extends object,
@@ -142,9 +156,9 @@ export class MongodbStore<
142
156
 
143
157
  public async update(id: T[TPrimaryKey], data: Partial<T>): Promise<void> {
144
158
  const collection = await this.getCollection()
145
- const updateResult = await collection.updateOne(this.createIdFilter(id), { $set: data } as UpdateFilter<T>)
159
+ const updateResult = await collection.updateOne(this.createIdFilter(id), { $set: data })
146
160
  if (updateResult.matchedCount < 1) {
147
- throw Error(`Entity not found with id '${String(id)}', cannot update!`)
161
+ throw new NotFoundError(`Entity not found with id '${String(id)}', cannot update`)
148
162
  }
149
163
  this.emit('onEntityUpdated', { id, change: data })
150
164
  }
@@ -1,33 +0,0 @@
1
- import type { Constructable, Injector } from '@furystack/inject';
2
- import type { MongoClientOptions } from 'mongodb';
3
- export declare const useMongoDb: <T extends object>({ injector, model, primaryKey, db, collection, url, options, }: {
4
- /**
5
- * The Injector instance to use
6
- */
7
- injector: Injector;
8
- /**
9
- * The constructable model class
10
- */
11
- model: Constructable<T>;
12
- /**
13
- * The name of the Primary Key property
14
- */
15
- primaryKey: keyof T;
16
- /**
17
- * Url of the MongoDb repository
18
- */
19
- url: string;
20
- /**
21
- * MongoDb Database name
22
- */
23
- db: string;
24
- /**
25
- * MongoDb Collection name
26
- */
27
- collection: string;
28
- /**
29
- * Optional options for the MongoDb Client
30
- */
31
- options?: MongoClientOptions;
32
- }) => void;
33
- //# sourceMappingURL=store-manager-helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"store-manager-helpers.d.ts","sourceRoot":"","sources":["../src/store-manager-helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAChE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAIjD,eAAO,MAAM,UAAU,GAAI,CAAC,SAAS,MAAM,EAAE,gEAQ1C;IACD;;OAEG;IACH,QAAQ,EAAE,QAAQ,CAAA;IAClB;;OAEG;IACH,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,CAAA;IACvB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC,CAAA;IACnB;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,EAAE,EAAE,MAAM,CAAA;IACV;;OAEG;IACH,UAAU,EAAE,MAAM,CAAA;IAElB;;OAEG;IACH,OAAO,CAAC,EAAE,kBAAkB,CAAA;CAC7B,SAUA,CAAA"}
@@ -1,15 +0,0 @@
1
- import { addStore } from '@furystack/core';
2
- import { MongoClientFactory } from './mongo-client-factory.js';
3
- import { MongodbStore } from './mongodb-store.js';
4
- export const useMongoDb = ({ injector, model, primaryKey, db, collection, url, options, }) => {
5
- const clientFactory = injector.getInstance(MongoClientFactory);
6
- const store = new MongodbStore({
7
- model,
8
- primaryKey,
9
- db,
10
- collection,
11
- mongoClient: () => clientFactory.getClientFor(url, options),
12
- });
13
- addStore(injector, store);
14
- };
15
- //# sourceMappingURL=store-manager-helpers.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"store-manager-helpers.js","sourceRoot":"","sources":["../src/store-manager-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAG1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAEjD,MAAM,CAAC,MAAM,UAAU,GAAG,CAAmB,EAC3C,QAAQ,EACR,KAAK,EACL,UAAU,EACV,EAAE,EACF,UAAU,EACV,GAAG,EACH,OAAO,GA+BR,EAAE,EAAE;IACH,MAAM,aAAa,GAAG,QAAQ,CAAC,WAAW,CAAC,kBAAkB,CAAC,CAAA;IAC9D,MAAM,KAAK,GAAG,IAAI,YAAY,CAAC;QAC7B,KAAK;QACL,UAAU;QACV,EAAE;QACF,UAAU;QACV,WAAW,EAAE,GAAG,EAAE,CAAC,aAAa,CAAC,YAAY,CAAC,GAAG,EAAE,OAAO,CAAC;KAC5D,CAAC,CAAA;IACF,QAAQ,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;AAC3B,CAAC,CAAA"}
@@ -1,55 +0,0 @@
1
- import { addStore } from '@furystack/core'
2
- import type { Constructable, Injector } from '@furystack/inject'
3
- import type { MongoClientOptions } from 'mongodb'
4
- import { MongoClientFactory } from './mongo-client-factory.js'
5
- import { MongodbStore } from './mongodb-store.js'
6
-
7
- export const useMongoDb = <T extends object>({
8
- injector,
9
- model,
10
- primaryKey,
11
- db,
12
- collection,
13
- url,
14
- options,
15
- }: {
16
- /**
17
- * The Injector instance to use
18
- */
19
- injector: Injector
20
- /**
21
- * The constructable model class
22
- */
23
- model: Constructable<T>
24
- /**
25
- * The name of the Primary Key property
26
- */
27
- primaryKey: keyof T
28
- /**
29
- * Url of the MongoDb repository
30
- */
31
- url: string
32
- /**
33
- * MongoDb Database name
34
- */
35
- db: string
36
- /**
37
- * MongoDb Collection name
38
- */
39
- collection: string
40
-
41
- /**
42
- * Optional options for the MongoDb Client
43
- */
44
- options?: MongoClientOptions
45
- }) => {
46
- const clientFactory = injector.getInstance(MongoClientFactory)
47
- const store = new MongodbStore({
48
- model,
49
- primaryKey,
50
- db,
51
- collection,
52
- mongoClient: () => clientFactory.getClientFor(url, options),
53
- })
54
- addStore(injector, store)
55
- }