@feathersjs/adapter-commons 5.0.0-pre.16 → 5.0.0-pre.19

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 CHANGED
@@ -3,6 +3,49 @@
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.19](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.18...v5.0.0-pre.19) (2022-05-01)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **adapter-commons:** Clarify adapter query filtering ([#2607](https://github.com/feathersjs/feathers/issues/2607)) ([2dac771](https://github.com/feathersjs/feathers/commit/2dac771b0a3298d6dd25994d05186701b0617718))
12
+
13
+
14
+ ### Features
15
+
16
+ * **mongodb:** Add feathers-mongodb adapter as @feathersjs/mongodb ([#2610](https://github.com/feathersjs/feathers/issues/2610)) ([6d43734](https://github.com/feathersjs/feathers/commit/6d43734a53db02c435cafc52a22dca414e5d0940))
17
+ * **typescript:** Improve adapter typings ([#2605](https://github.com/feathersjs/feathers/issues/2605)) ([3b2ca0a](https://github.com/feathersjs/feathers/commit/3b2ca0a6a8e03e8390272c4d7e930b4bffdaacf5))
18
+ * **typescript:** Improve params and query typeability ([#2600](https://github.com/feathersjs/feathers/issues/2600)) ([df28b76](https://github.com/feathersjs/feathers/commit/df28b7619161f1df5e700326f52cca1a92dc5d28))
19
+
20
+
21
+ ### BREAKING CHANGES
22
+
23
+ * **adapter-commons:** Changes the common adapter base class to use `sanitizeQuery` and `sanitizeData`
24
+
25
+
26
+
27
+
28
+
29
+ # [5.0.0-pre.18](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.17...v5.0.0-pre.18) (2022-04-11)
30
+
31
+
32
+ ### Features
33
+
34
+ * **core:** Add app.teardown functionality ([#2570](https://github.com/feathersjs/feathers/issues/2570)) ([fcdf524](https://github.com/feathersjs/feathers/commit/fcdf524ae1995bb59265d39f12e98b7794bed023))
35
+ * **core:** Finalize app.teardown() functionality ([#2584](https://github.com/feathersjs/feathers/issues/2584)) ([1a166f3](https://github.com/feathersjs/feathers/commit/1a166f3ded811ecacf0ae8cb67880bc9fa2eeafa))
36
+
37
+
38
+
39
+
40
+
41
+ # [5.0.0-pre.17](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.16...v5.0.0-pre.17) (2022-02-15)
42
+
43
+ **Note:** Version bump only for package @feathersjs/adapter-commons
44
+
45
+
46
+
47
+
48
+
6
49
  # [5.0.0-pre.16](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.15...v5.0.0-pre.16) (2022-01-12)
7
50
 
8
51
  **Note:** Version bump only for package @feathersjs/adapter-commons
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 Feathers
3
+ Copyright (c) 2022 Feathers
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Feathers Adapter Commons
2
2
 
3
3
  [![CI](https://github.com/feathersjs/feathers/workflows/Node.js%20CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3A%22Node.js+CI%22)
4
- [![Dependency Status](https://img.shields.io/david/feathersjs/feathers.svg?style=flat-square&path=packages/adapter-commons)](https://david-dm.org/feathersjs/feathers?path=packages/adapter-commons)
5
4
  [![Download Status](https://img.shields.io/npm/dm/@feathersjs/adapter-commons.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/adapter-commons)
5
+ [![Discord](https://badgen.net/badge/icon/discord?icon=discord&label)](https://discord.gg/qa8kez8QBx)
6
6
 
7
7
  > Shared utility functions for Feathers adatabase adapters
8
8
 
@@ -17,6 +17,6 @@ This is a repository for handling Feathers common database syntax. See the [API
17
17
 
18
18
  ## License
19
19
 
20
- Copyright (c) 2021 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
20
+ Copyright (c) 2022 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
21
21
 
22
22
  Licensed under the [MIT license](LICENSE).
@@ -0,0 +1,146 @@
1
+ import { Query, Params, Paginated, Id, NullableId } from '@feathersjs/feathers';
2
+ export declare type FilterQueryOptions = {
3
+ filters?: FilterSettings;
4
+ operators?: string[];
5
+ paginate?: PaginationParams;
6
+ };
7
+ export declare type QueryFilter = (value: any, options: FilterQueryOptions) => any;
8
+ export declare type FilterSettings = {
9
+ [key: string]: QueryFilter | true;
10
+ };
11
+ export interface PaginationOptions {
12
+ default?: number;
13
+ max?: number;
14
+ }
15
+ export declare type PaginationParams = false | PaginationOptions;
16
+ export interface AdapterServiceOptions {
17
+ /**
18
+ * Whether to allow multiple updates for everything (`true`) or specific methods (e.g. `['create', 'remove']`)
19
+ */
20
+ multi?: boolean | string[];
21
+ /**
22
+ * The name of the id property
23
+ */
24
+ id?: string;
25
+ /**
26
+ * Pagination settings for this service
27
+ */
28
+ paginate?: PaginationParams;
29
+ /**
30
+ * A list of additional property query operators to allow in a query
31
+ */
32
+ operators?: string[];
33
+ /**
34
+ * An object of additional top level query filters, e.g. `{ $populate: true }`
35
+ * Can also be a converter function like `{ $ignoreCase: (value) => value === 'true' ? true : false }`
36
+ */
37
+ filters?: FilterSettings;
38
+ /**
39
+ * @deprecated Use service `events` option when registering the service with `app.use`.
40
+ */
41
+ events?: string[];
42
+ /**
43
+ * @deprecated renamed to `operators`.
44
+ */
45
+ whitelist?: string[];
46
+ }
47
+ export interface AdapterQuery extends Query {
48
+ $limit?: number;
49
+ $skip?: number;
50
+ $select?: string[];
51
+ $sort?: {
52
+ [key: string]: 1 | -1;
53
+ };
54
+ }
55
+ /**
56
+ * Additional `params` that can be passed to an adapter service method call.
57
+ */
58
+ export interface AdapterParams<Q = AdapterQuery, A extends Partial<AdapterServiceOptions> = Partial<AdapterServiceOptions>> extends Params<Q> {
59
+ adapter?: A;
60
+ paginate?: PaginationParams;
61
+ }
62
+ /**
63
+ * Hook-less (internal) service methods. Directly call database adapter service methods
64
+ * without running any service-level hooks or sanitization. This can be useful if you need the raw data
65
+ * from the service and don't want to trigger any of its hooks.
66
+ *
67
+ * Important: These methods are only available internally on the server, not on the client
68
+ * side and only for the Feathers database adapters.
69
+ *
70
+ * These methods do not trigger events.
71
+ *
72
+ * @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
73
+ */
74
+ export interface InternalServiceMethods<T = any, D = Partial<T>, P extends AdapterParams = AdapterParams> {
75
+ /**
76
+ * Retrieve all resources from this service.
77
+ * Does not sanitize the query and should only be used on the server.
78
+ *
79
+ * @param _params - Service call parameters {@link Params}
80
+ */
81
+ $find(_params?: P & {
82
+ paginate?: PaginationOptions;
83
+ }): Promise<Paginated<T>>;
84
+ $find(_params?: P & {
85
+ paginate: false;
86
+ }): Promise<T[]>;
87
+ $find(params?: P): Promise<T[] | Paginated<T>>;
88
+ /**
89
+ * Retrieve a single resource matching the given ID, skipping any service-level hooks.
90
+ * Does not sanitize the query and should only be used on the server.
91
+ *
92
+ * @param id - ID of the resource to locate
93
+ * @param params - Service call parameters {@link Params}
94
+ * @see {@link HookLessServiceMethods}
95
+ * @see {@link https://docs.feathersjs.com/api/services.html#get-id-params|Feathers API Documentation: .get(id, params)}
96
+ */
97
+ $get(id: Id, params?: P): Promise<T>;
98
+ /**
99
+ * Create a new resource for this service, skipping any service-level hooks.
100
+ * Does not sanitize data or checks if multiple updates are allowed and should only be used on the server.
101
+ *
102
+ * @param data - Data to insert into this service.
103
+ * @param params - Service call parameters {@link Params}
104
+ * @see {@link HookLessServiceMethods}
105
+ * @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
106
+ */
107
+ $create(data: Partial<D>, params?: P): Promise<T>;
108
+ $create(data: Partial<D>[], params?: P): Promise<T[]>;
109
+ $create(data: Partial<D> | Partial<D>[], params?: P): Promise<T | T[]>;
110
+ /**
111
+ * Completely replace the resource identified by id, skipping any service-level hooks.
112
+ * Does not sanitize data or query and should only be used on the server.
113
+ *
114
+ * @param id - ID of the resource to be updated
115
+ * @param data - Data to be put in place of the current resource.
116
+ * @param params - Service call parameters {@link Params}
117
+ * @see {@link HookLessServiceMethods}
118
+ * @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
119
+ */
120
+ $update(id: Id, data: D, params?: P): Promise<T>;
121
+ /**
122
+ * Merge any resources matching the given ID with the given data, skipping any service-level hooks.
123
+ * Does not sanitize the data or query and should only be used on the server.
124
+ *
125
+ * @param id - ID of the resource to be patched
126
+ * @param data - Data to merge with the current resource.
127
+ * @param params - Service call parameters {@link Params}
128
+ * @see {@link HookLessServiceMethods}
129
+ * @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
130
+ */
131
+ $patch(id: null, data: Partial<D>, params?: P): Promise<T[]>;
132
+ $patch(id: Id, data: Partial<D>, params?: P): Promise<T>;
133
+ $patch(id: NullableId, data: Partial<D>, params?: P): Promise<T | T[]>;
134
+ /**
135
+ * Remove resources matching the given ID from the this service, skipping any service-level hooks.
136
+ * Does not sanitize query and should only be used on the server.
137
+ *
138
+ * @param id - ID of the resource to be removed
139
+ * @param params - Service call parameters {@link Params}
140
+ * @see {@link HookLessServiceMethods}
141
+ * @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
142
+ */
143
+ $remove(id: null, params?: P): Promise<T[]>;
144
+ $remove(id: Id, params?: P): Promise<T>;
145
+ $remove(id: NullableId, params?: P): Promise<T | T[]>;
146
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=declarations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"declarations.js","sourceRoot":"","sources":["../src/declarations.ts"],"names":[],"mappings":""}
package/lib/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- export { AdapterService, InternalServiceMethods, ServiceOptions, AdapterParams } from './service';
2
- export { filterQuery, FILTERS, OPERATORS } from './filter-query';
1
+ import { Params } from '@feathersjs/feathers';
2
+ export * from './declarations';
3
+ export * from './service';
4
+ export { filterQuery, FILTERS, OPERATORS } from './query';
3
5
  export * from './sort';
4
- export declare function select(params: any, ...otherFields: string[]): (result: any) => any;
6
+ export declare function select(params: Params, ...otherFields: string[]): (result: any) => any;
package/lib/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -10,14 +14,14 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
15
  };
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.select = exports.OPERATORS = exports.FILTERS = exports.filterQuery = exports.AdapterService = void 0;
17
+ exports.select = exports.OPERATORS = exports.FILTERS = exports.filterQuery = void 0;
14
18
  const commons_1 = require("@feathersjs/commons");
15
- var service_1 = require("./service");
16
- Object.defineProperty(exports, "AdapterService", { enumerable: true, get: function () { return service_1.AdapterService; } });
17
- var filter_query_1 = require("./filter-query");
18
- Object.defineProperty(exports, "filterQuery", { enumerable: true, get: function () { return filter_query_1.filterQuery; } });
19
- Object.defineProperty(exports, "FILTERS", { enumerable: true, get: function () { return filter_query_1.FILTERS; } });
20
- Object.defineProperty(exports, "OPERATORS", { enumerable: true, get: function () { return filter_query_1.OPERATORS; } });
19
+ __exportStar(require("./declarations"), exports);
20
+ __exportStar(require("./service"), exports);
21
+ var query_1 = require("./query");
22
+ Object.defineProperty(exports, "filterQuery", { enumerable: true, get: function () { return query_1.filterQuery; } });
23
+ Object.defineProperty(exports, "FILTERS", { enumerable: true, get: function () { return query_1.FILTERS; } });
24
+ Object.defineProperty(exports, "OPERATORS", { enumerable: true, get: function () { return query_1.OPERATORS; } });
21
25
  __exportStar(require("./sort"), exports);
22
26
  // Return a function that filters a result object or array
23
27
  // and picks only the fields passed as `params.query.$select`
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,iDAAwC;AAExC,qCAAkG;AAAzF,yGAAA,cAAc,OAAA;AACvB,+CAAiE;AAAxD,2GAAA,WAAW,OAAA;AAAE,uGAAA,OAAO,OAAA;AAAE,yGAAA,SAAS,OAAA;AACxC,yCAAuB;AAEvB,0DAA0D;AAC1D,6DAA6D;AAC7D,+BAA+B;AAC/B,SAAgB,MAAM,CAAE,MAAW,EAAE,GAAG,WAAqB;;IAC3D,MAAM,WAAW,GAAyB,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,KAAK,0CAAE,OAAO,CAAC;IAEjE,IAAI,CAAC,WAAW,EAAE;QAChB,OAAO,CAAC,MAAW,EAAE,EAAE,CAAC,MAAM,CAAC;KAChC;IAED,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACrD,MAAM,OAAO,GAAG,CAAC,MAAW,EAAE,EAAE,CAAC,WAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,CAAC;IAEjE,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACzB,OAAO,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;SAC5B;QAED,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC,CAAC;AACJ,CAAC;AAjBD,wBAiBC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,iDAAwC;AAGxC,iDAA+B;AAC/B,4CAA0B;AAC1B,iCAA0D;AAAjD,oGAAA,WAAW,OAAA;AAAE,gGAAA,OAAO,OAAA;AAAE,kGAAA,SAAS,OAAA;AACxC,yCAAuB;AAEvB,0DAA0D;AAC1D,6DAA6D;AAC7D,+BAA+B;AAC/B,SAAgB,MAAM,CAAE,MAAc,EAAE,GAAG,WAAqB;;IAC9D,MAAM,WAAW,GAAyB,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,KAAK,0CAAE,OAAO,CAAC;IAEjE,IAAI,CAAC,WAAW,EAAE;QAChB,OAAO,CAAC,MAAW,EAAE,EAAE,CAAC,MAAM,CAAC;KAChC;IAED,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACrD,MAAM,OAAO,GAAG,CAAC,MAAW,EAAE,EAAE,CAAC,WAAC,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,YAAY,CAAC,CAAC;IAEjE,OAAO,CAAC,MAAW,EAAE,EAAE;QACrB,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YACzB,OAAO,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;SAC5B;QAED,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC,CAAC;AACJ,CAAC;AAjBD,wBAiBC"}
package/lib/query.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import { Query } from '@feathersjs/feathers';
2
+ import { FilterQueryOptions, FilterSettings } from './declarations';
3
+ export declare const OPERATORS: string[];
4
+ export declare const FILTERS: FilterSettings;
5
+ /**
6
+ * Converts Feathers special query parameters and pagination settings
7
+ * and returns them separately as `filters` and the rest of the query
8
+ * as `query`. `options` also gets passed the pagination settings and
9
+ * a list of additional `operators` to allow when querying properties.
10
+ *
11
+ * @param query The initial query
12
+ * @param options Options for filtering the query
13
+ * @returns An object with `query` which contains the query without `filters`
14
+ * and `filters` which contains the converted values for each filter.
15
+ */
16
+ export declare function filterQuery(_query: Query, options?: FilterQueryOptions): {
17
+ filters: {
18
+ [key: string]: any;
19
+ };
20
+ query: Query;
21
+ };
package/lib/query.js ADDED
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterQuery = exports.FILTERS = exports.OPERATORS = void 0;
4
+ const commons_1 = require("@feathersjs/commons");
5
+ const errors_1 = require("@feathersjs/errors");
6
+ const parse = (value) => typeof value !== 'undefined' ? parseInt(value, 10) : value;
7
+ const isPlainObject = (value) => commons_1._.isObject(value) && value.constructor === {}.constructor;
8
+ const validateQueryProperty = (query, operators = []) => {
9
+ if (!isPlainObject(query)) {
10
+ return query;
11
+ }
12
+ for (const key of Object.keys(query)) {
13
+ if (key.startsWith('$') && !operators.includes(key)) {
14
+ throw new errors_1.BadRequest(`Invalid query parameter ${key}`, query);
15
+ }
16
+ const value = query[key];
17
+ if (isPlainObject(value)) {
18
+ query[key] = validateQueryProperty(value, operators);
19
+ }
20
+ }
21
+ return {
22
+ ...query
23
+ };
24
+ };
25
+ const getFilters = (query, settings) => {
26
+ const filterNames = Object.keys(settings.filters);
27
+ return filterNames.reduce((current, key) => {
28
+ const queryValue = query[key];
29
+ const filter = settings.filters[key];
30
+ if (filter) {
31
+ const value = typeof filter === 'function' ? filter(queryValue, settings) : queryValue;
32
+ if (value !== undefined) {
33
+ current[key] = value;
34
+ }
35
+ }
36
+ return current;
37
+ }, {});
38
+ };
39
+ const getQuery = (query, settings) => {
40
+ const keys = Object.keys(query).concat(Object.getOwnPropertySymbols(query));
41
+ return keys.reduce((result, key) => {
42
+ if (typeof key === 'string' && key.startsWith('$')) {
43
+ if (settings.filters[key] === undefined) {
44
+ throw new errors_1.BadRequest(`Invalid filter value ${key}`);
45
+ }
46
+ }
47
+ else {
48
+ result[key] = validateQueryProperty(query[key], settings.operators);
49
+ }
50
+ return result;
51
+ }, {});
52
+ };
53
+ exports.OPERATORS = ['$in', '$nin', '$lt', '$lte', '$gt', '$gte', '$ne', '$or'];
54
+ exports.FILTERS = {
55
+ $skip: (value) => parse(value),
56
+ $sort: (sort) => {
57
+ if (typeof sort !== 'object' || Array.isArray(sort)) {
58
+ return sort;
59
+ }
60
+ return Object.keys(sort).reduce((result, key) => {
61
+ result[key] = typeof sort[key] === 'object'
62
+ ? sort[key] : parse(sort[key]);
63
+ return result;
64
+ }, {});
65
+ },
66
+ $limit: (_limit, { paginate }) => {
67
+ const limit = parse(_limit);
68
+ if (paginate && (paginate.default || paginate.max)) {
69
+ const base = paginate.default || 0;
70
+ const lower = typeof limit === 'number' && !isNaN(limit) && limit >= 0 ? limit : base;
71
+ const upper = typeof paginate.max === 'number' ? paginate.max : Number.MAX_VALUE;
72
+ return Math.min(lower, upper);
73
+ }
74
+ return limit;
75
+ },
76
+ $select: (select) => {
77
+ if (Array.isArray(select)) {
78
+ return select.map(current => `${current}`);
79
+ }
80
+ return select;
81
+ },
82
+ $or: (or, { operators }) => {
83
+ if (Array.isArray(or)) {
84
+ return or.map(current => validateQueryProperty(current, operators));
85
+ }
86
+ return or;
87
+ }
88
+ };
89
+ /**
90
+ * Converts Feathers special query parameters and pagination settings
91
+ * and returns them separately as `filters` and the rest of the query
92
+ * as `query`. `options` also gets passed the pagination settings and
93
+ * a list of additional `operators` to allow when querying properties.
94
+ *
95
+ * @param query The initial query
96
+ * @param options Options for filtering the query
97
+ * @returns An object with `query` which contains the query without `filters`
98
+ * and `filters` which contains the converted values for each filter.
99
+ */
100
+ function filterQuery(_query, options = {}) {
101
+ const query = _query || {};
102
+ const settings = {
103
+ ...options,
104
+ filters: {
105
+ ...exports.FILTERS,
106
+ ...options.filters
107
+ },
108
+ operators: exports.OPERATORS.concat(options.operators || [])
109
+ };
110
+ return {
111
+ filters: getFilters(query, settings),
112
+ query: getQuery(query, settings)
113
+ };
114
+ }
115
+ exports.filterQuery = filterQuery;
116
+ //# sourceMappingURL=query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";;;AAAA,iDAAwC;AACxC,+CAAgD;AAIhD,MAAM,KAAK,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAEzF,MAAM,aAAa,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,WAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,KAAK,EAAE,CAAC,WAAW,CAAC;AAEhG,MAAM,qBAAqB,GAAG,CAAC,KAAU,EAAE,YAAsB,EAAE,EAAS,EAAE;IAC5E,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE;QACzB,OAAO,KAAK,CAAC;KACd;IAED,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;QACpC,IAAI,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;YACnD,MAAM,IAAI,mBAAU,CAAC,2BAA2B,GAAG,EAAE,EAAE,KAAK,CAAC,CAAC;SAC/D;QAED,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QAEzB,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE;YACxB,KAAK,CAAC,GAAG,CAAC,GAAG,qBAAqB,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;SACtD;KACF;IAED,OAAO;Q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O,EAAE,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC;QACpC,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC;KACjC,CAAA;AACH,CAAC;AAfD,kCAeC"}
package/lib/service.d.ts CHANGED
@@ -1,64 +1,96 @@
1
- import { ServiceMethods, Params, Id, NullableId, Paginated } from '@feathersjs/feathers';
2
- export interface ServiceOptions {
3
- events?: string[];
4
- multi?: boolean | string[];
5
- id?: string;
6
- paginate?: {
7
- default?: number;
8
- max?: number;
9
- };
10
- /**
11
- * @deprecated renamed to `allow`.
12
- */
13
- whitelist?: string[];
14
- allow?: string[];
15
- filters?: string[];
16
- }
17
- export interface AdapterOptions<M = any> extends Pick<ServiceOptions, 'multi' | 'allow' | 'paginate'> {
18
- Model?: M;
19
- }
20
- export interface AdapterParams<M = any> extends Params {
21
- adapter?: Partial<AdapterOptions<M>>;
22
- }
1
+ import { Id, NullableId, Paginated, Query } from '@feathersjs/feathers';
2
+ import { AdapterParams, AdapterServiceOptions, InternalServiceMethods, PaginationOptions } from './declarations';
23
3
  /**
24
- * Hook-less (internal) service methods. Directly call database adapter service methods
25
- * without running any service-level hooks. This can be useful if you need the raw data
26
- * from the service and don't want to trigger any of its hooks.
27
- *
28
- * Important: These methods are only available internally on the server, not on the client
29
- * side and only for the Feathers database adapters.
30
- *
31
- * These methods do not trigger events.
32
- *
33
- * @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
4
+ * An abstract base class that a database adapter can extend from to implement the
5
+ * `__find`, `__get`, `__update`, `__patch` and `__remove` methods.
34
6
  */
35
- export interface InternalServiceMethods<T = any, D = Partial<T>> {
7
+ export declare abstract class AdapterBase<T = any, D = Partial<T>, P extends AdapterParams = AdapterParams, O extends AdapterServiceOptions = AdapterServiceOptions> implements InternalServiceMethods<T, D, P> {
8
+ options: O;
9
+ constructor(options: O);
10
+ get id(): string;
11
+ get events(): string[];
12
+ /**
13
+ * Check if this adapter allows multiple updates for a method.
14
+ * @param method The method name to check.
15
+ * @param params The service call params.
16
+ * @returns Wether or not multiple updates are allowed.
17
+ */
18
+ allowsMulti(method: string, params?: P): boolean;
36
19
  /**
37
- * Retrieve all resources from this service, skipping any service-level hooks.
20
+ * Returns the combined options for a service call. Options will be merged
21
+ * with `this.options` and `params.adapter` for dynamic overrides.
22
+ *
23
+ * @param params The parameters for the service method call
24
+ * @returns The actual options for this call
25
+ */
26
+ getOptions(params: P): O;
27
+ /**
28
+ * Sanitize the incoming data, e.g. removing invalid keywords etc.
29
+ *
30
+ * @param data The data to sanitize
31
+ * @param _params Service call parameters
32
+ * @returns The sanitized data
33
+ */
34
+ sanitizeData<X = Partial<D>>(data: X, _params: P): Promise<X>;
35
+ /**
36
+ * Returns a sanitized version of `params.query`, converting filter values
37
+ * (like $limit and $skip) into the expected type. Will throw an error if
38
+ * a `$` prefixed filter or operator value that is not allowed in `filters`
39
+ * or `operators` is encountered.
40
+ *
41
+ * @param params The service call parameter.
42
+ * @returns A new object containing the sanitized query.
43
+ */
44
+ sanitizeQuery(params?: P): Promise<Query>;
45
+ abstract $find(_params?: P & {
46
+ paginate?: PaginationOptions;
47
+ }): Promise<Paginated<T>>;
48
+ abstract $find(_params?: P & {
49
+ paginate: false;
50
+ }): Promise<T[]>;
51
+ abstract $find(params?: P): Promise<T[] | Paginated<T>>;
52
+ /**
53
+ * Retrieve all resources from this service, skipping any service-level hooks but sanitize the query
54
+ * with allowed filters and properties by calling `sanitizeQuery`.
38
55
  *
39
56
  * @param params - Service call parameters {@link Params}
40
57
  * @see {@link HookLessServiceMethods}
41
58
  * @see {@link https://docs.feathersjs.com/api/services.html#find-params|Feathers API Documentation: .find(params)}
42
59
  */
43
- _find(params?: AdapterParams): Promise<T | T[] | Paginated<T>>;
60
+ _find(_params?: P & {
61
+ paginate?: PaginationOptions;
62
+ }): Promise<Paginated<T>>;
63
+ _find(_params?: P & {
64
+ paginate: false;
65
+ }): Promise<T[]>;
66
+ _find(params?: P): Promise<T | T[] | Paginated<T>>;
67
+ abstract $get(id: Id, params?: P): Promise<T>;
44
68
  /**
45
- * Retrieve a single resource matching the given ID, skipping any service-level hooks.
69
+ * Retrieve a single resource matching the given ID, skipping any service-level hooks but sanitize the query
70
+ * with allowed filters and properties by calling `sanitizeQuery`.
46
71
  *
47
72
  * @param id - ID of the resource to locate
48
73
  * @param params - Service call parameters {@link Params}
49
74
  * @see {@link HookLessServiceMethods}
50
75
  * @see {@link https://docs.feathersjs.com/api/services.html#get-id-params|Feathers API Documentation: .get(id, params)}
51
76
  */
52
- _get(id: Id, params?: AdapterParams): Promise<T>;
77
+ _get(id: Id, params?: P): Promise<T>;
78
+ abstract $create(data: Partial<D>, params?: P): Promise<T>;
79
+ abstract $create(data: Partial<D>[], params?: P): Promise<T[]>;
80
+ abstract $create(data: Partial<D> | Partial<D>[], params?: P): Promise<T | T[]>;
53
81
  /**
54
- * Create a new resource for this service, skipping any service-level hooks.
82
+ * Create a new resource for this service, skipping any service-level hooks, sanitize the data
83
+ * and check if multiple updates are allowed.
55
84
  *
56
85
  * @param data - Data to insert into this service.
57
86
  * @param params - Service call parameters {@link Params}
58
87
  * @see {@link HookLessServiceMethods}
59
88
  * @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
60
89
  */
61
- _create(data: D | D[], params?: AdapterParams): Promise<T | T[]>;
90
+ _create(data: Partial<D>, params?: P): Promise<T>;
91
+ _create(data: Partial<D>[], params?: P): Promise<T[]>;
92
+ _create(data: Partial<D> | Partial<D>[], params?: P): Promise<T | T[]>;
93
+ abstract $update(id: Id, data: D, params?: P): Promise<T>;
62
94
  /**
63
95
  * Replace any resources matching the given ID with the given data, skipping any service-level hooks.
64
96
  *
@@ -68,9 +100,13 @@ export interface InternalServiceMethods<T = any, D = Partial<T>> {
68
100
  * @see {@link HookLessServiceMethods}
69
101
  * @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
70
102
  */
71
- _update(id: Id, data: D, params?: AdapterParams): Promise<T>;
103
+ _update(id: Id, data: D, params?: P): Promise<T>;
104
+ abstract $patch(id: null, data: Partial<D>, params?: P): Promise<T[]>;
105
+ abstract $patch(id: Id, data: Partial<D>, params?: P): Promise<T>;
106
+ abstract $patch(id: NullableId, data: Partial<D>, params?: P): Promise<T | T[]>;
72
107
  /**
73
108
  * Merge any resources matching the given ID with the given data, skipping any service-level hooks.
109
+ * Sanitizes the query and data and checks it multiple updates are allowed.
74
110
  *
75
111
  * @param id - ID of the resource to be patched
76
112
  * @param data - Data to merge with the current resource.
@@ -78,41 +114,22 @@ export interface InternalServiceMethods<T = any, D = Partial<T>> {
78
114
  * @see {@link HookLessServiceMethods}
79
115
  * @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
80
116
  */
81
- _patch(id: NullableId, data: D, params?: AdapterParams): Promise<T | T[]>;
117
+ _patch(id: null, data: Partial<D>, params?: P): Promise<T[]>;
118
+ _patch(id: Id, data: Partial<D>, params?: P): Promise<T>;
119
+ _patch(id: NullableId, data: Partial<D>, params?: P): Promise<T | T[]>;
120
+ abstract $remove(id: null, params?: P): Promise<T[]>;
121
+ abstract $remove(id: Id, params?: P): Promise<T>;
122
+ abstract $remove(id: NullableId, params?: P): Promise<T | T[]>;
82
123
  /**
83
124
  * Remove resources matching the given ID from the this service, skipping any service-level hooks.
125
+ * Sanitized the query and verifies that multiple updates are allowed.
84
126
  *
85
127
  * @param id - ID of the resource to be removed
86
128
  * @param params - Service call parameters {@link Params}
87
129
  * @see {@link HookLessServiceMethods}
88
130
  * @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
89
131
  */
90
- _remove(id: NullableId, params?: AdapterParams): Promise<T | T[]>;
91
- }
92
- export declare class AdapterService<T = any, D = Partial<T>, O extends Partial<ServiceOptions> = Partial<ServiceOptions>> implements ServiceMethods<T | Paginated<T>, D> {
93
- options: ServiceOptions & O;
94
- constructor(options: O);
95
- get id(): string;
96
- get events(): string[];
97
- filterQuery(params?: AdapterParams, opts?: any): {
98
- [key: string]: any;
99
- } & {
100
- paginate: any;
101
- };
102
- allowsMulti(method: string, params?: AdapterParams): boolean;
103
- getOptions(params: AdapterParams): ServiceOptions & {
104
- model?: any;
105
- };
106
- find(params?: AdapterParams): Promise<T[] | Paginated<T>>;
107
- get(id: Id, params?: AdapterParams): Promise<T>;
108
- create(data: Partial<T>, params?: AdapterParams): Promise<T>;
109
- create(data: Partial<T>[], params?: AdapterParams): Promise<T[]>;
110
- update(id: Id, data: D, params?: AdapterParams): Promise<T>;
111
- patch(id: Id, data: Partial<T>, params?: AdapterParams): Promise<T>;
112
- patch(id: null, data: Partial<T>, params?: AdapterParams): Promise<T[]>;
113
- patch(id: NullableId, data: Partial<T>, params?: AdapterParams): Promise<T | T[]>;
114
- remove(id: Id, params?: AdapterParams): Promise<T>;
115
- remove(id: null, params?: AdapterParams): Promise<T[]>;
116
- remove(id: NullableId, params?: AdapterParams): Promise<T | T[]>;
117
- setup(): Promise<void>;
132
+ _remove(id: null, params?: P): Promise<T[]>;
133
+ _remove(id: Id, params?: P): Promise<T>;
134
+ _remove(id: NullableId, params?: P): Promise<T | T[]>;
118
135
  }