@feathersjs/memory 5.0.0-pre.20 → 5.0.0-pre.23
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 +24 -0
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/package.json +10 -10
- package/src/index.ts +119 -107
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,30 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.23](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.22...v5.0.0-pre.23) (2022-06-06)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @feathersjs/memory
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [5.0.0-pre.22](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.21...v5.0.0-pre.22) (2022-05-24)
|
|
15
|
+
|
|
16
|
+
**Note:** Version bump only for package @feathersjs/memory
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# [5.0.0-pre.21](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.20...v5.0.0-pre.21) (2022-05-23)
|
|
23
|
+
|
|
24
|
+
**Note:** Version bump only for package @feathersjs/memory
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
6
30
|
# [5.0.0-pre.20](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.19...v5.0.0-pre.20) (2022-05-04)
|
|
7
31
|
|
|
8
32
|
|
package/lib/index.js
CHANGED
|
@@ -57,7 +57,7 @@ class MemoryAdapter extends adapter_commons_1.AdapterBase {
|
|
|
57
57
|
total,
|
|
58
58
|
limit: filters.$limit,
|
|
59
59
|
skip: filters.$skip || 0,
|
|
60
|
-
data: values.map(value => _select(value, params))
|
|
60
|
+
data: values.map((value) => _select(value, params))
|
|
61
61
|
};
|
|
62
62
|
if (!paginate) {
|
|
63
63
|
return result.data;
|
|
@@ -76,7 +76,7 @@ class MemoryAdapter extends adapter_commons_1.AdapterBase {
|
|
|
76
76
|
}
|
|
77
77
|
async $create(data, params = {}) {
|
|
78
78
|
if (Array.isArray(data)) {
|
|
79
|
-
return Promise.all(data.map(current => this.$create(current, params)));
|
|
79
|
+
return Promise.all(data.map((current) => this.$create(current, params)));
|
|
80
80
|
}
|
|
81
81
|
const id = data[this.id] || this._uId++;
|
|
82
82
|
const current = commons_1._.extend({}, data, { [this.id]: id });
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,+
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;AAAA,+CAA6C;AAC7C,iDAAuC;AACvC,iEAOoC;AACpC,gDAAuB;AAcvB,MAAM,OAAO,GAAG,CAAC,IAAS,EAAE,MAAW,EAAE,GAAG,IAAW,EAAE,EAAE;IACzD,MAAM,IAAI,GAAG,IAAA,wBAAM,EAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAA;IAEpC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;AAC/C,CAAC,CAAA;AAED,MAAa,aAAkE,SAAQ,6BAKtF;IAIC,YAAY,UAAmC,EAAE;QAC/C,KAAK,CAAC;YACJ,EAAE,EAAE,IAAI;YACR,OAAO,EAAE,cAAI;YACb,MAAM,EAAN,wBAAM;YACN,KAAK,EAAE,EAAE;YACT,OAAO,EAAE,CAAC;YACV,GAAG,OAAO;SACX,CAAC,CAAA;QACF,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,OAAO,CA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
|
|
4
|
+
"version": "5.0.0-pre.23",
|
|
5
5
|
"homepage": "https://github.com/feathersjs/feathers",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -47,20 +47,20 @@
|
|
|
47
47
|
"lib": "lib"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@feathersjs/adapter-commons": "^5.0.0-pre.
|
|
51
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
52
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
50
|
+
"@feathersjs/adapter-commons": "^5.0.0-pre.23",
|
|
51
|
+
"@feathersjs/commons": "^5.0.0-pre.23",
|
|
52
|
+
"@feathersjs/errors": "^5.0.0-pre.23",
|
|
53
53
|
"sift": "^16.0.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@feathersjs/adapter-tests": "^5.0.0-pre.
|
|
57
|
-
"@feathersjs/feathers": "^5.0.0-pre.
|
|
56
|
+
"@feathersjs/adapter-tests": "^5.0.0-pre.23",
|
|
57
|
+
"@feathersjs/feathers": "^5.0.0-pre.23",
|
|
58
58
|
"@types/mocha": "^9.1.1",
|
|
59
|
-
"@types/node": "^17.0.
|
|
59
|
+
"@types/node": "^17.0.40",
|
|
60
60
|
"mocha": "^10.0.0",
|
|
61
61
|
"shx": "^0.3.4",
|
|
62
|
-
"ts-node": "^10.
|
|
63
|
-
"typescript": "^4.
|
|
62
|
+
"ts-node": "^10.8.1",
|
|
63
|
+
"typescript": "^4.7.3"
|
|
64
64
|
},
|
|
65
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "a60910bd730b88053ca6648337095f1ca1e3b39f"
|
|
66
66
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,31 +1,43 @@
|
|
|
1
|
-
import { NotFound } from '@feathersjs/errors'
|
|
2
|
-
import { _ } from '@feathersjs/commons'
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { NotFound } from '@feathersjs/errors'
|
|
2
|
+
import { _ } from '@feathersjs/commons'
|
|
3
|
+
import {
|
|
4
|
+
sorter,
|
|
5
|
+
select,
|
|
6
|
+
AdapterBase,
|
|
7
|
+
AdapterServiceOptions,
|
|
8
|
+
PaginationOptions,
|
|
9
|
+
AdapterParams
|
|
10
|
+
} from '@feathersjs/adapter-commons'
|
|
11
|
+
import sift from 'sift'
|
|
12
|
+
import { NullableId, Id, Params, ServiceMethods, Paginated } from '@feathersjs/feathers'
|
|
6
13
|
|
|
7
14
|
export interface MemoryServiceStore<T> {
|
|
8
|
-
[key: string]: T
|
|
15
|
+
[key: string]: T
|
|
9
16
|
}
|
|
10
17
|
|
|
11
18
|
export interface MemoryServiceOptions<T = any> extends AdapterServiceOptions {
|
|
12
|
-
store?: MemoryServiceStore<T
|
|
13
|
-
startId?: number
|
|
14
|
-
matcher?: (query: any) => any
|
|
15
|
-
sorter?: (sort: any) => any
|
|
19
|
+
store?: MemoryServiceStore<T>
|
|
20
|
+
startId?: number
|
|
21
|
+
matcher?: (query: any) => any
|
|
22
|
+
sorter?: (sort: any) => any
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
const _select = (data: any, params: any, ...args: any[]) => {
|
|
19
|
-
const base = select(params, ...args)
|
|
26
|
+
const base = select(params, ...args)
|
|
20
27
|
|
|
21
|
-
return base(JSON.parse(JSON.stringify(data)))
|
|
22
|
-
}
|
|
28
|
+
return base(JSON.parse(JSON.stringify(data)))
|
|
29
|
+
}
|
|
23
30
|
|
|
24
|
-
export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> extends AdapterBase<
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> extends AdapterBase<
|
|
32
|
+
T,
|
|
33
|
+
D,
|
|
34
|
+
P,
|
|
35
|
+
MemoryServiceOptions<T>
|
|
36
|
+
> {
|
|
37
|
+
store: MemoryServiceStore<T>
|
|
38
|
+
_uId: number
|
|
27
39
|
|
|
28
|
-
constructor
|
|
40
|
+
constructor(options: MemoryServiceOptions<T> = {}) {
|
|
29
41
|
super({
|
|
30
42
|
id: 'id',
|
|
31
43
|
matcher: sift,
|
|
@@ -33,22 +45,22 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
33
45
|
store: {},
|
|
34
46
|
startId: 0,
|
|
35
47
|
...options
|
|
36
|
-
})
|
|
37
|
-
this._uId = this.options.startId
|
|
38
|
-
this.store = { ...this.options.store }
|
|
48
|
+
})
|
|
49
|
+
this._uId = this.options.startId
|
|
50
|
+
this.store = { ...this.options.store }
|
|
39
51
|
}
|
|
40
52
|
|
|
41
|
-
async getEntries
|
|
42
|
-
const params = _params || {} as P
|
|
53
|
+
async getEntries(_params?: P) {
|
|
54
|
+
const params = _params || ({} as P)
|
|
43
55
|
|
|
44
56
|
return this.$find({
|
|
45
57
|
...params,
|
|
46
58
|
paginate: false
|
|
47
|
-
})
|
|
59
|
+
})
|
|
48
60
|
}
|
|
49
61
|
|
|
50
|
-
getQuery
|
|
51
|
-
const { $skip, $sort, $limit, $select, ...query } = params.query || {}
|
|
62
|
+
getQuery(params: P) {
|
|
63
|
+
const { $skip, $sort, $limit, $select, ...query } = params.query || {}
|
|
52
64
|
|
|
53
65
|
return {
|
|
54
66
|
query,
|
|
@@ -56,171 +68,171 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
56
68
|
}
|
|
57
69
|
}
|
|
58
70
|
|
|
59
|
-
async $find
|
|
60
|
-
async $find
|
|
61
|
-
async $find
|
|
62
|
-
async $find
|
|
63
|
-
const { paginate } = this.getOptions(params)
|
|
64
|
-
const { query, filters } = this.getQuery(params)
|
|
71
|
+
async $find(_params?: P & { paginate?: PaginationOptions }): Promise<Paginated<T>>
|
|
72
|
+
async $find(_params?: P & { paginate: false }): Promise<T[]>
|
|
73
|
+
async $find(_params?: P): Promise<Paginated<T> | T[]>
|
|
74
|
+
async $find(params: P = {} as P): Promise<Paginated<T> | T[]> {
|
|
75
|
+
const { paginate } = this.getOptions(params)
|
|
76
|
+
const { query, filters } = this.getQuery(params)
|
|
65
77
|
|
|
66
|
-
let values = _.values(this.store).filter(this.options.matcher(query))
|
|
67
|
-
const total = values.length
|
|
78
|
+
let values = _.values(this.store).filter(this.options.matcher(query))
|
|
79
|
+
const total = values.length
|
|
68
80
|
|
|
69
81
|
if (filters.$sort !== undefined) {
|
|
70
|
-
values.sort(this.options.sorter(filters.$sort))
|
|
82
|
+
values.sort(this.options.sorter(filters.$sort))
|
|
71
83
|
}
|
|
72
84
|
|
|
73
85
|
if (filters.$skip !== undefined) {
|
|
74
|
-
values = values.slice(filters.$skip)
|
|
86
|
+
values = values.slice(filters.$skip)
|
|
75
87
|
}
|
|
76
88
|
|
|
77
89
|
if (filters.$limit !== undefined) {
|
|
78
|
-
values = values.slice(0, filters.$limit)
|
|
90
|
+
values = values.slice(0, filters.$limit)
|
|
79
91
|
}
|
|
80
92
|
|
|
81
93
|
const result: Paginated<T> = {
|
|
82
94
|
total,
|
|
83
95
|
limit: filters.$limit,
|
|
84
96
|
skip: filters.$skip || 0,
|
|
85
|
-
data: values.map(value => _select(value, params))
|
|
86
|
-
}
|
|
97
|
+
data: values.map((value) => _select(value, params))
|
|
98
|
+
}
|
|
87
99
|
|
|
88
100
|
if (!paginate) {
|
|
89
|
-
return result.data
|
|
101
|
+
return result.data
|
|
90
102
|
}
|
|
91
103
|
|
|
92
|
-
return result
|
|
104
|
+
return result
|
|
93
105
|
}
|
|
94
106
|
|
|
95
|
-
async $get
|
|
96
|
-
const { query } = this.getQuery(params)
|
|
107
|
+
async $get(id: Id, params: P = {} as P): Promise<T> {
|
|
108
|
+
const { query } = this.getQuery(params)
|
|
97
109
|
|
|
98
110
|
if (id in this.store) {
|
|
99
|
-
const value = this.store[id]
|
|
111
|
+
const value = this.store[id]
|
|
100
112
|
|
|
101
113
|
if (this.options.matcher(query)(value)) {
|
|
102
|
-
return _select(value, params, this.id)
|
|
114
|
+
return _select(value, params, this.id)
|
|
103
115
|
}
|
|
104
116
|
}
|
|
105
117
|
|
|
106
|
-
throw new NotFound(`No record found for id '${id}'`)
|
|
118
|
+
throw new NotFound(`No record found for id '${id}'`)
|
|
107
119
|
}
|
|
108
120
|
|
|
109
|
-
async $create
|
|
110
|
-
async $create
|
|
111
|
-
async $create
|
|
112
|
-
async $create
|
|
121
|
+
async $create(data: Partial<D>, params?: P): Promise<T>
|
|
122
|
+
async $create(data: Partial<D>[], params?: P): Promise<T[]>
|
|
123
|
+
async $create(data: Partial<D> | Partial<D>[], _params?: P): Promise<T | T[]>
|
|
124
|
+
async $create(data: Partial<D> | Partial<D>[], params: P = {} as P): Promise<T | T[]> {
|
|
113
125
|
if (Array.isArray(data)) {
|
|
114
|
-
return Promise.all(data.map(current => this.$create(current, params)))
|
|
126
|
+
return Promise.all(data.map((current) => this.$create(current, params)))
|
|
115
127
|
}
|
|
116
128
|
|
|
117
|
-
const id = (data as any)[this.id] || this._uId
|
|
118
|
-
const current = _.extend({}, data, { [this.id]: id })
|
|
119
|
-
const result = (this.store[id] = current)
|
|
129
|
+
const id = (data as any)[this.id] || this._uId++
|
|
130
|
+
const current = _.extend({}, data, { [this.id]: id })
|
|
131
|
+
const result = (this.store[id] = current)
|
|
120
132
|
|
|
121
|
-
return _select(result, params, this.id) as T
|
|
133
|
+
return _select(result, params, this.id) as T
|
|
122
134
|
}
|
|
123
135
|
|
|
124
|
-
async $update
|
|
125
|
-
const oldEntry = await this.$get(id)
|
|
136
|
+
async $update(id: Id, data: D, params: P = {} as P): Promise<T> {
|
|
137
|
+
const oldEntry = await this.$get(id)
|
|
126
138
|
// We don't want our id to change type if it can be coerced
|
|
127
|
-
const oldId = (oldEntry as any)[this.id]
|
|
139
|
+
const oldId = (oldEntry as any)[this.id]
|
|
128
140
|
|
|
129
141
|
// eslint-disable-next-line eqeqeq
|
|
130
|
-
id = oldId == id ? oldId : id
|
|
142
|
+
id = oldId == id ? oldId : id
|
|
131
143
|
|
|
132
|
-
this.store[id] = _.extend({}, data, { [this.id]: id })
|
|
144
|
+
this.store[id] = _.extend({}, data, { [this.id]: id })
|
|
133
145
|
|
|
134
|
-
return this.$get(id, params)
|
|
146
|
+
return this.$get(id, params)
|
|
135
147
|
}
|
|
136
148
|
|
|
137
|
-
async $patch
|
|
138
|
-
async $patch
|
|
139
|
-
async $patch
|
|
140
|
-
async $patch
|
|
141
|
-
const { query } = this.getQuery(params)
|
|
149
|
+
async $patch(id: null, data: Partial<D>, params?: P): Promise<T[]>
|
|
150
|
+
async $patch(id: Id, data: Partial<D>, params?: P): Promise<T>
|
|
151
|
+
async $patch(id: NullableId, data: Partial<D>, _params?: P): Promise<T | T[]>
|
|
152
|
+
async $patch(id: NullableId, data: Partial<D>, params: P = {} as P): Promise<T | T[]> {
|
|
153
|
+
const { query } = this.getQuery(params)
|
|
142
154
|
const patchEntry = (entry: T) => {
|
|
143
|
-
const currentId = (entry as any)[this.id]
|
|
155
|
+
const currentId = (entry as any)[this.id]
|
|
144
156
|
|
|
145
|
-
this.store[currentId] = _.extend(this.store[currentId], _.omit(data, this.id))
|
|
157
|
+
this.store[currentId] = _.extend(this.store[currentId], _.omit(data, this.id))
|
|
146
158
|
|
|
147
|
-
return _select(this.store[currentId], params, this.id)
|
|
148
|
-
}
|
|
159
|
+
return _select(this.store[currentId], params, this.id)
|
|
160
|
+
}
|
|
149
161
|
|
|
150
162
|
if (id === null) {
|
|
151
163
|
const entries = await this.getEntries({
|
|
152
164
|
...params,
|
|
153
165
|
query
|
|
154
|
-
})
|
|
166
|
+
})
|
|
155
167
|
|
|
156
|
-
return entries.map(patchEntry)
|
|
168
|
+
return entries.map(patchEntry)
|
|
157
169
|
}
|
|
158
170
|
|
|
159
|
-
return patchEntry(await this.$get(id, params))
|
|
171
|
+
return patchEntry(await this.$get(id, params)) // Will throw an error if not found
|
|
160
172
|
}
|
|
161
173
|
|
|
162
|
-
async $remove
|
|
163
|
-
async $remove
|
|
164
|
-
async $remove
|
|
165
|
-
async $remove
|
|
166
|
-
const { query } = this.getQuery(params)
|
|
174
|
+
async $remove(id: null, params?: P): Promise<T[]>
|
|
175
|
+
async $remove(id: Id, params?: P): Promise<T>
|
|
176
|
+
async $remove(id: NullableId, _params?: P): Promise<T | T[]>
|
|
177
|
+
async $remove(id: NullableId, params: P = {} as P): Promise<T | T[]> {
|
|
178
|
+
const { query } = this.getQuery(params)
|
|
167
179
|
|
|
168
180
|
if (id === null) {
|
|
169
181
|
const entries = await this.getEntries({
|
|
170
182
|
...params,
|
|
171
183
|
query
|
|
172
|
-
})
|
|
184
|
+
})
|
|
173
185
|
|
|
174
|
-
return Promise.all(entries.map((current: any) =>
|
|
175
|
-
this.$remove(current[this.id] as Id, params)
|
|
176
|
-
));
|
|
186
|
+
return Promise.all(entries.map((current: any) => this.$remove(current[this.id] as Id, params)))
|
|
177
187
|
}
|
|
178
188
|
|
|
179
|
-
const entry = await this.$get(id, params)
|
|
189
|
+
const entry = await this.$get(id, params)
|
|
180
190
|
|
|
181
|
-
delete this.store[id]
|
|
191
|
+
delete this.store[id]
|
|
182
192
|
|
|
183
|
-
return entry
|
|
193
|
+
return entry
|
|
184
194
|
}
|
|
185
195
|
}
|
|
186
196
|
|
|
187
197
|
export class MemoryService<T = any, D = Partial<T>, P extends AdapterParams = AdapterParams>
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
async find
|
|
192
|
-
async find
|
|
193
|
-
|
|
198
|
+
extends MemoryAdapter<T, D, P>
|
|
199
|
+
implements ServiceMethods<T | Paginated<T>, D, P>
|
|
200
|
+
{
|
|
201
|
+
async find(params?: P & { paginate?: PaginationOptions }): Promise<Paginated<T>>
|
|
202
|
+
async find(params?: P & { paginate: false }): Promise<T[]>
|
|
203
|
+
async find(params?: P): Promise<Paginated<T> | T[]>
|
|
204
|
+
async find(params?: P): Promise<Paginated<T> | T[]> {
|
|
205
|
+
return this._find(params) as any
|
|
194
206
|
}
|
|
195
207
|
|
|
196
|
-
async get
|
|
197
|
-
return this._get(id, params)
|
|
208
|
+
async get(id: Id, params?: P): Promise<T> {
|
|
209
|
+
return this._get(id, params)
|
|
198
210
|
}
|
|
199
211
|
|
|
200
|
-
async create
|
|
201
|
-
async create
|
|
202
|
-
async create
|
|
203
|
-
return this._create(data, params)
|
|
212
|
+
async create(data: Partial<D>, params?: P): Promise<T>
|
|
213
|
+
async create(data: Partial<D>[], params?: P): Promise<T[]>
|
|
214
|
+
async create(data: Partial<D> | Partial<D>[], params?: P): Promise<T | T[]> {
|
|
215
|
+
return this._create(data, params)
|
|
204
216
|
}
|
|
205
217
|
|
|
206
|
-
async update
|
|
207
|
-
return this._update(id, data, params)
|
|
218
|
+
async update(id: Id, data: D, params?: P): Promise<T> {
|
|
219
|
+
return this._update(id, data, params)
|
|
208
220
|
}
|
|
209
221
|
|
|
210
|
-
async patch
|
|
211
|
-
async patch
|
|
212
|
-
async patch
|
|
213
|
-
return this._patch(id, data, params)
|
|
222
|
+
async patch(id: Id, data: Partial<D>, params?: P): Promise<T>
|
|
223
|
+
async patch(id: null, data: Partial<D>, params?: P): Promise<T[]>
|
|
224
|
+
async patch(id: NullableId, data: Partial<D>, params?: P): Promise<T | T[]> {
|
|
225
|
+
return this._patch(id, data, params)
|
|
214
226
|
}
|
|
215
227
|
|
|
216
|
-
async remove
|
|
217
|
-
async remove
|
|
218
|
-
async remove
|
|
219
|
-
return this._remove(id, params)
|
|
228
|
+
async remove(id: Id, params?: P): Promise<T>
|
|
229
|
+
async remove(id: null, params?: P): Promise<T[]>
|
|
230
|
+
async remove(id: NullableId, params?: P): Promise<T | T[]> {
|
|
231
|
+
return this._remove(id, params)
|
|
220
232
|
}
|
|
221
233
|
}
|
|
222
234
|
|
|
223
|
-
export function memory<T = any, D = Partial<T>, P extends Params = Params>
|
|
235
|
+
export function memory<T = any, D = Partial<T>, P extends Params = Params>(
|
|
224
236
|
options: Partial<MemoryServiceOptions<T>> = {}
|
|
225
237
|
) {
|
|
226
238
|
return new MemoryService<T, D, P>(options)
|