-
Notifications
You must be signed in to change notification settings - Fork 0
/
createDatabase.ts
54 lines (47 loc) · 1.31 KB
/
createDatabase.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import Knex from 'knex';
import { Client } from 'pg';
async function run() {
const client = new Client({
user: 'ob',
password: 'dev',
database: 'postgres',
});
await client.connect();
try {
await client.query(`CREATE DATABASE \"typedKnexTest\" OWNER ob;`);
console.log('created database');
await client.end();
const configObj = {
client: 'postgresql',
connection: {
host: '127.0.0.1',
port: '5432',
database: 'typedKnexTest',
user: 'ob',
password: 'dev',
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
directory: './build/migrations',
},
asyncStackTraces: true,
}
const database = Knex(configObj);
await database.migrate.latest();
console.log('Database has been migrated');
process.exit();
} catch (e) {
// code '42P04' means database already exists
if (e.code === '42P04') {
console.error(`database already exists... ${e}`);
} else {
console.error(e);
}
process.exit();
}
}
run();