@furystack/mongodb-store 11.0.3 → 11.0.5
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.
- package/CHANGELOG.md +215 -199
- package/LICENSE +339 -339
- package/README.md +51 -51
- package/esm/store-manager-helpers.d.ts +33 -0
- package/esm/store-manager-helpers.d.ts.map +1 -0
- package/esm/store-manager-helpers.js +15 -0
- package/esm/store-manager-helpers.js.map +1 -0
- package/package.json +7 -7
- package/src/define-mongodb-store.spec.ts +108 -108
- package/src/define-mongodb-store.ts +58 -58
- package/src/index.ts +3 -3
- package/src/mongo-client-factory.ts +45 -45
- package/src/mongodb-store.spec.ts +140 -140
- package/src/mongodb-store.ts +207 -207
|
@@ -1,140 +1,140 @@
|
|
|
1
|
-
import { TestClass, createStoreTest } from '@furystack/core/create-physical-store-tests'
|
|
2
|
-
import type { Injector } from '@furystack/inject'
|
|
3
|
-
import { createInjector } from '@furystack/inject'
|
|
4
|
-
import { usingAsync } from '@furystack/utils'
|
|
5
|
-
import { describe, expect, it } from 'vitest'
|
|
6
|
-
import { defineMongoDbStore } from './define-mongodb-store.js'
|
|
7
|
-
import { MongoClientFactory } from './mongo-client-factory.js'
|
|
8
|
-
|
|
9
|
-
class TestClassWithId {
|
|
10
|
-
declare _id: string
|
|
11
|
-
declare value: string
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
let storeCount = 0
|
|
15
|
-
|
|
16
|
-
const mongoDbUrl = process?.env?.MONGODB_URL || 'mongodb://localhost:27017'
|
|
17
|
-
|
|
18
|
-
describe('MongoDB Store', () => {
|
|
19
|
-
let dbIdx = 0
|
|
20
|
-
|
|
21
|
-
const getMongoOptions = () => ({
|
|
22
|
-
model: TestClass,
|
|
23
|
-
primaryKey: 'id' as const,
|
|
24
|
-
collection: `furystack-mongo-store-tests-${storeCount++}`,
|
|
25
|
-
db: `furystack-mongo-store-tests-${dbIdx++}`,
|
|
26
|
-
url: mongoDbUrl,
|
|
27
|
-
options: {},
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
createStoreTest({
|
|
31
|
-
typeName: 'mongodb-store',
|
|
32
|
-
skipStringTests: true,
|
|
33
|
-
createStore: (i) => {
|
|
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)
|
|
40
|
-
const oldDispose = i[Symbol.asyncDispose]
|
|
41
|
-
i[Symbol.asyncDispose] = async () => {
|
|
42
|
-
const client = i.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
|
|
43
|
-
const db = client.db(mongoOptions.db)
|
|
44
|
-
await db.dropDatabase()
|
|
45
|
-
await oldDispose.call(i)
|
|
46
|
-
}
|
|
47
|
-
return store
|
|
48
|
-
},
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
const createTestClassWithIdStore = (injector: Injector) => {
|
|
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
|
-
})
|
|
57
|
-
const oldDispose = injector[Symbol.asyncDispose]
|
|
58
|
-
injector[Symbol.asyncDispose] = async () => {
|
|
59
|
-
const client = injector.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
|
|
60
|
-
const db = client.db(mongoOptions.db)
|
|
61
|
-
await db.dropDatabase()
|
|
62
|
-
await oldDispose.call(injector)
|
|
63
|
-
}
|
|
64
|
-
return injector.get(token)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
it('Should retrieve an entity with its id', async () => {
|
|
68
|
-
await usingAsync(createInjector(), async (injector) => {
|
|
69
|
-
const store = createTestClassWithIdStore(injector)
|
|
70
|
-
const { created } = await store.add({ value: 'value1' })
|
|
71
|
-
|
|
72
|
-
expect(typeof created[0]._id).toBe('string')
|
|
73
|
-
|
|
74
|
-
const retrieved = await store.get(created[0]._id)
|
|
75
|
-
expect(retrieved).toEqual(created[0])
|
|
76
|
-
})
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
it('Should retrieve more entities with theis ids', async () => {
|
|
80
|
-
await usingAsync(createInjector(), async (injector) => {
|
|
81
|
-
const store = createTestClassWithIdStore(injector)
|
|
82
|
-
const { created } = await store.add({ value: 'value1' }, { value: 'value2' }, { value: 'value3' })
|
|
83
|
-
const retrieved = await store.find({ filter: { _id: { $in: created.map((c) => c._id) } } })
|
|
84
|
-
expect(retrieved.length).toBe(3)
|
|
85
|
-
expect(retrieved).toEqual(created)
|
|
86
|
-
})
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
it('Should filter by _id as a string', async () => {
|
|
90
|
-
await usingAsync(createInjector(), async (injector) => {
|
|
91
|
-
const store = createTestClassWithIdStore(injector)
|
|
92
|
-
const { created } = await store.add({ value: 'value1' })
|
|
93
|
-
const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } })
|
|
94
|
-
expect(retrieved.length).toBe(1)
|
|
95
|
-
expect(retrieved[0]).toEqual(created[0])
|
|
96
|
-
})
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('Should filter by _id.$eq as a string', async () => {
|
|
100
|
-
await usingAsync(createInjector(), async (injector) => {
|
|
101
|
-
const store = createTestClassWithIdStore(injector)
|
|
102
|
-
const { created } = await store.add({ value: 'value1' })
|
|
103
|
-
const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } })
|
|
104
|
-
expect(retrieved.length).toBe(1)
|
|
105
|
-
expect(retrieved[0]).toEqual(created[0])
|
|
106
|
-
})
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
it('Should filter by _id.$in as array of strings', async () => {
|
|
110
|
-
await usingAsync(createInjector(), async (injector) => {
|
|
111
|
-
const store = createTestClassWithIdStore(injector)
|
|
112
|
-
const { created } = await store.add({ value: 'value1' }, { value: 'value2' })
|
|
113
|
-
const ids = created.map((c) => c._id)
|
|
114
|
-
const retrieved = await store.find({ filter: { _id: { $in: ids } } })
|
|
115
|
-
expect(retrieved.length).toBe(2)
|
|
116
|
-
expect(retrieved).toEqual(created)
|
|
117
|
-
})
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
it('Should filter by _id.$nin as array of strings', async () => {
|
|
121
|
-
await usingAsync(createInjector(), async (injector) => {
|
|
122
|
-
const store = createTestClassWithIdStore(injector)
|
|
123
|
-
const { created } = await store.add({ value: 'value1' }, { value: 'value2' })
|
|
124
|
-
const ids = created.map((c) => c._id)
|
|
125
|
-
const retrieved = await store.find({ filter: { _id: { $nin: [ids[0]] } } })
|
|
126
|
-
expect(retrieved.length).toBe(1)
|
|
127
|
-
expect(retrieved[0]).toEqual(created[1])
|
|
128
|
-
})
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
it('Should filter by a non-_id field', async () => {
|
|
132
|
-
await usingAsync(createInjector(), async (injector) => {
|
|
133
|
-
const store = createTestClassWithIdStore(injector)
|
|
134
|
-
await store.add({ value: 'value1' }, { value: 'value2' })
|
|
135
|
-
const retrieved = await store.find({ filter: { value: { $eq: 'value2' } } })
|
|
136
|
-
expect(retrieved.length).toBe(1)
|
|
137
|
-
expect(retrieved[0].value).toBe('value2')
|
|
138
|
-
})
|
|
139
|
-
})
|
|
140
|
-
})
|
|
1
|
+
import { TestClass, createStoreTest } from '@furystack/core/create-physical-store-tests'
|
|
2
|
+
import type { Injector } from '@furystack/inject'
|
|
3
|
+
import { createInjector } from '@furystack/inject'
|
|
4
|
+
import { usingAsync } from '@furystack/utils'
|
|
5
|
+
import { describe, expect, it } from 'vitest'
|
|
6
|
+
import { defineMongoDbStore } from './define-mongodb-store.js'
|
|
7
|
+
import { MongoClientFactory } from './mongo-client-factory.js'
|
|
8
|
+
|
|
9
|
+
class TestClassWithId {
|
|
10
|
+
declare _id: string
|
|
11
|
+
declare value: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let storeCount = 0
|
|
15
|
+
|
|
16
|
+
const mongoDbUrl = process?.env?.MONGODB_URL || 'mongodb://localhost:27017'
|
|
17
|
+
|
|
18
|
+
describe('MongoDB Store', () => {
|
|
19
|
+
let dbIdx = 0
|
|
20
|
+
|
|
21
|
+
const getMongoOptions = () => ({
|
|
22
|
+
model: TestClass,
|
|
23
|
+
primaryKey: 'id' as const,
|
|
24
|
+
collection: `furystack-mongo-store-tests-${storeCount++}`,
|
|
25
|
+
db: `furystack-mongo-store-tests-${dbIdx++}`,
|
|
26
|
+
url: mongoDbUrl,
|
|
27
|
+
options: {},
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
createStoreTest({
|
|
31
|
+
typeName: 'mongodb-store',
|
|
32
|
+
skipStringTests: true,
|
|
33
|
+
createStore: (i) => {
|
|
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)
|
|
40
|
+
const oldDispose = i[Symbol.asyncDispose]
|
|
41
|
+
i[Symbol.asyncDispose] = async () => {
|
|
42
|
+
const client = i.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
|
|
43
|
+
const db = client.db(mongoOptions.db)
|
|
44
|
+
await db.dropDatabase()
|
|
45
|
+
await oldDispose.call(i)
|
|
46
|
+
}
|
|
47
|
+
return store
|
|
48
|
+
},
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const createTestClassWithIdStore = (injector: Injector) => {
|
|
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
|
+
})
|
|
57
|
+
const oldDispose = injector[Symbol.asyncDispose]
|
|
58
|
+
injector[Symbol.asyncDispose] = async () => {
|
|
59
|
+
const client = injector.get(MongoClientFactory).getClientFor(mongoOptions.url, mongoOptions.options)
|
|
60
|
+
const db = client.db(mongoOptions.db)
|
|
61
|
+
await db.dropDatabase()
|
|
62
|
+
await oldDispose.call(injector)
|
|
63
|
+
}
|
|
64
|
+
return injector.get(token)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
it('Should retrieve an entity with its id', async () => {
|
|
68
|
+
await usingAsync(createInjector(), async (injector) => {
|
|
69
|
+
const store = createTestClassWithIdStore(injector)
|
|
70
|
+
const { created } = await store.add({ value: 'value1' })
|
|
71
|
+
|
|
72
|
+
expect(typeof created[0]._id).toBe('string')
|
|
73
|
+
|
|
74
|
+
const retrieved = await store.get(created[0]._id)
|
|
75
|
+
expect(retrieved).toEqual(created[0])
|
|
76
|
+
})
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('Should retrieve more entities with theis ids', async () => {
|
|
80
|
+
await usingAsync(createInjector(), async (injector) => {
|
|
81
|
+
const store = createTestClassWithIdStore(injector)
|
|
82
|
+
const { created } = await store.add({ value: 'value1' }, { value: 'value2' }, { value: 'value3' })
|
|
83
|
+
const retrieved = await store.find({ filter: { _id: { $in: created.map((c) => c._id) } } })
|
|
84
|
+
expect(retrieved.length).toBe(3)
|
|
85
|
+
expect(retrieved).toEqual(created)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('Should filter by _id as a string', async () => {
|
|
90
|
+
await usingAsync(createInjector(), async (injector) => {
|
|
91
|
+
const store = createTestClassWithIdStore(injector)
|
|
92
|
+
const { created } = await store.add({ value: 'value1' })
|
|
93
|
+
const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } })
|
|
94
|
+
expect(retrieved.length).toBe(1)
|
|
95
|
+
expect(retrieved[0]).toEqual(created[0])
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('Should filter by _id.$eq as a string', async () => {
|
|
100
|
+
await usingAsync(createInjector(), async (injector) => {
|
|
101
|
+
const store = createTestClassWithIdStore(injector)
|
|
102
|
+
const { created } = await store.add({ value: 'value1' })
|
|
103
|
+
const retrieved = await store.find({ filter: { _id: { $eq: created[0]._id } } })
|
|
104
|
+
expect(retrieved.length).toBe(1)
|
|
105
|
+
expect(retrieved[0]).toEqual(created[0])
|
|
106
|
+
})
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('Should filter by _id.$in as array of strings', async () => {
|
|
110
|
+
await usingAsync(createInjector(), async (injector) => {
|
|
111
|
+
const store = createTestClassWithIdStore(injector)
|
|
112
|
+
const { created } = await store.add({ value: 'value1' }, { value: 'value2' })
|
|
113
|
+
const ids = created.map((c) => c._id)
|
|
114
|
+
const retrieved = await store.find({ filter: { _id: { $in: ids } } })
|
|
115
|
+
expect(retrieved.length).toBe(2)
|
|
116
|
+
expect(retrieved).toEqual(created)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('Should filter by _id.$nin as array of strings', async () => {
|
|
121
|
+
await usingAsync(createInjector(), async (injector) => {
|
|
122
|
+
const store = createTestClassWithIdStore(injector)
|
|
123
|
+
const { created } = await store.add({ value: 'value1' }, { value: 'value2' })
|
|
124
|
+
const ids = created.map((c) => c._id)
|
|
125
|
+
const retrieved = await store.find({ filter: { _id: { $nin: [ids[0]] } } })
|
|
126
|
+
expect(retrieved.length).toBe(1)
|
|
127
|
+
expect(retrieved[0]).toEqual(created[1])
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('Should filter by a non-_id field', async () => {
|
|
132
|
+
await usingAsync(createInjector(), async (injector) => {
|
|
133
|
+
const store = createTestClassWithIdStore(injector)
|
|
134
|
+
await store.add({ value: 'value1' }, { value: 'value2' })
|
|
135
|
+
const retrieved = await store.find({ filter: { value: { $eq: 'value2' } } })
|
|
136
|
+
expect(retrieved.length).toBe(1)
|
|
137
|
+
expect(retrieved[0].value).toBe('value2')
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
})
|