Skip to content

Commit 0cb4c47

Browse files
committed
refactor: knex config
1 parent f012928 commit 0cb4c47

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

config/default.cjs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ module.exports = {
1111
tls: false,
1212
},
1313
},
14-
sql: {
15-
url: 'mysql://directus:password@localhost:3306/directus',
14+
db: {
15+
type: 'mysql',
16+
connection: 'mysql://directus:password@localhost:3306/directus',
1617
},
1718
admin: {
1819
key: '',

knexfile.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import _ from 'lodash';
2+
import config from 'config';
3+
4+
const dbConfig = config.get('db');
5+
6+
/**
7+
* @typedef {import('knex').Knex.Config} KnexConfig
8+
* @type {{ [key: string]: KnexConfig }}
9+
*/
10+
export default _.merge({}, ...[ 'development', 'production', 'staging', 'test' ].map((environment) => {
11+
return {
12+
[environment]: {
13+
client: dbConfig.type,
14+
connection: dbConfig.connection,
15+
pool: {
16+
min: 0,
17+
max: 10,
18+
propagateCreateError: false,
19+
},
20+
acquireConnectionTimeout: 10000,
21+
},
22+
};
23+
}));

src/lib/sql/client.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import config from 'config';
21
import knex, { Knex } from 'knex';
2+
import knexfile from '../../../knexfile.js';
33

4-
export const client: Knex = knex({
5-
client: 'mysql',
6-
connection: config.get<string>('sql.url'),
7-
});
4+
const env = process.env['NODE_ENV'] || 'development';
5+
6+
export const client: Knex = knex(knexfile[env] || {});

0 commit comments

Comments
 (0)