mirror of
https://github.com/directus/directus.git
synced 2026-01-30 03:38:05 -05:00
54 lines
1022 B
TypeScript
54 lines
1022 B
TypeScript
import knex, { Config } from 'knex';
|
|
|
|
export type Credentials = {
|
|
filename?: string;
|
|
host?: string;
|
|
port?: number;
|
|
database?: string;
|
|
username?: string;
|
|
password?: string;
|
|
};
|
|
export default async function installDB(
|
|
client: 'sqlite3' | 'mysql' | 'pg' | 'oracledb' | 'mssql',
|
|
credentials: Credentials
|
|
): Promise<void> {
|
|
let connection: Config['connection'] = {};
|
|
|
|
if (client === 'sqlite3') {
|
|
const { filename } = credentials;
|
|
|
|
connection = {
|
|
filename: filename,
|
|
};
|
|
} else {
|
|
const { host, port, database, username, password } = credentials as Credentials;
|
|
|
|
connection = {
|
|
host: host,
|
|
port: port,
|
|
database: database,
|
|
user: username,
|
|
password: password,
|
|
};
|
|
}
|
|
|
|
const db = knex({
|
|
client: client,
|
|
connection: connection,
|
|
seeds:
|
|
process.env.NODE_ENV === 'development'
|
|
? {
|
|
extension: 'ts',
|
|
directory: './src/database/seeds/',
|
|
}
|
|
: {
|
|
extension: 'js',
|
|
directory: './dist/database/seeds/',
|
|
},
|
|
});
|
|
|
|
await db.seed.run();
|
|
|
|
db.destroy();
|
|
}
|