@feathersjs/adapter-commons 5.0.0-pre.3 → 5.0.0-pre.31
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 +156 -222
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/lib/declarations.d.ts +146 -0
- package/lib/declarations.js +3 -0
- package/lib/declarations.js.map +1 -0
- package/lib/index.d.ts +5 -3
- package/lib/index.js +18 -17
- package/lib/index.js.map +1 -1
- package/lib/query.d.ts +21 -0
- package/lib/query.js +115 -0
- package/lib/query.js.map +1 -0
- package/lib/service.d.ts +86 -62
- package/lib/service.js +129 -65
- package/lib/service.js.map +1 -1
- package/lib/sort.d.ts +6 -4
- package/lib/sort.js +35 -47
- package/lib/sort.js.map +1 -1
- package/package.json +18 -16
- package/src/declarations.ts +156 -0
- package/src/index.ts +15 -18
- package/src/query.ts +137 -0
- package/src/service.ts +209 -151
- package/src/sort.ts +86 -66
- package/lib/filter-query.d.ts +0 -10
- package/lib/filter-query.js +0 -96
- package/lib/filter-query.js.map +0 -1
- package/src/filter-query.ts +0 -116
package/src/service.ts
CHANGED
|
@@ -1,82 +1,194 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const callMethod = (self: any, name: any, ...args: any[]) => {
|
|
13
|
-
if (typeof self[name] !== 'function') {
|
|
14
|
-
return Promise.reject(new NotImplemented(`Method ${name} not available`));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return self[name](...args);
|
|
18
|
-
};
|
|
1
|
+
import { BadRequest, MethodNotAllowed } from '@feathersjs/errors'
|
|
2
|
+
import { Id, NullableId, Paginated, Query } from '@feathersjs/feathers'
|
|
3
|
+
import {
|
|
4
|
+
AdapterParams,
|
|
5
|
+
AdapterServiceOptions,
|
|
6
|
+
InternalServiceMethods,
|
|
7
|
+
PaginationOptions
|
|
8
|
+
} from './declarations'
|
|
9
|
+
import { filterQuery } from './query'
|
|
19
10
|
|
|
20
11
|
const alwaysMulti: { [key: string]: boolean } = {
|
|
21
12
|
find: true,
|
|
22
13
|
get: false,
|
|
23
14
|
update: false
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export interface ServiceOptions {
|
|
27
|
-
events: string[];
|
|
28
|
-
multi: boolean|string[];
|
|
29
|
-
id: string;
|
|
30
|
-
paginate: {
|
|
31
|
-
default?: number;
|
|
32
|
-
max?: number;
|
|
33
|
-
}
|
|
34
|
-
whitelist: string[];
|
|
35
|
-
filters: string[];
|
|
36
15
|
}
|
|
37
16
|
|
|
38
17
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
* from the service and don't want to trigger any of its hooks.
|
|
42
|
-
*
|
|
43
|
-
* Important: These methods are only available internally on the server, not on the client
|
|
44
|
-
* side and only for the Feathers database adapters.
|
|
45
|
-
*
|
|
46
|
-
* These methods do not trigger events.
|
|
47
|
-
*
|
|
48
|
-
* @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
|
|
18
|
+
* An abstract base class that a database adapter can extend from to implement the
|
|
19
|
+
* `__find`, `__get`, `__update`, `__patch` and `__remove` methods.
|
|
49
20
|
*/
|
|
50
|
-
export
|
|
21
|
+
export abstract class AdapterBase<
|
|
22
|
+
T = any,
|
|
23
|
+
D = Partial<T>,
|
|
24
|
+
P extends AdapterParams = AdapterParams,
|
|
25
|
+
O extends AdapterServiceOptions = AdapterServiceOptions
|
|
26
|
+
> implements InternalServiceMethods<T, D, P>
|
|
27
|
+
{
|
|
28
|
+
options: O
|
|
29
|
+
|
|
30
|
+
constructor(options: O) {
|
|
31
|
+
this.options = {
|
|
32
|
+
id: 'id',
|
|
33
|
+
events: [],
|
|
34
|
+
paginate: false,
|
|
35
|
+
multi: false,
|
|
36
|
+
filters: {},
|
|
37
|
+
operators: [],
|
|
38
|
+
...options
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
get id() {
|
|
43
|
+
return this.options.id
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get events() {
|
|
47
|
+
return this.options.events
|
|
48
|
+
}
|
|
51
49
|
|
|
52
50
|
/**
|
|
53
|
-
*
|
|
51
|
+
* Check if this adapter allows multiple updates for a method.
|
|
52
|
+
* @param method The method name to check.
|
|
53
|
+
* @param params The service call params.
|
|
54
|
+
* @returns Wether or not multiple updates are allowed.
|
|
55
|
+
*/
|
|
56
|
+
allowsMulti(method: string, params: P = {} as P) {
|
|
57
|
+
const always = alwaysMulti[method]
|
|
58
|
+
|
|
59
|
+
if (typeof always !== 'undefined') {
|
|
60
|
+
return always
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const { multi } = this.getOptions(params)
|
|
64
|
+
|
|
65
|
+
if (multi === true || multi === false) {
|
|
66
|
+
return multi
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return multi.includes(method)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Returns the combined options for a service call. Options will be merged
|
|
74
|
+
* with `this.options` and `params.adapter` for dynamic overrides.
|
|
75
|
+
*
|
|
76
|
+
* @param params The parameters for the service method call
|
|
77
|
+
* @returns The actual options for this call
|
|
78
|
+
*/
|
|
79
|
+
getOptions(params: P): O {
|
|
80
|
+
const paginate = params.paginate !== undefined ? params.paginate : this.options.paginate
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
...this.options,
|
|
84
|
+
paginate,
|
|
85
|
+
...params.adapter
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Sanitize the incoming data, e.g. removing invalid keywords etc.
|
|
91
|
+
*
|
|
92
|
+
* @param data The data to sanitize
|
|
93
|
+
* @param _params Service call parameters
|
|
94
|
+
* @returns The sanitized data
|
|
95
|
+
*/
|
|
96
|
+
async sanitizeData<X = Partial<D>>(data: X, _params: P) {
|
|
97
|
+
return data
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns a sanitized version of `params.query`, converting filter values
|
|
102
|
+
* (like $limit and $skip) into the expected type. Will throw an error if
|
|
103
|
+
* a `$` prefixed filter or operator value that is not allowed in `filters`
|
|
104
|
+
* or `operators` is encountered.
|
|
105
|
+
*
|
|
106
|
+
* @param params The service call parameter.
|
|
107
|
+
* @returns A new object containing the sanitized query.
|
|
108
|
+
*/
|
|
109
|
+
async sanitizeQuery(params: P = {} as P): Promise<Query> {
|
|
110
|
+
const options = this.getOptions(params)
|
|
111
|
+
const { query, filters } = filterQuery(params.query, options)
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
...filters,
|
|
115
|
+
...query
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
abstract $find(_params?: P & { paginate?: PaginationOptions }): Promise<Paginated<T>>
|
|
120
|
+
abstract $find(_params?: P & { paginate: false }): Promise<T[]>
|
|
121
|
+
abstract $find(params?: P): Promise<T[] | Paginated<T>>
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Retrieve all resources from this service, skipping any service-level hooks but sanitize the query
|
|
125
|
+
* with allowed filters and properties by calling `sanitizeQuery`.
|
|
54
126
|
*
|
|
55
127
|
* @param params - Service call parameters {@link Params}
|
|
56
128
|
* @see {@link HookLessServiceMethods}
|
|
57
129
|
* @see {@link https://docs.feathersjs.com/api/services.html#find-params|Feathers API Documentation: .find(params)}
|
|
58
130
|
*/
|
|
59
|
-
_find
|
|
131
|
+
async _find(_params?: P & { paginate?: PaginationOptions }): Promise<Paginated<T>>
|
|
132
|
+
async _find(_params?: P & { paginate: false }): Promise<T[]>
|
|
133
|
+
async _find(params?: P): Promise<T | T[] | Paginated<T>>
|
|
134
|
+
async _find(params?: P): Promise<T | T[] | Paginated<T>> {
|
|
135
|
+
const query = await this.sanitizeQuery(params)
|
|
136
|
+
|
|
137
|
+
return this.$find({
|
|
138
|
+
...params,
|
|
139
|
+
query
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
abstract $get(id: Id, params?: P): Promise<T>
|
|
60
144
|
|
|
61
145
|
/**
|
|
62
|
-
* Retrieve a single resource matching the given ID, skipping any service-level hooks
|
|
146
|
+
* Retrieve a single resource matching the given ID, skipping any service-level hooks but sanitize the query
|
|
147
|
+
* with allowed filters and properties by calling `sanitizeQuery`.
|
|
63
148
|
*
|
|
64
149
|
* @param id - ID of the resource to locate
|
|
65
150
|
* @param params - Service call parameters {@link Params}
|
|
66
151
|
* @see {@link HookLessServiceMethods}
|
|
67
152
|
* @see {@link https://docs.feathersjs.com/api/services.html#get-id-params|Feathers API Documentation: .get(id, params)}
|
|
68
153
|
*/
|
|
69
|
-
_get
|
|
154
|
+
async _get(id: Id, params?: P): Promise<T> {
|
|
155
|
+
const query = await this.sanitizeQuery(params)
|
|
156
|
+
|
|
157
|
+
return this.$get(id, {
|
|
158
|
+
...params,
|
|
159
|
+
query
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
abstract $create(data: Partial<D>, params?: P): Promise<T>
|
|
164
|
+
abstract $create(data: Partial<D>[], params?: P): Promise<T[]>
|
|
165
|
+
abstract $create(data: Partial<D> | Partial<D>[], params?: P): Promise<T | T[]>
|
|
70
166
|
|
|
71
167
|
/**
|
|
72
|
-
* Create a new resource for this service, skipping any service-level hooks
|
|
168
|
+
* Create a new resource for this service, skipping any service-level hooks, sanitize the data
|
|
169
|
+
* and check if multiple updates are allowed.
|
|
73
170
|
*
|
|
74
171
|
* @param data - Data to insert into this service.
|
|
75
172
|
* @param params - Service call parameters {@link Params}
|
|
76
173
|
* @see {@link HookLessServiceMethods}
|
|
77
174
|
* @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
|
|
78
175
|
*/
|
|
79
|
-
_create
|
|
176
|
+
async _create(data: Partial<D>, params?: P): Promise<T>
|
|
177
|
+
async _create(data: Partial<D>[], params?: P): Promise<T[]>
|
|
178
|
+
async _create(data: Partial<D> | Partial<D>[], params?: P): Promise<T | T[]>
|
|
179
|
+
async _create(data: Partial<D> | Partial<D>[], params?: P): Promise<T | T[]> {
|
|
180
|
+
if (Array.isArray(data) && !this.allowsMulti('create', params)) {
|
|
181
|
+
throw new MethodNotAllowed('Can not create multiple entries')
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const payload = Array.isArray(data)
|
|
185
|
+
? await Promise.all(data.map((current) => this.sanitizeData(current, params)))
|
|
186
|
+
: await this.sanitizeData(data, params)
|
|
187
|
+
|
|
188
|
+
return this.$create(payload, params)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
abstract $update(id: Id, data: D, params?: P): Promise<T>
|
|
80
192
|
|
|
81
193
|
/**
|
|
82
194
|
* Replace any resources matching the given ID with the given data, skipping any service-level hooks.
|
|
@@ -87,10 +199,27 @@ export interface InternalServiceMethods<T = any> {
|
|
|
87
199
|
* @see {@link HookLessServiceMethods}
|
|
88
200
|
* @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
|
|
89
201
|
*/
|
|
90
|
-
_update
|
|
202
|
+
async _update(id: Id, data: D, params?: P): Promise<T> {
|
|
203
|
+
if (id === null || Array.isArray(data)) {
|
|
204
|
+
throw new BadRequest("You can not replace multiple instances. Did you mean 'patch'?")
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const payload = await this.sanitizeData(data, params)
|
|
208
|
+
const query = await this.sanitizeQuery(params)
|
|
209
|
+
|
|
210
|
+
return this.$update(id, payload, {
|
|
211
|
+
...params,
|
|
212
|
+
query
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
abstract $patch(id: null, data: Partial<D>, params?: P): Promise<T[]>
|
|
217
|
+
abstract $patch(id: Id, data: Partial<D>, params?: P): Promise<T>
|
|
218
|
+
abstract $patch(id: NullableId, data: Partial<D>, params?: P): Promise<T | T[]>
|
|
91
219
|
|
|
92
220
|
/**
|
|
93
221
|
* Merge any resources matching the given ID with the given data, skipping any service-level hooks.
|
|
222
|
+
* Sanitizes the query and data and checks it multiple updates are allowed.
|
|
94
223
|
*
|
|
95
224
|
* @param id - ID of the resource to be patched
|
|
96
225
|
* @param data - Data to merge with the current resource.
|
|
@@ -98,120 +227,49 @@ export interface InternalServiceMethods<T = any> {
|
|
|
98
227
|
* @see {@link HookLessServiceMethods}
|
|
99
228
|
* @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
|
|
100
229
|
*/
|
|
101
|
-
_patch
|
|
230
|
+
async _patch(id: null, data: Partial<D>, params?: P): Promise<T[]>
|
|
231
|
+
async _patch(id: Id, data: Partial<D>, params?: P): Promise<T>
|
|
232
|
+
async _patch(id: NullableId, data: Partial<D>, params?: P): Promise<T | T[]>
|
|
233
|
+
async _patch(id: NullableId, data: Partial<D>, params?: P): Promise<T | T[]> {
|
|
234
|
+
if (id === null && !this.allowsMulti('patch', params)) {
|
|
235
|
+
throw new MethodNotAllowed('Can not patch multiple entries')
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const { $limit, ...query } = await this.sanitizeQuery(params)
|
|
239
|
+
const payload = await this.sanitizeData(data, params)
|
|
240
|
+
|
|
241
|
+
return this.$patch(id, payload, {
|
|
242
|
+
...params,
|
|
243
|
+
query
|
|
244
|
+
})
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
abstract $remove(id: null, params?: P): Promise<T[]>
|
|
248
|
+
abstract $remove(id: Id, params?: P): Promise<T>
|
|
249
|
+
abstract $remove(id: NullableId, params?: P): Promise<T | T[]>
|
|
102
250
|
|
|
103
251
|
/**
|
|
104
252
|
* Remove resources matching the given ID from the this service, skipping any service-level hooks.
|
|
253
|
+
* Sanitized the query and verifies that multiple updates are allowed.
|
|
105
254
|
*
|
|
106
255
|
* @param id - ID of the resource to be removed
|
|
107
256
|
* @param params - Service call parameters {@link Params}
|
|
108
257
|
* @see {@link HookLessServiceMethods}
|
|
109
258
|
* @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
|
|
110
259
|
*/
|
|
111
|
-
_remove
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
constructor (options: Partial<ServiceOptions>) {
|
|
118
|
-
this.options = Object.assign({
|
|
119
|
-
id: 'id',
|
|
120
|
-
events: [],
|
|
121
|
-
paginate: {},
|
|
122
|
-
multi: false,
|
|
123
|
-
filters: [],
|
|
124
|
-
whitelist: []
|
|
125
|
-
}, options);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
get id () {
|
|
129
|
-
return this.options.id;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
get events () {
|
|
133
|
-
return this.options.events;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
filterQuery (params: Params = {}, opts: any = {}) {
|
|
137
|
-
const paginate = typeof params.paginate !== 'undefined'
|
|
138
|
-
? params.paginate : this.options.paginate;
|
|
139
|
-
const { query = {} } = params;
|
|
140
|
-
const options = Object.assign({
|
|
141
|
-
operators: this.options.whitelist || [],
|
|
142
|
-
filters: this.options.filters,
|
|
143
|
-
paginate
|
|
144
|
-
}, opts);
|
|
145
|
-
const result = filterQuery(query, options);
|
|
146
|
-
|
|
147
|
-
return Object.assign(result, { paginate });
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
allowsMulti (method: string) {
|
|
151
|
-
const always = alwaysMulti[method];
|
|
152
|
-
|
|
153
|
-
if (typeof always !== 'undefined') {
|
|
154
|
-
return always;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const option = this.options.multi;
|
|
158
|
-
|
|
159
|
-
if (option === true || option === false) {
|
|
160
|
-
return option;
|
|
161
|
-
} else {
|
|
162
|
-
return option.includes(method);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
find (params?: Params): Promise<T[] | Paginated<T>> {
|
|
167
|
-
return callMethod(this, '_find', params);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
get (id: Id, params?: Params): Promise<T> {
|
|
171
|
-
return callMethod(this, '_get', id, params);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
create (data: Partial<T>, params?: Params): Promise<T>;
|
|
175
|
-
create (data: Partial<T>[], params?: Params): Promise<T[]>;
|
|
176
|
-
create (data: Partial<T> | Partial<T>[], params?: Params): Promise<T | T[]> {
|
|
177
|
-
if (Array.isArray(data) && !this.allowsMulti('create')) {
|
|
178
|
-
return Promise.reject(new MethodNotAllowed('Can not create multiple entries'));
|
|
260
|
+
async _remove(id: null, params?: P): Promise<T[]>
|
|
261
|
+
async _remove(id: Id, params?: P): Promise<T>
|
|
262
|
+
async _remove(id: NullableId, params?: P): Promise<T | T[]>
|
|
263
|
+
async _remove(id: NullableId, params?: P): Promise<T | T[]> {
|
|
264
|
+
if (id === null && !this.allowsMulti('remove', params)) {
|
|
265
|
+
throw new MethodNotAllowed('Can not remove multiple entries')
|
|
179
266
|
}
|
|
180
267
|
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
update (id: Id, data: T, params?: Params): Promise<T> {
|
|
185
|
-
if (id === null || Array.isArray(data)) {
|
|
186
|
-
return Promise.reject(new BadRequest(
|
|
187
|
-
'You can not replace multiple instances. Did you mean \'patch\'?'
|
|
188
|
-
));
|
|
189
|
-
}
|
|
268
|
+
const { $limit, ...query } = await this.sanitizeQuery(params)
|
|
190
269
|
|
|
191
|
-
return
|
|
270
|
+
return this.$remove(id, {
|
|
271
|
+
...params,
|
|
272
|
+
query
|
|
273
|
+
})
|
|
192
274
|
}
|
|
193
|
-
|
|
194
|
-
patch (id: Id, data: Partial<T>, params?: Params): Promise<T>;
|
|
195
|
-
patch (id: null, data: Partial<T>, params?: Params): Promise<T[]>;
|
|
196
|
-
patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;
|
|
197
|
-
patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]> {
|
|
198
|
-
if (id === null && !this.allowsMulti('patch')) {
|
|
199
|
-
return Promise.reject(new MethodNotAllowed('Can not patch multiple entries'));
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return callMethod(this, '_patch', id, data, params);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
remove (id: Id, params?: Params): Promise<T>;
|
|
206
|
-
remove (id: null, params?: Params): Promise<T[]>;
|
|
207
|
-
remove (id: NullableId, params?: Params): Promise<T | T[]>;
|
|
208
|
-
remove (id: NullableId, params?: Params): Promise<T | T[]> {
|
|
209
|
-
if (id === null && !this.allowsMulti('remove')) {
|
|
210
|
-
return Promise.reject(new MethodNotAllowed('Can not remove multiple entries'));
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
return callMethod(this, '_remove', id, params);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
async setup () {}
|
|
217
275
|
}
|
package/src/sort.ts
CHANGED
|
@@ -1,110 +1,130 @@
|
|
|
1
1
|
// Sorting algorithm taken from NeDB (https://github.com/louischatriot/nedb)
|
|
2
2
|
// See https://github.com/louischatriot/nedb/blob/e3f0078499aa1005a59d0c2372e425ab789145c1/lib/model.js#L189
|
|
3
3
|
|
|
4
|
-
export function compareNSB
|
|
5
|
-
if (a < b) {
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
export function compareNSB(a: any, b: any) {
|
|
5
|
+
if (a < b) {
|
|
6
|
+
return -1
|
|
7
|
+
}
|
|
8
|
+
if (a > b) {
|
|
9
|
+
return 1
|
|
10
|
+
}
|
|
11
|
+
return 0
|
|
8
12
|
}
|
|
9
13
|
|
|
10
|
-
export function compareArrays
|
|
11
|
-
let i;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
for (i = 0; i < Math.min(a.length, b.length); i += 1) {
|
|
15
|
-
comp = exports.compare(a[i], b[i]);
|
|
14
|
+
export function compareArrays(a: any[], b: any[]) {
|
|
15
|
+
for (let i = 0, l = Math.min(a.length, b.length); i < l; i++) {
|
|
16
|
+
const comparison = compare(a[i], b[i])
|
|
16
17
|
|
|
17
|
-
if (
|
|
18
|
+
if (comparison !== 0) {
|
|
19
|
+
return comparison
|
|
20
|
+
}
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
// Common section was identical, longest one wins
|
|
21
|
-
return
|
|
24
|
+
return compareNSB(a.length, b.length)
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
export function compare
|
|
25
|
-
|
|
27
|
+
export function compare(a: any, b: any, compareStrings: any = compareNSB): 0 | 1 | -1 {
|
|
28
|
+
if (a === b) {
|
|
29
|
+
return 0
|
|
30
|
+
}
|
|
26
31
|
|
|
27
32
|
// undefined
|
|
28
|
-
if (a === undefined) {
|
|
29
|
-
|
|
33
|
+
if (a === undefined) {
|
|
34
|
+
return -1
|
|
35
|
+
}
|
|
36
|
+
if (b === undefined) {
|
|
37
|
+
return 1
|
|
38
|
+
}
|
|
30
39
|
|
|
31
40
|
// null
|
|
32
|
-
if (a === null) {
|
|
33
|
-
|
|
41
|
+
if (a === null) {
|
|
42
|
+
return -1
|
|
43
|
+
}
|
|
44
|
+
if (b === null) {
|
|
45
|
+
return 1
|
|
46
|
+
}
|
|
34
47
|
|
|
35
48
|
// Numbers
|
|
36
|
-
if (typeof a === 'number') {
|
|
37
|
-
|
|
49
|
+
if (typeof a === 'number') {
|
|
50
|
+
return typeof b === 'number' ? compareNSB(a, b) : -1
|
|
51
|
+
}
|
|
52
|
+
if (typeof b === 'number') {
|
|
53
|
+
return 1
|
|
54
|
+
}
|
|
38
55
|
|
|
39
56
|
// Strings
|
|
40
|
-
if (typeof a === 'string') {
|
|
41
|
-
|
|
57
|
+
if (typeof a === 'string') {
|
|
58
|
+
return typeof b === 'string' ? compareStrings(a, b) : -1
|
|
59
|
+
}
|
|
60
|
+
if (typeof b === 'string') {
|
|
61
|
+
return 1
|
|
62
|
+
}
|
|
42
63
|
|
|
43
64
|
// Booleans
|
|
44
|
-
if (typeof a === 'boolean') {
|
|
45
|
-
|
|
65
|
+
if (typeof a === 'boolean') {
|
|
66
|
+
return typeof b === 'boolean' ? compareNSB(a, b) : -1
|
|
67
|
+
}
|
|
68
|
+
if (typeof b === 'boolean') {
|
|
69
|
+
return 1
|
|
70
|
+
}
|
|
46
71
|
|
|
47
72
|
// Dates
|
|
48
|
-
if (a instanceof Date) {
|
|
49
|
-
|
|
73
|
+
if (a instanceof Date) {
|
|
74
|
+
return b instanceof Date ? compareNSB(a.getTime(), b.getTime()) : -1
|
|
75
|
+
}
|
|
76
|
+
if (b instanceof Date) {
|
|
77
|
+
return 1
|
|
78
|
+
}
|
|
50
79
|
|
|
51
80
|
// Arrays (first element is most significant and so on)
|
|
52
|
-
if (Array.isArray(a)) {
|
|
53
|
-
|
|
81
|
+
if (Array.isArray(a)) {
|
|
82
|
+
return Array.isArray(b) ? compareArrays(a, b) : -1
|
|
83
|
+
}
|
|
84
|
+
if (Array.isArray(b)) {
|
|
85
|
+
return 1
|
|
86
|
+
}
|
|
54
87
|
|
|
55
88
|
// Objects
|
|
56
|
-
const aKeys = Object.keys(a).sort()
|
|
57
|
-
const bKeys = Object.keys(b).sort()
|
|
58
|
-
let comp = 0;
|
|
89
|
+
const aKeys = Object.keys(a).sort()
|
|
90
|
+
const bKeys = Object.keys(b).sort()
|
|
59
91
|
|
|
60
|
-
for (let i = 0
|
|
61
|
-
|
|
92
|
+
for (let i = 0, l = Math.min(aKeys.length, bKeys.length); i < l; i++) {
|
|
93
|
+
const comparison = compare(a[aKeys[i]], b[bKeys[i]])
|
|
62
94
|
|
|
63
|
-
if (
|
|
95
|
+
if (comparison !== 0) {
|
|
96
|
+
return comparison
|
|
97
|
+
}
|
|
64
98
|
}
|
|
65
99
|
|
|
66
|
-
return compareNSB(aKeys.length, bKeys.length)
|
|
100
|
+
return compareNSB(aKeys.length, bKeys.length)
|
|
67
101
|
}
|
|
68
102
|
|
|
69
103
|
// An in-memory sorting function according to the
|
|
70
104
|
// $sort special query parameter
|
|
71
|
-
export function sorter
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const getVal = (a: any, sortKeys: any[]) => {
|
|
75
|
-
let keys = sortKeys.map(key => key);
|
|
76
|
-
let val = a;
|
|
77
|
-
do {
|
|
78
|
-
let key = keys.shift();
|
|
79
|
-
val = val[key];
|
|
80
|
-
} while (keys.length);
|
|
81
|
-
|
|
82
|
-
return val;
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const criteria = Object.keys($sort).map(key => {
|
|
86
|
-
const direction = $sort[key];
|
|
87
|
-
const keys = key.split('.');
|
|
88
|
-
sortLevels = keys.length > 1;
|
|
89
|
-
|
|
90
|
-
return { keys, direction };
|
|
91
|
-
});
|
|
105
|
+
export function sorter($sort: { [key: string]: -1 | 1 }) {
|
|
106
|
+
const get = (value: any, path: string[]) => path.reduce((value, key) => value[key], value)
|
|
92
107
|
|
|
93
|
-
|
|
94
|
-
|
|
108
|
+
const compares = Object.keys($sort).map((key) => {
|
|
109
|
+
const direction = $sort[key]
|
|
110
|
+
const path = key.split('.')
|
|
95
111
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
compare = criterion.direction * exports.compare(getVal(a, criterion.keys), getVal(b, criterion.keys));
|
|
112
|
+
if (path.length === 1) {
|
|
113
|
+
return (a: any, b: any) => direction * compare(a[key], b[key])
|
|
99
114
|
} else {
|
|
100
|
-
|
|
115
|
+
return (a: any, b: any) => direction * compare(get(a, path), get(b, path))
|
|
101
116
|
}
|
|
117
|
+
})
|
|
102
118
|
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
return function (a: any, b: any) {
|
|
120
|
+
for (const compare of compares) {
|
|
121
|
+
const comparasion = compare(a, b)
|
|
122
|
+
|
|
123
|
+
if (comparasion !== 0) {
|
|
124
|
+
return comparasion
|
|
105
125
|
}
|
|
106
126
|
}
|
|
107
127
|
|
|
108
|
-
return 0
|
|
109
|
-
}
|
|
128
|
+
return 0
|
|
129
|
+
}
|
|
110
130
|
}
|
package/lib/filter-query.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export declare const FILTERS: {
|
|
2
|
-
$sort: (value: any) => any;
|
|
3
|
-
$limit: (value: any, options: any) => any;
|
|
4
|
-
$skip: (value: any) => number;
|
|
5
|
-
$select: (value: any) => any;
|
|
6
|
-
};
|
|
7
|
-
export declare const OPERATORS: string[];
|
|
8
|
-
export declare function filterQuery(query: any, options?: any): {
|
|
9
|
-
[key: string]: any;
|
|
10
|
-
};
|