-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
198 lines (171 loc) · 4.91 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// @flow
const knex = require("knex")
const fs = require("fs")
const { parse: parsePG } = require("pg-connection-string")
const debug = require("debug")("pgknexlove")
let singletonDB = null
const createDatabaseGetter = ({
migrationFile,
seedFile,
migrationSQL = "",
seedSQL = "",
pool,
defaults = {},
} = {}) => {
if (migrationFile) {
migrationSQL = fs.readFileSync(migrationFile).toString()
}
if (seedFile) {
seedSQL = fs.readFileSync(seedFile).toString()
}
const getConnectionInfo = (database, user) => {
if (singletonDB)
return {
...singletonDB.connection,
database: database || singletonDB.connection.database,
user: user || singletonDB.connection.user,
}
const uri =
process.env.POSTGRES_URI ||
process.env.PG_URI ||
process.env.DATABASE_URL ||
process.env.DATABASE_URI
if (uri) {
const uriObj = parsePG(uri)
return {
...uriObj,
database: database || uriObj.database,
user: user || uriObj.user,
ssl: uriObj.ssl ? { ...uriObj.ssl, rejectUnauthorized: false } : false,
}
} else {
return {
host: process.env.POSTGRES_HOST || defaults.host || "localhost",
user:
user ||
process.env.POSTGRES_USER ||
process.env.POSTGRES_USERNAME ||
defaults.user ||
"postgres",
port: process.env.POSTGRES_PORT || defaults.port || 5432,
password:
process.env.POSTGRES_PASS ||
process.env.POSTGRES_PASSWORD ||
defaults.password ||
"",
database:
database ||
process.env.POSTGRES_DATABASE ||
process.env.POSTGRES_DB ||
defaults.database ||
defaults.databaseName ||
"postgres",
ssl: process.env.POSTGRES_SSL ? { rejectUnauthorized: false } : false,
}
}
}
const getConnectionString = (...args) => {
const { host, password, port, database, user } = getConnectionInfo(...args)
// TODO sslmode?
return `postgresql://${user}:${password}@${host}:${port}/${database}`
}
const knexConfig = {
client: "pg",
pool,
}
const createDatabase = async (dbName) => {
try {
let conn = await knex({
...knexConfig,
connection: getConnectionInfo("postgres"),
})
await conn.raw(`CREATE DATABASE ${dbName}`)
await conn.destroy()
} catch (e) {}
}
const deleteDatabase = async (dbName) => {
try {
let conn = await knex({
...knexConfig,
connection: getConnectionInfo("postgres"),
})
await conn.raw(`DROP DATABASE ${dbName}`)
await conn.destroy()
} catch (e) {}
}
const getDB = async ({ seed, migrate, testMode, user } = {}) => {
if (singletonDB && !testMode) return singletonDB
testMode =
testMode === undefined ? Boolean(process.env.USE_TEST_DB) : testMode
const dbName = !testMode
? process.env.POSTGRES_DATABASE ||
process.env.POSTGRES_DB ||
defaults.database ||
defaults.databaseName ||
"postgres"
: `testdb_${Math.random().toString(36).slice(7)}`
if (testMode)
debug(`\n---\nUsing Test DB: ${dbName}, User: ${user || "none"}\n---`)
await createDatabase(dbName)
const connection = getConnectionInfo(dbName)
let pg = knex({
...knexConfig,
connection,
})
// test connection
try {
await pg.raw("select 1+1 as result")
} catch (e) {
throw new Error("Could not connect to database\n\n" + e.toString())
}
// upload migration
if (migrate) await pg.raw(migrationSQL)
if (seed) await pg.raw(seedSQL)
if (user) {
await pg.destroy()
pg = knex({
...knexConfig,
connection: getConnectionInfo(dbName),
})
await pg.raw(`SET ROLE ${user};`)
// test connection
try {
await pg.raw("select 1+1 as result")
} catch (e) {
throw new Error(
`Could not connect to database as "${user}"\n\n${e.toString()}`
)
}
}
// override pg.destroy so we can delete the test database in test mode
pg.destroyHooks = []
const proxiedPg = new Proxy(pg, {
get: (obj, prop) => {
if (prop === "destroy") {
return async () => {
singletonDB = null
for (const hook of obj.destroyHooks) {
await hook()
}
await obj.destroy()
if (testMode) await deleteDatabase(dbName)
}
} else if (prop === "connection") {
return connection
} else {
return obj[prop]
}
},
})
if (!testMode) {
singletonDB = proxiedPg
}
return proxiedPg
}
getDB.getConnectionInfo = getConnectionInfo
getDB.getConnectionString = getConnectionString
return getDB
}
module.exports = createDatabaseGetter
module.exports.default = createDatabaseGetter()
module.exports.knex = knex