@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,18 +1,16 @@
1
- import { StoreManager } from '@furystack/core';
2
1
  import { TestClass, createStoreTest } from '@furystack/core/create-physical-store-tests';
3
- import { Injector } from '@furystack/inject';
2
+ import { createInjector } from '@furystack/inject';
4
3
  import { usingAsync } from '@furystack/utils';
5
- import { describe, expect, it, vi } from 'vitest';
4
+ import { describe, expect, it } from 'vitest';
5
+ import { defineMongoDbStore } from './define-mongodb-store.js';
6
6
  import { MongoClientFactory } from './mongo-client-factory.js';
7
- import { useMongoDb } from './store-manager-helpers.js';
8
7
  class TestClassWithId {
9
8
  }
10
9
  let storeCount = 0;
11
10
  const mongoDbUrl = process?.env?.MONGODB_URL || 'mongodb://localhost:27017';
12
11
  describe('MongoDB Store', () => {
13
12
  let dbIdx = 0;
14
- const getMongoOptions = (injector) => ({
15
- injector,
13
+ const getMongoOptions = () => ({
16
14
  model: TestClass,
17
15
  primaryKey: 'id',
18
16
  collection: `furystack-mongo-store-tests-${storeCount++}`,
@@ -24,12 +22,15 @@ describe('MongoDB Store', () => {
24
22
  typeName: 'mongodb-store',
25
23
  skipStringTests: true,
26
24
  createStore: (i) => {
27
- const mongoOptions = getMongoOptions(i);
28
- useMongoDb(mongoOptions);
29
- const store = i.getInstance(StoreManager).getStoreFor(TestClass, 'id');
25
+ const mongoOptions = getMongoOptions();
26
+ const token = defineMongoDbStore({
27
+ name: `mongodb-store/${mongoOptions.collection}`,
28
+ ...mongoOptions,
29
+ });
30
+ const store = i.get(token);
30
31
  const oldDispose = i[Symbol.asyncDispose];
31
32
  i[Symbol.asyncDispose] = async () => {
32
- const client = i.getInstance(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options);
33
+ const client = i.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options);
33
34
  const db = client.db(mongoOptions.db);
34
35
  await db.dropDatabase();
35
36
  await oldDispose.call(i);
@@ -38,19 +39,22 @@ describe('MongoDB Store', () => {
38
39
  },
39
40
  });
40
41
  const createTestClassWithIdStore = (injector) => {
41
- const mongoOptions = { ...getMongoOptions(injector), model: TestClassWithId, primaryKey: '_id' };
42
- useMongoDb(mongoOptions);
42
+ const mongoOptions = { ...getMongoOptions(), model: TestClassWithId, primaryKey: '_id' };
43
+ const token = defineMongoDbStore({
44
+ name: `mongodb-store/${mongoOptions.collection}`,
45
+ ...mongoOptions,
46
+ });
43
47
  const oldDispose = injector[Symbol.asyncDispose];
44
48
  injector[Symbol.asyncDispose] = async () => {
45
- const client = injector.getInstance(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options);
49
+ const client = injector.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options);
46
50
  const db = client.db(mongoOptions.db);
47
51
  await db.dropDatabase();
48
52
  await oldDispose.call(injector);
49
53
  };
50
- return injector.getInstance(StoreManager).getStoreFor(TestClassWithId, '_id');
54
+ return injector.get(token);
51
55
  };
52
56
  it('Should retrieve an entity with its id', async () => {
53
- await usingAsync(new Injector(), async (injector) => {
57
+ await usingAsync(createInjector(), async (injector) => {
54
58
  const store = createTestClassWithIdStore(injector);
55
59
  const { created } = await store.add({ value: 'value1' });
56
60
  expect(typeof created[0]._id).toBe('string');
@@ -59,7 +63,7 @@ describe('MongoDB Store', () => {
59
63
  });
60
64
  });
61
65
  it('Should retrieve more entities with theis ids', async () => {
62
- await usingAsync(new Injector(), async (injector) => {
66
+ await usingAsync(createInjector(), async (injector) => {
63
67
  const store = createTestClassWithIdStore(injector);
64
68
  const { created } = await store.add({ value: 'value1' }, { value: 'value2' }, { value: 'value3' });
65
69
  const retrieved = await store.find({ filter: { _id: { $in: created.map((c) => c._id) } } });
@@ -68,7 +72,7 @@ describe('MongoDB Store', () => {
68
72
  });
69
73
  });
70
74
  it('Should filter by _id as a string', async () => {
71
- await usingAsync(new Injector(), async (injector) => {
75
+ await usingAsync(createInjector(), async (injector) => {
72
76
  const store = createTestClassWithIdStore(injector);
73
77
  const { created } = await store.add({ value: 'value1' });
74
78
  const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } });
@@ -77,7 +81,7 @@ describe('MongoDB Store', () => {
77
81
  });
78
82
  });
79
83
  it('Should filter by _id.$eq as a string', async () => {
80
- await usingAsync(new Injector(), async (injector) => {
84
+ await usingAsync(createInjector(), async (injector) => {
81
85
  const store = createTestClassWithIdStore(injector);
82
86
  const { created } = await store.add({ value: 'value1' });
83
87
  const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } });
@@ -86,7 +90,7 @@ describe('MongoDB Store', () => {
86
90
  });
87
91
  });
88
92
  it('Should filter by _id.$in as array of strings', async () => {
89
- await usingAsync(new Injector(), async (injector) => {
93
+ await usingAsync(createInjector(), async (injector) => {
90
94
  const store = createTestClassWithIdStore(injector);
91
95
  const { created } = await store.add({ value: 'value1' }, { value: 'value2' });
92
96
  const ids = created.map((c) => c._id);
@@ -96,7 +100,7 @@ describe('MongoDB Store', () => {
96
100
  });
97
101
  });
98
102
  it('Should filter by _id.$nin as array of strings', async () => {
99
- await usingAsync(new Injector(), async (injector) => {
103
+ await usingAsync(createInjector(), async (injector) => {
100
104
  const store = createTestClassWithIdStore(injector);
101
105
  const { created } = await store.add({ value: 'value1' }, { value: 'value2' });
102
106
  const ids = created.map((c) => c._id);
@@ -106,7 +110,7 @@ describe('MongoDB Store', () => {
106
110
  });
107
111
  });
108
112
  it('Should filter by a non-_id field', async () => {
109
- await usingAsync(new Injector(), async (injector) => {
113
+ await usingAsync(createInjector(), async (injector) => {
110
114
  const store = createTestClassWithIdStore(injector);
111
115
  await store.add({ value: 'value1' }, { value: 'value2' });
112
116
  const retrieved = await store.find({ filter: { value: { $eq: 'value2' } } });
@@ -115,32 +119,4 @@ describe('MongoDB Store', () => {
115
119
  });
116
120
  });
117
121
  });
118
- describe('MongoClientFactory EventHub', () => {
119
- it('should emit onClientCreated when a new client is created', async () => {
120
- const factory = new MongoClientFactory();
121
- try {
122
- const handler = vi.fn();
123
- factory.addListener('onClientCreated', handler);
124
- factory.getClientFor('mongodb://localhost:27017/test-db');
125
- expect(handler).toHaveBeenCalledTimes(1);
126
- expect(handler).toHaveBeenCalledWith({ url: 'mongodb://localhost:27017/test-db' });
127
- }
128
- finally {
129
- await factory[Symbol.asyncDispose]();
130
- }
131
- });
132
- it('should not emit onClientCreated when returning an existing client', async () => {
133
- const factory = new MongoClientFactory();
134
- try {
135
- const handler = vi.fn();
136
- factory.getClientFor('mongodb://localhost:27017/test-db');
137
- factory.addListener('onClientCreated', handler);
138
- factory.getClientFor('mongodb://localhost:27017/test-db');
139
- expect(handler).not.toHaveBeenCalled();
140
- }
141
- finally {
142
- await factory[Symbol.asyncDispose]();
143
- }
144
- });
145
- });
146
122
  //# sourceMappingURL=mongodb-store.spec.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mongodb-store.spec.js","sourceRoot":"","sources":["../src/mongodb-store.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,6CAA6C,CAAA;AACxF,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAA;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AAEvD,MAAM,eAAe;CAGpB;AAED,IAAI,UAAU,GAAG,CAAC,CAAA;AAElB,MAAM,UAAU,GAAG,OAAO,EAAE,GAAG,EAAE,WAAW,IAAI,2BAA2B,CAAA;AAE3E,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,IAAI,KAAK,GAAG,CAAC,CAAA;IAEb,MAAM,eAAe,GAAG,CAAC,QAAkB,EAAE,EAAE,CAAC,CAAC;QAC/C,QAAQ;QACR,KAAK,EAAE,SAAS;QAChB,UAAU,EAAE,IAAa;QACzB,UAAU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AAI,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAA;YAC3E,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;YAChC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAA;QAC1C,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,kCAAkC,EAAE,KAAK,IAAI,EAAE;QAChD,MAAM,UAAU,CAAC,cAAc,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE;YACpD,MAAM,KAAK,GAAG,0BAA0B,CAAC,QAAQ,CAAC,CAAA;YAClD,MAAM,KAAK,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAA;YACzD,MAAM,SAAS,GAAG,MAAM,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,CAAA;YAC5E,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;YAChC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAC3C,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@furystack/mongodb-store",
3
- "version": "10.1.7",
3
+ "version": "11.0.1",
4
4
  "description": "MongoDB Store implementation for FuryStack",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -38,15 +38,15 @@
38
38
  },
39
39
  "homepage": "https://github.com/furystack/furystack",
40
40
  "dependencies": {
41
- "@furystack/core": "^16.0.4",
42
- "@furystack/inject": "^12.0.36",
43
- "@furystack/utils": "^8.2.5",
44
- "mongodb": "7.1.1"
41
+ "@furystack/core": "^17.1.0",
42
+ "@furystack/inject": "^13.0.1",
43
+ "@furystack/utils": "^9.0.1",
44
+ "mongodb": "7.2.0"
45
45
  },
46
46
  "devDependencies": {
47
- "@types/node": "^25.6.0",
47
+ "@types/node": "^25.9.1",
48
48
  "typescript": "^6.0.3",
49
- "vitest": "^4.1.4"
49
+ "vitest": "^4.1.7"
50
50
  },
51
51
  "engines": {
52
52
  "node": ">=22.0.0"
@@ -0,0 +1,108 @@
1
+ import { createInjector } from '@furystack/inject'
2
+ import type { MongoClient } from 'mongodb'
3
+ import { describe, expect, it, vi } from 'vitest'
4
+ import { defineMongoDbStore } from './define-mongodb-store.js'
5
+ import { MongoClientFactory } from './mongo-client-factory.js'
6
+ import { MongodbStore } from './mongodb-store.js'
7
+
8
+ class Item {
9
+ declare id: string
10
+ declare value: string
11
+ }
12
+
13
+ describe('defineMongoDbStore (metadata)', () => {
14
+ it('propagates name, lifetime, model and primaryKey onto the returned token', () => {
15
+ const token = defineMongoDbStore<Item, 'id'>({
16
+ name: 'test/MongoItems',
17
+ model: Item,
18
+ primaryKey: 'id',
19
+ url: 'mongodb://localhost:27017',
20
+ db: 'unit-test',
21
+ collection: 'items',
22
+ })
23
+ expect(token.name).toBe('test/MongoItems')
24
+ expect(token.lifetime).toBe('singleton')
25
+ expect(token.model).toBe(Item)
26
+ expect(token.primaryKey).toBe('id')
27
+ })
28
+
29
+ it('mints a distinct token identity per call even when options match -- declare once at module scope', () => {
30
+ const options = {
31
+ name: 'test/MongoDupe',
32
+ model: Item,
33
+ primaryKey: 'id' as const,
34
+ url: 'mongodb://localhost:27017',
35
+ db: 'unit-test',
36
+ collection: 'items',
37
+ }
38
+ const a = defineMongoDbStore<Item, 'id'>(options)
39
+ const b = defineMongoDbStore<Item, 'id'>(options)
40
+ expect(a.id).not.toBe(b.id)
41
+ })
42
+ })
43
+
44
+ describe('defineMongoDbStore (factory + disposal)', () => {
45
+ const buildClientStub = () => {
46
+ const createIndex = vi.fn<() => Promise<string>>(() => Promise.resolve('id_1'))
47
+ const collection = vi.fn(() => ({ createIndex }))
48
+ const db = vi.fn(() => ({ collection }))
49
+ const client = { db } as unknown as MongoClient
50
+ return { client, db, collection, createIndex }
51
+ }
52
+
53
+ it('instantiates a MongodbStore whose lazy client pulls from the injected MongoClientFactory with the right url', async () => {
54
+ const { client, db, collection, createIndex } = buildClientStub()
55
+ const getClientFor = vi.fn(() => client)
56
+
57
+ const Token = defineMongoDbStore<Item, 'id'>({
58
+ name: 'test/MongoFactory',
59
+ model: Item,
60
+ primaryKey: 'id',
61
+ url: 'mongodb://stub-host:1234',
62
+ db: 'unit-db',
63
+ collection: 'unit-coll',
64
+ options: { appName: 'spec' },
65
+ })
66
+
67
+ const injector = createInjector()
68
+ injector.bind(MongoClientFactory, () => ({ getClientFor }))
69
+
70
+ try {
71
+ const store = injector.get(Token) as MongodbStore<Item, 'id'>
72
+ expect(store).toBeInstanceOf(MongodbStore)
73
+ expect(store.model).toBe(Item)
74
+ expect(store.primaryKey).toBe('id')
75
+ expect(getClientFor).not.toHaveBeenCalled()
76
+
77
+ await store.getCollection()
78
+
79
+ expect(getClientFor).toHaveBeenCalledTimes(1)
80
+ expect(getClientFor).toHaveBeenCalledWith('mongodb://stub-host:1234', { appName: 'spec' })
81
+ expect(db).toHaveBeenCalledWith('unit-db')
82
+ expect(collection).toHaveBeenCalledWith('unit-coll')
83
+ expect(createIndex).toHaveBeenCalledWith({ id: 1 }, { unique: true })
84
+ } finally {
85
+ await injector[Symbol.asyncDispose]()
86
+ }
87
+ })
88
+
89
+ it('disposing the injector tears down the bound MongoClientFactory', async () => {
90
+ const { client } = buildClientStub()
91
+ const getClientFor = vi.fn(() => client)
92
+
93
+ const Token = defineMongoDbStore<Item, 'id'>({
94
+ name: 'test/MongoDispose',
95
+ model: Item,
96
+ primaryKey: 'id',
97
+ url: 'mongodb://stub-host:1234',
98
+ db: 'unit-db',
99
+ collection: 'unit-coll',
100
+ })
101
+
102
+ const injector = createInjector()
103
+ injector.bind(MongoClientFactory, () => ({ getClientFor }))
104
+
105
+ injector.get(Token)
106
+ await expect(injector[Symbol.asyncDispose]()).resolves.toBeUndefined()
107
+ })
108
+ })
@@ -0,0 +1,58 @@
1
+ import type { Constructable, StoreToken } from '@furystack/core'
2
+ import { defineStore } from '@furystack/core'
3
+ import type { MongoClient, MongoClientOptions } from 'mongodb'
4
+ import { MongoClientFactory } from './mongo-client-factory.js'
5
+ import { MongodbStore } from './mongodb-store.js'
6
+
7
+ /**
8
+ * Options accepted by {@link defineMongoDbStore}.
9
+ */
10
+ export type DefineMongoDbStoreOptions<T extends object, TPrimaryKey extends keyof T> = {
11
+ name: string
12
+ model: Constructable<T>
13
+ primaryKey: TPrimaryKey
14
+ /** MongoDB deployment URL (e.g. `mongodb://localhost:27017`) — also acts as the pool key. */
15
+ url: string
16
+ db: string
17
+ collection: string
18
+ options?: MongoClientOptions
19
+ }
20
+
21
+ /**
22
+ * Mints a singleton {@link StoreToken} backed by a {@link MongodbStore}.
23
+ *
24
+ * Internally resolves the singleton {@link MongoClientFactory} so every store
25
+ * declared against the same `url` shares one {@link MongoClient}. Declare the
26
+ * token once at module scope — inline calls mint new identities and defeat
27
+ * singleton caching.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * export const UserStore = defineMongoDbStore({
32
+ * name: 'my-app/UserStore',
33
+ * model: User,
34
+ * primaryKey: 'username',
35
+ * url: 'mongodb://localhost:27017',
36
+ * db: 'my-app',
37
+ * collection: 'users',
38
+ * })
39
+ * ```
40
+ */
41
+ export const defineMongoDbStore = <T extends object, const TPrimaryKey extends keyof T>(
42
+ options: DefineMongoDbStoreOptions<T, TPrimaryKey>,
43
+ ): StoreToken<T, TPrimaryKey> =>
44
+ defineStore<T, TPrimaryKey>({
45
+ name: options.name,
46
+ model: options.model,
47
+ primaryKey: options.primaryKey,
48
+ factory: ({ inject }) => {
49
+ const factory = inject(MongoClientFactory)
50
+ return new MongodbStore<T, TPrimaryKey>({
51
+ model: options.model,
52
+ primaryKey: options.primaryKey,
53
+ db: options.db,
54
+ collection: options.collection,
55
+ mongoClient: () => factory.getClientFor(options.url, options.options),
56
+ })
57
+ },
58
+ })
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export * from './define-mongodb-store.js'
2
+ export * from './mongo-client-factory.js'
1
3
  export * from './mongodb-store.js'
2
- export * from './store-manager-helpers.js'
@@ -1,40 +1,45 @@
1
- import { Injectable } from '@furystack/inject'
2
- import { EventHub, type ListenerErrorPayload } from '@furystack/utils'
1
+ import type { Token } from '@furystack/inject'
2
+ import { defineService } from '@furystack/inject'
3
3
  import type { MongoClientOptions } from 'mongodb'
4
4
  import { MongoClient } from 'mongodb'
5
5
 
6
+ import type { defineMongoDbStore } from './define-mongodb-store.js'
7
+
6
8
  /**
7
- * Events emitted by the {@link MongoClientFactory}
9
+ * Pools {@link MongoClient} instances by URL so every store that targets the
10
+ * same MongoDB deployment shares a single connection.
11
+ *
12
+ * Application code rarely calls this directly — {@link defineMongoDbStore}
13
+ * resolves the factory token internally. Exported for tests and advanced
14
+ * integrations that need raw client access (e.g. dropping a test database).
8
15
  */
9
- export type MongoClientFactoryEvents = {
10
- onClientCreated: { url: string }
11
- onDisposed: undefined
12
- onListenerError: ListenerErrorPayload
16
+ export interface MongoClientFactory {
17
+ getClientFor(url: string, options?: MongoClientOptions): MongoClient
13
18
  }
14
19
 
15
20
  /**
16
- * Factory for instantiating MongoDb clients
21
+ * Singleton token for the {@link MongoClientFactory}. Closes every pooled
22
+ * client when the owning injector is disposed.
17
23
  */
18
- @Injectable({ lifetime: 'singleton' })
19
- export class MongoClientFactory extends EventHub<MongoClientFactoryEvents> implements AsyncDisposable {
20
- private connections: Map<string, MongoClient> = new Map()
21
-
22
- public async [Symbol.asyncDispose]() {
23
- await Promise.all([...this.connections.values()].map((c) => c.close(true)))
24
- this.connections.clear()
25
- this.emit('onDisposed', undefined)
26
- super[Symbol.dispose]()
27
- }
28
-
29
- public getClientFor(url: string, options?: MongoClientOptions) {
30
- const existing = this.connections.get(url)
31
- if (existing) {
32
- return existing
24
+ export const MongoClientFactory: Token<MongoClientFactory, 'singleton'> = defineService({
25
+ name: '@furystack/mongodb-store/MongoClientFactory',
26
+ lifetime: 'singleton',
27
+ factory: ({ onDispose }) => {
28
+ const connections = new Map<string, MongoClient>()
29
+ onDispose(async () => {
30
+ await Promise.all([...connections.values()].map((c) => c.close(true)))
31
+ connections.clear()
32
+ })
33
+ return {
34
+ getClientFor(url: string, options?: MongoClientOptions): MongoClient {
35
+ const existing = connections.get(url)
36
+ if (existing) {
37
+ return existing
38
+ }
39
+ const client = new MongoClient(url, options)
40
+ connections.set(url, client)
41
+ return client
42
+ },
33
43
  }
34
-
35
- const client = new MongoClient(url, options)
36
- this.connections.set(url, client)
37
- this.emit('onClientCreated', { url })
38
- return client
39
- }
40
- }
44
+ },
45
+ })
@@ -1,10 +1,10 @@
1
- import { StoreManager } from '@furystack/core'
2
1
  import { TestClass, createStoreTest } from '@furystack/core/create-physical-store-tests'
3
- import { Injector } from '@furystack/inject'
2
+ import type { Injector } from '@furystack/inject'
3
+ import { createInjector } from '@furystack/inject'
4
4
  import { usingAsync } from '@furystack/utils'
5
- import { describe, expect, it, vi } from 'vitest'
5
+ import { describe, expect, it } from 'vitest'
6
+ import { defineMongoDbStore } from './define-mongodb-store.js'
6
7
  import { MongoClientFactory } from './mongo-client-factory.js'
7
- import { useMongoDb } from './store-manager-helpers.js'
8
8
 
9
9
  class TestClassWithId {
10
10
  declare _id: string
@@ -18,8 +18,7 @@ const mongoDbUrl = process?.env?.MONGODB_URL || 'mongodb://localhost:27017'
18
18
  describe('MongoDB Store', () => {
19
19
  let dbIdx = 0
20
20
 
21
- const getMongoOptions = (injector: Injector) => ({
22
- injector,
21
+ const getMongoOptions = () => ({
23
22
  model: TestClass,
24
23
  primaryKey: 'id' as const,
25
24
  collection: `furystack-mongo-store-tests-${storeCount++}`,
@@ -27,16 +26,20 @@ describe('MongoDB Store', () => {
27
26
  url: mongoDbUrl,
28
27
  options: {},
29
28
  })
29
+
30
30
  createStoreTest({
31
31
  typeName: 'mongodb-store',
32
32
  skipStringTests: true,
33
33
  createStore: (i) => {
34
- const mongoOptions = getMongoOptions(i)
35
- useMongoDb(mongoOptions)
36
- const store = i.getInstance(StoreManager).getStoreFor(TestClass, 'id')
34
+ const mongoOptions = getMongoOptions()
35
+ const token = defineMongoDbStore<TestClass, 'id'>({
36
+ name: `mongodb-store/${mongoOptions.collection}`,
37
+ ...mongoOptions,
38
+ })
39
+ const store = i.get(token)
37
40
  const oldDispose = i[Symbol.asyncDispose]
38
41
  i[Symbol.asyncDispose] = async () => {
39
- const client = i.getInstance(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
42
+ const client = i.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
40
43
  const db = client.db(mongoOptions.db)
41
44
  await db.dropDatabase()
42
45
  await oldDispose.call(i)
@@ -46,20 +49,23 @@ describe('MongoDB Store', () => {
46
49
  })
47
50
 
48
51
  const createTestClassWithIdStore = (injector: Injector) => {
49
- const mongoOptions = { ...getMongoOptions(injector), model: TestClassWithId, primaryKey: '_id' as const }
50
- useMongoDb(mongoOptions)
52
+ const mongoOptions = { ...getMongoOptions(), model: TestClassWithId, primaryKey: '_id' as const }
53
+ const token = defineMongoDbStore<TestClassWithId, '_id'>({
54
+ name: `mongodb-store/${mongoOptions.collection}`,
55
+ ...mongoOptions,
56
+ })
51
57
  const oldDispose = injector[Symbol.asyncDispose]
52
58
  injector[Symbol.asyncDispose] = async () => {
53
- const client = injector.getInstance(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
59
+ const client = injector.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
54
60
  const db = client.db(mongoOptions.db)
55
61
  await db.dropDatabase()
56
62
  await oldDispose.call(injector)
57
63
  }
58
- return injector.getInstance(StoreManager).getStoreFor(TestClassWithId, '_id')
64
+ return injector.get(token)
59
65
  }
60
66
 
61
67
  it('Should retrieve an entity with its id', async () => {
62
- await usingAsync(new Injector(), async (injector) => {
68
+ await usingAsync(createInjector(), async (injector) => {
63
69
  const store = createTestClassWithIdStore(injector)
64
70
  const { created } = await store.add({ value: 'value1' })
65
71
 
@@ -71,7 +77,7 @@ describe('MongoDB Store', () => {
71
77
  })
72
78
 
73
79
  it('Should retrieve more entities with theis ids', async () => {
74
- await usingAsync(new Injector(), async (injector) => {
80
+ await usingAsync(createInjector(), async (injector) => {
75
81
  const store = createTestClassWithIdStore(injector)
76
82
  const { created } = await store.add({ value: 'value1' }, { value: 'value2' }, { value: 'value3' })
77
83
  const retrieved = await store.find({ filter: { _id: { $in: created.map((c) => c._id) } } })
@@ -81,7 +87,7 @@ describe('MongoDB Store', () => {
81
87
  })
82
88
 
83
89
  it('Should filter by _id as a string', async () => {
84
- await usingAsync(new Injector(), async (injector) => {
90
+ await usingAsync(createInjector(), async (injector) => {
85
91
  const store = createTestClassWithIdStore(injector)
86
92
  const { created } = await store.add({ value: 'value1' })
87
93
  const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } })
@@ -91,7 +97,7 @@ describe('MongoDB Store', () => {
91
97
  })
92
98
 
93
99
  it('Should filter by _id.$eq as a string', async () => {
94
- await usingAsync(new Injector(), async (injector) => {
100
+ await usingAsync(createInjector(), async (injector) => {
95
101
  const store = createTestClassWithIdStore(injector)
96
102
  const { created } = await store.add({ value: 'value1' })
97
103
  const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } })
@@ -101,7 +107,7 @@ describe('MongoDB Store', () => {
101
107
  })
102
108
 
103
109
  it('Should filter by _id.$in as array of strings', async () => {
104
- await usingAsync(new Injector(), async (injector) => {
110
+ await usingAsync(createInjector(), async (injector) => {
105
111
  const store = createTestClassWithIdStore(injector)
106
112
  const { created } = await store.add({ value: 'value1' }, { value: 'value2' })
107
113
  const ids = created.map((c) => c._id)
@@ -112,7 +118,7 @@ describe('MongoDB Store', () => {
112
118
  })
113
119
 
114
120
  it('Should filter by _id.$nin as array of strings', async () => {
115
- await usingAsync(new Injector(), async (injector) => {
121
+ await usingAsync(createInjector(), async (injector) => {
116
122
  const store = createTestClassWithIdStore(injector)
117
123
  const { created } = await store.add({ value: 'value1' }, { value: 'value2' })
118
124
  const ids = created.map((c) => c._id)
@@ -123,7 +129,7 @@ describe('MongoDB Store', () => {
123
129
  })
124
130
 
125
131
  it('Should filter by a non-_id field', async () => {
126
- await usingAsync(new Injector(), async (injector) => {
132
+ await usingAsync(createInjector(), async (injector) => {
127
133
  const store = createTestClassWithIdStore(injector)
128
134
  await store.add({ value: 'value1' }, { value: 'value2' })
129
135
  const retrieved = await store.find({ filter: { value: { $eq: 'value2' } } })
@@ -132,35 +138,3 @@ describe('MongoDB Store', () => {
132
138
  })
133
139
  })
134
140
  })
135
-
136
- describe('MongoClientFactory EventHub', () => {
137
- it('should emit onClientCreated when a new client is created', async () => {
138
- const factory = new MongoClientFactory()
139
- try {
140
- const handler = vi.fn()
141
- factory.addListener('onClientCreated', handler)
142
-
143
- factory.getClientFor('mongodb://localhost:27017/test-db')
144
-
145
- expect(handler).toHaveBeenCalledTimes(1)
146
- expect(handler).toHaveBeenCalledWith({ url: 'mongodb://localhost:27017/test-db' })
147
- } finally {
148
- await factory[Symbol.asyncDispose]()
149
- }
150
- })
151
-
152
- it('should not emit onClientCreated when returning an existing client', async () => {
153
- const factory = new MongoClientFactory()
154
- try {
155
- const handler = vi.fn()
156
-
157
- factory.getClientFor('mongodb://localhost:27017/test-db')
158
- factory.addListener('onClientCreated', handler)
159
- factory.getClientFor('mongodb://localhost:27017/test-db')
160
-
161
- expect(handler).not.toHaveBeenCalled()
162
- } finally {
163
- await factory[Symbol.asyncDispose]()
164
- }
165
- })
166
- })