@feathersjs/knex 5.0.0-pre.33 → 5.0.0-pre.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/lib/adapter.d.ts +21 -21
- package/lib/adapter.js +20 -11
- package/lib/adapter.js.map +1 -1
- package/lib/index.d.ts +14 -14
- package/lib/index.js +26 -5
- package/lib/index.js.map +1 -1
- package/package.json +10 -10
- package/src/adapter.ts +53 -33
- package/src/index.ts +52 -23
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [5.0.0-pre.34](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.33...v5.0.0-pre.34) (2022-12-14)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **adapter:** Add patch data type to adapters and refactor AdapterBase usage ([#2906](https://github.com/feathersjs/feathers/issues/2906)) ([9ddc2e6](https://github.com/feathersjs/feathers/commit/9ddc2e6b028f026f939d6af68125847e5c6734b4))
|
|
11
|
+
- **cli:** Use separate patch schema and types ([#2916](https://github.com/feathersjs/feathers/issues/2916)) ([7088af6](https://github.com/feathersjs/feathers/commit/7088af64a539dc7f1a016d832b77b98aaaf92603))
|
|
12
|
+
|
|
6
13
|
# [5.0.0-pre.33](https://github.com/feathersjs/feathers/compare/v5.0.0-pre.32...v5.0.0-pre.33) (2022-11-08)
|
|
7
14
|
|
|
8
15
|
**Note:** Version bump only for package @feathersjs/knex
|
package/lib/adapter.d.ts
CHANGED
|
@@ -2,39 +2,39 @@ import { Id, NullableId, Paginated, Query } from '@feathersjs/feathers';
|
|
|
2
2
|
import { AdapterBase, PaginationOptions } from '@feathersjs/adapter-commons';
|
|
3
3
|
import { Knex } from 'knex';
|
|
4
4
|
import { KnexAdapterOptions, KnexAdapterParams } from './declarations';
|
|
5
|
-
export declare class KnexAdapter<
|
|
5
|
+
export declare class KnexAdapter<Result, Data = Partial<Result>, ServiceParams extends KnexAdapterParams<any> = KnexAdapterParams, PatchData = Partial<Data>> extends AdapterBase<Result, Data, PatchData, ServiceParams, KnexAdapterOptions> {
|
|
6
6
|
table: string;
|
|
7
7
|
schema?: string;
|
|
8
8
|
constructor(options: KnexAdapterOptions);
|
|
9
9
|
get Model(): Knex<any, any[]>;
|
|
10
10
|
get fullName(): string;
|
|
11
|
-
db(params?:
|
|
11
|
+
db(params?: ServiceParams): Knex.QueryBuilder<any, any>;
|
|
12
12
|
knexify(knexQuery: Knex.QueryBuilder, query?: Query, parentKey?: string): Knex.QueryBuilder;
|
|
13
|
-
createQuery(params:
|
|
14
|
-
filterQuery(params:
|
|
13
|
+
createQuery(params: ServiceParams): Knex.QueryBuilder<any, any>;
|
|
14
|
+
filterQuery(params: ServiceParams): {
|
|
15
15
|
filters: {
|
|
16
16
|
[key: string]: any;
|
|
17
17
|
};
|
|
18
18
|
query: Query;
|
|
19
19
|
paginate: import("@feathersjs/adapter-commons").PaginationParams;
|
|
20
20
|
};
|
|
21
|
-
|
|
21
|
+
_find(params?: ServiceParams & {
|
|
22
22
|
paginate?: PaginationOptions;
|
|
23
|
-
}): Promise<Paginated<
|
|
24
|
-
|
|
23
|
+
}): Promise<Paginated<Result>>;
|
|
24
|
+
_find(params?: ServiceParams & {
|
|
25
25
|
paginate: false;
|
|
26
|
-
}): Promise<
|
|
27
|
-
|
|
28
|
-
_findOrGet(id: NullableId, params?:
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
26
|
+
}): Promise<Result[]>;
|
|
27
|
+
_find(params?: ServiceParams): Promise<Paginated<Result> | Result[]>;
|
|
28
|
+
_findOrGet(id: NullableId, params?: ServiceParams): Promise<Result[]>;
|
|
29
|
+
_get(id: Id, params?: ServiceParams): Promise<Result>;
|
|
30
|
+
_create(data: Data, params?: ServiceParams): Promise<Result>;
|
|
31
|
+
_create(data: Data[], params?: ServiceParams): Promise<Result[]>;
|
|
32
|
+
_create(data: Data | Data[], _params?: ServiceParams): Promise<Result | Result[]>;
|
|
33
|
+
_patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>;
|
|
34
|
+
_patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>;
|
|
35
|
+
_patch(id: NullableId, data: PatchData, _params?: ServiceParams): Promise<Result | Result[]>;
|
|
36
|
+
_update(id: Id, _data: Data, params?: ServiceParams): Promise<Result>;
|
|
37
|
+
_remove(id: null, params?: ServiceParams): Promise<Result[]>;
|
|
38
|
+
_remove(id: Id, params?: ServiceParams): Promise<Result>;
|
|
39
|
+
_remove(id: NullableId, _params?: ServiceParams): Promise<Result | Result[]>;
|
|
40
40
|
}
|
package/lib/adapter.js
CHANGED
|
@@ -114,7 +114,7 @@ class KnexAdapter extends adapter_commons_1.AdapterBase {
|
|
|
114
114
|
const { filters, query } = (0, adapter_commons_1.filterQuery)((params === null || params === void 0 ? void 0 : params.query) || {}, options);
|
|
115
115
|
return { filters, query, paginate: options.paginate };
|
|
116
116
|
}
|
|
117
|
-
async
|
|
117
|
+
async _find(params = {}) {
|
|
118
118
|
const { filters, paginate } = this.filterQuery(params);
|
|
119
119
|
const builder = params.knex ? params.knex.clone() : this.createQuery(params);
|
|
120
120
|
const countBuilder = builder.clone().clearSelect().clearOrder().count(`${this.table}.${this.id} as total`);
|
|
@@ -151,19 +151,19 @@ class KnexAdapter extends adapter_commons_1.AdapterBase {
|
|
|
151
151
|
...(id !== null ? { [`${this.table}.${this.id}`]: id } : {})
|
|
152
152
|
}
|
|
153
153
|
};
|
|
154
|
-
return this
|
|
154
|
+
return this._find(findParams);
|
|
155
155
|
}
|
|
156
|
-
async
|
|
156
|
+
async _get(id, params = {}) {
|
|
157
157
|
const data = await this._findOrGet(id, params);
|
|
158
158
|
if (data.length !== 1) {
|
|
159
159
|
throw new errors_1.NotFound(`No record found for id '${id}'`);
|
|
160
160
|
}
|
|
161
161
|
return data[0];
|
|
162
162
|
}
|
|
163
|
-
async
|
|
163
|
+
async _create(_data, params = {}) {
|
|
164
164
|
const data = _data;
|
|
165
165
|
if (Array.isArray(data)) {
|
|
166
|
-
return Promise.all(data.map((current) => this
|
|
166
|
+
return Promise.all(data.map((current) => this._create(current, params)));
|
|
167
167
|
}
|
|
168
168
|
const client = this.db(params).client.config.client;
|
|
169
169
|
const returning = RETURNING_CLIENTS.includes(client) ? [this.id] : [];
|
|
@@ -172,10 +172,13 @@ class KnexAdapter extends adapter_commons_1.AdapterBase {
|
|
|
172
172
|
if (!id) {
|
|
173
173
|
return rows;
|
|
174
174
|
}
|
|
175
|
-
return this
|
|
175
|
+
return this._get(id, params);
|
|
176
176
|
}
|
|
177
|
-
async
|
|
177
|
+
async _patch(id, raw, params = {}) {
|
|
178
178
|
var _a, _b;
|
|
179
|
+
if (id === null && !this.allowsMulti('patch', params)) {
|
|
180
|
+
throw new errors_1.MethodNotAllowed('Can not patch multiple entries');
|
|
181
|
+
}
|
|
179
182
|
const data = commons_1._.omit(raw, this.id);
|
|
180
183
|
const results = await this._findOrGet(id, {
|
|
181
184
|
...params,
|
|
@@ -205,9 +208,12 @@ class KnexAdapter extends adapter_commons_1.AdapterBase {
|
|
|
205
208
|
}
|
|
206
209
|
return items;
|
|
207
210
|
}
|
|
208
|
-
async
|
|
211
|
+
async _update(id, _data, params = {}) {
|
|
212
|
+
if (id === null || Array.isArray(_data)) {
|
|
213
|
+
throw new errors_1.BadRequest("You can not replace multiple instances. Did you mean 'patch'?");
|
|
214
|
+
}
|
|
209
215
|
const data = commons_1._.omit(_data, this.id);
|
|
210
|
-
const oldData = await this
|
|
216
|
+
const oldData = await this._get(id, params);
|
|
211
217
|
const newObject = Object.keys(oldData).reduce((result, key) => {
|
|
212
218
|
if (key !== this.id) {
|
|
213
219
|
// We don't want the id field to be changed
|
|
@@ -216,9 +222,12 @@ class KnexAdapter extends adapter_commons_1.AdapterBase {
|
|
|
216
222
|
return result;
|
|
217
223
|
}, {});
|
|
218
224
|
await this.db(params).update(newObject, '*').where(this.id, id);
|
|
219
|
-
return this
|
|
225
|
+
return this._get(id, params);
|
|
220
226
|
}
|
|
221
|
-
async
|
|
227
|
+
async _remove(id, params = {}) {
|
|
228
|
+
if (id === null && !this.allowsMulti('remove', params)) {
|
|
229
|
+
throw new errors_1.MethodNotAllowed('Can not remove multiple entries');
|
|
230
|
+
}
|
|
222
231
|
const items = await this._findOrGet(id, params);
|
|
223
232
|
const { query } = this.filterQuery(params);
|
|
224
233
|
const q = this.db(params);
|
package/lib/adapter.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":";;;AACA,iDAAuC;AACvC,iEAAyF;AACzF,+
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../src/adapter.ts"],"names":[],"mappings":";;;AACA,iDAAuC;AACvC,iEAAyF;AACzF,+CAA2E;AAG3E,mDAA8C;AAE9C,MAAM,OAAO,GAAG;IACd,GAAG,EAAE,UAAU;IACf,GAAG,EAAE,SAAS;IACd,IAAI,EAAE,YAAY;IAClB,GAAG,EAAE,SAAS;IACd,IAAI,EAAE,UAAU;CACjB,CAAA;AAED,MAAM,SAAS,GAAG;IAChB,GAAG,EAAE,GAAG;IACR,IAAI,EAAE,IAAI;IACV,GAAG,EAAE,GAAG;IACR,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,MAAM;IACb,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,OAAO;CAChB,CAAA;AAED,MAAM,iBAAiB,GAAG,CAAC,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,CAAC,CAAA;AAEnE,MAAa,WAKX,SAAQ,6BAAuE;IAI/E,YAAY,O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
|
|
6
6
|
export * from './adapter';
|
|
7
7
|
export * from './error-handler';
|
|
8
8
|
export * as transaction from './hooks';
|
|
9
|
-
export declare class KnexService<
|
|
10
|
-
find(params?:
|
|
9
|
+
export declare class KnexService<Result = any, Data = Partial<Result>, ServiceParams extends Params<any> = KnexAdapterParams, PatchData = Partial<Data>> extends KnexAdapter<Result, Data, ServiceParams, PatchData> implements ServiceMethods<Result | Paginated<Result>, Data, ServiceParams, PatchData> {
|
|
10
|
+
find(params?: ServiceParams & {
|
|
11
11
|
paginate?: PaginationOptions;
|
|
12
|
-
}): Promise<Paginated<
|
|
13
|
-
find(params?:
|
|
12
|
+
}): Promise<Paginated<Result>>;
|
|
13
|
+
find(params?: ServiceParams & {
|
|
14
14
|
paginate: false;
|
|
15
|
-
}): Promise<
|
|
16
|
-
find(params?:
|
|
17
|
-
get(id: Id, params?:
|
|
18
|
-
create(data:
|
|
19
|
-
create(data:
|
|
20
|
-
update(id: Id, data:
|
|
21
|
-
patch(id: Id, data:
|
|
22
|
-
patch(id: null, data:
|
|
23
|
-
remove(id: Id, params?:
|
|
24
|
-
remove(id: null, params?:
|
|
15
|
+
}): Promise<Result[]>;
|
|
16
|
+
find(params?: ServiceParams): Promise<Paginated<Result> | Result[]>;
|
|
17
|
+
get(id: Id, params?: ServiceParams): Promise<Result>;
|
|
18
|
+
create(data: Data, params?: ServiceParams): Promise<Result>;
|
|
19
|
+
create(data: Data[], params?: ServiceParams): Promise<Result[]>;
|
|
20
|
+
update(id: Id, data: Data, params?: ServiceParams): Promise<Result>;
|
|
21
|
+
patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>;
|
|
22
|
+
patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>;
|
|
23
|
+
remove(id: Id, params?: ServiceParams): Promise<Result>;
|
|
24
|
+
remove(id: null, params?: ServiceParams): Promise<Result[]>;
|
|
25
25
|
}
|
package/lib/index.js
CHANGED
|
@@ -27,6 +27,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.KnexService = exports.transaction = void 0;
|
|
30
|
+
const lib_1 = require("@feathersjs/errors/lib");
|
|
30
31
|
const adapter_1 = require("./adapter");
|
|
31
32
|
__exportStar(require("./declarations"), exports);
|
|
32
33
|
__exportStar(require("./adapter"), exports);
|
|
@@ -34,22 +35,42 @@ __exportStar(require("./error-handler"), exports);
|
|
|
34
35
|
exports.transaction = __importStar(require("./hooks"));
|
|
35
36
|
class KnexService extends adapter_1.KnexAdapter {
|
|
36
37
|
async find(params) {
|
|
37
|
-
return this._find(
|
|
38
|
+
return this._find({
|
|
39
|
+
...params,
|
|
40
|
+
query: await this.sanitizeQuery(params)
|
|
41
|
+
});
|
|
38
42
|
}
|
|
39
43
|
async get(id, params) {
|
|
40
|
-
return this._get(id,
|
|
44
|
+
return this._get(id, {
|
|
45
|
+
...params,
|
|
46
|
+
query: await this.sanitizeQuery(params)
|
|
47
|
+
});
|
|
41
48
|
}
|
|
42
49
|
async create(data, params) {
|
|
50
|
+
if (Array.isArray(data) && !this.allowsMulti('create', params)) {
|
|
51
|
+
throw new lib_1.MethodNotAllowed('Can not create multiple entries');
|
|
52
|
+
}
|
|
43
53
|
return this._create(data, params);
|
|
44
54
|
}
|
|
45
55
|
async update(id, data, params) {
|
|
46
|
-
return this._update(id, data,
|
|
56
|
+
return this._update(id, data, {
|
|
57
|
+
...params,
|
|
58
|
+
query: await this.sanitizeQuery(params)
|
|
59
|
+
});
|
|
47
60
|
}
|
|
48
61
|
async patch(id, data, params) {
|
|
49
|
-
|
|
62
|
+
const { $limit, ...query } = await this.sanitizeQuery(params);
|
|
63
|
+
return this._patch(id, data, {
|
|
64
|
+
...params,
|
|
65
|
+
query
|
|
66
|
+
});
|
|
50
67
|
}
|
|
51
68
|
async remove(id, params) {
|
|
52
|
-
|
|
69
|
+
const { $limit, ...query } = await this.sanitizeQuery(params);
|
|
70
|
+
return this._remove(id, {
|
|
71
|
+
...params,
|
|
72
|
+
query
|
|
73
|
+
});
|
|
53
74
|
}
|
|
54
75
|
}
|
|
55
76
|
exports.KnexService = KnexService;
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,gDAAyD;AAEzD,uCAAuC;AAGvC,iDAA8B;AAC9B,4CAAyB;AACzB,kDAA+B;AAC/B,uDAAsC;AAEtC,MAAa,WAMX,SAAQ,qBAAmD;IAM3D,KAAK,CAAC,IAAI,CAAC,MAAsB;QAC/B,OAAO,IAAI,CAAC,KAAK,CAAC;YAChB,GAAG,MAAM;YACT,KAAK,EAAE,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;SACxC,CAAC,CAAA;IACJ,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,EAAM,EAAE,MAAsB;QACtC,OAAO,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE;YACnB,GAAG,MAAM;YACT,KAAK,EAAE,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;SACxC,CAAC,CAAA;IACJ,CAAC;IAID,KAAK,CAAC,MAAM,CAAC,IAAmB,EAAE,MAAsB;QACtD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE;YAC9D,MAAM,IAAI,sBAAgB,CAAC,iCAAiC,CAAC,CAAA;SAC9D;QAED,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACnC,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,EAAM,EAAE,IAAU,EAAE,MAAsB;QACrD,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,IAAI,EAAE;YAC5B,GAAG,MAAM;YACT,KAAK,EAAE,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC;SACxC,CAAC,CAAA;IACJ,CAAC;IAID,KAAK,CAAC,KAAK,CAAC,EAAc,EAAE,IAAe,EAAE,MAAsB;QACjE,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAE7D,OAAO,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,IAAI,EAAE;YAC3B,GAAG,MAAM;YACT,KAAK;SACN,CAAC,CAAA;IACJ,CAAC;IAID,KAAK,CAAC,MAAM,CAAC,EAAc,EAAE,MAAsB;QACjD,MAAM,EAAE,MAAM,EAAE,GAAG,KAAK,EAAE,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QAE7D,OAAO,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE;YACtB,GAAG,MAAM;YACT,KAAK;SACN,CAAC,CAAA;IACJ,CAAC;CACF;AAhED,kCAgEC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/knex",
|
|
3
3
|
"description": "Feathers SQL service adapter using KnexJS",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.34",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"keywords": [
|
|
@@ -51,24 +51,24 @@
|
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@feathersjs/adapter-commons": "^5.0.0-pre.
|
|
55
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
56
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
57
|
-
"@feathersjs/feathers": "^5.0.0-pre.
|
|
54
|
+
"@feathersjs/adapter-commons": "^5.0.0-pre.34",
|
|
55
|
+
"@feathersjs/commons": "^5.0.0-pre.34",
|
|
56
|
+
"@feathersjs/errors": "^5.0.0-pre.34",
|
|
57
|
+
"@feathersjs/feathers": "^5.0.0-pre.34"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
60
|
"knex": "^2.3.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@feathersjs/adapter-tests": "^5.0.0-pre.
|
|
64
|
-
"@types/mocha": "^10.0.
|
|
65
|
-
"@types/node": "^18.11.
|
|
63
|
+
"@feathersjs/adapter-tests": "^5.0.0-pre.34",
|
|
64
|
+
"@types/mocha": "^10.0.1",
|
|
65
|
+
"@types/node": "^18.11.10",
|
|
66
66
|
"knex": "^2.3.0",
|
|
67
67
|
"mocha": "^10.1.0",
|
|
68
68
|
"pg": "^8.8.0",
|
|
69
69
|
"shx": "^0.3.4",
|
|
70
70
|
"sqlite3": "^5.1.2",
|
|
71
|
-
"typescript": "^4.
|
|
71
|
+
"typescript": "^4.9.3"
|
|
72
72
|
},
|
|
73
|
-
"gitHead": "
|
|
73
|
+
"gitHead": "42cca600d00f0b3b9d89fa79be30fcd46bc50132"
|
|
74
74
|
}
|
package/src/adapter.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Id, NullableId, Paginated, Query } from '@feathersjs/feathers'
|
|
2
2
|
import { _ } from '@feathersjs/commons'
|
|
3
3
|
import { AdapterBase, PaginationOptions, filterQuery } from '@feathersjs/adapter-commons'
|
|
4
|
-
import { NotFound } from '@feathersjs/errors'
|
|
4
|
+
import { BadRequest, MethodNotAllowed, NotFound } from '@feathersjs/errors'
|
|
5
5
|
import { Knex } from 'knex'
|
|
6
6
|
|
|
7
7
|
import { errorHandler } from './error-handler'
|
|
@@ -27,10 +27,11 @@ const OPERATORS = {
|
|
|
27
27
|
const RETURNING_CLIENTS = ['postgresql', 'pg', 'oracledb', 'mssql']
|
|
28
28
|
|
|
29
29
|
export class KnexAdapter<
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
Result,
|
|
31
|
+
Data = Partial<Result>,
|
|
32
|
+
ServiceParams extends KnexAdapterParams<any> = KnexAdapterParams,
|
|
33
|
+
PatchData = Partial<Data>
|
|
34
|
+
> extends AdapterBase<Result, Data, PatchData, ServiceParams, KnexAdapterOptions> {
|
|
34
35
|
table: string
|
|
35
36
|
schema?: string
|
|
36
37
|
|
|
@@ -65,7 +66,7 @@ export class KnexAdapter<
|
|
|
65
66
|
return this.schema ? `${this.schema}.${this.table}` : this.table
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
db(params?:
|
|
69
|
+
db(params?: ServiceParams) {
|
|
69
70
|
const { Model, table, schema } = this
|
|
70
71
|
|
|
71
72
|
if (params && params.transaction && params.transaction.trx) {
|
|
@@ -114,7 +115,7 @@ export class KnexAdapter<
|
|
|
114
115
|
}, knexQuery)
|
|
115
116
|
}
|
|
116
117
|
|
|
117
|
-
createQuery(params:
|
|
118
|
+
createQuery(params: ServiceParams) {
|
|
118
119
|
const { table, id } = this
|
|
119
120
|
const { filters, query } = this.filterQuery(params)
|
|
120
121
|
const builder = this.db(params)
|
|
@@ -144,17 +145,17 @@ export class KnexAdapter<
|
|
|
144
145
|
return builder
|
|
145
146
|
}
|
|
146
147
|
|
|
147
|
-
filterQuery(params:
|
|
148
|
+
filterQuery(params: ServiceParams) {
|
|
148
149
|
const options = this.getOptions(params)
|
|
149
150
|
const { filters, query } = filterQuery(params?.query || {}, options)
|
|
150
151
|
|
|
151
152
|
return { filters, query, paginate: options.paginate }
|
|
152
153
|
}
|
|
153
154
|
|
|
154
|
-
async
|
|
155
|
-
async
|
|
156
|
-
async
|
|
157
|
-
async
|
|
155
|
+
async _find(params?: ServiceParams & { paginate?: PaginationOptions }): Promise<Paginated<Result>>
|
|
156
|
+
async _find(params?: ServiceParams & { paginate: false }): Promise<Result[]>
|
|
157
|
+
async _find(params?: ServiceParams): Promise<Paginated<Result> | Result[]>
|
|
158
|
+
async _find(params: ServiceParams = {} as ServiceParams): Promise<Paginated<Result> | Result[]> {
|
|
158
159
|
const { filters, paginate } = this.filterQuery(params)
|
|
159
160
|
const builder = params.knex ? params.knex.clone() : this.createQuery(params)
|
|
160
161
|
const countBuilder = builder.clone().clearSelect().clearOrder().count(`${this.table}.${this.id} as total`)
|
|
@@ -190,7 +191,7 @@ export class KnexAdapter<
|
|
|
190
191
|
return data
|
|
191
192
|
}
|
|
192
193
|
|
|
193
|
-
async _findOrGet(id: NullableId, params?:
|
|
194
|
+
async _findOrGet(id: NullableId, params?: ServiceParams) {
|
|
194
195
|
const findParams = {
|
|
195
196
|
...params,
|
|
196
197
|
paginate: false,
|
|
@@ -200,10 +201,10 @@ export class KnexAdapter<
|
|
|
200
201
|
}
|
|
201
202
|
}
|
|
202
203
|
|
|
203
|
-
return this
|
|
204
|
+
return this._find(findParams as any) as any as Promise<Result[]>
|
|
204
205
|
}
|
|
205
206
|
|
|
206
|
-
async
|
|
207
|
+
async _get(id: Id, params: ServiceParams = {} as ServiceParams): Promise<Result> {
|
|
207
208
|
const data = await this._findOrGet(id, params)
|
|
208
209
|
|
|
209
210
|
if (data.length !== 1) {
|
|
@@ -213,14 +214,17 @@ export class KnexAdapter<
|
|
|
213
214
|
return data[0]
|
|
214
215
|
}
|
|
215
216
|
|
|
216
|
-
async
|
|
217
|
-
async
|
|
218
|
-
async
|
|
219
|
-
async
|
|
217
|
+
async _create(data: Data, params?: ServiceParams): Promise<Result>
|
|
218
|
+
async _create(data: Data[], params?: ServiceParams): Promise<Result[]>
|
|
219
|
+
async _create(data: Data | Data[], _params?: ServiceParams): Promise<Result | Result[]>
|
|
220
|
+
async _create(
|
|
221
|
+
_data: Data | Data[],
|
|
222
|
+
params: ServiceParams = {} as ServiceParams
|
|
223
|
+
): Promise<Result | Result[]> {
|
|
220
224
|
const data = _data as any
|
|
221
225
|
|
|
222
226
|
if (Array.isArray(data)) {
|
|
223
|
-
return Promise.all(data.map((current) => this
|
|
227
|
+
return Promise.all(data.map((current) => this._create(current, params)))
|
|
224
228
|
}
|
|
225
229
|
|
|
226
230
|
const client = this.db(params).client.config.client
|
|
@@ -229,16 +233,24 @@ export class KnexAdapter<
|
|
|
229
233
|
const id = data[this.id] || rows[0][this.id] || rows[0]
|
|
230
234
|
|
|
231
235
|
if (!id) {
|
|
232
|
-
return rows as
|
|
236
|
+
return rows as Result[]
|
|
233
237
|
}
|
|
234
238
|
|
|
235
|
-
return this
|
|
239
|
+
return this._get(id, params)
|
|
236
240
|
}
|
|
237
241
|
|
|
238
|
-
async
|
|
239
|
-
async
|
|
240
|
-
async
|
|
241
|
-
async
|
|
242
|
+
async _patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
|
|
243
|
+
async _patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>
|
|
244
|
+
async _patch(id: NullableId, data: PatchData, _params?: ServiceParams): Promise<Result | Result[]>
|
|
245
|
+
async _patch(
|
|
246
|
+
id: NullableId,
|
|
247
|
+
raw: PatchData,
|
|
248
|
+
params: ServiceParams = {} as ServiceParams
|
|
249
|
+
): Promise<Result | Result[]> {
|
|
250
|
+
if (id === null && !this.allowsMulti('patch', params)) {
|
|
251
|
+
throw new MethodNotAllowed('Can not patch multiple entries')
|
|
252
|
+
}
|
|
253
|
+
|
|
242
254
|
const data = _.omit(raw, this.id)
|
|
243
255
|
const results = await this._findOrGet(id, {
|
|
244
256
|
...params,
|
|
@@ -272,9 +284,13 @@ export class KnexAdapter<
|
|
|
272
284
|
return items
|
|
273
285
|
}
|
|
274
286
|
|
|
275
|
-
async
|
|
287
|
+
async _update(id: Id, _data: Data, params: ServiceParams = {} as ServiceParams): Promise<Result> {
|
|
288
|
+
if (id === null || Array.isArray(_data)) {
|
|
289
|
+
throw new BadRequest("You can not replace multiple instances. Did you mean 'patch'?")
|
|
290
|
+
}
|
|
291
|
+
|
|
276
292
|
const data = _.omit(_data, this.id)
|
|
277
|
-
const oldData = await this
|
|
293
|
+
const oldData = await this._get(id, params)
|
|
278
294
|
const newObject = Object.keys(oldData).reduce((result: any, key) => {
|
|
279
295
|
if (key !== this.id) {
|
|
280
296
|
// We don't want the id field to be changed
|
|
@@ -286,13 +302,17 @@ export class KnexAdapter<
|
|
|
286
302
|
|
|
287
303
|
await this.db(params).update(newObject, '*').where(this.id, id)
|
|
288
304
|
|
|
289
|
-
return this
|
|
305
|
+
return this._get(id, params)
|
|
290
306
|
}
|
|
291
307
|
|
|
292
|
-
async
|
|
293
|
-
async
|
|
294
|
-
async
|
|
295
|
-
async
|
|
308
|
+
async _remove(id: null, params?: ServiceParams): Promise<Result[]>
|
|
309
|
+
async _remove(id: Id, params?: ServiceParams): Promise<Result>
|
|
310
|
+
async _remove(id: NullableId, _params?: ServiceParams): Promise<Result | Result[]>
|
|
311
|
+
async _remove(id: NullableId, params: ServiceParams = {} as ServiceParams): Promise<Result | Result[]> {
|
|
312
|
+
if (id === null && !this.allowsMulti('remove', params)) {
|
|
313
|
+
throw new MethodNotAllowed('Can not remove multiple entries')
|
|
314
|
+
}
|
|
315
|
+
|
|
296
316
|
const items = await this._findOrGet(id, params)
|
|
297
317
|
const { query } = this.filterQuery(params)
|
|
298
318
|
const q = this.db(params)
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PaginationOptions } from '@feathersjs/adapter-commons'
|
|
2
|
+
import { MethodNotAllowed } from '@feathersjs/errors/lib'
|
|
2
3
|
import { Paginated, ServiceMethods, Id, NullableId, Params } from '@feathersjs/feathers'
|
|
3
4
|
import { KnexAdapter } from './adapter'
|
|
4
5
|
import { KnexAdapterParams } from './declarations'
|
|
@@ -8,40 +9,68 @@ export * from './adapter'
|
|
|
8
9
|
export * from './error-handler'
|
|
9
10
|
export * as transaction from './hooks'
|
|
10
11
|
|
|
11
|
-
export class KnexService<
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
export class KnexService<
|
|
13
|
+
Result = any,
|
|
14
|
+
Data = Partial<Result>,
|
|
15
|
+
ServiceParams extends Params<any> = KnexAdapterParams,
|
|
16
|
+
PatchData = Partial<Data>
|
|
17
|
+
>
|
|
18
|
+
extends KnexAdapter<Result, Data, ServiceParams, PatchData>
|
|
19
|
+
implements ServiceMethods<Result | Paginated<Result>, Data, ServiceParams, PatchData>
|
|
14
20
|
{
|
|
15
|
-
async find(params?:
|
|
16
|
-
async find(params?:
|
|
17
|
-
async find(params?:
|
|
18
|
-
async find(params?:
|
|
19
|
-
return this._find(
|
|
21
|
+
async find(params?: ServiceParams & { paginate?: PaginationOptions }): Promise<Paginated<Result>>
|
|
22
|
+
async find(params?: ServiceParams & { paginate: false }): Promise<Result[]>
|
|
23
|
+
async find(params?: ServiceParams): Promise<Paginated<Result> | Result[]>
|
|
24
|
+
async find(params?: ServiceParams): Promise<Paginated<Result> | Result[]> {
|
|
25
|
+
return this._find({
|
|
26
|
+
...params,
|
|
27
|
+
query: await this.sanitizeQuery(params)
|
|
28
|
+
})
|
|
20
29
|
}
|
|
21
30
|
|
|
22
|
-
async get(id: Id, params?:
|
|
23
|
-
return this._get(id,
|
|
31
|
+
async get(id: Id, params?: ServiceParams): Promise<Result> {
|
|
32
|
+
return this._get(id, {
|
|
33
|
+
...params,
|
|
34
|
+
query: await this.sanitizeQuery(params)
|
|
35
|
+
})
|
|
24
36
|
}
|
|
25
37
|
|
|
26
|
-
async create(data:
|
|
27
|
-
async create(data:
|
|
28
|
-
async create(data:
|
|
38
|
+
async create(data: Data, params?: ServiceParams): Promise<Result>
|
|
39
|
+
async create(data: Data[], params?: ServiceParams): Promise<Result[]>
|
|
40
|
+
async create(data: Data | Data[], params?: ServiceParams): Promise<Result | Result[]> {
|
|
41
|
+
if (Array.isArray(data) && !this.allowsMulti('create', params)) {
|
|
42
|
+
throw new MethodNotAllowed('Can not create multiple entries')
|
|
43
|
+
}
|
|
44
|
+
|
|
29
45
|
return this._create(data, params)
|
|
30
46
|
}
|
|
31
47
|
|
|
32
|
-
async update(id: Id, data:
|
|
33
|
-
return this._update(id, data,
|
|
48
|
+
async update(id: Id, data: Data, params?: ServiceParams): Promise<Result> {
|
|
49
|
+
return this._update(id, data, {
|
|
50
|
+
...params,
|
|
51
|
+
query: await this.sanitizeQuery(params)
|
|
52
|
+
})
|
|
34
53
|
}
|
|
35
54
|
|
|
36
|
-
async patch(id: Id, data:
|
|
37
|
-
async patch(id: null, data:
|
|
38
|
-
async patch(id: NullableId, data:
|
|
39
|
-
|
|
55
|
+
async patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>
|
|
56
|
+
async patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
|
|
57
|
+
async patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]> {
|
|
58
|
+
const { $limit, ...query } = await this.sanitizeQuery(params)
|
|
59
|
+
|
|
60
|
+
return this._patch(id, data, {
|
|
61
|
+
...params,
|
|
62
|
+
query
|
|
63
|
+
})
|
|
40
64
|
}
|
|
41
65
|
|
|
42
|
-
async remove(id: Id, params?:
|
|
43
|
-
async remove(id: null, params?:
|
|
44
|
-
async remove(id: NullableId, params?:
|
|
45
|
-
|
|
66
|
+
async remove(id: Id, params?: ServiceParams): Promise<Result>
|
|
67
|
+
async remove(id: null, params?: ServiceParams): Promise<Result[]>
|
|
68
|
+
async remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]> {
|
|
69
|
+
const { $limit, ...query } = await this.sanitizeQuery(params)
|
|
70
|
+
|
|
71
|
+
return this._remove(id, {
|
|
72
|
+
...params,
|
|
73
|
+
query
|
|
74
|
+
})
|
|
46
75
|
}
|
|
47
76
|
}
|