Skip to content

Commit 2b2cd43

Browse files
committed
feat: db util
1 parent 5f0757d commit 2b2cd43

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"mig:make": "npm run knex migrate:make -- -x ts",
1414
"seed": "npm run knex seed:run",
1515
"seed:make": "npm run knex seed:make -- -x ts",
16-
"seed:single": "ts-node --project ./apps/library/src/app/db-config/tsconfig.json ./node_modules/.bin/knex --knexfile ./apps/library/src/app/db-config/knexfile.ts seed:run -- --verbose --specific"
16+
"seed:single": "ts-node --project ./apps/library/src/app/db-config/tsconfig.json ./node_modules/.bin/knex --knexfile ./apps/library/src/app/db-config/knexfile.ts seed:run -- --verbose --specific",
17+
"db:reset": "node reset-db.js"
1718
},
1819
"private": true,
1920
"dependencies": {

migration.js reset-db.js

+18-16
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,40 @@ const Knex = require('knex')
33
// You can dynamically pass the database name
44
// as a command-line argument, or obtain it from
55
// a .env file
6-
const databaseName = 'lib1'
6+
const databaseName = 'lib1';
77

88
const connection = {
99
host: 'localhost',
1010
user: 'postgres',
11-
password: 'postgres'
12-
}
11+
password: 'postgres',
12+
port: 5432,
13+
ssl: false,
14+
};
1315

1416
async function main() {
1517
let knex = Knex({
16-
client: 'mysql',
18+
client: 'pg',
1719
connection
18-
})
20+
});
1921

22+
await knex.raw('DROP DATABASE IF EXISTS ?? ', databaseName)
2023
// Lets create our database if it does not exist
21-
await knex.raw('CREATE DATABASE IF NOT EXISTS ??', databaseName)
22-
24+
await knex.raw('CREATE DATABASE ?? ', databaseName)
2325

2426
// Now that our database is known, let's create another knex object
2527
// with database name specified so that we can run our migrations
26-
knex = Knex({
27-
client: 'mysql',
28-
connection: {
29-
...connection,
30-
database: databaseName,
31-
}
32-
})
28+
// knex = Knex({
29+
// client: 'pg',
30+
// connection: {
31+
// ...connection,
32+
// database: databaseName,
33+
// }
34+
// });
3335

3436
// Now we can happily run our migrations
35-
await knex.migrate.latest()
37+
// await knex.migrate.latest();
3638

3739
// Done!!
3840
}
3941

40-
main().catch(console.log).then(process.exit)
42+
main().catch(console.log).then(process.exit);

0 commit comments

Comments
 (0)