|
| 1 | +const ConfigurationService = require('../configuration-service'), |
| 2 | + debug = require('debug')('virtual-assistant:database-sql'), |
| 3 | + SqlAdapter = require('js-data-sql').SqlAdapter, |
| 4 | + Container = require('js-data').Container; |
| 5 | + |
| 6 | + |
| 7 | +class SqlDao { |
| 8 | + |
| 9 | + static getContainer(name) { |
| 10 | + if(!this.dbClient) { |
| 11 | + this.dbClient = ConfigurationService.get('database.sql.client'); |
| 12 | + if(!this.dbClient) { |
| 13 | + debug(`Error: missing sql client configuration. |
| 14 | +Please set the configuration \`database.sql.client\` with the sql client you want to use: "mysql", "pg", or "sqlite3"`); |
| 15 | + } |
| 16 | + } |
| 17 | + if((this.dbClient === 'pg' || this.dbClient === 'mysql') && !this.dbConnectionHost) { |
| 18 | + this.dbConnectionHost = ConfigurationService.get('database.sql.connection.host'); |
| 19 | + if(!this.dbConnectionHost) { |
| 20 | + debug(`Error: missing sql host configuration. |
| 21 | +Please set the configuration \`database.sql.connection.host\` with the sql hostname`); |
| 22 | + } |
| 23 | + } |
| 24 | + if((this.dbClient === 'pg' || this.dbClient === 'mysql') && !this.dbConnectionUser) { |
| 25 | + this.dbConnectionUser = ConfigurationService.get('database.sql.connection.user'); |
| 26 | + if(!this.dbConnectionUser) { |
| 27 | + debug(`Error: missing sql user configuration. |
| 28 | +Please set the configuration \`database.sql.connection.user\` with the sql connection user`); |
| 29 | + } |
| 30 | + } |
| 31 | + if((this.dbClient === 'pg' || this.dbClient === 'mysql') && !this.dbConnectionPassword) { |
| 32 | + this.dbConnectionPassword = ConfigurationService.get('database.sql.connection.password'); |
| 33 | + if(!this.dbConnectionPassword) { |
| 34 | + debug(`Error: missing sql password configuration. |
| 35 | +Please set the configuration \`database.sql.connection.password\` with the sql connection password`); |
| 36 | + } |
| 37 | + } |
| 38 | + if((this.dbClient === 'pg' || this.dbClient === 'mysql') && !this.dbConnectionDatabase) { |
| 39 | + this.dbConnectionDatabase = ConfigurationService.get('database.sql.connection.database'); |
| 40 | + if(!this.dbConnectionDatabase) { |
| 41 | + debug(`Error: missing sql database configuration. |
| 42 | +Please set the configuration \`database.sql.connection.database\` with the sql connection database`); |
| 43 | + } |
| 44 | + } |
| 45 | + if((this.dbClient === 'pg' || this.dbClient === 'mysql') && !this.dbConnectionPort) { |
| 46 | + this.dbConnectionPort = ConfigurationService.get('database.sql.connection.port'); |
| 47 | + if(!this.dbConnectionPort) { |
| 48 | + debug(`Error: missing sql port configuration. |
| 49 | +Please set the configuration \`database.sql.connection.port\` with the sql connection port`); |
| 50 | + } |
| 51 | + } |
| 52 | + if(this.dbClient === 'sqlite3' && !this.dbConnectionFile) { |
| 53 | + this.dbConnectionFile = ConfigurationService.get('database.sql.connection.file'); |
| 54 | + if(!this.dbConnectionFile) { |
| 55 | + debug(`Error: missing sql file configuration. |
| 56 | +Please set the configuration \`database.sql.connection.file\` with the sql connection file`); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + let adapter; |
| 61 | + if(this.adapter) { |
| 62 | + adapter = this.adapter; |
| 63 | + } |
| 64 | + else { |
| 65 | + let opts = {}; |
| 66 | + if(this.dbClient === 'sqlite3') { |
| 67 | + opts = { |
| 68 | + filename: this.dbConnectionFile |
| 69 | + }; |
| 70 | + } |
| 71 | + else if(this.dbClient === 'pg' || this.dbClient === 'mysql') { |
| 72 | + opts = { |
| 73 | + host: this.dbConnectionHost, |
| 74 | + user: this.dbConnectionUser, |
| 75 | + password: this.dbConnectionPassword, |
| 76 | + database: this.dbConnectionDatabase, |
| 77 | + port: this.dbConnectionPort |
| 78 | + }; |
| 79 | + } |
| 80 | + else { |
| 81 | + debug(`Error: invalid sql client [${this.dbClient}]. |
| 82 | +Please set the configuration \`database.sql.client\` with the sql client you want to use: "mysql", "pg", or "sqlite3".`); |
| 83 | + } |
| 84 | + adapter = new SqlAdapter({ |
| 85 | + knexOpts: { |
| 86 | + client: this.dbClient, |
| 87 | + connection: opts |
| 88 | + } |
| 89 | + }); |
| 90 | + if(this.dbClient && this.dbConnectionHost && this.dbConnectionUser && this.dbConnectionPassword && this.dbConnectionDatabase && this.dbConnectionPort) { |
| 91 | + // Only keep adapter if the config is set, to avoid reloading on first configuration |
| 92 | + this.adapter = adapter; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + let store = new Container(); |
| 97 | + store.registerAdapter('sql', adapter, { default: true }); |
| 98 | + return store; |
| 99 | + } |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = SqlDao; |
0 commit comments