| 18 | options__encrypt?: boolean; |
| 19 | }; |
| 20 | export default function createDBConnection(client: Driver, credentials: Credentials): Knex<any, unknown[]> { |
| 21 | let connection: any = {}; |
| 22 | |
| 23 | if (client === 'sqlite3') { |
| 24 | const { filename } = credentials; |
| 25 | |
| 26 | connection = { |
| 27 | filename: filename as string, |
| 28 | }; |
| 29 | } else { |
| 30 | const { host, port, database, user, password } = credentials as Credentials; |
| 31 | |
| 32 | connection = { |
| 33 | host: host, |
| 34 | port: Number(port), |
| 35 | database: database, |
| 36 | user: user, |
| 37 | password: password, |
| 38 | }; |
| 39 | |
| 40 | if (client === 'pg' || client === 'cockroachdb') { |
| 41 | const { ssl } = credentials as Credentials; |
| 42 | connection.ssl = ssl; |
| 43 | } |
| 44 | |
| 45 | if (client === 'mssql') { |
| 46 | const { options__encrypt } = credentials as Credentials; |
| 47 | |
| 48 | connection = { |
| 49 | ...connection, |
| 50 | encrypt: options__encrypt, |
| 51 | }; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | const knexConfig: Knex.Config = { |
| 56 | client: client, |
| 57 | connection: connection, |
| 58 | seeds: { |
| 59 | extension: 'js', |
| 60 | directory: path.resolve(__dirname, '../../database/seeds/'), |
| 61 | }, |
| 62 | pool: {}, |
| 63 | }; |
| 64 | |
| 65 | if (client === 'sqlite3') { |
| 66 | knexConfig.useNullAsDefault = true; |
| 67 | } |
| 68 | |
| 69 | if (client === 'cockroachdb') { |
| 70 | knexConfig.pool!.afterCreate = (conn: any, callback: any) => { |
| 71 | conn.query('SET serial_normalization = "sql_sequence"'); |
| 72 | conn.query('SET default_int_size = 4'); |
| 73 | |
| 74 | callback(null, conn); |
| 75 | }; |
| 76 | } |
| 77 | |