beech-api 3.4.12 → 3.5.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -16
- package/package.json +15 -9
- package/packages/cli/beech +2 -2
- package/packages/cli/bin/beech-service.js +305 -0
- package/packages/cli/bin/beech.js +133 -66
- package/packages/cli/core/auth/Credentials.js +34 -1
- package/packages/cli/core/auth/Passport.js +215 -88
- package/packages/cli/core/configure/{app.config.js → app.config-basic.js} +4 -4
- package/packages/cli/core/configure/app.config-sequelize.js +89 -0
- package/packages/cli/core/configure/global.config-basic.js +9 -0
- package/packages/cli/core/configure/global.config-sequelize.js +9 -0
- package/packages/cli/core/configure/passport.config.js +4 -4
- package/packages/cli/core/databases/{mysql.connection.js → mysql.js} +12 -11
- package/packages/cli/core/databases/sequelize.js +149 -0
- package/packages/cli/core/generator/_basic-helpers +23 -0
- package/packages/cli/core/generator/_basic-models +25 -0
- package/packages/cli/core/generator/{endpoints → _endpoints} +5 -5
- package/packages/cli/core/generator/_helpers +10 -0
- package/packages/cli/core/generator/_models +23 -0
- package/packages/cli/core/generator/{package → _package} +2 -2
- package/packages/cli/core/generator/_service +8 -0
- package/packages/cli/core/generator/index.js +91 -24
- package/packages/cli/core/index.js +11 -4
- package/packages/cli/core/services/http.express.js +17 -15
- package/packages/lib/beech.js +90 -26
- package/packages/package.json +20 -0
- package/packages/public/icon/beech_128.png +0 -0
- package/packages/src-/Add-on.js +9 -0
- package/packages/src-/endpoints/hello-endpoints.js +118 -0
- package/packages/src-/endpoints/test2-endpoints.js +75 -0
- package/packages/src-/endpoints/testSequalize-endpoints.js +23 -0
- package/packages/src-/helpers/my/Test.js +11 -0
- package/packages/src-/helpers/my/Test2.js +11 -0
- package/packages/src-/helpers/my/Test3.js +11 -0
- package/packages/src-/helpers/my/Test4.js +11 -0
- package/packages/src-/models/Jubu.js +29 -0
- package/packages/src-/models/Jubu2.js +20 -0
- package/packages/src-/models/Map_master.js +22 -0
- package/packages/{cli/core/generator/models → src-/models/Test.js} +1 -1
- package/packages/src-/models/Test2.js +29 -0
- package/packages/src-/models/User2Sequelize.js +23 -0
- package/packages/src-/models/Users-sqlite.js +21 -0
- package/packages/src-/models/Users.js +53 -0
- package/packages/src-/models/Uuuuuxxx.js +23 -0
- package/packages/src-/models/xxx/Uuuuuxxx.js +23 -0
- package/packages/cli/core/configure/global.config.js +0 -7
- /package/packages/cli/core/configure/{gitignore → _gitignore} +0 -0
- /package/packages/cli/core/configure/{sequelizerc → _sequelizerc} +0 -0
- /package/packages/cli/core/generator/{add-on → _add-on} +0 -0
- /package/packages/cli/core/generator/{create → _create} +0 -0
- /package/packages/cli/core/generator/{help → _help} +0 -0
- /package/packages/cli/core/generator/{spec → _spec} +0 -0
- /package/packages/{cli/core/generator/helpers → src-/helpers/Test2.js} +0 -0
|
@@ -3,18 +3,19 @@ const fs = require("fs");
|
|
|
3
3
|
const passport_config_file = appRoot + "/passport.config.js";
|
|
4
4
|
const md5 = require("md5");
|
|
5
5
|
const secret = require("../../../lib/salt").salt;
|
|
6
|
+
const { QueryTypes } = require("sequelize");
|
|
6
7
|
|
|
7
8
|
module.exports = {
|
|
8
9
|
init() {
|
|
9
10
|
try {
|
|
10
11
|
if (fs.existsSync(passport_config_file)) {
|
|
11
12
|
var passport = require("passport"),
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
LocalStrategy = require("passport-local").Strategy,
|
|
14
|
+
GoogleStrategy = require("passport-google-oauth").OAuth2Strategy,
|
|
15
|
+
FacebookStrategy = require('passport-facebook').Strategy;
|
|
15
16
|
var passportJWT = require("passport-jwt"),
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
JWTStrategy = passportJWT.Strategy,
|
|
18
|
+
ExtractJWT = passportJWT.ExtractJwt;
|
|
18
19
|
const auth = require("./Credentials");
|
|
19
20
|
var passport_config = require(passport_config_file);
|
|
20
21
|
if (passport_config.jwt_allow) {
|
|
@@ -31,27 +32,47 @@ module.exports = {
|
|
|
31
32
|
let passportUsernameField = passport_config.model.username_field || "username";
|
|
32
33
|
let passportPasswordField = passport_config.model.password_field || "password";
|
|
33
34
|
let passportTable = passport_config.model.table || "users";
|
|
34
|
-
let passportFields = (passport_config.model.fields.length) ? passport_config.model.fields : ["id", "name", "email"];
|
|
35
|
+
let passportFields = (passport_config.model.fields.length) ? passport_config.model.fields : [ "id", "name", "email" ];
|
|
35
36
|
// passport initial with token (encoder)
|
|
36
37
|
passport.use(new LocalStrategy({
|
|
37
38
|
usernameField: passportUsernameField,
|
|
38
39
|
passwordField: passportPasswordField
|
|
39
|
-
}, (username, password, done) => {
|
|
40
|
-
let pool = eval("
|
|
40
|
+
}, async (username, password, done) => {
|
|
41
|
+
let pool = eval("sql." + passport_config.model.name);
|
|
41
42
|
if (pool) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
43
|
+
if (pool_base == "basic") {
|
|
44
|
+
// pool base is MySQL
|
|
45
|
+
pool.query("SELECT " + passportFields + " FROM ?? WHERE ?? = ? AND ?? = ?", [
|
|
46
|
+
passportTable,
|
|
47
|
+
passportUsernameField,
|
|
48
|
+
username,
|
|
49
|
+
passportPasswordField,
|
|
50
|
+
md5(password + secret)
|
|
51
|
+
], (err, result) => {
|
|
52
|
+
if (err) {
|
|
53
|
+
return done(err, null);
|
|
54
|
+
} else {
|
|
55
|
+
return done(null, JSON.parse(JSON.stringify(result[ 0 ] || null)));
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
} else if (pool_base == "sequelize") {
|
|
59
|
+
// pool base is Sequelize
|
|
60
|
+
try {
|
|
61
|
+
let result = await pool.query("SELECT " + passportFields + " FROM " + passportTable + " WHERE " + passportUsernameField + " = :username AND " + passportPasswordField + " = :password", {
|
|
62
|
+
replacements: {
|
|
63
|
+
fields: passportFields,
|
|
64
|
+
username: username,
|
|
65
|
+
password: md5(password + secret)
|
|
66
|
+
},
|
|
67
|
+
type: QueryTypes.SELECT
|
|
68
|
+
});
|
|
69
|
+
return done(null, JSON.parse(JSON.stringify(result[ 0 ] || null)));
|
|
70
|
+
} catch (error) {
|
|
71
|
+
return done(error, null);
|
|
53
72
|
}
|
|
54
|
-
}
|
|
73
|
+
} else {
|
|
74
|
+
return done({ error: "Base pool SQL error." }, null);
|
|
75
|
+
}
|
|
55
76
|
} else {
|
|
56
77
|
return done(null, null, true);
|
|
57
78
|
}
|
|
@@ -60,36 +81,54 @@ module.exports = {
|
|
|
60
81
|
passport.use(new JWTStrategy({
|
|
61
82
|
jwtFromRequest: ExtractJWT.fromAuthHeaderAsBearerToken(),
|
|
62
83
|
secretOrKey: passport_config.secret
|
|
63
|
-
}, (jwtPayload, done) => {
|
|
64
|
-
let pool = eval("
|
|
84
|
+
}, async (jwtPayload, done) => {
|
|
85
|
+
let pool = eval("sql." + passport_config.model.name);
|
|
65
86
|
if (pool) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
if (pool_base == "basic") {
|
|
88
|
+
// pool base is MySQL
|
|
89
|
+
pool.query("SELECT " + passportFields + " FROM ?? WHERE id = ?", [
|
|
90
|
+
passportTable,
|
|
91
|
+
jwtPayload.id
|
|
92
|
+
], (err, result) => {
|
|
93
|
+
if (err) {
|
|
94
|
+
return done(err, null);
|
|
95
|
+
} else {
|
|
96
|
+
return done(null, JSON.parse(JSON.stringify(result[ 0 ] || null)));
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
} else if (pool_base == "sequelize") {
|
|
100
|
+
// pool base is Sequelize
|
|
101
|
+
try {
|
|
102
|
+
let result = await pool.query("SELECT " + passportFields + " FROM " + passportTable + " WHERE id = :id", {
|
|
103
|
+
replacements: {
|
|
104
|
+
id: jwtPayload.id
|
|
105
|
+
},
|
|
106
|
+
type: QueryTypes.SELECT
|
|
107
|
+
});
|
|
108
|
+
return done(null, JSON.parse(JSON.stringify(result[ 0 ] || null)));
|
|
109
|
+
} catch (error) {
|
|
110
|
+
return done(error, null);
|
|
74
111
|
}
|
|
75
|
-
}
|
|
112
|
+
} else {
|
|
113
|
+
return done({ error: "Base pool SQL error." }, null);
|
|
114
|
+
}
|
|
76
115
|
} else {
|
|
77
116
|
return done(null, null, true);
|
|
78
117
|
}
|
|
79
118
|
}));
|
|
80
|
-
|
|
119
|
+
|
|
81
120
|
// declare head authentication enpoint for all strategy
|
|
82
|
-
let auth_endpoint = (passport_config.auth_endpoint) ? (passport_config.auth_endpoint[0] === "/" ? passport_config.auth_endpoint : "/" + passport_config.auth_endpoint) : "/authentication";
|
|
83
|
-
|
|
121
|
+
let auth_endpoint = (passport_config.auth_endpoint) ? (passport_config.auth_endpoint[ 0 ] === "/" ? passport_config.auth_endpoint : "/" + passport_config.auth_endpoint) : "/authentication";
|
|
122
|
+
|
|
84
123
|
/**
|
|
85
124
|
* Passport Google Strategy
|
|
86
125
|
*
|
|
87
126
|
*/
|
|
88
|
-
let google_callbackURL = (passport_config.strategy.google.callbackURL) ? (passport_config.strategy.google.callbackURL[0] === "/" ? passport_config.strategy.google.callbackURL : "/" + passport_config.strategy.google.callbackURL) : "/google/callback";
|
|
127
|
+
let google_callbackURL = (passport_config.strategy.google.callbackURL) ? (passport_config.strategy.google.callbackURL[ 0 ] === "/" ? passport_config.strategy.google.callbackURL : "/" + passport_config.strategy.google.callbackURL) : "/google/callback";
|
|
89
128
|
passport.use(new GoogleStrategy({
|
|
90
129
|
clientID: passport_config.strategy.google.client_id,
|
|
91
130
|
clientSecret: passport_config.strategy.google.client_secret,
|
|
92
|
-
callbackURL:
|
|
131
|
+
callbackURL: auth_endpoint + google_callbackURL
|
|
93
132
|
}, (accessToken, refreshToken, profile, done) => {
|
|
94
133
|
// find google user
|
|
95
134
|
let googleIdField = (passport_config.strategy.google.local_profile_fields.google_id) ? passport_config.strategy.google.local_profile_fields.google_id : "google_id";
|
|
@@ -101,20 +140,24 @@ module.exports = {
|
|
|
101
140
|
}
|
|
102
141
|
});
|
|
103
142
|
}));
|
|
104
|
-
|
|
143
|
+
|
|
105
144
|
/**
|
|
106
145
|
* Passport Facebook Strategy
|
|
107
146
|
*
|
|
108
147
|
*/
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
148
|
+
let facebook_callbackURL = (passport_config.strategy.facebook.callbackURL) ? (passport_config.strategy.facebook.callbackURL[ 0 ] === "/" ? passport_config.strategy.facebook.callbackURL : "/" + passport_config.strategy.facebook.callbackURL) : "/facebook/callback";
|
|
149
|
+
// merge fields permisions
|
|
150
|
+
let allow_permisions_fields = [ ...new Set([ ...[ 'id', 'email' ], ...passport_config.strategy.facebook.profileFieldsAllow ]) ];
|
|
151
|
+
passport.use(new FacebookStrategy({
|
|
113
152
|
clientID: passport_config.strategy.facebook.app_id,
|
|
114
153
|
clientSecret: passport_config.strategy.facebook.app_secret,
|
|
115
154
|
callbackURL: auth_endpoint + facebook_callbackURL,
|
|
116
155
|
profileFields: allow_permisions_fields
|
|
117
|
-
}, (accessToken, refreshToken, profile, done) => {
|
|
156
|
+
}, (accessToken, refreshToken, profile, done) => {
|
|
157
|
+
// Check if the email permission is granted
|
|
158
|
+
if (!profile.emails || profile.emails.length === 0) {
|
|
159
|
+
return done(new Error('Email permission not granted.'));
|
|
160
|
+
}
|
|
118
161
|
// find facebook user
|
|
119
162
|
let faecbookIdField = (passport_config.strategy.facebook.local_profile_fields.facebook_id) ? passport_config.strategy.facebook.local_profile_fields.facebook_id : "facebook_id";
|
|
120
163
|
this.findOrCreate(passport_config, "facebook", passportFields, passportTable, accessToken, refreshToken, profile, faecbookIdField, (err, res, dbFailed) => {
|
|
@@ -125,27 +168,28 @@ module.exports = {
|
|
|
125
168
|
}
|
|
126
169
|
});
|
|
127
170
|
}
|
|
128
|
-
|
|
171
|
+
));
|
|
129
172
|
} catch (error) {
|
|
130
173
|
throw error;
|
|
131
174
|
}
|
|
132
175
|
},
|
|
133
176
|
findOrCreate(passport_config, strategy_name, passportFields, passportTable, accessToken, refreshToken, profile, idField, cb) {
|
|
134
|
-
let pool = eval("
|
|
177
|
+
let pool = eval("sql." + passport_config.model.name);
|
|
135
178
|
if (pool) {
|
|
136
|
-
|
|
137
|
-
passportTable,
|
|
138
|
-
idField,
|
|
139
|
-
profile.id
|
|
140
|
-
], (err, result) => {
|
|
179
|
+
this.query_one_where(pool, "SELECT " + passportFields + " FROM " + passportTable, idField, profile.id, async (err, result) => {
|
|
141
180
|
if (err) {
|
|
142
181
|
cb(err);
|
|
143
182
|
} else {
|
|
144
183
|
// declare data response
|
|
145
184
|
let data = {};
|
|
185
|
+
// prepare data for store
|
|
186
|
+
let usr = passport_config.model.username_field || "username";
|
|
187
|
+
let psw = passport_config.model.password_field || "password";
|
|
188
|
+
let profileEmail = profile.emails[ 0 ].value.split("@")[ 0 ];
|
|
189
|
+
let md5Psw = md5(profile.id + secret);
|
|
146
190
|
// check strategy name for store
|
|
147
191
|
if (strategy_name == "google") {
|
|
148
|
-
if (!result[0]) { // find not found and create
|
|
192
|
+
if (!result[ 0 ]) { // find not found and create
|
|
149
193
|
// filter fields
|
|
150
194
|
let fields = [].concat.apply([], [
|
|
151
195
|
(passport_config.strategy.google.local_profile_fields.name) ? passport_config.strategy.google.local_profile_fields.name : null,
|
|
@@ -156,36 +200,62 @@ module.exports = {
|
|
|
156
200
|
// fileter values
|
|
157
201
|
let values = [].concat.apply([], [
|
|
158
202
|
(passport_config.strategy.google.local_profile_fields.name) ? profile.displayName : null,
|
|
159
|
-
(passport_config.strategy.google.local_profile_fields.email) ? profile.emails[0].value : null,
|
|
160
|
-
(passport_config.strategy.google.local_profile_fields.photos) ? profile.photos[0].value : null,
|
|
203
|
+
(passport_config.strategy.google.local_profile_fields.email) ? profile.emails[ 0 ].value : null,
|
|
204
|
+
(passport_config.strategy.google.local_profile_fields.photos) ? profile.photos[ 0 ].value : null,
|
|
161
205
|
(passport_config.strategy.google.local_profile_fields.locate) ? profile._json.locale : null
|
|
162
206
|
].filter((el) => el != null));
|
|
163
207
|
// Store google profile
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
208
|
+
if (pool_base == "basic") {
|
|
209
|
+
// pool base is MySQL
|
|
210
|
+
pool.query("INSERT INTO ??(??,??,??,??) VALUES(?,?,?,?)", [
|
|
211
|
+
passportTable,
|
|
212
|
+
usr,
|
|
213
|
+
psw,
|
|
214
|
+
idField,
|
|
215
|
+
fields,
|
|
216
|
+
profileEmail,
|
|
217
|
+
md5Psw,
|
|
218
|
+
profile.id,
|
|
219
|
+
values
|
|
220
|
+
], (err, result) => {
|
|
221
|
+
data.result = result;
|
|
222
|
+
data.google = profile;
|
|
223
|
+
cb(err, data);
|
|
224
|
+
});
|
|
225
|
+
} else if (pool_base == "sequelize") {
|
|
226
|
+
// pool base is Sequelize
|
|
227
|
+
try {
|
|
228
|
+
let result = await pool.query(`INSERT INTO ${passportTable}(${usr},${psw},${idField},${fields}) VALUES(:profileEmail,:md5Psw,:profileId,:values)`, {
|
|
229
|
+
replacements: {
|
|
230
|
+
usr: usr,
|
|
231
|
+
psw: psw,
|
|
232
|
+
idField: idField,
|
|
233
|
+
profileEmail: profileEmail,
|
|
234
|
+
md5Psw: md5Psw,
|
|
235
|
+
profileId: profile.id,
|
|
236
|
+
values: values
|
|
237
|
+
},
|
|
238
|
+
type: QueryTypes.INSERT
|
|
239
|
+
});
|
|
240
|
+
data.result = result;
|
|
241
|
+
data.google = profile;
|
|
242
|
+
cb(err, data);
|
|
243
|
+
} catch (error) {
|
|
244
|
+
cb(error, null);
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
cb({ error: "Base pool SQL error." }, null);
|
|
248
|
+
}
|
|
179
249
|
} else { // find found
|
|
180
250
|
let users = {};
|
|
181
251
|
users.google = profile;
|
|
182
252
|
users.google.accessToken = accessToken;
|
|
183
253
|
users.google.refreshToken = refreshToken;
|
|
184
|
-
users.user = result[0];
|
|
254
|
+
users.user = result[ 0 ];
|
|
185
255
|
cb(err, users);
|
|
186
256
|
}
|
|
187
257
|
} else if (strategy_name == "facebook") {
|
|
188
|
-
if (!result[0]) { // find not found and create
|
|
258
|
+
if (!result[ 0 ]) { // find not found and create
|
|
189
259
|
// filter fields
|
|
190
260
|
let fields = [].concat.apply([], [
|
|
191
261
|
(passport_config.strategy.facebook.local_profile_fields.name) ? passport_config.strategy.facebook.local_profile_fields.name : null,
|
|
@@ -196,40 +266,97 @@ module.exports = {
|
|
|
196
266
|
// fileter values
|
|
197
267
|
let values = [].concat.apply([], [
|
|
198
268
|
(passport_config.strategy.facebook.local_profile_fields.name) ? profile.displayName : null,
|
|
199
|
-
(passport_config.strategy.facebook.local_profile_fields.email) ? profile.emails[0].value : null,
|
|
200
|
-
(passport_config.strategy.facebook.local_profile_fields.photos) ? profile.photos[0].value : null,
|
|
269
|
+
(passport_config.strategy.facebook.local_profile_fields.email) ? profile.emails[ 0 ].value : null,
|
|
270
|
+
(passport_config.strategy.facebook.local_profile_fields.photos) ? profile.photos[ 0 ].value : null,
|
|
201
271
|
(passport_config.strategy.facebook.local_profile_fields.locate) ? profile._json.location.name : null
|
|
202
272
|
].filter((el) => el != null));
|
|
203
273
|
// Store facebook profile
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
274
|
+
if (pool_base == "basic") {
|
|
275
|
+
// pool base is MySQL
|
|
276
|
+
pool.query("INSERT INTO ??(??,??,??,??) VALUES(?,?,?,?)", [
|
|
277
|
+
passportTable,
|
|
278
|
+
usr,
|
|
279
|
+
psw,
|
|
280
|
+
idField,
|
|
281
|
+
fields,
|
|
282
|
+
profileEmail,
|
|
283
|
+
md5Psw,
|
|
284
|
+
profile.id,
|
|
285
|
+
values
|
|
286
|
+
], (err, result) => {
|
|
287
|
+
data.result = result;
|
|
288
|
+
data.facebook = profile;
|
|
289
|
+
cb(err, data);
|
|
290
|
+
});
|
|
291
|
+
} else if (pool_base == "sequelize") {
|
|
292
|
+
// pool base is Sequelize
|
|
293
|
+
try {
|
|
294
|
+
let result = await pool.query(`INSERT INTO ${passportTable}(${usr},${psw},${idField},${fields}) VALUES(:profileEmail,:md5Psw,:profileId,:values)`, {
|
|
295
|
+
replacements: {
|
|
296
|
+
usr: usr,
|
|
297
|
+
psw: psw,
|
|
298
|
+
idField: idField,
|
|
299
|
+
profileEmail: profileEmail,
|
|
300
|
+
md5Psw: md5Psw,
|
|
301
|
+
profileId: profile.id,
|
|
302
|
+
values: values
|
|
303
|
+
},
|
|
304
|
+
type: QueryTypes.INSERT
|
|
305
|
+
});
|
|
306
|
+
data.result = result;
|
|
307
|
+
data.facebook = profile;
|
|
308
|
+
cb(err, data);
|
|
309
|
+
} catch (error) {
|
|
310
|
+
cb(error, null);
|
|
311
|
+
}
|
|
312
|
+
} else {
|
|
313
|
+
cb({ error: "Base pool SQL error." }, null);
|
|
314
|
+
}
|
|
219
315
|
} else { // find found
|
|
220
316
|
let users = {};
|
|
221
317
|
users.facebook = profile;
|
|
222
318
|
users.facebook.accessToken = accessToken;
|
|
223
319
|
users.facebook.refreshToken = refreshToken;
|
|
224
|
-
users.user = result[0];
|
|
320
|
+
users.user = result[ 0 ];
|
|
225
321
|
cb(err, users);
|
|
226
322
|
}
|
|
227
323
|
}
|
|
228
324
|
}
|
|
229
|
-
});
|
|
325
|
+
});
|
|
230
326
|
} else {
|
|
231
327
|
cb(null, null, true);
|
|
232
328
|
}
|
|
329
|
+
},
|
|
330
|
+
async query_one_where(pool, sql, field, id, cb) {
|
|
331
|
+
try {
|
|
332
|
+
if (pool_base == "basic") {
|
|
333
|
+
// pool base is MySQL
|
|
334
|
+
pool.query(sql + " WHERE ?? = ?", [ field, id ], (err, result) => {
|
|
335
|
+
if (err) {
|
|
336
|
+
return cb(err, null);
|
|
337
|
+
} else {
|
|
338
|
+
return cb(null, JSON.parse(JSON.stringify(result || null)));
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
} else if (pool_base == "sequelize") {
|
|
342
|
+
// pool base is Sequelize
|
|
343
|
+
try {
|
|
344
|
+
let result = await pool.query(`${sql} WHERE ${field} = :id`, {
|
|
345
|
+
replacements: {
|
|
346
|
+
id: id
|
|
347
|
+
},
|
|
348
|
+
type: QueryTypes.SELECT
|
|
349
|
+
});
|
|
350
|
+
return cb(null, JSON.parse(JSON.stringify(result || null)));
|
|
351
|
+
} catch (error) {
|
|
352
|
+
return cb(error, null);
|
|
353
|
+
}
|
|
354
|
+
} else {
|
|
355
|
+
return done({ error: "Base pool SQL error." }, null);
|
|
356
|
+
}
|
|
357
|
+
} catch (error) {
|
|
358
|
+
cb(error, null);
|
|
359
|
+
}
|
|
233
360
|
}
|
|
234
361
|
|
|
235
362
|
}
|
|
@@ -12,7 +12,7 @@ module.exports = {
|
|
|
12
12
|
app_port: 9000,
|
|
13
13
|
app_host: "localhost",
|
|
14
14
|
client_host: "http://0.0.0.0:9000",
|
|
15
|
-
app_secret: ["2cc118cd91b52ff99e3c005ddced76fb"]
|
|
15
|
+
app_secret: [ "2cc118cd91b52ff99e3c005ddced76fb" ]
|
|
16
16
|
},
|
|
17
17
|
|
|
18
18
|
// Add-on it's work when enabled. You can enable add-on by run CMD `$ beech add-on init`.
|
|
@@ -31,7 +31,7 @@ module.exports = {
|
|
|
31
31
|
* @exports is_connect : The sql connection flag (boolean)
|
|
32
32
|
*
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
database_config: [
|
|
35
35
|
{
|
|
36
36
|
name: "default_db",
|
|
37
37
|
host: "127.0.0.1",
|
|
@@ -40,7 +40,7 @@ module.exports = {
|
|
|
40
40
|
database: "example1_db",
|
|
41
41
|
port: "3306",
|
|
42
42
|
charset: "utf8",
|
|
43
|
-
is_connect: false
|
|
43
|
+
is_connect: false,
|
|
44
44
|
},
|
|
45
45
|
{
|
|
46
46
|
name: "second_db",
|
|
@@ -50,7 +50,7 @@ module.exports = {
|
|
|
50
50
|
database: "example2_db",
|
|
51
51
|
port: "3306",
|
|
52
52
|
charset: "utf8",
|
|
53
|
-
is_connect: false
|
|
53
|
+
is_connect: false,
|
|
54
54
|
}
|
|
55
55
|
]
|
|
56
56
|
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
/**
|
|
3
|
+
* Service configuration
|
|
4
|
+
*
|
|
5
|
+
* @exports app_port : Listening for start service
|
|
6
|
+
* @exports app_host : Server http localhost
|
|
7
|
+
* @exports client_host : Production http client host
|
|
8
|
+
* @exports app_secret : App secret key for request with endpoints
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
main_config: {
|
|
12
|
+
app_port: 9000,
|
|
13
|
+
app_host: "localhost",
|
|
14
|
+
client_host: "http://0.0.0.0:9000",
|
|
15
|
+
app_secret: [ "2cc118cd91b52ff99e3c005ddced76fb" ]
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
// Add-on it's work when enabled. You can enable add-on by run CMD `$ beech add-on init`.
|
|
19
|
+
addOn: true,
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The Database configuration (mutiple connection) currenty support for MySQL, SQLite, MariaDB, PostgreSQL and Microsoft SQL Server
|
|
23
|
+
*
|
|
24
|
+
* Basic parameter following:
|
|
25
|
+
* @exports dialect The engine SQL connection one of 'mysql' | 'sqlite' | 'mariadb' | 'postgres' | 'mssql'
|
|
26
|
+
* The Dialect need of the following:
|
|
27
|
+
* - $ npm install --save pg pg-hstore # Postgres
|
|
28
|
+
* - $ npm install --save mysql2
|
|
29
|
+
* - $ npm install --save mariadb
|
|
30
|
+
* - $ npm install --save sqlite3 (Need NodeJS v12.x)
|
|
31
|
+
* - $ npm install --save tedious # Microsoft SQL Server (Need NodeJS v14.x)
|
|
32
|
+
* @exports name The Connection name
|
|
33
|
+
* @exports host The Host address
|
|
34
|
+
* @exports username The Host username connection
|
|
35
|
+
* @exports password Host The password connection
|
|
36
|
+
* @exports database The database name
|
|
37
|
+
* @exports port The sql port (default port by dialect mysql:3306, marialdb:3306, postgres:5432 and mssql:1433)
|
|
38
|
+
* @exports define The character encoding and optional. See more: https://sequelize.org/docs/v6/other-topics/dialect-specific-things/
|
|
39
|
+
* @exports is_connect The sql connection flag (boolean)
|
|
40
|
+
*
|
|
41
|
+
* learn more of parameter:
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
database_config: [
|
|
45
|
+
{
|
|
46
|
+
dialect: "mysql",
|
|
47
|
+
name: "default_db",
|
|
48
|
+
host: "127.0.0.1",
|
|
49
|
+
username: "root",
|
|
50
|
+
password: "",
|
|
51
|
+
database: "example1_db",
|
|
52
|
+
port: "3306",
|
|
53
|
+
define: {
|
|
54
|
+
charset: "utf8",
|
|
55
|
+
dialectOptions: {
|
|
56
|
+
collate: "utf8_general_ci"
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
is_connect: false,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
dialect: "sqlite",
|
|
63
|
+
name: "second_db",
|
|
64
|
+
storage: "usr/sqliteDB/mydatabase.sqlite", // or ":memory:"
|
|
65
|
+
is_connect: false,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
dialect: "mssql",
|
|
69
|
+
name: "thirdth_db",
|
|
70
|
+
host: "127.0.0.1",
|
|
71
|
+
username: "root",
|
|
72
|
+
password: "",
|
|
73
|
+
database: "example3_db",
|
|
74
|
+
port: "1433",
|
|
75
|
+
define: {
|
|
76
|
+
charset: "utf8",
|
|
77
|
+
dialectOptions: {
|
|
78
|
+
collate: "utf8_general_ci"
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
dialectOptions: { // ssl
|
|
82
|
+
options: {
|
|
83
|
+
encrypt: false,
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
is_connect: false,
|
|
87
|
+
},
|
|
88
|
+
]
|
|
89
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module.exports.init = () => {
|
|
2
|
+
// pool base config
|
|
3
|
+
global.pool_base = "basic"; // one of "basic" | "sequelize"
|
|
4
|
+
|
|
5
|
+
// example declare global varables library, config and anything
|
|
6
|
+
global.app_secret = require("./app.config.js").main_config.app_secret;
|
|
7
|
+
|
|
8
|
+
// anything config for up to you ...
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
module.exports.init = () => {
|
|
2
|
+
// pool base config
|
|
3
|
+
global.pool_base = "sequelize"; // one of "basic" | "sequelize"
|
|
4
|
+
|
|
5
|
+
// example declare global varables library, config and anything
|
|
6
|
+
global.app_secret = require("./app.config.js").main_config.app_secret;
|
|
7
|
+
|
|
8
|
+
// anything config for up to you ...
|
|
9
|
+
}
|
|
@@ -12,18 +12,18 @@ module.exports = {
|
|
|
12
12
|
token_expired: 86400,
|
|
13
13
|
|
|
14
14
|
model: {
|
|
15
|
-
// Main
|
|
15
|
+
// Main sql connection name. You must make sure connection name like inside `app.config.js` file and choose one connection name.
|
|
16
16
|
name: "default_db",
|
|
17
|
-
// The user table name for store your authenticate, default table `users`
|
|
17
|
+
// The user table name for store your authenticate, (default table `users`)
|
|
18
18
|
table: "",
|
|
19
|
-
// The fields for authenticate, default fields: `username` and `password`
|
|
19
|
+
// The fields for authenticate, default fields: (`username` and `password`)
|
|
20
20
|
username_field: "",
|
|
21
21
|
password_field: "",
|
|
22
22
|
// Show JWT fields, default show fields: ["id", "name", "email"]
|
|
23
23
|
fields: []
|
|
24
24
|
},
|
|
25
25
|
|
|
26
|
-
// Allow using with app_secret
|
|
26
|
+
// Allow using with app_secret request (Every request must be using the app_secret parameter)
|
|
27
27
|
app_secret_allow: false,
|
|
28
28
|
|
|
29
29
|
// Official strategy
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
const mysql = require("mysql");
|
|
2
|
+
global.sql = {};
|
|
2
3
|
|
|
3
|
-
exports.
|
|
4
|
+
exports.connect = () => {
|
|
4
5
|
return new Promise((resolve, reject) => {
|
|
5
6
|
try {
|
|
6
|
-
mysqlInProcess(_config_.
|
|
7
|
+
mysqlInProcess(_config_.database_config, true, (err, result) => {
|
|
7
8
|
if (!err) {
|
|
8
9
|
resolve(result);
|
|
9
10
|
} else {
|
|
@@ -16,13 +17,13 @@ exports.mySqlConnection = () => {
|
|
|
16
17
|
})
|
|
17
18
|
}
|
|
18
19
|
|
|
19
|
-
mysqlInProcess = (
|
|
20
|
+
mysqlInProcess = (database_config, headDbShow, cb) => {
|
|
20
21
|
try {
|
|
21
|
-
let val =
|
|
22
|
+
let val = database_config.shift();
|
|
22
23
|
// checking turn on db connect
|
|
23
24
|
if (val.is_connect) {
|
|
24
25
|
// db connection config
|
|
25
|
-
let connection =
|
|
26
|
+
let connection = mysql.createConnection({
|
|
26
27
|
host: val.host,
|
|
27
28
|
user: val.username,
|
|
28
29
|
password: val.password,
|
|
@@ -39,11 +40,11 @@ mysqlInProcess = (mysql_config, headDbShow, cb) => {
|
|
|
39
40
|
headDbShow = false;
|
|
40
41
|
}
|
|
41
42
|
// declare to global mysql variable
|
|
42
|
-
|
|
43
|
+
sql[ val.name ] = connection;
|
|
43
44
|
console.log(' - [36m ' + val.name + ' [0m->[93m ' + connection.config.database + ':' + connection.config.port + '[0m');
|
|
44
45
|
// checking recursive database connection
|
|
45
|
-
if (
|
|
46
|
-
mysqlInProcess(
|
|
46
|
+
if (database_config.length > 0) {
|
|
47
|
+
mysqlInProcess(database_config, headDbShow, e => {
|
|
47
48
|
cb(e, true);
|
|
48
49
|
});
|
|
49
50
|
} else {
|
|
@@ -56,8 +57,8 @@ mysqlInProcess = (mysql_config, headDbShow, cb) => {
|
|
|
56
57
|
}
|
|
57
58
|
});
|
|
58
59
|
} else {
|
|
59
|
-
if (
|
|
60
|
-
mysqlInProcess(
|
|
60
|
+
if (database_config.length > 0) {
|
|
61
|
+
mysqlInProcess(database_config, headDbShow, e => {
|
|
61
62
|
cb(e, true);
|
|
62
63
|
});
|
|
63
64
|
} else {
|