@feathersjs/adapter-commons 5.0.0-pre.9 → 5.0.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 Feathers
3
+ Copyright (c) 2023 Feathers Contributors
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
 
@@ -10,13 +10,12 @@
10
10
 
11
11
  This is a repository for handling Feathers common database syntax. See the [API documentation](https://docs.feathersjs.com/api/databases/common.html) for more information.
12
12
 
13
-
14
13
  ## Authors
15
14
 
16
15
  [Feathers contributors](https://github.com/feathersjs/adapter-commons/graphs/contributors)
17
16
 
18
17
  ## License
19
18
 
20
- Copyright (c) 2021 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
19
+ Copyright (c) 2023 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
21
20
 
22
21
  Licensed under the [MIT license](LICENSE).
@@ -0,0 +1,150 @@
1
+ import { Query, Params, Paginated, Id } from '@feathersjs/feathers';
2
+ export type FilterQueryOptions = {
3
+ filters?: FilterSettings;
4
+ operators?: string[];
5
+ paginate?: PaginationParams;
6
+ };
7
+ export type QueryFilter = (value: any, options: FilterQueryOptions) => any;
8
+ export type FilterSettings = {
9
+ [key: string]: QueryFilter | true;
10
+ };
11
+ export interface PaginationOptions {
12
+ default?: number;
13
+ max?: number;
14
+ }
15
+ export 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
+ * @deprecated No longer needed when a query schema is used
33
+ */
34
+ operators?: string[];
35
+ /**
36
+ * An object of additional top level query filters, e.g. `{ $populate: true }`
37
+ * Can also be a converter function like `{ $ignoreCase: (value) => value === 'true' ? true : false }`
38
+ *
39
+ * @deprecated No longer needed when a query schema is used
40
+ */
41
+ filters?: FilterSettings;
42
+ /**
43
+ * @deprecated Use service `events` option when registering the service with `app.use`.
44
+ */
45
+ events?: string[];
46
+ /**
47
+ * @deprecated No longer needed when a query schema is used
48
+ */
49
+ whitelist?: string[];
50
+ }
51
+ export interface AdapterQuery extends Query {
52
+ $limit?: number;
53
+ $skip?: number;
54
+ $select?: string[];
55
+ $sort?: {
56
+ [key: string]: 1 | -1;
57
+ };
58
+ }
59
+ /**
60
+ * Additional `params` that can be passed to an adapter service method call.
61
+ */
62
+ export interface AdapterParams<Q = AdapterQuery, A extends Partial<AdapterServiceOptions> = Partial<AdapterServiceOptions>> extends Params<Q> {
63
+ adapter?: A;
64
+ paginate?: PaginationParams;
65
+ }
66
+ /**
67
+ * Hook-less (internal) service methods. Directly call database adapter service methods
68
+ * without running any service-level hooks or sanitization. This can be useful if you need the raw data
69
+ * from the service and don't want to trigger any of its hooks.
70
+ *
71
+ * Important: These methods are only available internally on the server, not on the client
72
+ * side and only for the Feathers database adapters.
73
+ *
74
+ * These methods do not trigger events.
75
+ *
76
+ * @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
77
+ */
78
+ export interface InternalServiceMethods<Result = any, Data = Result, PatchData = Partial<Data>, Params extends AdapterParams = AdapterParams, IdType = Id> {
79
+ /**
80
+ * Retrieve all resources from this service.
81
+ * Does not sanitize the query and should only be used on the server.
82
+ *
83
+ * @param _params - Service call parameters {@link Params}
84
+ */
85
+ _find(_params?: Params & {
86
+ paginate?: PaginationOptions;
87
+ }): Promise<Paginated<Result>>;
88
+ _find(_params?: Params & {
89
+ paginate: false;
90
+ }): Promise<Result[]>;
91
+ _find(params?: Params): Promise<Result[] | Paginated<Result>>;
92
+ /**
93
+ * Retrieve a single resource matching the given ID, skipping any service-level hooks.
94
+ * Does not sanitize the query and should only be used on the server.
95
+ *
96
+ * @param id - ID of the resource to locate
97
+ * @param params - Service call parameters {@link Params}
98
+ * @see {@link HookLessServiceMethods}
99
+ * @see {@link https://docs.feathersjs.com/api/services.html#get-id-params|Feathers API Documentation: .get(id, params)}
100
+ */
101
+ _get(id: IdType, params?: Params): Promise<Result>;
102
+ /**
103
+ * Create a new resource for this service, skipping any service-level hooks.
104
+ * Does not sanitize data or checks if multiple updates are allowed and should only be used on the server.
105
+ *
106
+ * @param data - Data to insert into this service.
107
+ * @param params - Service call parameters {@link Params}
108
+ * @see {@link HookLessServiceMethods}
109
+ * @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
110
+ */
111
+ _create(data: Data, params?: Params): Promise<Result>;
112
+ _create(data: Data[], params?: Params): Promise<Result[]>;
113
+ _create(data: Data | Data[], params?: Params): Promise<Result | Result[]>;
114
+ /**
115
+ * Completely replace the resource identified by id, skipping any service-level hooks.
116
+ * Does not sanitize data or query and should only be used on the server.
117
+ *
118
+ * @param id - ID of the resource to be updated
119
+ * @param data - Data to be put in place of the current resource.
120
+ * @param params - Service call parameters {@link Params}
121
+ * @see {@link HookLessServiceMethods}
122
+ * @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
123
+ */
124
+ _update(id: IdType, data: Data, params?: Params): Promise<Result>;
125
+ /**
126
+ * Merge any resources matching the given ID with the given data, skipping any service-level hooks.
127
+ * Does not sanitize the data or query and should only be used on the server.
128
+ *
129
+ * @param id - ID of the resource to be patched
130
+ * @param data - Data to merge with the current resource.
131
+ * @param params - Service call parameters {@link Params}
132
+ * @see {@link HookLessServiceMethods}
133
+ * @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
134
+ */
135
+ _patch(id: null, data: PatchData, params?: Params): Promise<Result[]>;
136
+ _patch(id: IdType, data: PatchData, params?: Params): Promise<Result>;
137
+ _patch(id: IdType | null, data: PatchData, params?: Params): Promise<Result | Result[]>;
138
+ /**
139
+ * Remove resources matching the given ID from the this service, skipping any service-level hooks.
140
+ * Does not sanitize query and should only be used on the server.
141
+ *
142
+ * @param id - ID of the resource to be removed
143
+ * @param params - Service call parameters {@link Params}
144
+ * @see {@link HookLessServiceMethods}
145
+ * @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
146
+ */
147
+ _remove(id: null, params?: Params): Promise<Result[]>;
148
+ _remove(id: IdType, params?: Params): Promise<Result>;
149
+ _remove(id: IdType | null, params?: Params): Promise<Result | Result[]>;
150
+ }
@@ -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 * from './query';
3
5
  export * from './sort';
4
- export declare function select(params: any, ...otherFields: any[]): (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,29 +14,23 @@ 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 = 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
+ __exportStar(require("./query"), exports);
21
22
  __exportStar(require("./sort"), exports);
22
23
  // Return a function that filters a result object or array
23
24
  // and picks only the fields passed as `params.query.$select`
24
25
  // and additional `otherFields`
25
26
  function select(params, ...otherFields) {
26
- const fields = params && params.query && params.query.$select;
27
- if (Array.isArray(fields) && otherFields.length) {
28
- fields.push(...otherFields);
27
+ var _a;
28
+ const queryFields = (_a = params === null || params === void 0 ? void 0 : params.query) === null || _a === void 0 ? void 0 : _a.$select;
29
+ if (!queryFields) {
30
+ return (result) => result;
29
31
  }
30
- const convert = (result) => {
31
- if (!Array.isArray(fields)) {
32
- return result;
33
- }
34
- return commons_1._.pick(result, ...fields);
35
- };
32
+ const resultFields = queryFields.concat(otherFields);
33
+ const convert = (result) => commons_1._.pick(result, ...resultFields);
36
34
  return (result) => {
37
35
  if (Array.isArray(result)) {
38
36
  return result.map(convert);
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,WAAkB;IACxD,MAAM,MAAM,GAAG,MAAM,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC;IAE9D,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC,MAAM,EAAE;QAC/C,MAAM,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC;KAC7B;IAED,MAAM,OAAO,GAAG,CAAC,MAAW,EAAE,EAAE;QAC9B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;YA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ts ADDED
@@ -0,0 +1,28 @@
1
+ import { Query } from '@feathersjs/feathers';
2
+ import { FilterQueryOptions, FilterSettings, PaginationParams } from './declarations';
3
+ /**
4
+ * Returns the converted `$limit` value based on the `paginate` configuration.
5
+ * @param _limit The limit value
6
+ * @param paginate The pagination options
7
+ * @returns The converted $limit value
8
+ */
9
+ export declare const getLimit: (_limit: any, paginate?: PaginationParams) => number;
10
+ export declare const OPERATORS: string[];
11
+ export declare const FILTERS: FilterSettings;
12
+ /**
13
+ * Converts Feathers special query parameters and pagination settings
14
+ * and returns them separately as `filters` and the rest of the query
15
+ * as `query`. `options` also gets passed the pagination settings and
16
+ * a list of additional `operators` to allow when querying properties.
17
+ *
18
+ * @param query The initial query
19
+ * @param options Options for filtering the query
20
+ * @returns An object with `query` which contains the query without `filters`
21
+ * and `filters` which contains the converted values for each filter.
22
+ */
23
+ export declare function filterQuery(_query: Query, options?: FilterQueryOptions): {
24
+ filters: {
25
+ [key: string]: any;
26
+ };
27
+ query: Query;
28
+ };
package/lib/query.js ADDED
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.filterQuery = exports.FILTERS = exports.OPERATORS = exports.getLimit = 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
+ /**
54
+ * Returns the converted `$limit` value based on the `paginate` configuration.
55
+ * @param _limit The limit value
56
+ * @param paginate The pagination options
57
+ * @returns The converted $limit value
58
+ */
59
+ const getLimit = (_limit, paginate) => {
60
+ const limit = parse(_limit);
61
+ if (paginate && (paginate.default || paginate.max)) {
62
+ const base = paginate.default || 0;
63
+ const lower = typeof limit === 'number' && !isNaN(limit) && limit >= 0 ? limit : base;
64
+ const upper = typeof paginate.max === 'number' ? paginate.max : Number.MAX_VALUE;
65
+ return Math.min(lower, upper);
66
+ }
67
+ return limit;
68
+ };
69
+ exports.getLimit = getLimit;
70
+ exports.OPERATORS = ['$in', '$nin', '$lt', '$lte', '$gt', '$gte', '$ne', '$or'];
71
+ exports.FILTERS = {
72
+ $skip: (value) => parse(value),
73
+ $sort: (sort) => {
74
+ if (typeof sort !== 'object' || Array.isArray(sort)) {
75
+ return sort;
76
+ }
77
+ return Object.keys(sort).reduce((result, key) => {
78
+ result[key] = typeof sort[key] === 'object' ? sort[key] : parse(sort[key]);
79
+ return result;
80
+ }, {});
81
+ },
82
+ $limit: (_limit, { paginate }) => (0, exports.getLimit)(_limit, paginate),
83
+ $select: (select) => {
84
+ if (Array.isArray(select)) {
85
+ return select.map((current) => `${current}`);
86
+ }
87
+ return select;
88
+ },
89
+ $or: (or, { operators }) => {
90
+ if (Array.isArray(or)) {
91
+ return or.map((current) => validateQueryProperty(current, operators));
92
+ }
93
+ return or;
94
+ },
95
+ $and: (and, { operators }) => {
96
+ if (Array.isArray(and)) {
97
+ return and.map((current) => validateQueryProperty(current, operators));
98
+ }
99
+ return and;
100
+ }
101
+ };
102
+ /**
103
+ * Converts Feathers special query parameters and pagination settings
104
+ * and returns them separately as `filters` and the rest of the query
105
+ * as `query`. `options` also gets passed the pagination settings and
106
+ * a list of additional `operators` to allow when querying properties.
107
+ *
108
+ * @param query The initial query
109
+ * @param options Options for filtering the query
110
+ * @returns An object with `query` which contains the query without `filters`
111
+ * and `filters` which contains the converted values for each filter.
112
+ */
113
+ function filterQuery(_query, options = {}) {
114
+ const query = _query || {};
115
+ const settings = {
116
+ ...options,
117
+ filters: {
118
+ ...exports.FILTERS,
119
+ ...options.filters
120
+ },
121
+ operators: exports.OPERATORS.concat(options.operators || [])
122
+ };
123
+ return {
124
+ filters: getFilters(query, settings),
125
+ query: getQuery(query, settings)
126
+ };
127
+ }
128
+ exports.filterQuery = filterQuery;
129
+ //# sourceMappingURL=query.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.js","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":";;;AAAA,iDAAuC;AACvC,+CAA+C;AAI/C,MAAM,KAAK,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;AAE1F,MAAM,aAAa,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,WAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,WAAW,KAAK,EAAE,CAAC,WAAW,CAAA;AAE/F,MAAM,qB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Methods, 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
- whitelist?: string[];
11
- allow: string[];
12
- filters: string[];
13
- }
14
- export interface AdapterOptions<M = any> extends Pick<ServiceOptions, 'multi' | 'allow' | 'paginate'> {
15
- Model?: M;
16
- }
17
- export interface AdapterParams<M = any> extends Params {
18
- adapter?: Partial<AdapterOptions<M>>;
19
- }
1
+ import { Id, Paginated, Query } from '@feathersjs/feathers';
2
+ import { AdapterParams, AdapterServiceOptions, InternalServiceMethods, PaginationOptions } from './declarations';
3
+ export declare const VALIDATED: unique symbol;
20
4
  /**
21
- * Hook-less (internal) service methods. Directly call database adapter service methods
22
- * without running any service-level hooks. This can be useful if you need the raw data
23
- * from the service and don't want to trigger any of its hooks.
24
- *
25
- * Important: These methods are only available internally on the server, not on the client
26
- * side and only for the Feathers database adapters.
27
- *
28
- * These methods do not trigger events.
29
- *
30
- * @see {@link https://docs.feathersjs.com/guides/migrating.html#hook-less-service-methods}
5
+ * An abstract base class that a database adapter can extend from to implement the
6
+ * `__find`, `__get`, `__update`, `__patch` and `__remove` methods.
31
7
  */
32
- export interface InternalServiceMethods<T = any, D = Partial<T>> {
8
+ export declare abstract class AdapterBase<Result = any, Data = Result, PatchData = Partial<Data>, ServiceParams extends AdapterParams = AdapterParams, Options extends AdapterServiceOptions = AdapterServiceOptions, IdType = Id> implements InternalServiceMethods<Result, Data, PatchData, ServiceParams, IdType> {
9
+ options: Options;
10
+ constructor(options: Options);
11
+ get id(): string;
12
+ get events(): string[];
33
13
  /**
34
- * Retrieve all resources from this service, skipping any service-level hooks.
14
+ * Check if this adapter allows multiple updates for a method.
15
+ * @param method The method name to check.
16
+ * @param params The service call params.
17
+ * @returns Wether or not multiple updates are allowed.
18
+ */
19
+ allowsMulti(method: string, params?: ServiceParams): boolean | string[];
20
+ /**
21
+ * Returns the combined options for a service call. Options will be merged
22
+ * with `this.options` and `params.adapter` for dynamic overrides.
35
23
  *
36
- * @param params - Service call parameters {@link Params}
37
- * @see {@link HookLessServiceMethods}
38
- * @see {@link https://docs.feathersjs.com/api/services.html#find-params|Feathers API Documentation: .find(params)}
24
+ * @param params The parameters for the service method call
25
+ * @returns The actual options for this call
39
26
  */
40
- _find(params?: AdapterParams): Promise<T | T[] | Paginated<T>>;
27
+ getOptions(params: ServiceParams): Options;
28
+ /**
29
+ * Returns a sanitized version of `params.query`, converting filter values
30
+ * (like $limit and $skip) into the expected type. Will throw an error if
31
+ * a `$` prefixed filter or operator value that is not allowed in `filters`
32
+ * or `operators` is encountered.
33
+ *
34
+ * @param params The service call parameter.
35
+ * @returns A new object containing the sanitized query.
36
+ */
37
+ sanitizeQuery(params?: ServiceParams): Promise<Query>;
38
+ /**
39
+ * Retrieve all resources from this service.
40
+ * Does not sanitize the query and should only be used on the server.
41
+ *
42
+ * @param _params - Service call parameters {@link ServiceParams}
43
+ */
44
+ abstract _find(_params?: ServiceParams & {
45
+ paginate?: PaginationOptions;
46
+ }): Promise<Paginated<Result>>;
47
+ abstract _find(_params?: ServiceParams & {
48
+ paginate: false;
49
+ }): Promise<Result[]>;
50
+ abstract _find(params?: ServiceParams): Promise<Result[] | Paginated<Result>>;
41
51
  /**
42
52
  * Retrieve a single resource matching the given ID, skipping any service-level hooks.
53
+ * Does not sanitize the query and should only be used on the server.
43
54
  *
44
55
  * @param id - ID of the resource to locate
45
- * @param params - Service call parameters {@link Params}
56
+ * @param params - Service call parameters {@link ServiceParams}
46
57
  * @see {@link HookLessServiceMethods}
47
58
  * @see {@link https://docs.feathersjs.com/api/services.html#get-id-params|Feathers API Documentation: .get(id, params)}
48
59
  */
49
- _get(id: Id, params?: AdapterParams): Promise<T>;
60
+ abstract _get(id: IdType, params?: ServiceParams): Promise<Result>;
50
61
  /**
51
62
  * Create a new resource for this service, skipping any service-level hooks.
63
+ * Does not check if multiple updates are allowed and should only be used on the server.
52
64
  *
53
65
  * @param data - Data to insert into this service.
54
- * @param params - Service call parameters {@link Params}
66
+ * @param params - Service call parameters {@link ServiceParams}
55
67
  * @see {@link HookLessServiceMethods}
56
68
  * @see {@link https://docs.feathersjs.com/api/services.html#create-data-params|Feathers API Documentation: .create(data, params)}
57
69
  */
58
- _create(data: D | D[], params?: AdapterParams): Promise<T | T[]>;
70
+ abstract _create(data: Data, params?: ServiceParams): Promise<Result>;
71
+ abstract _create(data: Data[], params?: ServiceParams): Promise<Result[]>;
72
+ abstract _create(data: Data | Data[], params?: ServiceParams): Promise<Result | Result[]>;
59
73
  /**
60
- * Replace any resources matching the given ID with the given data, skipping any service-level hooks.
74
+ * Completely replace the resource identified by id, skipping any service-level hooks.
75
+ * Does not sanitize the query and should only be used on the server.
61
76
  *
62
77
  * @param id - ID of the resource to be updated
63
78
  * @param data - Data to be put in place of the current resource.
64
- * @param params - Service call parameters {@link Params}
79
+ * @param params - Service call parameters {@link ServiceParams}
65
80
  * @see {@link HookLessServiceMethods}
66
81
  * @see {@link https://docs.feathersjs.com/api/services.html#update-id-data-params|Feathers API Documentation: .update(id, data, params)}
67
82
  */
68
- _update(id: Id, data: D, params?: AdapterParams): Promise<T>;
83
+ abstract _update(id: IdType, data: Data, params?: ServiceParams): Promise<Result>;
69
84
  /**
70
85
  * Merge any resources matching the given ID with the given data, skipping any service-level hooks.
86
+ * Does not sanitize the query and should only be used on the server.
71
87
  *
72
88
  * @param id - ID of the resource to be patched
73
89
  * @param data - Data to merge with the current resource.
74
- * @param params - Service call parameters {@link Params}
90
+ * @param params - Service call parameters {@link ServiceParams}
75
91
  * @see {@link HookLessServiceMethods}
76
92
  * @see {@link https://docs.feathersjs.com/api/services.html#patch-id-data-params|Feathers API Documentation: .patch(id, data, params)}
77
93
  */
78
- _patch(id: NullableId, data: D, params?: AdapterParams): Promise<T | T[]>;
94
+ abstract _patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>;
95
+ abstract _patch(id: IdType, data: PatchData, params?: ServiceParams): Promise<Result>;
96
+ abstract _patch(id: IdType | null, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>;
79
97
  /**
80
98
  * Remove resources matching the given ID from the this service, skipping any service-level hooks.
99
+ * Does not sanitize query and should only be used on the server.
81
100
  *
82
101
  * @param id - ID of the resource to be removed
83
- * @param params - Service call parameters {@link Params}
102
+ * @param params - Service call parameters {@link ServiceParams}
84
103
  * @see {@link HookLessServiceMethods}
85
104
  * @see {@link https://docs.feathersjs.com/api/services.html#remove-id-params|Feathers API Documentation: .remove(id, params)}
86
105
  */
87
- _remove(id: NullableId, params?: AdapterParams): Promise<T | T[]>;
88
- }
89
- export declare class AdapterService<T = any, D = Partial<T>, O extends Partial<ServiceOptions> = Partial<ServiceOptions>> implements ServiceMethods<T | Paginated<T>, D> {
90
- options: ServiceOptions & O;
91
- constructor(options: O);
92
- get id(): string;
93
- get events(): string[];
94
- filterQuery(params?: AdapterParams, opts?: any): {
95
- [key: string]: any;
96
- } & {
97
- paginate: any;
98
- };
99
- allowsMulti(method: string, params?: AdapterParams): boolean;
100
- getOptions(params: AdapterParams): ServiceOptions & {
101
- model?: any;
102
- };
103
- find(params?: AdapterParams): Promise<T[] | Paginated<T>>;
104
- get(id: Id, params?: AdapterParams): Promise<T>;
105
- create(data: Partial<T>, params?: AdapterParams): Promise<T>;
106
- create(data: Partial<T>[], params?: AdapterParams): Promise<T[]>;
107
- update(id: Id, data: D, params?: AdapterParams): Promise<T>;
108
- patch(id: Id, data: Partial<T>, params?: AdapterParams): Promise<T>;
109
- patch(id: null, data: Partial<T>, params?: AdapterParams): Promise<T[]>;
110
- patch(id: NullableId, data: Partial<T>, params?: AdapterParams): Promise<T | T[]>;
111
- remove(id: Id, params?: AdapterParams): Promise<T>;
112
- remove(id: null, params?: AdapterParams): Promise<T[]>;
113
- remove(id: NullableId, params?: AdapterParams): Promise<T | T[]>;
114
- setup(): Promise<void>;
106
+ abstract _remove(id: null, params?: ServiceParams): Promise<Result[]>;
107
+ abstract _remove(id: IdType, params?: ServiceParams): Promise<Result>;
108
+ abstract _remove(id: IdType | null, params?: ServiceParams): Promise<Result | Result[]>;
115
109
  }