Skip to content

Commit

Permalink
fix(databases): Make sure adapter method signatures are exported prop…
Browse files Browse the repository at this point in the history
…erly
  • Loading branch information
daffl committed Dec 20, 2022
1 parent a443673 commit 5912c28
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/knex/src/error-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
3 changes: 3 additions & 0 deletions packages/knex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class KnexService<

async create(data: Data, params?: ServiceParams): Promise<Result>
async create(data: Data[], params?: ServiceParams): Promise<Result[]>
async create(data: Data | Data[], params?: ServiceParams): Promise<Result | Result[]>
async create(data: Data | Data[], params?: ServiceParams): Promise<Result | Result[]> {
if (Array.isArray(data) && !this.allowsMulti('create', params)) {
throw new MethodNotAllowed('Can not create multiple entries')
Expand All @@ -54,6 +55,7 @@ export class KnexService<

async patch(id: Id, data: PatchData, params?: ServiceParams): Promise<Result>
async patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
async patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>
async patch(id: NullableId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]> {
const { $limit, ...query } = await this.sanitizeQuery(params)

Expand All @@ -65,6 +67,7 @@ export class KnexService<

async remove(id: Id, params?: ServiceParams): Promise<Result>
async remove(id: null, params?: ServiceParams): Promise<Result[]>
async remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]>
async remove(id: NullableId, params?: ServiceParams): Promise<Result | Result[]> {
const { $limit, ...query } = await this.sanitizeQuery(params)

Expand Down
65 changes: 65 additions & 0 deletions packages/knex/test/error-handler.test.ts
Original file line number Diff line number Diff line change
@@ -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'
})
})
})
4 changes: 1 addition & 3 deletions packages/mongodb/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions packages/mongodb/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class MongoDBService<

async create(data: Data, params?: ServiceParams): Promise<Result>
async create(data: Data[], params?: ServiceParams): Promise<Result[]>
async create(data: Data | Data[], params?: ServiceParams): Promise<Result | Result[]>
async create(data: Data | Data[], params?: ServiceParams): Promise<Result | Result[]> {
if (Array.isArray(data) && !this.allowsMulti('create', params)) {
throw new MethodNotAllowed('Can not create multiple entries')
Expand All @@ -49,6 +50,7 @@ export class MongoDBService<

async patch(id: null, data: PatchData, params?: ServiceParams): Promise<Result[]>
async patch(id: AdapterId, data: PatchData, params?: ServiceParams): Promise<Result>
async patch(id: NullableAdapterId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]>
async patch(id: NullableAdapterId, data: PatchData, params?: ServiceParams): Promise<Result | Result[]> {
const { $limit, ...query } = await this.sanitizeQuery(params)

Expand All @@ -60,6 +62,7 @@ export class MongoDBService<

async remove(id: AdapterId, params?: ServiceParams): Promise<Result>
async remove(id: null, params?: ServiceParams): Promise<Result[]>
async remove(id: NullableAdapterId, params?: ServiceParams): Promise<Result | Result[]>
async remove(id: NullableAdapterId, params?: ServiceParams): Promise<Result | Result[]> {
const { $limit, ...query } = await this.sanitizeQuery(params)

Expand Down

0 comments on commit 5912c28

Please # to comment.