-
-
Notifications
You must be signed in to change notification settings - Fork 795
Expand file tree
/
Copy pathservice.ts
More file actions
217 lines (184 loc) · 7.13 KB
/
service.ts
File metadata and controls
217 lines (184 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { NotImplemented, BadRequest, MethodNotAllowed } from '@feathersjs/errors';
import { ServiceMethods, Params, Id, NullableId } from '@feathersjs/feathers';
import { filterQuery } from './filter-query';
export interface Paginated<T> {
total: number;
limit: number;
skip: number;
data: T[];
}
const callMethod = (self: any, name: any, ...args: any[]) => {
if (typeof self[name] !== 'function') {
return Promise.reject(new NotImplemented(`Method ${name} not available`));
}
return self[name](...args);
};
const alwaysMulti: { [key: string]: boolean } = {
find: true,
get: false,
update: false
};
export interface ServiceOptions {
events: string[];
multi: boolean|string[];
id: string;
paginate: {
default?: number;
max?: number;
}
whitelist: string[];
filters: string[];
}
/**
* Hook-less (internal) service methods. Directly call database adapter service methods
* without running any service-level hooks. This can be useful if you need the raw data
* from the service and don't want to trigger any of its hooks.
*
* Important: These methods are only available internally on the server, not on the client
* side and only for the Feathers database adapters.
*
* These methods do not trigger events.
*
* @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
*/
export interface InternalServiceMethods<T = any> {
/**
* Retrieve all resources from this service, skipping any service-level hooks.
*
* @param params - Service call parameters {@link Params}
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#find-params|Feathers API Documentation: .find(params)}
*/
_find (params?: Params): Promise<T | T[] | Paginated<T>>;
/**
* Retrieve a single resource matching the given ID, skipping any service-level hooks.
*
* @param id - ID of the resource to locate
* @param params - Service call parameters {@link Params}
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#get-id-params|Feathers API Documentation: .get(id, params)}
*/
_get (id: Id, params?: Params): Promise<T>;
/**
* Create a new resource for this service, skipping any service-level hooks.
*
* @param data - Data to insert into this service.
* @param params - Service call parameters {@link Params}
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
*/
_create (data: Partial<T> | Partial<T>[], params?: Params): Promise<T | T[]>;
/**
* Replace any resources matching the given ID with the given data, skipping any service-level hooks.
*
* @param id - ID of the resource to be updated
* @param data - Data to be put in place of the current resource.
* @param params - Service call parameters {@link Params}
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
*/
_update (id: Id, data: T, params?: Params): Promise<T>;
/**
* Merge any resources matching the given ID with the given data, skipping any service-level hooks.
*
* @param id - ID of the resource to be patched
* @param data - Data to merge with the current resource.
* @param params - Service call parameters {@link Params}
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
*/
_patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;
/**
* Remove resources matching the given ID from the this service, skipping any service-level hooks.
*
* @param id - ID of the resource to be removed
* @param params - Service call parameters {@link Params}
* @see {@link HookLessServiceMethods}
* @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
*/
_remove (id: NullableId, params?: Params): Promise<T | T[]>;
}
export class AdapterService<T = any> implements ServiceMethods<T|Paginated<T>> {
options: ServiceOptions;
constructor (options: Partial<ServiceOptions>) {
this.options = Object.assign({
id: 'id',
events: [],
paginate: {},
multi: false,
filters: [],
whitelist: []
}, options);
}
get id () {
return this.options.id;
}
get events () {
return this.options.events;
}
filterQuery (params: Params = {}, opts: any = {}) {
const paginate = typeof params.paginate !== 'undefined'
? params.paginate : this.options.paginate;
const { query = {} } = params;
const options = Object.assign({
operators: this.options.whitelist || [],
filters: this.options.filters,
paginate
}, opts);
const result = filterQuery(query, options);
return Object.assign(result, { paginate });
}
allowsMulti (method: string) {
const always = alwaysMulti[method];
if (typeof always !== 'undefined') {
return always;
}
const option = this.options.multi;
if (option === true || option === false) {
return option;
} else {
return option.includes(method);
}
}
find (params?: Params): Promise<T[] | Paginated<T>> {
return callMethod(this, '_find', params);
}
get (id: Id, params?: Params): Promise<T> {
return callMethod(this, '_get', id, params);
}
create (data: Partial<T>, params?: Params): Promise<T>;
create (data: Partial<T>[], params?: Params): Promise<T[]>;
create (data: Partial<T> | Partial<T>[], params?: Params): Promise<T | T[]> {
if (Array.isArray(data) && !this.allowsMulti('create')) {
return Promise.reject(new MethodNotAllowed('Can not create multiple entries'));
}
return callMethod(this, '_create', data, params);
}
update (id: Id, data: T, params?: Params): Promise<T> {
if (id === null || Array.isArray(data)) {
return Promise.reject(new BadRequest(
'You can not replace multiple instances. Did you mean \'patch\'?'
));
}
return callMethod(this, '_update', id, data, params);
}
patch (id: Id, data: Partial<T>, params?: Params): Promise<T>;
patch (id: null, data: Partial<T>, params?: Params): Promise<T[]>;
patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]>;
patch (id: NullableId, data: Partial<T>, params?: Params): Promise<T | T[]> {
if (id === null && !this.allowsMulti('patch')) {
return Promise.reject(new MethodNotAllowed('Can not patch multiple entries'));
}
return callMethod(this, '_patch', id, data, params);
}
remove (id: Id, params?: Params): Promise<T>;
remove (id: null, params?: Params): Promise<T[]>;
remove (id: NullableId, params?: Params): Promise<T | T[]>;
remove (id: NullableId, params?: Params): Promise<T | T[]> {
if (id === null && !this.allowsMulti('remove')) {
return Promise.reject(new MethodNotAllowed('Can not remove multiple entries'));
}
return callMethod(this, '_remove', id, params);
}
async setup () {}
}