pqb 0.3.7 → 0.3.9
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/dist/index.d.ts +31 -6
- package/dist/index.esm.js +57 -9
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +56 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/pnpm-lock.yaml +2821 -0
- package/src/errors.ts +44 -0
- package/src/queryMethods/then.test.ts +84 -4
- package/src/queryMethods/then.ts +51 -18
- package/src/test-utils.ts +8 -0
package/src/errors.ts
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
|
+
import { DatabaseError } from 'pg';
|
|
2
|
+
|
|
1
3
|
export class PormError extends Error {}
|
|
2
4
|
|
|
5
|
+
export class QueryError extends DatabaseError {
|
|
6
|
+
message!: string;
|
|
7
|
+
code: string | undefined;
|
|
8
|
+
detail: string | undefined;
|
|
9
|
+
severity: string | undefined;
|
|
10
|
+
hint: string | undefined;
|
|
11
|
+
position: string | undefined;
|
|
12
|
+
internalPosition: string | undefined;
|
|
13
|
+
internalQuery: string | undefined;
|
|
14
|
+
where: string | undefined;
|
|
15
|
+
schema: string | undefined;
|
|
16
|
+
table: string | undefined;
|
|
17
|
+
column: string | undefined;
|
|
18
|
+
dataType: string | undefined;
|
|
19
|
+
constraint: string | undefined;
|
|
20
|
+
file: string | undefined;
|
|
21
|
+
line: string | undefined;
|
|
22
|
+
routine: string | undefined;
|
|
23
|
+
|
|
24
|
+
get isUnique() {
|
|
25
|
+
return this.code === '23505';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
columnsCache?: Record<string, boolean>;
|
|
29
|
+
get columns() {
|
|
30
|
+
if (this.columnsCache) return this.columnsCache;
|
|
31
|
+
|
|
32
|
+
const columns: Record<string, boolean> = {};
|
|
33
|
+
|
|
34
|
+
if (this.detail) {
|
|
35
|
+
const list = this.detail.match(/\((.*)\)=/)?.[1];
|
|
36
|
+
if (list) {
|
|
37
|
+
list.split(', ').forEach((column) => {
|
|
38
|
+
columns[column.startsWith('"') ? column.slice(1, -1) : column] = true;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return (this.columnsCache = columns);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
3
47
|
export class NotFoundError extends PormError {
|
|
4
48
|
constructor(message = 'Record is not found') {
|
|
5
49
|
super(message);
|
|
@@ -1,12 +1,92 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { assertType, UniqueTable, User, useTestDatabase } from '../test-utils';
|
|
2
|
+
import { raw } from '../common';
|
|
3
|
+
import { columnTypes } from '../columnSchema';
|
|
4
|
+
import { QueryError } from '../errors';
|
|
2
5
|
|
|
3
6
|
describe('then', () => {
|
|
7
|
+
useTestDatabase();
|
|
8
|
+
|
|
4
9
|
describe('catch', () => {
|
|
5
|
-
it
|
|
6
|
-
|
|
7
|
-
|
|
10
|
+
it('should catch error', (done) => {
|
|
11
|
+
const query = User.select({
|
|
12
|
+
column: raw(columnTypes.boolean(), 'koko'),
|
|
13
|
+
}).catch((err) => {
|
|
14
|
+
expect(err.message).toBe(`column "koko" does not exist`);
|
|
8
15
|
done();
|
|
9
16
|
});
|
|
17
|
+
|
|
18
|
+
assertType<Awaited<typeof query>, { column: boolean }[] | void>();
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('error handling', () => {
|
|
23
|
+
it('should capture stack trace properly', async () => {
|
|
24
|
+
let err: Error | undefined;
|
|
25
|
+
|
|
26
|
+
await User.select({ column: raw(columnTypes.boolean(), 'koko') }).catch(
|
|
27
|
+
(error) => (err = error),
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
expect(err?.stack).toContain('then.test.ts');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should have isUnique and column names map when violating unique error over single column', async () => {
|
|
34
|
+
await UniqueTable.insert({
|
|
35
|
+
one: 'one',
|
|
36
|
+
two: 1,
|
|
37
|
+
thirdColumn: 'three',
|
|
38
|
+
fourthColumn: 1,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
let err: QueryError | undefined;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
await UniqueTable.insert({
|
|
45
|
+
one: 'one',
|
|
46
|
+
two: 2,
|
|
47
|
+
thirdColumn: 'three',
|
|
48
|
+
fourthColumn: 2,
|
|
49
|
+
});
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (error instanceof QueryError) {
|
|
52
|
+
err = error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
expect(err?.isUnique).toBe(true);
|
|
57
|
+
expect(err?.columns).toEqual({
|
|
58
|
+
one: true,
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should have isUnique and column names map when violating unique error over multiple columns', async () => {
|
|
63
|
+
await UniqueTable.insert({
|
|
64
|
+
one: 'one',
|
|
65
|
+
two: 1,
|
|
66
|
+
thirdColumn: 'three',
|
|
67
|
+
fourthColumn: 1,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
let err: QueryError | undefined;
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
await UniqueTable.insert({
|
|
74
|
+
one: 'two',
|
|
75
|
+
two: 2,
|
|
76
|
+
thirdColumn: 'three',
|
|
77
|
+
fourthColumn: 1,
|
|
78
|
+
});
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (error instanceof QueryError) {
|
|
81
|
+
err = error;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
expect(err?.isUnique).toBe(true);
|
|
86
|
+
expect(err?.columns).toEqual({
|
|
87
|
+
thirdColumn: true,
|
|
88
|
+
fourthColumn: true,
|
|
89
|
+
});
|
|
10
90
|
});
|
|
11
91
|
});
|
|
12
92
|
});
|
package/src/queryMethods/then.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { ColumnsParsers, Query, QueryReturnType } from '../query';
|
|
2
2
|
import { getQueryParsers } from '../common';
|
|
3
|
-
import { NotFoundError } from '../errors';
|
|
3
|
+
import { NotFoundError, QueryError } from '../errors';
|
|
4
4
|
import { QueryArraysResult, QueryResult } from '../adapter';
|
|
5
5
|
import { CommonQueryData, Sql } from '../sql';
|
|
6
6
|
import { AfterCallback, BeforeCallback } from './callbacks';
|
|
7
7
|
import { getValueKey } from './get';
|
|
8
|
+
import { DatabaseError } from 'pg';
|
|
8
9
|
|
|
9
10
|
export type ThenResult<Res> = (
|
|
10
11
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -28,27 +29,24 @@ export const queryMethodByReturnType: Record<
|
|
|
28
29
|
void: 'arrays',
|
|
29
30
|
};
|
|
30
31
|
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
type Resolve = (result: any) => any;
|
|
34
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
|
+
type Reject = (error: any) => any;
|
|
36
|
+
|
|
37
|
+
let queryError: QueryError = undefined as unknown as QueryError;
|
|
38
|
+
|
|
31
39
|
export class Then {
|
|
32
|
-
then(
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
resolve?: (result: any) => any,
|
|
36
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
37
|
-
reject?: (error: any) => any,
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
-
): Promise<any> {
|
|
40
|
-
if (this.query.wrapInTransaction && !this.query.inTransaction) {
|
|
41
|
-
return this.transaction((q) => then(q, resolve, reject));
|
|
42
|
-
} else {
|
|
43
|
-
return then(this, resolve, reject);
|
|
44
|
-
}
|
|
40
|
+
get then() {
|
|
41
|
+
queryError = new (QueryError as unknown as new () => QueryError)();
|
|
42
|
+
return maybeWrappedThen;
|
|
45
43
|
}
|
|
46
44
|
|
|
47
|
-
async catch<Result>(
|
|
48
|
-
this:
|
|
45
|
+
async catch<T extends Query, Result>(
|
|
46
|
+
this: T,
|
|
49
47
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
50
48
|
fn: (reason: any) => Result | PromiseLike<Result>,
|
|
51
|
-
): Promise<Result> {
|
|
49
|
+
): Promise<ReturnType<T['then']> | Result> {
|
|
52
50
|
return this.then(undefined, fn);
|
|
53
51
|
}
|
|
54
52
|
}
|
|
@@ -60,6 +58,14 @@ export const handleResult: CommonQueryData['handleResult'] = async (
|
|
|
60
58
|
return parseResult(q, q.query.returnType || 'all', result);
|
|
61
59
|
};
|
|
62
60
|
|
|
61
|
+
function maybeWrappedThen(this: Query, resolve?: Resolve, reject?: Reject) {
|
|
62
|
+
if (this.query.wrapInTransaction && !this.query.inTransaction) {
|
|
63
|
+
return this.transaction((q) => then(q, resolve, reject));
|
|
64
|
+
} else {
|
|
65
|
+
return then(this, resolve, reject);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
63
69
|
const then = async (
|
|
64
70
|
q: Query,
|
|
65
71
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -117,7 +123,10 @@ const then = async (
|
|
|
117
123
|
}
|
|
118
124
|
|
|
119
125
|
resolve?.(result);
|
|
120
|
-
} catch (
|
|
126
|
+
} catch (err) {
|
|
127
|
+
const error =
|
|
128
|
+
err instanceof DatabaseError ? assignError(queryError, err) : err;
|
|
129
|
+
|
|
121
130
|
if (q.query.log && sql && logData) {
|
|
122
131
|
q.query.log.onError(error as Error, sql, logData);
|
|
123
132
|
}
|
|
@@ -125,6 +134,30 @@ const then = async (
|
|
|
125
134
|
}
|
|
126
135
|
};
|
|
127
136
|
|
|
137
|
+
const assignError = (to: QueryError, from: DatabaseError) => {
|
|
138
|
+
to.message = from.message;
|
|
139
|
+
(to as { length?: number }).length = from.length;
|
|
140
|
+
(to as { name?: string }).name = from.name;
|
|
141
|
+
to.severity = from.severity;
|
|
142
|
+
to.code = from.code;
|
|
143
|
+
to.detail = from.detail;
|
|
144
|
+
to.hint = from.hint;
|
|
145
|
+
to.position = from.position;
|
|
146
|
+
to.internalPosition = from.internalPosition;
|
|
147
|
+
to.internalQuery = from.internalQuery;
|
|
148
|
+
to.where = from.where;
|
|
149
|
+
to.schema = from.schema;
|
|
150
|
+
to.table = from.table;
|
|
151
|
+
to.column = from.column;
|
|
152
|
+
to.dataType = from.dataType;
|
|
153
|
+
to.constraint = from.constraint;
|
|
154
|
+
to.file = from.file;
|
|
155
|
+
to.line = from.line;
|
|
156
|
+
to.routine = from.routine;
|
|
157
|
+
|
|
158
|
+
return to;
|
|
159
|
+
};
|
|
160
|
+
|
|
128
161
|
export const parseResult = (
|
|
129
162
|
q: Query,
|
|
130
163
|
returnType: QueryReturnType | undefined = 'all',
|
package/src/test-utils.ts
CHANGED
|
@@ -60,6 +60,14 @@ export const Chat = db('chat', (t) => ({
|
|
|
60
60
|
...t.timestamps(),
|
|
61
61
|
}));
|
|
62
62
|
|
|
63
|
+
export const UniqueTable = db('uniqueTable', (t) => ({
|
|
64
|
+
one: t.text().unique(),
|
|
65
|
+
two: t.integer().unique(),
|
|
66
|
+
thirdColumn: t.text(),
|
|
67
|
+
fourthColumn: t.integer(),
|
|
68
|
+
...t.unique(['thirdColumn', 'fourthColumn']),
|
|
69
|
+
}));
|
|
70
|
+
|
|
63
71
|
export type MessageRecord = typeof Message['type'];
|
|
64
72
|
export const Message = db('message', (t) => ({
|
|
65
73
|
id: t.serial().primaryKey(),
|