@feathersjs/memory 5.0.0-pre.32 → 5.0.0-pre.34
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 +12 -0
- package/lib/index.d.ts +37 -37
- package/lib/index.js +74 -27
- package/lib/index.js.map +1 -1
- package/package.json +12 -12
- package/src/index.ts +141 -74
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
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.34](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.33...v5.0.0-pre.34) (2022-12-14)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **adapter:** Add patch data type to adapters and refactor AdapterBase usage ([#2906](https://github.com/feathersjs/feathers/issues/2906)) ([9ddc2e6](https://github.com/feathersjs/feathers/commit/9ddc2e6b028f026f939d6af68125847e5c6734b4))
|
|
11
|
+
|
|
12
|
+
# [5.0.0-pre.33](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.32...v5.0.0-pre.33) (2022-11-08)
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
- **memory:** Use for loop in \_find() for better performance ([#2844](https://github.com/feathersjs/feathers/issues/2844)) ([d6ee5f1](https://github.com/feathersjs/feathers/commit/d6ee5f1c869f0c65cb470130f35956a52356e5c3))
|
|
17
|
+
|
|
6
18
|
# [5.0.0-pre.32](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.31...v5.0.0-pre.32) (2022-10-26)
|
|
7
19
|
|
|
8
20
|
**Note:** Version bump only for package @feathersjs/memory
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AdapterBase, AdapterServiceOptions, PaginationOptions, AdapterParams } from '@feathersjs/adapter-commons';
|
|
2
|
-
import { NullableId, Id, Params,
|
|
2
|
+
import { NullableId, Id, Params, Paginated } from '@feathersjs/feathers';
|
|
3
3
|
export interface MemoryServiceStore<T> {
|
|
4
4
|
[key: string]: T;
|
|
5
5
|
}
|
|
@@ -9,12 +9,12 @@ export interface MemoryServiceOptions<T = any> extends AdapterServiceOptions {
|
|
|
9
9
|
matcher?: (query: any) => any;
|
|
10
10
|
sorter?: (sort: any) => any;
|
|
11
11
|
}
|
|
12
|
-
export declare class MemoryAdapter<
|
|
13
|
-
store: MemoryServiceStore<
|
|
12
|
+
export declare class MemoryAdapter<Result = any, Data = Partial<Result>, ServiceParams extends Params = Params, PatchData = Partial<Data>> extends AdapterBase<Result, Data, PatchData, ServiceParams, MemoryServiceOptions<Result>> {
|
|
13
|
+
store: MemoryServiceStore<Result>;
|
|
14
14
|
_uId: number;
|
|
15
|
-
constructor(options?: MemoryServiceOptions<
|
|
16
|
-
getEntries(_params?:
|
|
17
|
-
getQuery(params:
|
|
15
|
+
constructor(options?: MemoryServiceOptions<Result>);
|
|
16
|
+
getEntries(_params?: ServiceParams): Promise<Result[]>;
|
|
17
|
+
getQuery(params: ServiceParams): {
|
|
18
18
|
query: {
|
|
19
19
|
[key: string]: any;
|
|
20
20
|
};
|
|
@@ -25,40 +25,40 @@ export declare class MemoryAdapter<T = any, D = Partial<T>, P extends Params = P
|
|
|
25
25
|
$select: any;
|
|
26
26
|
};
|
|
27
27
|
};
|
|
28
|
-
|
|
28
|
+
_find(_params?: ServiceParams & {
|
|
29
29
|
paginate?: PaginationOptions;
|
|
30
|
-
}): Promise<Paginated<
|
|
31
|
-
|
|
30
|
+
}): Promise<Paginated<Result>>;
|
|
31
|
+
_find(_params?: ServiceParams & {
|
|
32
32
|
paginate: false;
|
|
33
|
-
}): Promise<
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
}): Promise<Result[]>;
|
|
34
|
+
_find(_params?: ServiceParams): Promise<Paginated<Result> | Result[]>;
|
|
35
|
+
_get(id: Id, params?: ServiceParams): Promise<Result>;
|
|
36
|
+
_create(data: Partial<Data>, params?: ServiceParams): Promise<Result>;
|
|
37
|
+
_create(data: Partial<Data>[], params?: ServiceParams): Promise<Result[]>;
|
|
38
|
+
_create(data: Partial<Data> | Partial<Data>[], _params?: ServiceParams): Promise<Result | Result[]>;
|
|
39
|
+
_update(id: Id, data: Data, params?: ServiceParams): Promise<Result>;
|
|
40
|
+
_patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>;
|
|
41
|
+
_patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>;
|
|
42
|
+
_patch(id: NullableId, data: PatchData, _params?: ServiceParams): Promise<Result | Result[]>;
|
|
43
|
+
_remove(id: null, params?: ServiceParams): Promise<Result[]>;
|
|
44
|
+
_remove(id: Id, params?: ServiceParams): Promise<Result>;
|
|
45
|
+
_remove(id: NullableId, _params?: ServiceParams): Promise<Result | Result[]>;
|
|
46
46
|
}
|
|
47
|
-
export declare class MemoryService<
|
|
48
|
-
find(params?:
|
|
47
|
+
export declare class MemoryService<Result = any, Data = Partial<Result>, ServiceParams extends AdapterParams = AdapterParams, PatchData = Partial<Data>> extends MemoryAdapter<Result, Data, ServiceParams, PatchData> {
|
|
48
|
+
find(params?: ServiceParams & {
|
|
49
49
|
paginate?: PaginationOptions;
|
|
50
|
-
}): Promise<Paginated<
|
|
51
|
-
find(params?:
|
|
50
|
+
}): Promise<Paginated<Result>>;
|
|
51
|
+
find(params?: ServiceParams & {
|
|
52
52
|
paginate: false;
|
|
53
|
-
}): Promise<
|
|
54
|
-
find(params?:
|
|
55
|
-
get(id: Id, params?:
|
|
56
|
-
create(data:
|
|
57
|
-
create(data:
|
|
58
|
-
update(id: Id, data:
|
|
59
|
-
patch(id: Id, data:
|
|
60
|
-
patch(id: null, data:
|
|
61
|
-
remove(id: Id, params?:
|
|
62
|
-
remove(id: null, params?:
|
|
53
|
+
}): Promise<Result[]>;
|
|
54
|
+
find(params?: ServiceParams): Promise<Paginated<Result> | Result[]>;
|
|
55
|
+
get(id: Id, params?: ServiceParams): Promise<Result>;
|
|
56
|
+
create(data: Data, params?: ServiceParams): Promise<Result>;
|
|
57
|
+
create(data: Data[], params?: ServiceParams): Promise<Result[]>;
|
|
58
|
+
update(id: Id, data: Data, params?: ServiceParams): Promise<Result>;
|
|
59
|
+
patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>;
|
|
60
|
+
patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>;
|
|
61
|
+
remove(id: Id, params?: ServiceParams): Promise<Result>;
|
|
62
|
+
remove(id: null, params?: ServiceParams): Promise<Result[]>;
|
|
63
63
|
}
|
|
64
|
-
export declare function memory<T = any, D = Partial<T>, P extends Params = Params>(options?: Partial<MemoryServiceOptions<T>>): MemoryService<T, D, P
|
|
64
|
+
export declare function memory<T = any, D = Partial<T>, P extends Params = Params>(options?: Partial<MemoryServiceOptions<T>>): MemoryService<T, D, P, Partial<D>>;
|
package/lib/index.js
CHANGED
|
@@ -27,7 +27,7 @@ class MemoryAdapter extends adapter_commons_1.AdapterBase {
|
|
|
27
27
|
}
|
|
28
28
|
async getEntries(_params) {
|
|
29
29
|
const params = _params || {};
|
|
30
|
-
return this
|
|
30
|
+
return this._find({
|
|
31
31
|
...params,
|
|
32
32
|
paginate: false
|
|
33
33
|
});
|
|
@@ -39,32 +39,50 @@ class MemoryAdapter extends adapter_commons_1.AdapterBase {
|
|
|
39
39
|
filters: { $skip, $sort, $limit, $select }
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
|
-
async
|
|
42
|
+
async _find(params = {}) {
|
|
43
43
|
const { paginate } = this.getOptions(params);
|
|
44
44
|
const { query, filters } = this.getQuery(params);
|
|
45
|
-
let values = commons_1._.values(this.store)
|
|
45
|
+
let values = commons_1._.values(this.store);
|
|
46
46
|
const total = values.length;
|
|
47
|
-
|
|
47
|
+
const hasSkip = filters.$skip !== undefined;
|
|
48
|
+
const hasSort = filters.$sort !== undefined;
|
|
49
|
+
const hasLimit = filters.$limit !== undefined;
|
|
50
|
+
const hasQuery = commons_1._.keys(query).length > 0;
|
|
51
|
+
if (hasSort) {
|
|
48
52
|
values.sort(this.options.sorter(filters.$sort));
|
|
49
53
|
}
|
|
50
|
-
if (
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
if (hasQuery || hasLimit || hasSkip) {
|
|
55
|
+
let skipped = 0;
|
|
56
|
+
const matcher = this.options.matcher(query);
|
|
57
|
+
const matched = [];
|
|
58
|
+
for (let index = 0, length = values.length; index < length; index++) {
|
|
59
|
+
const value = values[index];
|
|
60
|
+
if (hasQuery && !matcher(value, index, values)) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (hasSkip && filters.$skip > skipped) {
|
|
64
|
+
skipped++;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
matched.push(_select(value, params));
|
|
68
|
+
if (hasLimit && filters.$limit === matched.length) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
values = matched;
|
|
55
73
|
}
|
|
56
74
|
const result = {
|
|
57
|
-
total,
|
|
75
|
+
total: hasQuery ? values.length : total,
|
|
58
76
|
limit: filters.$limit,
|
|
59
77
|
skip: filters.$skip || 0,
|
|
60
|
-
data:
|
|
78
|
+
data: filters.$limit === 0 ? [] : values
|
|
61
79
|
};
|
|
62
80
|
if (!paginate) {
|
|
63
81
|
return result.data;
|
|
64
82
|
}
|
|
65
83
|
return result;
|
|
66
84
|
}
|
|
67
|
-
async
|
|
85
|
+
async _get(id, params = {}) {
|
|
68
86
|
const { query } = this.getQuery(params);
|
|
69
87
|
if (id in this.store) {
|
|
70
88
|
const value = this.store[id];
|
|
@@ -74,25 +92,31 @@ class MemoryAdapter extends adapter_commons_1.AdapterBase {
|
|
|
74
92
|
}
|
|
75
93
|
throw new errors_1.NotFound(`No record found for id '${id}'`);
|
|
76
94
|
}
|
|
77
|
-
async
|
|
95
|
+
async _create(data, params = {}) {
|
|
78
96
|
if (Array.isArray(data)) {
|
|
79
|
-
return Promise.all(data.map((current) => this
|
|
97
|
+
return Promise.all(data.map((current) => this._create(current, params)));
|
|
80
98
|
}
|
|
81
99
|
const id = data[this.id] || this._uId++;
|
|
82
100
|
const current = commons_1._.extend({}, data, { [this.id]: id });
|
|
83
101
|
const result = (this.store[id] = current);
|
|
84
102
|
return _select(result, params, this.id);
|
|
85
103
|
}
|
|
86
|
-
async
|
|
87
|
-
|
|
104
|
+
async _update(id, data, params = {}) {
|
|
105
|
+
if (id === null || Array.isArray(data)) {
|
|
106
|
+
throw new errors_1.BadRequest("You can not replace multiple instances. Did you mean 'patch'?");
|
|
107
|
+
}
|
|
108
|
+
const oldEntry = await this._get(id);
|
|
88
109
|
// We don't want our id to change type if it can be coerced
|
|
89
110
|
const oldId = oldEntry[this.id];
|
|
90
111
|
// eslint-disable-next-line eqeqeq
|
|
91
112
|
id = oldId == id ? oldId : id;
|
|
92
113
|
this.store[id] = commons_1._.extend({}, data, { [this.id]: id });
|
|
93
|
-
return this
|
|
114
|
+
return this._get(id, params);
|
|
94
115
|
}
|
|
95
|
-
async
|
|
116
|
+
async _patch(id, data, params = {}) {
|
|
117
|
+
if (id === null && !this.allowsMulti('patch', params)) {
|
|
118
|
+
throw new errors_1.MethodNotAllowed('Can not patch multiple entries');
|
|
119
|
+
}
|
|
96
120
|
const { query } = this.getQuery(params);
|
|
97
121
|
const patchEntry = (entry) => {
|
|
98
122
|
const currentId = entry[this.id];
|
|
@@ -106,18 +130,21 @@ class MemoryAdapter extends adapter_commons_1.AdapterBase {
|
|
|
106
130
|
});
|
|
107
131
|
return entries.map(patchEntry);
|
|
108
132
|
}
|
|
109
|
-
return patchEntry(await this
|
|
133
|
+
return patchEntry(await this._get(id, params)); // Will throw an error if not found
|
|
110
134
|
}
|
|
111
|
-
async
|
|
135
|
+
async _remove(id, params = {}) {
|
|
136
|
+
if (id === null && !this.allowsMulti('remove', params)) {
|
|
137
|
+
throw new errors_1.MethodNotAllowed('Can not remove multiple entries');
|
|
138
|
+
}
|
|
112
139
|
const { query } = this.getQuery(params);
|
|
113
140
|
if (id === null) {
|
|
114
141
|
const entries = await this.getEntries({
|
|
115
142
|
...params,
|
|
116
143
|
query
|
|
117
144
|
});
|
|
118
|
-
return Promise.all(entries.map((current) => this
|
|
145
|
+
return Promise.all(entries.map((current) => this._remove(current[this.id], params)));
|
|
119
146
|
}
|
|
120
|
-
const entry = await this
|
|
147
|
+
const entry = await this._get(id, params);
|
|
121
148
|
delete this.store[id];
|
|
122
149
|
return entry;
|
|
123
150
|
}
|
|
@@ -125,22 +152,42 @@ class MemoryAdapter extends adapter_commons_1.AdapterBase {
|
|
|
125
152
|
exports.MemoryAdapter = MemoryAdapter;
|
|
126
153
|
class MemoryService extends MemoryAdapter {
|
|
127
154
|
async find(params) {
|
|
128
|
-
return this._find(
|
|
155
|
+
return this._find({
|
|
156
|
+
...params,
|
|
157
|
+
query: await this.sanitizeQuery(params)
|
|
158
|
+
});
|
|
129
159
|
}
|
|
130
160
|
async get(id, params) {
|
|
131
|
-
return this._get(id,
|
|
161
|
+
return this._get(id, {
|
|
162
|
+
...params,
|
|
163
|
+
query: await this.sanitizeQuery(params)
|
|
164
|
+
});
|
|
132
165
|
}
|
|
133
166
|
async create(data, params) {
|
|
167
|
+
if (Array.isArray(data) && !this.allowsMulti('create', params)) {
|
|
168
|
+
throw new errors_1.MethodNotAllowed('Can not create multiple entries');
|
|
169
|
+
}
|
|
134
170
|
return this._create(data, params);
|
|
135
171
|
}
|
|
136
172
|
async update(id, data, params) {
|
|
137
|
-
return this._update(id, data,
|
|
173
|
+
return this._update(id, data, {
|
|
174
|
+
...params,
|
|
175
|
+
query: await this.sanitizeQuery(params)
|
|
176
|
+
});
|
|
138
177
|
}
|
|
139
178
|
async patch(id, data, params) {
|
|
140
|
-
|
|
179
|
+
const { $limit, ...query } = await this.sanitizeQuery(params);
|
|
180
|
+
return this._patch(id, data, {
|
|
181
|
+
...params,
|
|
182
|
+
query
|
|
183
|
+
});
|
|
141
184
|
}
|
|
142
185
|
async remove(id, params) {
|
|
143
|
-
|
|
186
|
+
const { $limit, ...query } = await this.sanitizeQuery(params);
|
|
187
|
+
return this._remove(id, {
|
|
188
|
+
...params,
|
|
189
|
+
query
|
|
190
|
+
});
|
|
144
191
|
}
|
|
145
192
|
}
|
|
146
193
|
exports.MemoryService = MemoryService;
|
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,+CAA2E;AAC3E,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,aAKX,SAAQ,6BAAiF;IAIzF,YAAY,UAAwC,EAAE;QACpD,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,CAAC,OAAO,CAAA;QAChC,IAAI,CAAC,KAAK,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I,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE;YAC3B,GAAG,MAAM;YACT,KAAK;SACN,CAAC,CAAA;IACJ,CAAC;IAID,KAAK,CAAC,MAAM,CAAC,EAAc,EAAE,MAAsB;QACjD,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAE7D,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE;YACtB,GAAG,MAAM;YACT,KAAK;SACN,CAAC,CAAA;IACJ,CAAC;CACF;AA7DD,sCA6DC;AAED,SAAgB,MAAM,CACpB,UAA4C,EAAE;IAE9C,OAAO,IAAI,aAAa,CAAU,OAAO,CAAC,CAAA;AAC5C,CAAC;AAJD,wBAIC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/memory",
|
|
3
3
|
"description": "An in memory service store",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.34",
|
|
5
5
|
"homepage": "https://github.com/feathersjs/feathers",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"types": "lib/",
|
|
@@ -49,20 +49,20 @@
|
|
|
49
49
|
"lib": "lib"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@feathersjs/adapter-commons": "^5.0.0-pre.
|
|
53
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
54
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
55
|
-
"sift": "^16.0.
|
|
52
|
+
"@feathersjs/adapter-commons": "^5.0.0-pre.34",
|
|
53
|
+
"@feathersjs/commons": "^5.0.0-pre.34",
|
|
54
|
+
"@feathersjs/errors": "^5.0.0-pre.34",
|
|
55
|
+
"sift": "^16.0.1"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
|
-
"@feathersjs/adapter-tests": "^5.0.0-pre.
|
|
59
|
-
"@feathersjs/feathers": "^5.0.0-pre.
|
|
60
|
-
"@types/mocha": "^10.0.
|
|
61
|
-
"@types/node": "^18.
|
|
62
|
-
"mocha": "^10.
|
|
58
|
+
"@feathersjs/adapter-tests": "^5.0.0-pre.34",
|
|
59
|
+
"@feathersjs/feathers": "^5.0.0-pre.34",
|
|
60
|
+
"@types/mocha": "^10.0.1",
|
|
61
|
+
"@types/node": "^18.11.10",
|
|
62
|
+
"mocha": "^10.1.0",
|
|
63
63
|
"shx": "^0.3.4",
|
|
64
64
|
"ts-node": "^10.9.1",
|
|
65
|
-
"typescript": "^4.
|
|
65
|
+
"typescript": "^4.9.3"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "42cca600d00f0b3b9d89fa79be30fcd46bc50132"
|
|
68
68
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { NotFound } from '@feathersjs/errors'
|
|
1
|
+
import { BadRequest, MethodNotAllowed, NotFound } from '@feathersjs/errors'
|
|
2
2
|
import { _ } from '@feathersjs/commons'
|
|
3
3
|
import {
|
|
4
4
|
sorter,
|
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
AdapterParams
|
|
10
10
|
} from '@feathersjs/adapter-commons'
|
|
11
11
|
import sift from 'sift'
|
|
12
|
-
import { NullableId, Id, Params,
|
|
12
|
+
import { NullableId, Id, Params, Paginated } from '@feathersjs/feathers'
|
|
13
13
|
|
|
14
14
|
export interface MemoryServiceStore<T> {
|
|
15
15
|
[key: string]: T
|
|
@@ -28,16 +28,16 @@ const _select = (data: any, params: any, ...args: any[]) => {
|
|
|
28
28
|
return base(JSON.parse(JSON.stringify(data)))
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
export class MemoryAdapter<
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
> {
|
|
37
|
-
store: MemoryServiceStore<
|
|
31
|
+
export class MemoryAdapter<
|
|
32
|
+
Result = any,
|
|
33
|
+
Data = Partial<Result>,
|
|
34
|
+
ServiceParams extends Params = Params,
|
|
35
|
+
PatchData = Partial<Data>
|
|
36
|
+
> extends AdapterBase<Result, Data, PatchData, ServiceParams, MemoryServiceOptions<Result>> {
|
|
37
|
+
store: MemoryServiceStore<Result>
|
|
38
38
|
_uId: number
|
|
39
39
|
|
|
40
|
-
constructor(options: MemoryServiceOptions<
|
|
40
|
+
constructor(options: MemoryServiceOptions<Result> = {}) {
|
|
41
41
|
super({
|
|
42
42
|
id: 'id',
|
|
43
43
|
matcher: sift,
|
|
@@ -50,16 +50,16 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
50
50
|
this.store = { ...this.options.store }
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
async getEntries(_params?:
|
|
54
|
-
const params = _params || ({} as
|
|
53
|
+
async getEntries(_params?: ServiceParams) {
|
|
54
|
+
const params = _params || ({} as ServiceParams)
|
|
55
55
|
|
|
56
|
-
return this
|
|
56
|
+
return this._find({
|
|
57
57
|
...params,
|
|
58
58
|
paginate: false
|
|
59
59
|
})
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
getQuery(params:
|
|
62
|
+
getQuery(params: ServiceParams) {
|
|
63
63
|
const { $skip, $sort, $limit, $select, ...query } = params.query || {}
|
|
64
64
|
|
|
65
65
|
return {
|
|
@@ -68,33 +68,56 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
async
|
|
72
|
-
async
|
|
73
|
-
async
|
|
74
|
-
async
|
|
71
|
+
async _find(_params?: ServiceParams & { paginate?: PaginationOptions }): Promise<Paginated<Result>>
|
|
72
|
+
async _find(_params?: ServiceParams & { paginate: false }): Promise<Result[]>
|
|
73
|
+
async _find(_params?: ServiceParams): Promise<Paginated<Result> | Result[]>
|
|
74
|
+
async _find(params: ServiceParams = {} as ServiceParams): Promise<Paginated<Result> | Result[]> {
|
|
75
75
|
const { paginate } = this.getOptions(params)
|
|
76
76
|
const { query, filters } = this.getQuery(params)
|
|
77
77
|
|
|
78
|
-
let values = _.values(this.store)
|
|
78
|
+
let values = _.values(this.store)
|
|
79
79
|
const total = values.length
|
|
80
|
+
const hasSkip = filters.$skip !== undefined
|
|
81
|
+
const hasSort = filters.$sort !== undefined
|
|
82
|
+
const hasLimit = filters.$limit !== undefined
|
|
83
|
+
const hasQuery = _.keys(query).length > 0
|
|
80
84
|
|
|
81
|
-
if (
|
|
85
|
+
if (hasSort) {
|
|
82
86
|
values.sort(this.options.sorter(filters.$sort))
|
|
83
87
|
}
|
|
84
88
|
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
if (hasQuery || hasLimit || hasSkip) {
|
|
90
|
+
let skipped = 0
|
|
91
|
+
const matcher = this.options.matcher(query)
|
|
92
|
+
const matched = []
|
|
93
|
+
|
|
94
|
+
for (let index = 0, length = values.length; index < length; index++) {
|
|
95
|
+
const value = values[index]
|
|
96
|
+
|
|
97
|
+
if (hasQuery && !matcher(value, index, values)) {
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (hasSkip && filters.$skip > skipped) {
|
|
102
|
+
skipped++
|
|
103
|
+
continue
|
|
104
|
+
}
|
|
88
105
|
|
|
89
|
-
|
|
90
|
-
|
|
106
|
+
matched.push(_select(value, params))
|
|
107
|
+
|
|
108
|
+
if (hasLimit && filters.$limit === matched.length) {
|
|
109
|
+
break
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
values = matched
|
|
91
114
|
}
|
|
92
115
|
|
|
93
|
-
const result: Paginated<
|
|
94
|
-
total,
|
|
116
|
+
const result: Paginated<Result> = {
|
|
117
|
+
total: hasQuery ? values.length : total,
|
|
95
118
|
limit: filters.$limit,
|
|
96
119
|
skip: filters.$skip || 0,
|
|
97
|
-
data:
|
|
120
|
+
data: filters.$limit === 0 ? [] : values
|
|
98
121
|
}
|
|
99
122
|
|
|
100
123
|
if (!paginate) {
|
|
@@ -104,7 +127,7 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
104
127
|
return result
|
|
105
128
|
}
|
|
106
129
|
|
|
107
|
-
async
|
|
130
|
+
async _get(id: Id, params: ServiceParams = {} as ServiceParams): Promise<Result> {
|
|
108
131
|
const { query } = this.getQuery(params)
|
|
109
132
|
|
|
110
133
|
if (id in this.store) {
|
|
@@ -118,23 +141,30 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
118
141
|
throw new NotFound(`No record found for id '${id}'`)
|
|
119
142
|
}
|
|
120
143
|
|
|
121
|
-
async
|
|
122
|
-
async
|
|
123
|
-
async
|
|
124
|
-
async
|
|
144
|
+
async _create(data: Partial<Data>, params?: ServiceParams): Promise<Result>
|
|
145
|
+
async _create(data: Partial<Data>[], params?: ServiceParams): Promise<Result[]>
|
|
146
|
+
async _create(data: Partial<Data> | Partial<Data>[], _params?: ServiceParams): Promise<Result | Result[]>
|
|
147
|
+
async _create(
|
|
148
|
+
data: Partial<Data> | Partial<Data>[],
|
|
149
|
+
params: ServiceParams = {} as ServiceParams
|
|
150
|
+
): Promise<Result | Result[]> {
|
|
125
151
|
if (Array.isArray(data)) {
|
|
126
|
-
return Promise.all(data.map((current) => this
|
|
152
|
+
return Promise.all(data.map((current) => this._create(current, params)))
|
|
127
153
|
}
|
|
128
154
|
|
|
129
155
|
const id = (data as any)[this.id] || this._uId++
|
|
130
156
|
const current = _.extend({}, data, { [this.id]: id })
|
|
131
157
|
const result = (this.store[id] = current)
|
|
132
158
|
|
|
133
|
-
return _select(result, params, this.id) as
|
|
159
|
+
return _select(result, params, this.id) as Result
|
|
134
160
|
}
|
|
135
161
|
|
|
136
|
-
async
|
|
137
|
-
|
|
162
|
+
async _update(id: Id, data: Data, params: ServiceParams = {} as ServiceParams): Promise<Result> {
|
|
163
|
+
if (id === null || Array.isArray(data)) {
|
|
164
|
+
throw new BadRequest("You can not replace multiple instances. Did you mean 'patch'?")
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const oldEntry = await this._get(id)
|
|
138
168
|
// We don't want our id to change type if it can be coerced
|
|
139
169
|
const oldId = (oldEntry as any)[this.id]
|
|
140
170
|
|
|
@@ -143,15 +173,23 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
143
173
|
|
|
144
174
|
this.store[id] = _.extend({}, data, { [this.id]: id })
|
|
145
175
|
|
|
146
|
-
return this
|
|
176
|
+
return this._get(id, params)
|
|
147
177
|
}
|
|
148
178
|
|
|
149
|
-
async
|
|
150
|
-
async
|
|
151
|
-
async
|
|
152
|
-
async
|
|
179
|
+
async _patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
|
|
180
|
+
async _patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>
|
|
181
|
+
async _patch(id: NullableId, data: PatchData, _params?: ServiceParams): Promise<Result | Result[]>
|
|
182
|
+
async _patch(
|
|
183
|
+
id: NullableId,
|
|
184
|
+
data: PatchData,
|
|
185
|
+
params: ServiceParams = {} as ServiceParams
|
|
186
|
+
): Promise<Result | Result[]> {
|
|
187
|
+
if (id === null && !this.allowsMulti('patch', params)) {
|
|
188
|
+
throw new MethodNotAllowed('Can not patch multiple entries')
|
|
189
|
+
}
|
|
190
|
+
|
|
153
191
|
const { query } = this.getQuery(params)
|
|
154
|
-
const patchEntry = (entry:
|
|
192
|
+
const patchEntry = (entry: Result) => {
|
|
155
193
|
const currentId = (entry as any)[this.id]
|
|
156
194
|
|
|
157
195
|
this.store[currentId] = _.extend(this.store[currentId], _.omit(data, this.id))
|
|
@@ -168,13 +206,17 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
168
206
|
return entries.map(patchEntry)
|
|
169
207
|
}
|
|
170
208
|
|
|
171
|
-
return patchEntry(await this
|
|
209
|
+
return patchEntry(await this._get(id, params)) // Will throw an error if not found
|
|
172
210
|
}
|
|
173
211
|
|
|
174
|
-
async
|
|
175
|
-
async
|
|
176
|
-
async
|
|
177
|
-
async
|
|
212
|
+
async _remove(id: null, params?: ServiceParams): Promise<Result[]>
|
|
213
|
+
async _remove(id: Id, params?: ServiceParams): Promise<Result>
|
|
214
|
+
async _remove(id: NullableId, _params?: ServiceParams): Promise<Result | Result[]>
|
|
215
|
+
async _remove(id: NullableId, params: ServiceParams = {} as ServiceParams): Promise<Result | Result[]> {
|
|
216
|
+
if (id === null && !this.allowsMulti('remove', params)) {
|
|
217
|
+
throw new MethodNotAllowed('Can not remove multiple entries')
|
|
218
|
+
}
|
|
219
|
+
|
|
178
220
|
const { query } = this.getQuery(params)
|
|
179
221
|
|
|
180
222
|
if (id === null) {
|
|
@@ -183,10 +225,10 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
183
225
|
query
|
|
184
226
|
})
|
|
185
227
|
|
|
186
|
-
return Promise.all(entries.map((current: any) => this
|
|
228
|
+
return Promise.all(entries.map((current: any) => this._remove(current[this.id] as Id, params)))
|
|
187
229
|
}
|
|
188
230
|
|
|
189
|
-
const entry = await this
|
|
231
|
+
const entry = await this._get(id, params)
|
|
190
232
|
|
|
191
233
|
delete this.store[id]
|
|
192
234
|
|
|
@@ -194,41 +236,66 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
|
|
|
194
236
|
}
|
|
195
237
|
}
|
|
196
238
|
|
|
197
|
-
export class MemoryService<
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
async find(params?:
|
|
204
|
-
async find(params?:
|
|
205
|
-
|
|
239
|
+
export class MemoryService<
|
|
240
|
+
Result = any,
|
|
241
|
+
Data = Partial<Result>,
|
|
242
|
+
ServiceParams extends AdapterParams = AdapterParams,
|
|
243
|
+
PatchData = Partial<Data>
|
|
244
|
+
> extends MemoryAdapter<Result, Data, ServiceParams, PatchData> {
|
|
245
|
+
async find(params?: ServiceParams & { paginate?: PaginationOptions }): Promise<Paginated<Result>>
|
|
246
|
+
async find(params?: ServiceParams & { paginate: false }): Promise<Result[]>
|
|
247
|
+
async find(params?: ServiceParams): Promise<Paginated<Result> | Result[]>
|
|
248
|
+
async find(params?: ServiceParams): Promise<Paginated<Result> | Result[]> {
|
|
249
|
+
return this._find({
|
|
250
|
+
...params,
|
|
251
|
+
query: await this.sanitizeQuery(params)
|
|
252
|
+
})
|
|
206
253
|
}
|
|
207
254
|
|
|
208
|
-
async get(id: Id, params?:
|
|
209
|
-
return this._get(id,
|
|
255
|
+
async get(id: Id, params?: ServiceParams): Promise<Result> {
|
|
256
|
+
return this._get(id, {
|
|
257
|
+
...params,
|
|
258
|
+
query: await this.sanitizeQuery(params)
|
|
259
|
+
})
|
|
210
260
|
}
|
|
211
261
|
|
|
212
|
-
async create(data:
|
|
213
|
-
async create(data:
|
|
214
|
-
async create(data:
|
|
262
|
+
async create(data: Data, params?: ServiceParams): Promise<Result>
|
|
263
|
+
async create(data: Data[], params?: ServiceParams): Promise<Result[]>
|
|
264
|
+
async create(data: Data | Data[], params?: ServiceParams): Promise<Result | Result[]> {
|
|
265
|
+
if (Array.isArray(data) && !this.allowsMulti('create', params)) {
|
|
266
|
+
throw new MethodNotAllowed('Can not create multiple entries')
|
|
267
|
+
}
|
|
268
|
+
|
|
215
269
|
return this._create(data, params)
|
|
216
270
|
}
|
|
217
271
|
|
|
218
|
-
async update(id: Id, data:
|
|
219
|
-
return this._update(id, data,
|
|
272
|
+
async update(id: Id, data: Data, params?: ServiceParams): Promise<Result> {
|
|
273
|
+
return this._update(id, data, {
|
|
274
|
+
...params,
|
|
275
|
+
query: await this.sanitizeQuery(params)
|
|
276
|
+
})
|
|
220
277
|
}
|
|
221
278
|
|
|
222
|
-
async patch(id: Id, data:
|
|
223
|
-
async patch(id: null, data:
|
|
224
|
-
async patch(id: NullableId, data:
|
|
225
|
-
|
|
279
|
+
async patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>
|
|
280
|
+
async patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
|
|
281
|
+
async patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]> {
|
|
282
|
+
const { $limit, ...query } = await this.sanitizeQuery(params)
|
|
283
|
+
|
|
284
|
+
return this._patch(id, data, {
|
|
285
|
+
...params,
|
|
286
|
+
query
|
|
287
|
+
})
|
|
226
288
|
}
|
|
227
289
|
|
|
228
|
-
async remove(id: Id, params?:
|
|
229
|
-
async remove(id: null, params?:
|
|
230
|
-
async remove(id: NullableId, params?:
|
|
231
|
-
|
|
290
|
+
async remove(id: Id, params?: ServiceParams): Promise<Result>
|
|
291
|
+
async remove(id: null, params?: ServiceParams): Promise<Result[]>
|
|
292
|
+
async remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]> {
|
|
293
|
+
const { $limit, ...query } = await this.sanitizeQuery(params)
|
|
294
|
+
|
|
295
|
+
return this._remove(id, {
|
|
296
|
+
...params,
|
|
297
|
+
query
|
|
298
|
+
})
|
|
232
299
|
}
|
|
233
300
|
}
|
|
234
301
|
|