Skip to content

Commit 625bb08

Browse files
committed
feat(database): add SQL support for persistence. Based on js-data (which internally uses Knex)
1 parent 0d985ea commit 625bb08

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@
1313
"js-data-adapter": "^0.8.3",
1414
"js-data-cloud-datastore": "^1.0.0-rc.1",
1515
"js-data-mongodb": "^0.7.0",
16+
"js-data-sql": "git://github.com/js-data/js-data-sql.git#v3",
17+
"knex": "^0.12.6",
1618
"lodash": "^4.17.3",
1719
"memory-cache": "^0.1.6",
18-
"object-path": "^0.11.2"
20+
"mysql": "^2.13.0",
21+
"object-path": "^0.11.2",
22+
"pg": "^6.1.2",
23+
"sqlite3": "^3.1.8"
1924
},
2025
"devDependencies": {
2126
"standard-version": "^4.0.0"

src/helpers/database/sql.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)