diff --git a/packages/knex/src/error-handler.ts b/packages/knex/src/error-handler.ts index 97ec4873a6..201d9528eb 100644 --- a/packages/knex/src/error-handler.ts +++ b/packages/knex/src/error-handler.ts @@ -65,7 +65,7 @@ export function errorHandler(error: any) { // NOTE: Error codes taken from // https://www.postgresql.org/docs/9.6/static/errcodes-appendix.html // Omit query information - const messages = error.message.split('-') + const messages = (error.message || '').split('-') error.message = messages[messages.length - 1] switch (error.code.slice(0, 2)) { diff --git a/packages/knex/src/index.ts b/packages/knex/src/index.ts index 59a59e1841..7f2c347302 100644 --- a/packages/knex/src/index.ts +++ b/packages/knex/src/index.ts @@ -37,6 +37,7 @@ export class KnexService< async create(data: Data, params?: ServiceParams): Promise async create(data: Data[], params?: ServiceParams): Promise + async create(data: Data | Data[], params?: ServiceParams): Promise async create(data: Data | Data[], params?: ServiceParams): Promise { if (Array.isArray(data) && !this.allowsMulti('create', params)) { throw new MethodNotAllowed('Can not create multiple entries') @@ -54,6 +55,7 @@ export class KnexService< async patch(id: Id, data: PatchData, params?: ServiceParams): Promise async patch(id: null, data: PatchData, params?: ServiceParams): Promise + async patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise async patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise { const { $limit, ...query } = await this.sanitizeQuery(params) @@ -65,6 +67,7 @@ export class KnexService< async remove(id: Id, params?: ServiceParams): Promise async remove(id: null, params?: ServiceParams): Promise + async remove(id: NullableId, params?: ServiceParams): Promise async remove(id: NullableId, params?: ServiceParams): Promise { const { $limit, ...query } = await this.sanitizeQuery(params) diff --git a/packages/knex/test/error-handler.test.ts b/packages/knex/test/error-handler.test.ts new file mode 100644 index 0000000000..04723a45d1 --- /dev/null +++ b/packages/knex/test/error-handler.test.ts @@ -0,0 +1,65 @@ +import assert from 'assert' +import { errorHandler } from '../src' + +describe('Knex Error handler', () => { + it('sqlState', () => { + assert.throws( + () => + errorHandler({ + sqlState: '#23503' + }), + { + name: 'BadRequest' + } + ) + }) + + it('sqliteError', () => { + assert.throws( + () => + errorHandler({ + code: 'SQLITE_ERROR', + errno: 1 + }), + { + name: 'BadRequest' + } + ) + assert.throws(() => errorHandler({ code: 'SQLITE_ERROR', errno: 2 }), { name: 'Unavailable' }) + assert.throws(() => errorHandler({ code: 'SQLITE_ERROR', errno: 3 }), { name: 'Forbidden' }) + assert.throws(() => errorHandler({ code: 'SQLITE_ERROR', errno: 12 }), { name: 'NotFound' }) + assert.throws(() => errorHandler({ code: 'SQLITE_ERROR', errno: 13 }), { name: 'GeneralError' }) + }) + + it('postgresqlError', () => { + assert.throws( + () => + errorHandler({ + code: '22P02', + message: 'Key (id)=(1) is not present in table "users".', + severity: 'ERROR', + routine: 'ExecConstraints' + }), + { + name: 'NotFound' + } + ) + assert.throws( + () => + errorHandler({ code: '2874', message: 'Something', severity: 'ERROR', routine: 'ExecConstraints' }), + { + name: 'Forbidden' + } + ) + assert.throws( + () => + errorHandler({ code: '3D74', message: 'Something', severity: 'ERROR', routine: 'ExecConstraints' }), + { + name: 'Unprocessable' + } + ) + assert.throws(() => errorHandler({ code: 'XYZ', severity: 'ERROR', routine: 'ExecConstraints' }), { + name: 'GeneralError' + }) + }) +}) diff --git a/packages/mongodb/src/adapter.ts b/packages/mongodb/src/adapter.ts index b41c5125ba..d653de0969 100644 --- a/packages/mongodb/src/adapter.ts +++ b/packages/mongodb/src/adapter.ts @@ -281,9 +281,7 @@ export class MongoDbAdapter< ? model .insertMany(data.map(setId), writeOptions) .then(async (result) => - Promise.all( - Object.values(result.insertedIds).map(async (_id) => model.findOne({ _id }, params.mongodb)) - ) + model.find({ _id: { $in: Object.values(result.insertedIds) } }, params.mongodb).toArray() ) : model .insertOne(setId(data), writeOptions) diff --git a/packages/mongodb/src/index.ts b/packages/mongodb/src/index.ts index 1b86dffff1..e6b73579b5 100644 --- a/packages/mongodb/src/index.ts +++ b/packages/mongodb/src/index.ts @@ -32,6 +32,7 @@ export class MongoDBService< async create(data: Data, params?: ServiceParams): Promise async create(data: Data[], params?: ServiceParams): Promise + async create(data: Data | Data[], params?: ServiceParams): Promise async create(data: Data | Data[], params?: ServiceParams): Promise { if (Array.isArray(data) && !this.allowsMulti('create', params)) { throw new MethodNotAllowed('Can not create multiple entries') @@ -49,6 +50,7 @@ export class MongoDBService< async patch(id: null, data: PatchData, params?: ServiceParams): Promise async patch(id: AdapterId, data: PatchData, params?: ServiceParams): Promise + async patch(id: NullableAdapterId, data: PatchData, params?: ServiceParams): Promise async patch(id: NullableAdapterId, data: PatchData, params?: ServiceParams): Promise { const { $limit, ...query } = await this.sanitizeQuery(params) @@ -60,6 +62,7 @@ export class MongoDBService< async remove(id: AdapterId, params?: ServiceParams): Promise async remove(id: null, params?: ServiceParams): Promise + async remove(id: NullableAdapterId, params?: ServiceParams): Promise async remove(id: NullableAdapterId, params?: ServiceParams): Promise { const { $limit, ...query } = await this.sanitizeQuery(params)