-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpg.ts
40 lines (32 loc) · 990 Bytes
/
pg.ts
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
import {
IPGQueryable,
IPGQueryConfig,
IPGQueryResult,
TemplateLiteralFunc
} from './lib/utils'
import _sql = require('./sql')
type PGSqlHelper<T> = (db: IPGQueryable) => TemplateLiteralFunc<Promise<T>>
type PGSql = TemplateLiteralFunc<IPGQueryConfig> & {
query: PGSqlHelper<IPGQueryResult>
many: PGSqlHelper<any[]>
one: PGSqlHelper<any>
count: PGSqlHelper<number>
}
const sql = ((chains, ...expressions) => _sql(chains, ...expressions)) as PGSql
sql.query = db => (chains, ...expressions) =>
db.query(_sql(chains, ...expressions))
sql.one = db => async (chains, ...expressions) => {
const {
rows: [row]
} = await db.query(_sql(chains, ...expressions))
return row
}
sql.many = db => async (chains, ...expressions) => {
const { rows } = await db.query(_sql(chains, ...expressions))
return rows
}
sql.count = db => async (chains, ...expressions) => {
const { rowCount } = await db.query(_sql(chains, ...expressions))
return rowCount
}
export = sql