This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstorage.js
129 lines (116 loc) · 4.12 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const funcs = [];
funcs.push(require('./models/users.js'));
funcs.push(require('./models/challenges.js'));
funcs.push(require('./models/roles.js'));
funcs.push(require('./models/user_roles.js'));
funcs.push(require('./models/role_permissions.js'));
const Schema = require('caminte').Schema
const memoryUpdate = (model, filter, data, callback) => {
'use strict';
if ('function' === typeof filter) {
return filter(new Error('Get parametrs undefined'), null);
}
if ('function' === typeof data) {
return data(new Error('Set parametrs undefined'), null);
}
filter = filter.where ? filter.where : filter;
const mem = this;
// filter input to make sure it only contains valid fields
const cleanData = this.toDatabase(model, data);
if (data.id) {
// should find one and only one
this.exists(model, data.id, function (err, exists) {
if (exists) {
mem.save(model, Object.assign(exists, cleanData), callback);
} else {
callback(err, cleanData);
}
})
} else {
this.all(model, filter, function(err, nodes) {
if (!nodes.length) {
return callback(false, cleanData);
}
nodes.forEach(function(node) {
mem.cache[model][node.id] = Object.assign(node, cleanData);
});
callback(false, cleanData);
});
}
}
function start(config) {
// schema backend type
const schemaType = process.env.database__default__type || config && config.database && config.database.type ||'memory';
// FIXME: move to lib.config
// expose the set schemaType
module.exports.schemaType = schemaType;
//console.log('storage config', config)
const options = {
host: config && config.database && config.database.host || 'localhost',
database: config && config.database && config.database.database || 'lmpc_overlay',
username: config && config.database && config.database.username || 'overlay',
password: config && config.database && config.database.password || 'publicchat_test',
};
//console.log('options', options)
if (process.env.database__default__options__host) {
options.host = process.env.database__default__options__host;
}
if (process.env.database__default__options__database) {
options.database = process.env.database__default__options__database;
}
if (process.env.database__default__options__username) {
options.username = process.env.database__default__options__username;
}
if (process.env.database__default__options__password) {
options.password = process.env.database__default__options__password;
}
if (process.env.database__overlay__options__username) {
options.username = process.env.database__overlay__options__username;
}
if (process.env.database__overlay__options__password) {
options.password = process.env.database__overlay__options__password;
}
if (process.env.database__overlay__options__database) {
options.database = process.env.database__overlay__options__database;
}
const schema = new Schema(schemaType, options);
if (schemaType === 'memory') {
schema.adapter.update = memoryUpdate;
}
if (schemaType==='mysql') {
//charset: "utf8_general_ci" / utf8mb4_general_ci
// run a query "set names utf8"
schema.client.changeUser({ charset: 'utf8mb4' }, function(err) {
if (err) console.error('Couldnt set UTF8mb4', err);
});
// to enable emojis we may need to run these
// alter table X MODIFY `Y` type CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
}
const modelOptions = {
schema: schema,
};
funcs.forEach((func) => {
func.start(modelOptions);
});
if (schemaType=='mysql' || schemaType=='sqlite3') {
//schema.automigrate(function() {});
// don't lose data
schema.autoupdate(function() {});
}
var dbMonitor=function () {
if (schemaType=='mysql') {
schema.client.ping(function (err) {
if (err) {
console.log('trying to reconnect to data db');
schema = new Schema(schemaType, options);
}
})
}
}
dbMonitor();
setInterval(dbMonitor, 60*1000);
}
module.exports = {};
funcs.map(func => Object.assign(module.exports, func));
// override all those starts
module.exports.start = start;