beech-api 3.5.12 → 3.7.0
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 +400 -185
- package/index.js +2 -3
- package/package.json +9 -6
- package/packages/cli/beech +2 -2
- package/packages/cli/bin/{beech.js → beech-app.js} +86 -28
- package/packages/cli/bin/beech-service.js +40 -212
- package/packages/cli/core/auth/Credentials.js +76 -35
- package/packages/cli/core/auth/Passport.js +318 -188
- package/packages/cli/core/auth/_Request.js +12 -0
- package/packages/cli/core/configure/_gitignore +9 -1
- package/packages/cli/core/configure/app.config-basic.js +10 -10
- package/packages/cli/core/configure/app.config-sequelize.js +12 -12
- package/packages/cli/core/configure/beech.config.js +8 -0
- package/packages/cli/core/configure/global.config-basic.js +4 -4
- package/packages/cli/core/configure/global.config-sequelize.js +4 -4
- package/packages/cli/core/configure/passport.config.js +5 -15
- package/packages/cli/core/databases/mysql.js +53 -29
- package/packages/cli/core/databases/sequelize.js +135 -107
- package/packages/cli/core/databases/test.js +168 -0
- package/packages/cli/core/file-walk/file-walk.js +31 -14
- package/packages/cli/core/generator/_add-on +15 -2
- package/packages/cli/core/generator/_endpoints +15 -82
- package/packages/cli/core/generator/_endpoints_basic +39 -0
- package/packages/cli/core/generator/_help +12 -11
- package/packages/cli/core/generator/_help_create +11 -0
- package/packages/cli/core/generator/{_service → _help_service} +4 -1
- package/packages/cli/core/generator/_models +20 -15
- package/packages/cli/core/generator/{_basic-models → _models_basic} +0 -2
- package/packages/cli/core/generator/_package +4 -3
- package/packages/cli/core/generator/index.js +302 -95
- package/packages/cli/core/helpers/math.js +55 -0
- package/packages/cli/core/helpers/poolEntity.js +31 -0
- package/packages/cli/core/index.js +96 -28
- package/packages/cli/core/origin/index.js +2 -0
- package/packages/cli/core/origin/whitelist/cors.js +91 -0
- package/packages/cli/core/services/http.express.js +338 -203
- package/packages/cli/core/test/utils.js +5 -5
- package/packages/lib/index.js +4 -0
- package/packages/lib/src/endpoint.js +98 -0
- package/packages/lib/src/salt.js +3 -0
- package/packages/lib/src/schema.js +62 -0
- package/packages/lib/src/user.js +196 -0
- package/packages/package.json +4 -7
- package/packages/cli/core/generator/_create +0 -7
- package/packages/lib/beech.js +0 -136
- package/packages/lib/salt.js +0 -3
- package/packages/src-/Add-on.js +0 -9
- package/packages/src-/endpoints/hello-endpoints.js +0 -118
- package/packages/src-/endpoints/test2-endpoints.js +0 -75
- package/packages/src-/endpoints/testSequalize-endpoints.js +0 -23
- package/packages/src-/helpers/Test2.js +0 -11
- package/packages/src-/helpers/my/Test.js +0 -11
- package/packages/src-/helpers/my/Test2.js +0 -11
- package/packages/src-/helpers/my/Test3.js +0 -11
- package/packages/src-/helpers/my/Test4.js +0 -11
- package/packages/src-/models/Jubu.js +0 -29
- package/packages/src-/models/Jubu2.js +0 -20
- package/packages/src-/models/Map_master.js +0 -22
- package/packages/src-/models/Test.js +0 -29
- package/packages/src-/models/Test2.js +0 -29
- package/packages/src-/models/User2Sequelize.js +0 -23
- package/packages/src-/models/Users-sqlite.js +0 -21
- package/packages/src-/models/Users.js +0 -53
- package/packages/src-/models/Uuuuuxxx.js +0 -23
- package/packages/src-/models/xxx/Uuuuuxxx.js +0 -23
- /package/packages/cli/core/generator/{_basic-helpers → _helpers_basic} +0 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const Base = (Pj = []) => {
|
|
2
|
+
Pj.map((Project) => {
|
|
3
|
+
let endpointName = Object.values(Project)[4];
|
|
4
|
+
endpoint.get(`/${endpointName}/:limit?/:offset?`, Credentials, async (req, res) => {
|
|
5
|
+
const results = await Project.findAll({ offset: (parseInt(req.params.offset) || 0), limit: (parseInt(req.params.limit) || 100) });
|
|
6
|
+
await res.json({
|
|
7
|
+
code: 200,
|
|
8
|
+
status: "SUCCESS",
|
|
9
|
+
results,
|
|
10
|
+
length: results.length,
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
endpoint.post(`/${endpointName}`, Credentials, (req, res) => {
|
|
14
|
+
Project.create(req.body)
|
|
15
|
+
.then((created) => {
|
|
16
|
+
// @return
|
|
17
|
+
res.status(201).json({
|
|
18
|
+
code: 201,
|
|
19
|
+
status: "CREATE_SUCCESS",
|
|
20
|
+
createdId: created.id,
|
|
21
|
+
});
|
|
22
|
+
})
|
|
23
|
+
.catch((err) => {
|
|
24
|
+
// @return
|
|
25
|
+
res.status(501).json({
|
|
26
|
+
code: 501,
|
|
27
|
+
status: "CREATE_FAILED",
|
|
28
|
+
error: err,
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
endpoint.patch(`/${endpointName}/:id`, Credentials, (req, res) => {
|
|
33
|
+
Project.update(req.body, {
|
|
34
|
+
where: {
|
|
35
|
+
id: req.params.id,
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
.then((updated) => {
|
|
39
|
+
// @return
|
|
40
|
+
res.status(200).json({
|
|
41
|
+
code: 200,
|
|
42
|
+
status: "UPDATE_SUCCESS",
|
|
43
|
+
result: {
|
|
44
|
+
updateId: req.params.id,
|
|
45
|
+
affectedRows: updated[0],
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
})
|
|
49
|
+
.catch((err) => {
|
|
50
|
+
// @return
|
|
51
|
+
res.status(501).json({
|
|
52
|
+
code: 501,
|
|
53
|
+
status: "UPDATE_FAILED",
|
|
54
|
+
error: err,
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
endpoint.delete(`/${endpointName}/:id`, Credentials, (req, res) => {
|
|
59
|
+
Project.destroy({
|
|
60
|
+
where: {
|
|
61
|
+
id: req.params.id,
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
.then((deleted) => {
|
|
65
|
+
if(deleted) {
|
|
66
|
+
// @return
|
|
67
|
+
res.status(200).json({
|
|
68
|
+
code: 200,
|
|
69
|
+
status: "DELETE_SUCCESS",
|
|
70
|
+
result: {
|
|
71
|
+
deleteId: req.params.id,
|
|
72
|
+
affectedRows: deleted,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
} else {
|
|
76
|
+
res.status(406).json({
|
|
77
|
+
code: 406,
|
|
78
|
+
status: "NOT_ACCEPTABLE",
|
|
79
|
+
result: {
|
|
80
|
+
deleteId: req.params.id,
|
|
81
|
+
affectedRows: deleted,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
.catch((err) => {
|
|
87
|
+
// @return
|
|
88
|
+
res.status(501).json({
|
|
89
|
+
code: 501,
|
|
90
|
+
status: "DELETE_FAILED",
|
|
91
|
+
error: err,
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
module.exports = { Base };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
function Schema(Sequelize) {
|
|
2
|
+
return {
|
|
3
|
+
define: (table, schemaProps = {}) => {
|
|
4
|
+
let Project = Sequelize.define(table, schemaProps);
|
|
5
|
+
return Object.assign(Project, {
|
|
6
|
+
query: (
|
|
7
|
+
rawSql,
|
|
8
|
+
props = {
|
|
9
|
+
model: Project,
|
|
10
|
+
mapToModel: false,
|
|
11
|
+
}
|
|
12
|
+
) => {
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
if (props.mapToModel) {
|
|
15
|
+
let data = Sequelize.query(rawSql, props);
|
|
16
|
+
let results = [];
|
|
17
|
+
mapDataToModel(data, props.model, results, (err, r) => {
|
|
18
|
+
if (err) {
|
|
19
|
+
resolve([]);
|
|
20
|
+
} else {
|
|
21
|
+
resolve(r);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
} else {
|
|
25
|
+
resolve(Sequelize.query(rawSql, props));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
Sequelize,
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
mapDataToModel = async (data, modelsArr = [], results = [], cb) => {
|
|
36
|
+
let dataArr = await data;
|
|
37
|
+
var models = modelsArr.length ? modelsArr.slice() : modelsArr;
|
|
38
|
+
let row = dataArr.shift();
|
|
39
|
+
let buildModelData = [];
|
|
40
|
+
recursiveBuildModels(models, row, buildModelData, (builed, e) => {
|
|
41
|
+
if (builed) {
|
|
42
|
+
results.push({ ...e });
|
|
43
|
+
if (dataArr.length > 0) {
|
|
44
|
+
mapDataToModel(dataArr, modelsArr, results, cb);
|
|
45
|
+
} else {
|
|
46
|
+
cb(null, results);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
recursiveBuildModels = (models, row, data = [], cb) => {
|
|
53
|
+
let model = models.length ? models.shift() : models;
|
|
54
|
+
data.push(model.build(row).dataValues);
|
|
55
|
+
if (models.length > 0) {
|
|
56
|
+
recursiveBuildModels(models, row, data, cb);
|
|
57
|
+
} else {
|
|
58
|
+
cb(true, { ...Object.assign(...data) });
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
module.exports = { Schema };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
const appRoot = require("app-root-path");
|
|
2
|
+
const md5 = require("md5");
|
|
3
|
+
const secret = require("./salt").salt;
|
|
4
|
+
const { findPassportPk } = require("../../cli/core/helpers/poolEntity");
|
|
5
|
+
|
|
6
|
+
async function FindOne(fields, fieldCondArr, cb) {
|
|
7
|
+
try {
|
|
8
|
+
const passport_config = require(appRoot + "/passport.config.js");
|
|
9
|
+
let stm = '';
|
|
10
|
+
let cond = '1';
|
|
11
|
+
let passportTable = await [passport_config.model.table || "users"];
|
|
12
|
+
const pool = await eval("sql." + passport_config.model.name);
|
|
13
|
+
let expectFields = await (fields[0]) ? fields : (passport_config.model.fields.length) ? passport_config.model.fields : [];
|
|
14
|
+
await findPassportPk(pool_base, pool, passportTable, expectFields, async (err, passportFields) => {
|
|
15
|
+
if(err) {
|
|
16
|
+
cb(err, null);
|
|
17
|
+
} else {
|
|
18
|
+
// Generate condition
|
|
19
|
+
await Object.keys(fieldCondArr).forEach(key => {
|
|
20
|
+
cond += ' AND ' + key + '=' + fieldCondArr[key]
|
|
21
|
+
});
|
|
22
|
+
// check base pool
|
|
23
|
+
if (pool_base == "basic") {
|
|
24
|
+
// pool base is MySQL
|
|
25
|
+
stm += 'SELECT ?? FROM ?? WHERE ' + cond;
|
|
26
|
+
await pool.query(stm, [passportFields, passportTable], (err, row) => {
|
|
27
|
+
if(err) {
|
|
28
|
+
cb(err, null);
|
|
29
|
+
} else {
|
|
30
|
+
cb(null, row);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
} else if (pool_base == "sequelize") {
|
|
34
|
+
// pool base is Sequelize
|
|
35
|
+
try {
|
|
36
|
+
stm += `SELECT ${passportFields} FROM ${passportTable} WHERE ` + cond;
|
|
37
|
+
let result = await pool.query(stm, {
|
|
38
|
+
type: QueryTypes.SELECT
|
|
39
|
+
});
|
|
40
|
+
return cb(null, result);
|
|
41
|
+
} catch (error) {
|
|
42
|
+
return cb(error.errors[0], null);
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
return cb({ error: "The Base pool error. UNKNOWN pool_base = '"+ pool_base +"'" }, null);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
} catch (error) {
|
|
50
|
+
cb(error, null);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function Store(fields, cb) {
|
|
55
|
+
try {
|
|
56
|
+
const passport_config = require(appRoot + "/passport.config.js");
|
|
57
|
+
let stm = '';
|
|
58
|
+
let keys = [];
|
|
59
|
+
let escaped = [];
|
|
60
|
+
let values = await [passport_config.model.table || "users"];
|
|
61
|
+
let usernameField = await String(passport_config.model.username_field || "username");
|
|
62
|
+
let passwordField = await String(passport_config.model.password_field || "password");
|
|
63
|
+
const pool = await eval("sql." + passport_config.model.name);
|
|
64
|
+
// declare for check have a username and password ?
|
|
65
|
+
let haveUsernameAndPassword = 0;
|
|
66
|
+
// sql generate
|
|
67
|
+
await Object.keys(fields).forEach(key => {
|
|
68
|
+
// check have a username and password fields
|
|
69
|
+
if(key == usernameField) {
|
|
70
|
+
haveUsernameAndPassword += 1;
|
|
71
|
+
}
|
|
72
|
+
if(key == passwordField) {
|
|
73
|
+
haveUsernameAndPassword += 1;
|
|
74
|
+
}
|
|
75
|
+
keys.push(key);
|
|
76
|
+
values.push(fields[key]);
|
|
77
|
+
escaped.push('?');
|
|
78
|
+
});
|
|
79
|
+
if(haveUsernameAndPassword > 1) {
|
|
80
|
+
// asign password hash
|
|
81
|
+
fields[passwordField] = md5(fields[passwordField] + secret);
|
|
82
|
+
// check base pool
|
|
83
|
+
if (pool_base == "basic") {
|
|
84
|
+
// pool base is MySQL
|
|
85
|
+
stm += 'INSERT INTO ?? (' + keys.join() + ') VALUES (' + escaped.join() + ')';
|
|
86
|
+
await pool.query(stm, values, (err, result) => {
|
|
87
|
+
if(err) {
|
|
88
|
+
let error = JSON.parse(JSON.stringify(err));
|
|
89
|
+
delete error.sql;
|
|
90
|
+
cb(error, null);
|
|
91
|
+
} else {
|
|
92
|
+
cb(null, {
|
|
93
|
+
insertId: result.insertId,
|
|
94
|
+
affectedRows: result.affectedRows,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
} else if (pool_base == "sequelize") {
|
|
99
|
+
// pool base is Sequelize
|
|
100
|
+
try {
|
|
101
|
+
stm += `INSERT INTO ${values.shift()} (` + keys.join() + ') VALUES (' + escaped.join() + ')';
|
|
102
|
+
let result = await pool.query(stm, {
|
|
103
|
+
replacements: values,
|
|
104
|
+
type: QueryTypes.INSERT
|
|
105
|
+
});
|
|
106
|
+
return cb(null, {
|
|
107
|
+
insertId: result[0],
|
|
108
|
+
affectedRows: result[1]
|
|
109
|
+
});
|
|
110
|
+
} catch (error) {
|
|
111
|
+
return cb(error.errors[0], null);
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
return cb({ error: "The Base pool error. UNKNOWN pool_base = '"+ pool_base +"'" }, null);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
cb({ code: "ER_BAD_FIELD_ERROR", message: `You lost some fields ${usernameField} or ${passwordField} ?` }, null);
|
|
118
|
+
}
|
|
119
|
+
} catch (error) {
|
|
120
|
+
cb(`${error}`, null);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function Update(someFields, id, cb) {
|
|
125
|
+
try {
|
|
126
|
+
const passport_config = require(appRoot + "/passport.config.js");
|
|
127
|
+
let stm = '';
|
|
128
|
+
let keys = [];
|
|
129
|
+
let values = await [passport_config.model.table || "users"];
|
|
130
|
+
let passwordField = await String(passport_config.model.password_field || "password");
|
|
131
|
+
const pool = await eval("sql." + passport_config.model.name);
|
|
132
|
+
// check password body for asign hash
|
|
133
|
+
if (someFields[passwordField]) {
|
|
134
|
+
someFields[passwordField] = md5(someFields[passwordField] + secret);
|
|
135
|
+
}
|
|
136
|
+
// sql generate
|
|
137
|
+
Object.keys(someFields).forEach(key => {
|
|
138
|
+
keys.push(`${key}=?`);
|
|
139
|
+
values.push(someFields[key]);
|
|
140
|
+
});
|
|
141
|
+
values.push(id);
|
|
142
|
+
// check base pool
|
|
143
|
+
if (pool_base == "basic") {
|
|
144
|
+
// pool base is MySQL
|
|
145
|
+
pool.query("SHOW KEYS FROM " + values[0] + " WHERE Key_name = 'PRIMARY'", (err, pk) => {
|
|
146
|
+
if(err) {
|
|
147
|
+
return done(err, null);
|
|
148
|
+
} else {
|
|
149
|
+
let fieldPk = pk[0].Column_name;
|
|
150
|
+
stm += 'UPDATE ?? SET ' + keys.join() + ' WHERE ' + fieldPk + ' = ?';
|
|
151
|
+
pool.query(stm, values, (err, result) => {
|
|
152
|
+
if(err) {
|
|
153
|
+
let error = JSON.parse(JSON.stringify(err));
|
|
154
|
+
delete error.sql;
|
|
155
|
+
cb(error, null);
|
|
156
|
+
} else {
|
|
157
|
+
cb(null, {
|
|
158
|
+
updateId: parseInt(id),
|
|
159
|
+
affectedRows: result.changedRows
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
} else if (pool_base == "sequelize") {
|
|
166
|
+
// pool base is Sequelize
|
|
167
|
+
try {
|
|
168
|
+
pool.query("SHOW KEYS FROM " + values[0] + " WHERE Key_name = 'PRIMARY'", { type: QueryTypes.SELECT }).then((pk) => {
|
|
169
|
+
let fieldPk = pk[0].Column_name;
|
|
170
|
+
stm += `UPDATE ${values.shift()} SET ` + keys.join() + ' WHERE ' + fieldPk + ' = ?';
|
|
171
|
+
pool.query(stm, {
|
|
172
|
+
replacements: values,
|
|
173
|
+
type: QueryTypes.UPDATE
|
|
174
|
+
}).then((result) => {
|
|
175
|
+
return cb(null, {
|
|
176
|
+
updateId: parseInt(id),
|
|
177
|
+
affectedRows: result[1]
|
|
178
|
+
});
|
|
179
|
+
}).catch((err) => {
|
|
180
|
+
return cb(err.errors[0], null);
|
|
181
|
+
});
|
|
182
|
+
}).catch((err) => {
|
|
183
|
+
return cb(err, null);
|
|
184
|
+
});
|
|
185
|
+
} catch (error) {
|
|
186
|
+
return cb(error.errors[0], null);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
return cb({ error: "The Base pool error. UNKNOWN pool_base = '"+ pool_base +"'" }, null);
|
|
190
|
+
}
|
|
191
|
+
} catch (error) {
|
|
192
|
+
cb(error, null);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
module.exports = { Store, Update, FindOne };
|
package/packages/package.json
CHANGED
|
@@ -2,19 +2,16 @@
|
|
|
2
2
|
"name": "application",
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"description": "The project is powered by Beech API framework",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"main": "index.js",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"start": "
|
|
8
|
+
"start": "beech-service serve",
|
|
8
9
|
"test": "node ./node_modules/jest/bin/jest __tests__ -o --watch --config"
|
|
9
10
|
},
|
|
10
11
|
"dependencies": {
|
|
11
|
-
"beech-api": "^3.
|
|
12
|
-
"nodemon": "^3.0.1",
|
|
13
|
-
"sqlite3": "^5.0.11",
|
|
14
|
-
"tedious": "^15.0.1"
|
|
12
|
+
"beech-api": "^3.7.10"
|
|
15
13
|
},
|
|
16
14
|
"devDependencies": {
|
|
17
|
-
"jest": "^
|
|
18
|
-
"sequelize-cli": "^5.5.1"
|
|
15
|
+
"jest": "^28.1.3"
|
|
19
16
|
}
|
|
20
17
|
}
|
package/packages/lib/beech.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
const appRoot = require("app-root-path");
|
|
2
|
-
const passport_config = require(appRoot + "/passport.config.js");
|
|
3
|
-
const md5 = require("md5");
|
|
4
|
-
const secret = require("./salt").salt;
|
|
5
|
-
|
|
6
|
-
module.exports = {
|
|
7
|
-
async findOne(fields, fieldCondArr, cb) {
|
|
8
|
-
try {
|
|
9
|
-
let stm = '';
|
|
10
|
-
let cond = '1';
|
|
11
|
-
let table = await [passport_config.model.table || "users"];
|
|
12
|
-
let passportFields = await (fields[0]) ? fields : (passport_config.model.fields.length) ? passport_config.model.fields : ["id", "name", "email"];
|
|
13
|
-
const pool = await eval("sql." + passport_config.model.name);
|
|
14
|
-
// Generate condition
|
|
15
|
-
await Object.keys(fieldCondArr).forEach(key => {
|
|
16
|
-
cond += ' AND ' + key + '=' + fieldCondArr[key]
|
|
17
|
-
});
|
|
18
|
-
// check base pool
|
|
19
|
-
if (pool_base == "basic") {
|
|
20
|
-
// pool base is MySQL
|
|
21
|
-
stm += 'SELECT ?? FROM ?? WHERE ' + cond;
|
|
22
|
-
await pool.query(stm, [passportFields, table], (err, row) => {
|
|
23
|
-
cb(err, row);
|
|
24
|
-
});
|
|
25
|
-
} else if (pool_base == "sequelize") {
|
|
26
|
-
// pool base is Sequelize
|
|
27
|
-
try {
|
|
28
|
-
stm += `SELECT ${passportFields} FROM ${table} WHERE ` + cond;
|
|
29
|
-
let result = await pool.query(stm, {
|
|
30
|
-
type: QueryTypes.SELECT
|
|
31
|
-
});
|
|
32
|
-
return cb(null, result);
|
|
33
|
-
} catch (error) {
|
|
34
|
-
return cb(error, null);
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
return cb({ error: "Base pool SQL error." }, null);
|
|
38
|
-
}
|
|
39
|
-
} catch (error) {
|
|
40
|
-
cb(error, null);
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
async store(fields, cb) {
|
|
44
|
-
try {
|
|
45
|
-
let stm = '';
|
|
46
|
-
let keys = [];
|
|
47
|
-
let escaped = [];
|
|
48
|
-
let values = await [passport_config.model.table || "users"];
|
|
49
|
-
let passwordField = await String(passport_config.model.password_field || "password");
|
|
50
|
-
const pool = await eval("sql." + passport_config.model.name);
|
|
51
|
-
// asign password hash
|
|
52
|
-
fields[passwordField] = md5(fields[passwordField] + secret);
|
|
53
|
-
// sql generate
|
|
54
|
-
await Object.keys(fields).forEach(key => {
|
|
55
|
-
keys.push(key);
|
|
56
|
-
values.push(fields[key]);
|
|
57
|
-
escaped.push('?');
|
|
58
|
-
});
|
|
59
|
-
// check base pool
|
|
60
|
-
if (pool_base == "basic") {
|
|
61
|
-
// pool base is MySQL
|
|
62
|
-
stm += 'INSERT INTO ?? (' + keys.join() + ') VALUES (' + escaped.join() + ')';
|
|
63
|
-
await pool.query(stm, values, (err, result) => {
|
|
64
|
-
cb(err, result);
|
|
65
|
-
});
|
|
66
|
-
} else if (pool_base == "sequelize") {
|
|
67
|
-
// pool base is Sequelize
|
|
68
|
-
try {
|
|
69
|
-
stm += `INSERT INTO ${values.shift()} (` + keys.join() + ') VALUES (' + escaped.join() + ')';
|
|
70
|
-
let result = await pool.query(stm, {
|
|
71
|
-
replacements: values,
|
|
72
|
-
type: QueryTypes.INSERT
|
|
73
|
-
});
|
|
74
|
-
return cb(null, {
|
|
75
|
-
insertId: result[0],
|
|
76
|
-
affectedRows: result[1]
|
|
77
|
-
});
|
|
78
|
-
} catch (error) {
|
|
79
|
-
return cb(error, null);
|
|
80
|
-
}
|
|
81
|
-
} else {
|
|
82
|
-
return cb({ error: "Base pool SQL error." }, null);
|
|
83
|
-
}
|
|
84
|
-
} catch (error) {
|
|
85
|
-
cb(error, null);
|
|
86
|
-
}
|
|
87
|
-
},
|
|
88
|
-
async update(someFields, id, cb) {
|
|
89
|
-
try {
|
|
90
|
-
let stm = '';
|
|
91
|
-
let keys = [];
|
|
92
|
-
let values = await [passport_config.model.table || "users"];
|
|
93
|
-
let passwordField = await String(passport_config.model.password_field || "password");
|
|
94
|
-
const pool = await eval("sql." + passport_config.model.name);
|
|
95
|
-
// check password body for asign hash
|
|
96
|
-
if (someFields[passwordField]) {
|
|
97
|
-
someFields[passwordField] = md5(someFields[passwordField] + secret);
|
|
98
|
-
}
|
|
99
|
-
// sql generate
|
|
100
|
-
Object.keys(someFields).forEach(key => {
|
|
101
|
-
keys.push(`${key}=?`);
|
|
102
|
-
values.push(someFields[key]);
|
|
103
|
-
});
|
|
104
|
-
values.push(id);
|
|
105
|
-
// check base pool
|
|
106
|
-
if (pool_base == "basic") {
|
|
107
|
-
// pool base is MySQL
|
|
108
|
-
stm += 'UPDATE ?? SET ' + keys.join() + ' WHERE id = ?';
|
|
109
|
-
await pool.query(stm, values, (err, result) => {
|
|
110
|
-
cb(err, result);
|
|
111
|
-
});
|
|
112
|
-
} else if (pool_base == "sequelize") {
|
|
113
|
-
// pool base is Sequelize
|
|
114
|
-
try {
|
|
115
|
-
stm += `UPDATE ${values.shift()} SET ` + keys.join() + ' WHERE id = ?';
|
|
116
|
-
console.log(stm, values);
|
|
117
|
-
let result = await pool.query(stm, {
|
|
118
|
-
replacements: values,
|
|
119
|
-
type: QueryTypes.UPDATE
|
|
120
|
-
});
|
|
121
|
-
return cb(null, {
|
|
122
|
-
updateId: parseInt(id),
|
|
123
|
-
affectedRows: result[1]
|
|
124
|
-
});
|
|
125
|
-
} catch (error) {
|
|
126
|
-
return cb(error, null);
|
|
127
|
-
}
|
|
128
|
-
} else {
|
|
129
|
-
return cb({ error: "Base pool SQL error." }, null);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
} catch (error) {
|
|
133
|
-
cb(error, null);
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
}
|
package/packages/lib/salt.js
DELETED
package/packages/src-/Add-on.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
const Beech = require("../../lib/beech");
|
|
2
|
-
const Users = require("@/models/Users");
|
|
3
|
-
const Users2 = require("@/models/Users-sqlite");
|
|
4
|
-
const User2Sequelize = require("@/models/User2Sequelize");
|
|
5
|
-
const Map_master = require("@/models/Map_master");
|
|
6
|
-
|
|
7
|
-
exports.init = () => {
|
|
8
|
-
// Initiate with basic request currently support GET, POST, PUT, PATCH and DELETE
|
|
9
|
-
|
|
10
|
-
/@GET/
|
|
11
|
-
endpoint.get('/hello', Credentials, async (req, res) => {
|
|
12
|
-
// basic get method
|
|
13
|
-
let data = {};
|
|
14
|
-
data.code = 200;
|
|
15
|
-
data.message = 'Got a GET request.';
|
|
16
|
-
|
|
17
|
-
/* await Users.findAll().then(e=> {
|
|
18
|
-
data.results = e
|
|
19
|
-
}); */
|
|
20
|
-
|
|
21
|
-
let fields = {
|
|
22
|
-
username: "eiei",
|
|
23
|
-
password: "xxx",
|
|
24
|
-
email: "bomb@bomb.com",
|
|
25
|
-
name: "jubu eiei"
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
/* Beech.store(fields, (err, result) => {
|
|
30
|
-
if(err) {
|
|
31
|
-
console.log(err);
|
|
32
|
-
} else {
|
|
33
|
-
console.log(result);
|
|
34
|
-
}
|
|
35
|
-
}); */
|
|
36
|
-
|
|
37
|
-
/* await Beech.findOne([], { id: 66 }, (err, result) => {
|
|
38
|
-
data.result = result
|
|
39
|
-
}) */
|
|
40
|
-
|
|
41
|
-
/* Beech.update({ username: "eiei", name: 'eiei222' }, 69, (err, result) => {
|
|
42
|
-
console.log(result);
|
|
43
|
-
}) */
|
|
44
|
-
|
|
45
|
-
data.result = await Users2.findAll(); // basic
|
|
46
|
-
//data.results = await User2Sequelize.findAll(); // sequelize
|
|
47
|
-
//data.results = await User2Sequelize.findUser2SequelizeById(7) // sequelize
|
|
48
|
-
|
|
49
|
-
//data.results = await Map_master.findAll() // sequelize
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
// @return
|
|
56
|
-
res.json(data);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
/@POST/
|
|
60
|
-
endpoint.post('/hello', Credentials, (req, res) => {
|
|
61
|
-
/**
|
|
62
|
-
* @param Integer id
|
|
63
|
-
*
|
|
64
|
-
* @return json
|
|
65
|
-
*/
|
|
66
|
-
let id = req.body.id;
|
|
67
|
-
let data = {};
|
|
68
|
-
data.code = 200;
|
|
69
|
-
data.message = 'Got a POST request.';
|
|
70
|
-
// @return
|
|
71
|
-
res.json(data);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
/@PUT/
|
|
75
|
-
endpoint.put('/hello/:id', Credentials, (req, res) => {
|
|
76
|
-
/**
|
|
77
|
-
* @param Integer id
|
|
78
|
-
*
|
|
79
|
-
* @return json
|
|
80
|
-
*/
|
|
81
|
-
let id = req.params.id;
|
|
82
|
-
let data = {};
|
|
83
|
-
data.code = 200;
|
|
84
|
-
data.message = 'Got a PUT request at /hello/' + id;
|
|
85
|
-
// @return
|
|
86
|
-
res.json(data);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
/@PATCH/
|
|
90
|
-
endpoint.patch('/hello/:id', Credentials, (req, res) => {
|
|
91
|
-
/**
|
|
92
|
-
* @param Integer id
|
|
93
|
-
*
|
|
94
|
-
* @return json
|
|
95
|
-
*/
|
|
96
|
-
let id = req.params.id;
|
|
97
|
-
let data = {};
|
|
98
|
-
data.code = 200;
|
|
99
|
-
data.message = 'Got a PATCH request at /hello/' + id;
|
|
100
|
-
// @return
|
|
101
|
-
res.json(data);
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
/@DELETE/
|
|
105
|
-
endpoint.delete('/hello/:id', Credentials, (req, res) => {
|
|
106
|
-
/**
|
|
107
|
-
* @param Integer id
|
|
108
|
-
*
|
|
109
|
-
* @return json
|
|
110
|
-
*/
|
|
111
|
-
let id = req.params.id;
|
|
112
|
-
let data = {};
|
|
113
|
-
data.code = 200;
|
|
114
|
-
data.message = 'Got a DELETE request at /hello/' + id;
|
|
115
|
-
// @return
|
|
116
|
-
res.json(data);
|
|
117
|
-
});
|
|
118
|
-
}
|