@furystack/repository 5.0.34 → 6.0.2

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.
@@ -1 +1 @@
1
- {"version":3,"file":"repository.spec.js","sourceRoot":"","sources":["../src/repository.spec.ts"],"names":[],"mappings":";;AAAA,8CAA4C;AAC5C,4CAAwC;AACxC,gCAA6B;AAC7B,0CAA+C;AAC/C,yCAAoC;AAEpC,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,2BAA2B,EAAE,GAAG,EAAE;QACnC,IAAA,aAAK,EAAC,IAAI,iBAAQ,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE;YAC1B,MAAM,YAAY;aAEjB;YACD,CAAC,CAAC,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,oBAAa,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,eAAe,CAC9G,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,aAAa,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC,CAC/C,CAAA;YAED,MAAM,OAAO,GAAG,CAAC,CAAC,aAAa,CAAC,YAAY,EAAE,IAAI,CAAC,CAAA;YACnD,MAAM,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,kBAAO,CAAC,CAAA;QACzC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
1
+ {"version":3,"file":"repository.spec.js","sourceRoot":"","sources":["../src/repository.spec.ts"],"names":[],"mappings":";;AAAA,8CAA4C;AAC5C,4CAAwC;AACxC,uCAAwD;AACxD,0CAAyD;AACzD,yCAAoC;AAEpC,QAAQ,CAAC,YAAY,EAAE,GAAG,EAAE;IAC1B,EAAE,CAAC,2BAA2B,EAAE,GAAG,EAAE;QACnC,IAAA,aAAK,EAAC,IAAI,iBAAQ,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE;YAC1B,MAAM,YAAY;aAEjB;YACD,IAAA,eAAQ,EAAC,CAAC,EAAE,IAAI,oBAAa,CAAC,EAAE,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;YACzE,IAAA,uBAAa,EAAC,CAAC,CAAC,CAAC,aAAa,CAAC,YAAY,EAAE,IAAI,EAAE,EAAE,CAAC,CAAA;YAEtD,MAAM,OAAO,GAAG,IAAA,uBAAa,EAAC,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,CAAA;YACpD,MAAM,CAAC,OAAO,CAAC,CAAC,cAAc,CAAC,kBAAO,CAAC,CAAA;QACzC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@furystack/repository",
3
- "version": "5.0.34",
3
+ "version": "6.0.2",
4
4
  "description": "Repository implementation for FuryStack",
5
5
  "main": "dist/index.js",
6
6
  "files": [
7
7
  "dist",
8
+ "types",
8
9
  "src"
9
10
  ],
10
11
  "repository": {
@@ -28,15 +29,15 @@
28
29
  },
29
30
  "homepage": "https://github.com/furystack/furystack",
30
31
  "dependencies": {
31
- "@furystack/core": "^10.4.10",
32
- "@furystack/inject": "^5.0.7",
33
- "@furystack/logging": "^2.0.91",
34
- "@furystack/utils": "^2.1.0",
35
- "tslib": "^2.3.1"
32
+ "@furystack/core": "^11.0.2",
33
+ "@furystack/inject": "^6.0.2",
34
+ "@furystack/logging": "^3.0.2",
35
+ "@furystack/utils": "^3.0.2",
36
+ "tslib": "^2.4.0"
36
37
  },
37
38
  "devDependencies": {
38
- "@types/jest": "^27.4.1"
39
+ "@types/jest": "^27.5.0"
39
40
  },
40
- "typings": "./dist/index.d.ts",
41
+ "typings": "./types/index.d.ts",
41
42
  "gitHead": "1045d854bfd8c475b7035471d130d401417a2321"
42
43
  }
@@ -1,9 +1,9 @@
1
1
  import { Injector } from '@furystack/inject'
2
2
  import { using, usingAsync } from '@furystack/utils'
3
- import { InMemoryStore, WithOptionalId } from '@furystack/core'
3
+ import { InMemoryStore, WithOptionalId, addStore } from '@furystack/core'
4
4
  import { Repository } from './repository'
5
5
  import { AuthorizationResult, DataSetSettings } from './data-set-setting'
6
- import './injector-extension'
6
+ import { getDataSetFor, getRepository } from './helpers'
7
7
 
8
8
  class TestClass {
9
9
  public id = 1
@@ -14,37 +14,37 @@ describe('DataSet', () => {
14
14
  describe('Construction', () => {
15
15
  it('can be retrieved from an extension method with class', () => {
16
16
  using(new Injector(), (i) => {
17
- i.setupStores((stores) =>
18
- stores.addStore(
19
- new InMemoryStore({
20
- model: TestClass,
21
- primaryKey: 'id',
22
- }),
23
- ),
24
- ).setupRepository((r) => r.createDataSet(TestClass, 'id'))
25
- const dataSet = i.getDataSetFor(TestClass, 'id')
17
+ addStore(
18
+ i,
19
+ new InMemoryStore({
20
+ model: TestClass,
21
+ primaryKey: 'id',
22
+ }),
23
+ )
24
+ getRepository(i).createDataSet(TestClass, 'id')
25
+ const dataSet = getDataSetFor(i, TestClass, 'id')
26
26
  expect(dataSet.settings.physicalStore.model).toBe(TestClass)
27
27
  })
28
28
  })
29
29
 
30
30
  it('can be retrieved from an extension method with string', () => {
31
31
  using(new Injector(), (i) => {
32
- i.setupStores((stores) =>
33
- stores.addStore(
34
- new InMemoryStore({
35
- model: TestClass,
36
- primaryKey: 'id',
37
- }),
38
- ),
39
- ).setupRepository((r) => r.createDataSet(TestClass, 'id'))
40
- const dataSet = i.getDataSetFor(TestClass, 'id')
32
+ addStore(
33
+ i,
34
+ new InMemoryStore({
35
+ model: TestClass,
36
+ primaryKey: 'id',
37
+ }),
38
+ )
39
+ getRepository(i).createDataSet(TestClass, 'id')
40
+ const dataSet = getDataSetFor(i, TestClass, 'id')
41
41
  expect(dataSet.settings.physicalStore.model).toBe(TestClass)
42
42
  })
43
43
  })
44
44
 
45
45
  it('Should throw if dataset is not registered through extension', () => {
46
46
  using(new Injector(), (i) => {
47
- expect(() => i.getDataSetFor(TestClass, 'id')).toThrowError('')
47
+ expect(() => getDataSetFor(i, TestClass, 'id')).toThrowError('')
48
48
  })
49
49
  })
50
50
 
@@ -59,11 +59,11 @@ describe('DataSet', () => {
59
59
  describe('Add', () => {
60
60
  it('should add an entity if no settings are provided', async () => {
61
61
  await usingAsync(new Injector(), async (i) => {
62
- i.setupStores((stores) =>
63
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
64
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
62
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
63
+
64
+ getRepository(i).createDataSet(TestClass, 'id')
65
65
 
66
- const dataSet = i.getDataSetFor(TestClass, 'id')
66
+ const dataSet = getDataSetFor(i, TestClass, 'id')
67
67
  await dataSet.add(i, { id: 1, value: 'asd' })
68
68
  const result = await dataSet.get(i, 1)
69
69
  expect(result && result.value).toBe('asd')
@@ -73,10 +73,10 @@ describe('DataSet', () => {
73
73
  it('should call the add async authorizer and add the entity on pass', async () => {
74
74
  await usingAsync(new Injector(), async (i) => {
75
75
  const authorizeAdd = jest.fn(async () => ({ isAllowed: true } as AuthorizationResult))
76
- i.setupStores((stores) =>
77
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
78
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeAdd }))
79
- const dataSet = i.getDataSetFor(TestClass, 'id')
76
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
77
+
78
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeAdd })
79
+ const dataSet = getDataSetFor(i, TestClass, 'id')
80
80
  await dataSet.add(i, { id: 1, value: 'asd' })
81
81
  expect(authorizeAdd).toBeCalled()
82
82
  const added = await dataSet.get(i, 1)
@@ -87,11 +87,11 @@ describe('DataSet', () => {
87
87
  it('should throw if the add authorizer returns a non-valid result and should not add a value to the store', async () => {
88
88
  await usingAsync(new Injector(), async (i) => {
89
89
  const authorizeAdd = jest.fn(async () => ({ isAllowed: false, message: '...' } as AuthorizationResult))
90
- i.setupStores((stores) =>
91
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
92
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeAdd }))
90
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
91
+
92
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeAdd })
93
93
 
94
- const dataSet = await i.getDataSetFor(TestClass, 'id')
94
+ const dataSet = await getDataSetFor(i, TestClass, 'id')
95
95
 
96
96
  try {
97
97
  await dataSet.add(i, { id: 1, value: 'asd' })
@@ -114,11 +114,10 @@ describe('DataSet', () => {
114
114
  }),
115
115
  ) as any
116
116
 
117
- i.setupStores((stores) =>
118
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
119
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { modifyOnAdd }))
117
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
118
+ getRepository(i).createDataSet(TestClass, 'id', { modifyOnAdd })
120
119
 
121
- const dataSet = i.getDataSetFor(TestClass, 'id')
120
+ const dataSet = getDataSetFor(i, TestClass, 'id')
122
121
  await dataSet.add(i, { id: 1, value: 'asd' })
123
122
  const result = await dataSet.get(i, 1)
124
123
  expect(modifyOnAdd).toBeCalled()
@@ -129,15 +128,17 @@ describe('DataSet', () => {
129
128
  it('should call the onEntityAdded callback if an entity has been added', async () => {
130
129
  await usingAsync(new Injector(), async (i) => {
131
130
  expect.assertions(1)
132
- i.setupStores((stores) =>
133
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
134
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', {}))
131
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
135
132
 
136
- i.getDataSetFor(TestClass, 'id').onEntityAdded.subscribe(({ entity }) => {
137
- expect(entity.value).toBe('asd')
138
- })
133
+ getRepository(i).createDataSet(TestClass, 'id', {})
134
+
135
+ getRepository(i)
136
+ .getDataSetFor(TestClass, 'id')
137
+ .onEntityAdded.subscribe(({ entity }) => {
138
+ expect(entity.value).toBe('asd')
139
+ })
139
140
 
140
- const dataSet = i.getDataSetFor(TestClass, 'id')
141
+ const dataSet = getDataSetFor(i, TestClass, 'id')
141
142
  await dataSet.add(i, { id: 1, value: 'asd' })
142
143
  })
143
144
  })
@@ -146,11 +147,10 @@ describe('DataSet', () => {
146
147
  describe('Update', () => {
147
148
  it('should update an entity if no settings are provided', async () => {
148
149
  await usingAsync(new Injector(), async (i) => {
149
- i.setupStores((stores) =>
150
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
151
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
150
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
151
+ getRepository(i).createDataSet(TestClass, 'id')
152
152
 
153
- const dataSet = i.getDataSetFor(TestClass, 'id')
153
+ const dataSet = getDataSetFor(i, TestClass, 'id')
154
154
  await dataSet.add(i, { id: 1, value: 'asd' })
155
155
  await dataSet.update(i, 1, { id: 1, value: 'asd2' })
156
156
  const result = await dataSet.get(i, 1)
@@ -161,10 +161,9 @@ describe('DataSet', () => {
161
161
  it('should call the authorizeUpdate authorizer and add the entity on pass', async () => {
162
162
  await usingAsync(new Injector(), async (i) => {
163
163
  const authorizeUpdate = jest.fn(async () => ({ isAllowed: true } as AuthorizationResult))
164
- i.setupStores((stores) =>
165
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
166
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeUpdate }))
167
- const dataSet = i.getDataSetFor(TestClass, 'id')
164
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
165
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeUpdate })
166
+ const dataSet = getDataSetFor(i, TestClass, 'id')
168
167
  await dataSet.add(i, { id: 1, value: 'asd' })
169
168
  await dataSet.update(i, 1, { id: 1, value: 'asd2' })
170
169
  expect(authorizeUpdate).toBeCalled()
@@ -178,11 +177,10 @@ describe('DataSet', () => {
178
177
  const authorizeUpdateEntity = jest.fn(
179
178
  async () => ({ isAllowed: false, message: '...' } as AuthorizationResult),
180
179
  )
181
- i.setupStores((stores) =>
182
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
183
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeUpdateEntity }))
180
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
181
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeUpdateEntity })
184
182
 
185
- const dataSet = await i.getDataSetFor(TestClass, 'id')
183
+ const dataSet = await getDataSetFor(i, TestClass, 'id')
186
184
 
187
185
  try {
188
186
  await dataSet.add(i, { id: 1, value: 'asd' })
@@ -199,10 +197,9 @@ describe('DataSet', () => {
199
197
  it('should call the authorizeUpdateEntity authorizer and add the entity on pass', async () => {
200
198
  await usingAsync(new Injector(), async (i) => {
201
199
  const authorizeUpdateEntity = jest.fn(async () => ({ isAllowed: true } as AuthorizationResult))
202
- i.setupStores((stores) =>
203
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
204
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeUpdateEntity }))
205
- const dataSet = i.getDataSetFor(TestClass, 'id')
200
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
201
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeUpdateEntity })
202
+ const dataSet = getDataSetFor(i, TestClass, 'id')
206
203
  await dataSet.add(i, { id: 1, value: 'asd' })
207
204
  await dataSet.update(i, 1, { id: 1, value: 'asd2' })
208
205
  expect(authorizeUpdateEntity).toBeCalled()
@@ -214,11 +211,10 @@ describe('DataSet', () => {
214
211
  it('should throw if the authorizeUpdate returns a non-valid result and should not update a value to the store', async () => {
215
212
  await usingAsync(new Injector(), async (i) => {
216
213
  const authorizeUpdate = jest.fn(async () => ({ isAllowed: false, message: '...' } as AuthorizationResult))
217
- i.setupStores((stores) =>
218
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
219
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeUpdate }))
214
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
215
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeUpdate })
220
216
 
221
- const dataSet = await i.getDataSetFor(TestClass, 'id')
217
+ const dataSet = await getDataSetFor(i, TestClass, 'id')
222
218
 
223
219
  try {
224
220
  await dataSet.add(i, { id: 1, value: 'asd' })
@@ -240,11 +236,10 @@ describe('DataSet', () => {
240
236
  value: options.entity.value?.toUpperCase(),
241
237
  }))
242
238
 
243
- i.setupStores((stores) =>
244
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
245
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { modifyOnUpdate }))
239
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
240
+ getRepository(i).createDataSet(TestClass, 'id', { modifyOnUpdate })
246
241
 
247
- const dataSet = i.getDataSetFor(TestClass, 'id')
242
+ const dataSet = getDataSetFor(i, TestClass, 'id')
248
243
  await dataSet.add(i, { id: 1, value: 'asd' })
249
244
  await dataSet.update(i, 1, { id: 1, value: 'asd2' })
250
245
  const result = await dataSet.get(i, 1)
@@ -256,15 +251,16 @@ describe('DataSet', () => {
256
251
  it('should publish to the onEntityUpdated observable if an entity has been updated', async () => {
257
252
  await usingAsync(new Injector(), async (i) => {
258
253
  expect.assertions(1)
259
- i.setupStores((stores) =>
260
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
261
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
254
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
255
+ getRepository(i).createDataSet(TestClass, 'id')
262
256
 
263
- i.getDataSetFor(TestClass, 'id').onEntityUpdated.subscribe(({ change }) => {
264
- expect(change).toEqual({ id: 1, value: 'asd2' })
265
- })
257
+ getRepository(i)
258
+ .getDataSetFor(TestClass, 'id')
259
+ .onEntityUpdated.subscribe(({ change }) => {
260
+ expect(change).toEqual({ id: 1, value: 'asd2' })
261
+ })
266
262
 
267
- const dataSet = i.getDataSetFor(TestClass, 'id')
263
+ const dataSet = getDataSetFor(i, TestClass, 'id')
268
264
  await dataSet.add(i, { id: 1, value: 'asd' })
269
265
  await dataSet.update(i, 1, { id: 1, value: 'asd2' })
270
266
  })
@@ -274,11 +270,10 @@ describe('DataSet', () => {
274
270
  describe('Count', () => {
275
271
  it('should return the count if no settings are provided', async () => {
276
272
  await usingAsync(new Injector(), async (i) => {
277
- i.setupStores((stores) =>
278
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
279
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
273
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
274
+ getRepository(i).createDataSet(TestClass, 'id')
280
275
 
281
- const dataSet = i.getDataSetFor(TestClass, 'id')
276
+ const dataSet = getDataSetFor(i, TestClass, 'id')
282
277
  await dataSet.add(i, { id: 1, value: 'asd' })
283
278
  const result = await dataSet.count(i)
284
279
  expect(result).toBe(1)
@@ -288,11 +283,10 @@ describe('DataSet', () => {
288
283
  it('should return the count if authorizeGet returns valid result', async () => {
289
284
  await usingAsync(new Injector(), async (i) => {
290
285
  const authorizeGet = jest.fn(async () => ({ isAllowed: true, message: '' }))
291
- i.setupStores((stores) =>
292
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
293
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGet }))
286
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
287
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGet })
294
288
 
295
- const dataSet = i.getDataSetFor(TestClass, 'id')
289
+ const dataSet = getDataSetFor(i, TestClass, 'id')
296
290
  await dataSet.add(i, { id: 1, value: 'asd' })
297
291
  const result = await dataSet.count(i)
298
292
  expect(result).toBe(1)
@@ -302,11 +296,10 @@ describe('DataSet', () => {
302
296
  it('should throw if authorizeGet returns invalid result', async () => {
303
297
  await usingAsync(new Injector(), async (i) => {
304
298
  const authorizeGet = jest.fn(async () => ({ isAllowed: false, message: ':(' }))
305
- i.setupStores((stores) =>
306
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
307
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGet }))
299
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
300
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGet })
308
301
 
309
- const dataSet = i.getDataSetFor(TestClass, 'id')
302
+ const dataSet = getDataSetFor(i, TestClass, 'id')
310
303
  await dataSet.add(i, { id: 1, value: 'asd' })
311
304
  try {
312
305
  await dataSet.count(i)
@@ -322,11 +315,10 @@ describe('DataSet', () => {
322
315
  describe('filter', () => {
323
316
  it('should return the unfiltered result if no settings are provided', async () => {
324
317
  await usingAsync(new Injector(), async (i) => {
325
- i.setupStores((stores) =>
326
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
327
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
318
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
319
+ getRepository(i).createDataSet(TestClass, 'id')
328
320
 
329
- const dataSet = i.getDataSetFor(TestClass, 'id')
321
+ const dataSet = getDataSetFor(i, TestClass, 'id')
330
322
  await dataSet.add(i, { id: 1, value: 'asd' })
331
323
  const result = await dataSet.find(i, {})
332
324
  expect(result.length).toBe(1)
@@ -336,11 +328,10 @@ describe('DataSet', () => {
336
328
  it('should return the unfiltered result if authorizeGet returns valid result', async () => {
337
329
  await usingAsync(new Injector(), async (i) => {
338
330
  const authorizeGet = jest.fn(async () => ({ isAllowed: true, message: '' }))
339
- i.setupStores((stores) =>
340
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
341
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGet }))
331
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
332
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGet })
342
333
 
343
- const dataSet = i.getDataSetFor(TestClass, 'id')
334
+ const dataSet = getDataSetFor(i, TestClass, 'id')
344
335
  await dataSet.add(i, { id: 1, value: 'asd' })
345
336
  const result = await dataSet.find(i, {})
346
337
  expect(result.length).toBe(1)
@@ -350,11 +341,10 @@ describe('DataSet', () => {
350
341
  it('should throw if authorizeGet returns invalid result', async () => {
351
342
  await usingAsync(new Injector(), async (i) => {
352
343
  const authorizeGet = jest.fn(async () => ({ isAllowed: false, message: ':(' }))
353
- i.setupStores((stores) =>
354
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
355
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGet }))
344
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
345
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGet })
356
346
 
357
- const dataSet = i.getDataSetFor(TestClass, 'id')
347
+ const dataSet = getDataSetFor(i, TestClass, 'id')
358
348
  await dataSet.add(i, { id: 1, value: 'asd' })
359
349
  try {
360
350
  await dataSet.find(i, {})
@@ -369,11 +359,10 @@ describe('DataSet', () => {
369
359
  describe('get', () => {
370
360
  it('should return the entity if no settings are provided', async () => {
371
361
  await usingAsync(new Injector(), async (i) => {
372
- i.setupStores((stores) =>
373
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
374
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
362
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
363
+ getRepository(i).createDataSet(TestClass, 'id')
375
364
 
376
- const dataSet = i.getDataSetFor(TestClass, 'id')
365
+ const dataSet = getDataSetFor(i, TestClass, 'id')
377
366
  await dataSet.add(i, { id: 1, value: 'asd' })
378
367
  const result = await dataSet.get(i, 1)
379
368
  expect(result && result.id).toBe(1)
@@ -383,11 +372,10 @@ describe('DataSet', () => {
383
372
  it('should return the entity if authorizeGet returns valid result', async () => {
384
373
  await usingAsync(new Injector(), async (i) => {
385
374
  const authorizeGet = jest.fn(async () => ({ isAllowed: true, message: '' }))
386
- i.setupStores((stores) =>
387
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
388
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGet }))
375
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
376
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGet })
389
377
 
390
- const dataSet = i.getDataSetFor(TestClass, 'id')
378
+ const dataSet = getDataSetFor(i, TestClass, 'id')
391
379
  await dataSet.add(i, { id: 1, value: 'asd' })
392
380
  const result = await dataSet.get(i, 1)
393
381
  expect(result && result.id).toBe(1)
@@ -397,11 +385,10 @@ describe('DataSet', () => {
397
385
  it('should throw if authorizeGet returns invalid result', async () => {
398
386
  await usingAsync(new Injector(), async (i) => {
399
387
  const authorizeGet = jest.fn(async () => ({ isAllowed: false, message: ':(' }))
400
- i.setupStores((stores) =>
401
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
402
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGet }))
388
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
389
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGet })
403
390
 
404
- const dataSet = i.getDataSetFor(TestClass, 'id')
391
+ const dataSet = getDataSetFor(i, TestClass, 'id')
405
392
  await dataSet.add(i, { id: 1, value: 'asd' })
406
393
  try {
407
394
  await dataSet.get(i, 1)
@@ -415,11 +402,10 @@ describe('DataSet', () => {
415
402
  it('should return the entity if authorizeGetEntity returns valid result', async () => {
416
403
  await usingAsync(new Injector(), async (i) => {
417
404
  const authorizeGetEntity = jest.fn(async () => ({ isAllowed: true, message: '' }))
418
- i.setupStores((stores) =>
419
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
420
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGetEntity }))
405
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
406
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGetEntity })
421
407
 
422
- const dataSet = i.getDataSetFor(TestClass, 'id')
408
+ const dataSet = getDataSetFor(i, TestClass, 'id')
423
409
  await dataSet.add(i, { id: 1, value: 'asd' })
424
410
  const result = await dataSet.get(i, 1)
425
411
  expect(result && result.id).toBe(1)
@@ -429,11 +415,10 @@ describe('DataSet', () => {
429
415
  it('should throw if authorizeGetEntity returns invalid result', async () => {
430
416
  await usingAsync(new Injector(), async (i) => {
431
417
  const authorizeGetEntity = jest.fn(async () => ({ isAllowed: false, message: ':(' }))
432
- i.setupStores((stores) =>
433
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
434
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeGetEntity }))
418
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
419
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeGetEntity })
435
420
 
436
- const dataSet = i.getDataSetFor(TestClass, 'id')
421
+ const dataSet = getDataSetFor(i, TestClass, 'id')
437
422
  await dataSet.add(i, { id: 1, value: 'asd' })
438
423
  try {
439
424
  await dataSet.get(i, 1)
@@ -447,11 +432,10 @@ describe('DataSet', () => {
447
432
  describe('remove', () => {
448
433
  it('should remove the entity if no settings are provided', async () => {
449
434
  await usingAsync(new Injector(), async (i) => {
450
- i.setupStores((stores) =>
451
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
452
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
435
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
436
+ getRepository(i).createDataSet(TestClass, 'id')
453
437
 
454
- const dataSet = i.getDataSetFor(TestClass, 'id')
438
+ const dataSet = getDataSetFor(i, TestClass, 'id')
455
439
  await dataSet.add(i, { id: 1, value: 'asd' })
456
440
  await dataSet.remove(i, 1)
457
441
  const countValue = await dataSet.count(i)
@@ -462,11 +446,10 @@ describe('DataSet', () => {
462
446
  it('should remove the entity if authorizeRemove returns valid result', async () => {
463
447
  await usingAsync(new Injector(), async (i) => {
464
448
  const authorizeRemove = jest.fn(async () => ({ isAllowed: true, message: '' }))
465
- i.setupStores((stores) =>
466
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
467
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeRemove }))
449
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
450
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeRemove })
468
451
 
469
- const dataSet = i.getDataSetFor(TestClass, 'id')
452
+ const dataSet = getDataSetFor(i, TestClass, 'id')
470
453
  await dataSet.add(i, { id: 1, value: 'asd' })
471
454
  await dataSet.remove(i, 1)
472
455
  const count = await dataSet.count(i)
@@ -477,11 +460,10 @@ describe('DataSet', () => {
477
460
  it('should throw if authorizeRemove returns invalid result', async () => {
478
461
  await usingAsync(new Injector(), async (i) => {
479
462
  const authorizeRemove = jest.fn(async () => ({ isAllowed: false, message: ':(' }))
480
- i.setupStores((stores) =>
481
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
482
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authorizeRemove }))
463
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
464
+ getRepository(i).createDataSet(TestClass, 'id', { authorizeRemove })
483
465
 
484
- const dataSet = i.getDataSetFor(TestClass, 'id')
466
+ const dataSet = getDataSetFor(i, TestClass, 'id')
485
467
  await dataSet.add(i, { id: 1, value: 'asd' })
486
468
  try {
487
469
  await dataSet.remove(i, 1)
@@ -497,11 +479,10 @@ describe('DataSet', () => {
497
479
  it('should remove the entity if authroizeRemoveEntity returns valid result', async () => {
498
480
  await usingAsync(new Injector(), async (i) => {
499
481
  const authroizeRemoveEntity = jest.fn(async () => ({ isAllowed: true, message: '' }))
500
- i.setupStores((stores) =>
501
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
502
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authroizeRemoveEntity }))
482
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
483
+ getRepository(i).createDataSet(TestClass, 'id', { authroizeRemoveEntity })
503
484
 
504
- const dataSet = i.getDataSetFor(TestClass, 'id')
485
+ const dataSet = getDataSetFor(i, TestClass, 'id')
505
486
  await dataSet.add(i, { id: 1, value: 'asd' })
506
487
  await dataSet.remove(i, 1)
507
488
  const count = await dataSet.count(i)
@@ -512,11 +493,10 @@ describe('DataSet', () => {
512
493
  it('should throw if authroizeRemoveEntity returns invalid result', async () => {
513
494
  await usingAsync(new Injector(), async (i) => {
514
495
  const authroizeRemoveEntity = jest.fn(async () => ({ isAllowed: false, message: ':(' }))
515
- i.setupStores((stores) =>
516
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
517
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id', { authroizeRemoveEntity }))
496
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
497
+ getRepository(i).createDataSet(TestClass, 'id', { authroizeRemoveEntity })
518
498
 
519
- const dataSet = i.getDataSetFor(TestClass, 'id')
499
+ const dataSet = getDataSetFor(i, TestClass, 'id')
520
500
  await dataSet.add(i, { id: 1, value: 'asd' })
521
501
  try {
522
502
  await dataSet.remove(i, 1)
@@ -531,15 +511,16 @@ describe('DataSet', () => {
531
511
  it('should publish to the onEntityRemoved observable if an entity has been removed', async () => {
532
512
  await usingAsync(new Injector(), async (i) => {
533
513
  expect.assertions(1)
534
- i.setupStores((stores) =>
535
- stores.addStore(new InMemoryStore({ model: TestClass, primaryKey: 'id' })),
536
- ).setupRepository((repo) => repo.createDataSet(TestClass, 'id'))
514
+ addStore(i, new InMemoryStore({ model: TestClass, primaryKey: 'id' }))
515
+ getRepository(i).createDataSet(TestClass, 'id')
537
516
 
538
- i.getDataSetFor(TestClass, 'id').onEntityRemoved.subscribe(({ key }) => {
539
- expect(key).toEqual(1)
540
- })
517
+ getRepository(i)
518
+ .getDataSetFor(TestClass, 'id')
519
+ .onEntityRemoved.subscribe(({ key }) => {
520
+ expect(key).toEqual(1)
521
+ })
541
522
 
542
- const dataSet = i.getDataSetFor(TestClass, 'id')
523
+ const dataSet = getDataSetFor(i, TestClass, 'id')
543
524
  await dataSet.add(i, { id: 1, value: 'asd' })
544
525
  await dataSet.remove(i, 1)
545
526
  })
package/src/helpers.ts ADDED
@@ -0,0 +1,24 @@
1
+ import { Constructable } from '@furystack/inject'
2
+ import { Injector } from '@furystack/inject'
3
+ import { Repository } from './repository'
4
+
5
+ /**
6
+ * Returns a Repository on an injector
7
+ *
8
+ * @param injector The Injector instance
9
+ * @returns The Repository instance
10
+ */
11
+ export const getRepository = (injector: Injector) => injector.getInstance(Repository)
12
+
13
+ /**
14
+ *
15
+ * @param injector The Injector instance
16
+ * @param model The Model
17
+ * @param primaryKey The Primary Key field
18
+ * @returns A Repository DataSet for a specific model
19
+ */
20
+ export const getDataSetFor = <T, TPrimaryKey extends keyof T>(
21
+ injector: Injector,
22
+ model: Constructable<T>,
23
+ primaryKey: TPrimaryKey,
24
+ ) => injector.getInstance(Repository).getDataSetFor(model, primaryKey)
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  export * from './repository'
2
2
  export * from './data-set'
3
3
  export * from './data-set-setting'
4
- import './injector-extension'
4
+ export * from './helpers'
@@ -1,7 +1,7 @@
1
1
  import { Injector } from '@furystack/inject'
2
2
  import { using } from '@furystack/utils'
3
- import './injector-extension'
4
- import { InMemoryStore } from '@furystack/core'
3
+ import { getDataSetFor, getRepository } from './helpers'
4
+ import { addStore, InMemoryStore } from '@furystack/core'
5
5
  import { DataSet } from './data-set'
6
6
 
7
7
  describe('Repository', () => {
@@ -10,11 +10,10 @@ describe('Repository', () => {
10
10
  class ExampleClass {
11
11
  id!: number
12
12
  }
13
- i.setupStores((sm) => sm.addStore(new InMemoryStore({ model: ExampleClass, primaryKey: 'id' }))).setupRepository(
14
- (r) => r.createDataSet(ExampleClass, 'id', {}),
15
- )
13
+ addStore(i, new InMemoryStore({ model: ExampleClass, primaryKey: 'id' }))
14
+ getRepository(i).createDataSet(ExampleClass, 'id', {})
16
15
 
17
- const dataSet = i.getDataSetFor(ExampleClass, 'id')
16
+ const dataSet = getDataSetFor(i, ExampleClass, 'id')
18
17
  expect(dataSet).toBeInstanceOf(DataSet)
19
18
  })
20
19
  })
File without changes
File without changes
File without changes
File without changes