-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
index.js
150 lines (121 loc) · 3.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// @flow
import pg, {
types
} from 'pg';
import {
parse as parseConnectionString
} from 'pg-connection-string';
import createDebug from 'debug';
import prettyHrtime from 'pretty-hrtime';
import arrayFlatten from 'array-flatten';
import {
DataIntegrityError,
DuplicateEntryError,
NotFoundError
} from './errors';
import {
normalizeValuePlaceholders
} from './utilities';
import type {
DatabaseConfigurationType,
DatabasePoolConnectionType,
DatabaseSingleConnectionType,
InternalQueryAnyType,
InternalQueryManyType,
InternalQueryOneType,
InternalQueryType
} from './types';
export type {
DatabaseConnectionType,
DatabasePoolConnectionType,
DatabaseSingleConnectionType
} from './types';
export {
DataIntegrityError,
DuplicateEntryError,
NotFoundError
};
types.setTypeParser(20, (value) => {
return parseInt(value, 10);
});
const debug = createDebug('mightyql');
export const query: InternalQueryType = async (connection, sql, values = []) => {
debug('query input', sql, values);
try {
const start = process.hrtime();
const result = await connection.query(normalizeValuePlaceholders(sql), arrayFlatten(values));
const end = process.hrtime(start);
debug('query execution time', prettyHrtime(end));
if (result.rowCount) {
debug('query returned %d row(s)', result.rowCount);
}
return result;
} catch (error) {
if (error.code === 'ER_DUP_ENTRY') {
throw new DuplicateEntryError(error.message);
}
throw error;
}
};
/**
* Makes a query and expects exactly one result.
*
* @throws NotFoundError If query returns no rows.
* @throws DataIntegrityError If query returns multiple rows.
*/
export const one: InternalQueryOneType = async (connection, sql, values) => {
const {rows} = await query(connection, sql, values);
if (rows.length === 0) {
throw new NotFoundError();
}
if (rows.length > 1) {
throw new DataIntegrityError();
}
return rows[0];
};
/**
* Makes a query and expects at least 1 result.
*
* @throws NotFoundError If query returns no rows.
*/
export const many: InternalQueryManyType = async (connection, sql, values) => {
const {rows} = await query(connection, sql, values);
if (rows.length === 0) {
throw new NotFoundError();
}
return rows;
};
/**
* Makes a query and expects any number of results.
*/
export const any: InternalQueryAnyType = async (connection, sql, values) => {
const {rows} = await query(connection, sql, values);
return rows;
};
const createConnection = async (configuration: DatabaseConfigurationType): Promise<DatabaseSingleConnectionType> => {
const pool = new pg.Pool(configuration);
const connection = await pool.connect();
return {
any: any.bind(null, connection),
end: async () => {
await connection.release();
return pool.end();
},
many: many.bind(null, connection),
one: one.bind(null, connection),
query: query.bind(null, connection)
};
};
const createPool = (configuration: DatabaseConfigurationType): DatabasePoolConnectionType => {
const pool = new pg.Pool(typeof configuration === 'string' ? parseConnectionString(configuration) : configuration);
return {
any: any.bind(null, pool),
many: many.bind(null, pool),
one: one.bind(null, pool),
query: query.bind(null, pool)
};
};
export {
createConnection,
createPool
};