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
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
const Sequelize = require('sequelize');
|
|
2
|
+
global.sql = {};
|
|
3
|
+
|
|
4
|
+
exports.connect = () => {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
try {
|
|
7
|
+
connectInProcess(_config_.database_config, true, (err, result) => {
|
|
8
|
+
if (!err) {
|
|
9
|
+
resolve(result);
|
|
10
|
+
} else {
|
|
11
|
+
reject(err);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
} catch (error) {
|
|
15
|
+
reject(error);
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
connectInProcess = async (database_config, headDbShow, cb) => {
|
|
21
|
+
try {
|
|
22
|
+
let val = database_config.shift();
|
|
23
|
+
// checking turn on db connect
|
|
24
|
+
if (val.is_connect) {
|
|
25
|
+
const sq = await new Sequelize({
|
|
26
|
+
// one of 'mysql' | 'sqlite' | 'mariadb' | 'postgres' | 'mssql'
|
|
27
|
+
dialect: val.dialect || "mysql",
|
|
28
|
+
|
|
29
|
+
// for postgres, you can also specify an absolute path to a directory
|
|
30
|
+
// containing a UNIX socket to connect over
|
|
31
|
+
// host: '/sockets/psql_sockets'.
|
|
32
|
+
host: val.host,
|
|
33
|
+
username: val.username,
|
|
34
|
+
password: val.password,
|
|
35
|
+
database: val.database,
|
|
36
|
+
port: val.port,
|
|
37
|
+
|
|
38
|
+
// the storage engine for sqlite
|
|
39
|
+
// - default ':memory:'
|
|
40
|
+
storage: val.storage || ":memory:",
|
|
41
|
+
|
|
42
|
+
// custom protocol; default: 'tcp'
|
|
43
|
+
// postgres only, useful for Heroku
|
|
44
|
+
protocol: val.protocol || null,
|
|
45
|
+
// disable logging or provide a custom logging function; default: console.log
|
|
46
|
+
logging: val.logging || false,
|
|
47
|
+
|
|
48
|
+
// you can also pass any dialect options to the underlying dialect library
|
|
49
|
+
// - default is empty
|
|
50
|
+
// - currently supported: 'mysql', 'postgres', 'mssql'
|
|
51
|
+
dialectOptions: {
|
|
52
|
+
socketPath: ((val.dialectOptions) ? ((val.dialectOptions.socketPath) ? val.dialectOptions.socketPath : "") : ""), //Applications/MAMP/tmp/mysql/mysql.sock
|
|
53
|
+
supportBigNumbers: ((val.dialectOptions) ? ((val.dialectOptions.supportBigNumbers) ? val.dialectOptions.supportBigNumbers : false) : false),
|
|
54
|
+
bigNumberStrings: ((val.dialectOptions) ? ((val.dialectOptions.bigNumberStrings) ? val.dialectOptions.bigNumberStrings : false) : false),
|
|
55
|
+
options: ((val.dialectOptions) ? ((val.dialectOptions.options) ? ({ encrypt: false, ...val.dialectOptions.options }) : { encrypt: false }) : { encrypt: false }),
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
// disable inserting undefined values as NULL
|
|
59
|
+
// - default: false
|
|
60
|
+
omitNull: val.omitNull || false,
|
|
61
|
+
|
|
62
|
+
// a flag for using a native library or not.
|
|
63
|
+
// in the case of 'pg' -- set this to true will allow SSL support
|
|
64
|
+
// - default: false
|
|
65
|
+
native: val.native || false,
|
|
66
|
+
|
|
67
|
+
// Specify options, which are used when sequelize.define is called.
|
|
68
|
+
// The following example:
|
|
69
|
+
// define: { timestamps: false }
|
|
70
|
+
// is basically the same as:
|
|
71
|
+
// Model.init(attributes, { timestamps: false });
|
|
72
|
+
// sequelize.define(name, attributes, { timestamps: false });
|
|
73
|
+
// so defining the timestamps for each model will be not necessary
|
|
74
|
+
define: {
|
|
75
|
+
underscored: ((val.define) ? ((val.define.underscored) ? val.define.underscored : false) : false),
|
|
76
|
+
freezeTableName: ((val.define) ? ((val.define.freezeTableName === false) ? val.define.freezeTableName : true) : true),
|
|
77
|
+
charset: ((val.define) ? ((val.define.charset) ? val.define.charset : "utf8") : "utf8"),
|
|
78
|
+
dialectOptions: {
|
|
79
|
+
collate: ((val.define) ? ((val.define.dialectOptions) ? ((val.define.dialectOptions.timestamps) ? val.define.dialectOptions.timestamps : "utf8_general_ci") : "utf8_general_ci") : "utf8_general_ci"),
|
|
80
|
+
},
|
|
81
|
+
timestamps: ((val.define) ? ((val.define.timestamps) ? val.define.timestamps : false) : false),
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
// similar for sync: you can define this to always force sync for models
|
|
85
|
+
sync: {
|
|
86
|
+
force: ((val.sync) ? ((val.sync.force) ? val.sync.force : false) : false),
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
// pool configuration used to pool database connections
|
|
90
|
+
pool: {
|
|
91
|
+
max: ((val.pool) ? ((val.pool.max) ? val.pool.max : 5) : 5),
|
|
92
|
+
idle: ((val.pool) ? ((val.pool.idle) ? val.pool.idle : 30000) : 30000),
|
|
93
|
+
acquire: ((val.pool) ? ((val.pool.acquire) ? val.pool.acquire : 60000) : 60000),
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
// isolation level of each transaction
|
|
97
|
+
// defaults to dialect default
|
|
98
|
+
isolationLevel: val.isolationLevel || "Transaction.ISOLATION_LEVELS.REPEATABLE_READ",
|
|
99
|
+
|
|
100
|
+
// JSON response
|
|
101
|
+
query: {
|
|
102
|
+
raw: ((val.query) ? ((val.query.raw) ? val.query.raw : true) : true),
|
|
103
|
+
nest: ((val.query) ? ((val.query.nest) ? val.query.nest : true) : true),
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// connection
|
|
108
|
+
await sq.authenticate()
|
|
109
|
+
.then(() => {
|
|
110
|
+
// show only one text db connnections
|
|
111
|
+
if (headDbShow) {
|
|
112
|
+
console.log('\n[102m[90m Passed [0m [0mDatabase is connected at:');
|
|
113
|
+
headDbShow = false;
|
|
114
|
+
}
|
|
115
|
+
// create database pool
|
|
116
|
+
sql[ val.name ] = sq;
|
|
117
|
+
// checking dialect sql
|
|
118
|
+
if (val.dialect == "sqlite") {
|
|
119
|
+
console.log('- [91m[' + val.dialect + '] [0m[36m' + val.name + ' [0m->[93m ' + sq.options.storage + '[0m');
|
|
120
|
+
} else {
|
|
121
|
+
console.log('- [91m[' + val.dialect + '] [0m[36m' + val.name + ' [0m->[93m ' + sq.config.database + ':' + sq.config.port + '[0m');
|
|
122
|
+
}
|
|
123
|
+
// checking recursive database connection
|
|
124
|
+
if (database_config.length > 0) {
|
|
125
|
+
connectInProcess(database_config, headDbShow, e => {
|
|
126
|
+
cb(e, true);
|
|
127
|
+
});
|
|
128
|
+
} else {
|
|
129
|
+
// perfectly recursive
|
|
130
|
+
cb(null, true);
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
.catch(err => {
|
|
134
|
+
console.error('Unable to connect to the database:', err);
|
|
135
|
+
});
|
|
136
|
+
} else {
|
|
137
|
+
if (database_config.length > 0) {
|
|
138
|
+
connectInProcess(database_config, headDbShow, e => {
|
|
139
|
+
cb(e, true);
|
|
140
|
+
});
|
|
141
|
+
} else {
|
|
142
|
+
// perfectly recursive
|
|
143
|
+
cb(null, true);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
cb(error, null);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
|
|
3
|
+
textLowerCase(text) {
|
|
4
|
+
return text.toLowerCase();
|
|
5
|
+
},
|
|
6
|
+
|
|
7
|
+
textUpperCase(text) {
|
|
8
|
+
return text.toUpperCase();
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
digit9(number) {
|
|
12
|
+
return ('000000000' + number).slice(-9);
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
dateToThai(date) {
|
|
16
|
+
return result = date.toLocaleDateString('th-TH', {
|
|
17
|
+
year: 'numeric',
|
|
18
|
+
month: 'long',
|
|
19
|
+
day: 'numeric',
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
|
|
3
|
+
// Example basic function return exam data
|
|
4
|
+
getData() {
|
|
5
|
+
return [
|
|
6
|
+
{ "id": 1, "name": "Oggy" },
|
|
7
|
+
{ "id": 2, "name": "Jack" },
|
|
8
|
+
{ "id": 3, "name": "The Cockroaches Gang" }
|
|
9
|
+
];
|
|
10
|
+
},
|
|
11
|
+
|
|
12
|
+
// Example basic function get data from table {{modelName}}
|
|
13
|
+
findAll() {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
|
|
16
|
+
// call sql.{{dbSelected}} database connection name.
|
|
17
|
+
sql.{{dbSelected}}.query("SELECT * FROM {{modelName}}", (err, results) => {
|
|
18
|
+
if (err) reject(err);
|
|
19
|
+
resolve(results);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
exports.init = () => {
|
|
3
3
|
// Initiate with basic request currently support GET, POST, PUT, PATCH and DELETE
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
//@GET
|
|
6
6
|
endpoint.get('{{endpoint}}', Credentials, (req, res) => {
|
|
7
7
|
/**
|
|
8
8
|
* @return json
|
|
@@ -16,7 +16,7 @@ exports.init = () => {
|
|
|
16
16
|
res.json(data);
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
//@POST
|
|
20
20
|
endpoint.post('{{endpoint}}', Credentials, (req, res) => {
|
|
21
21
|
/**
|
|
22
22
|
* @param Integer id
|
|
@@ -33,7 +33,7 @@ exports.init = () => {
|
|
|
33
33
|
res.json(data);
|
|
34
34
|
});
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
//@PUT
|
|
37
37
|
endpoint.put('{{endpoint}}/:id', Credentials, (req, res) => {
|
|
38
38
|
/**
|
|
39
39
|
* @param Integer id
|
|
@@ -50,7 +50,7 @@ exports.init = () => {
|
|
|
50
50
|
res.json(data);
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
//@PATCH
|
|
54
54
|
endpoint.patch('{{endpoint}}/:id', Credentials, (req, res) => {
|
|
55
55
|
/**
|
|
56
56
|
* @param Integer id
|
|
@@ -67,7 +67,7 @@ exports.init = () => {
|
|
|
67
67
|
res.json(data);
|
|
68
68
|
});
|
|
69
69
|
|
|
70
|
-
|
|
70
|
+
//@DELETE
|
|
71
71
|
endpoint.delete('{{endpoint}}/:id', Credentials, (req, res) => {
|
|
72
72
|
/**
|
|
73
73
|
* @param Integer id
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const {{modelNameUppercase}} = sql.{{dbSelected}}.define("{{modelName}}", {
|
|
2
|
+
id: {
|
|
3
|
+
type: DataTypes.INTEGER,
|
|
4
|
+
autoIncrement: true,
|
|
5
|
+
primaryKey: true,
|
|
6
|
+
},
|
|
7
|
+
examField1: DataTypes.STRING, // asign more DataTypes see more: https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types
|
|
8
|
+
examField2: DataTypes.DATE,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
module.exports = {
|
|
12
|
+
|
|
13
|
+
// Example basic function get one by id
|
|
14
|
+
find{{modelNameUppercase}}ById(id) {
|
|
15
|
+
return {{modelNameUppercase}}.findOne({ where: { id: id } });
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
// Example basic function get all data from table {{modelName}}
|
|
19
|
+
findAll() {
|
|
20
|
+
return {{modelNameUppercase}}.findAll();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
}
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
"description": "The project is powered by Beech API framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"start": "
|
|
7
|
+
"start": "beech-service serve",
|
|
8
8
|
"test": "node ./node_modules/jest/bin/jest __tests__ -o --watch --config"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"beech-api": "^3.
|
|
11
|
+
"beech-api": "^3.5.12"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"jest": "^25.2.7"
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
const logUpdate = require("log-update");
|
|
3
|
+
const inquirer = require('inquirer');
|
|
3
4
|
|
|
4
5
|
class Generator {
|
|
5
6
|
constructor() {
|
|
@@ -10,6 +11,9 @@ class Generator {
|
|
|
10
11
|
throw err;
|
|
11
12
|
})
|
|
12
13
|
)
|
|
14
|
+
.catch(err => {
|
|
15
|
+
throw err
|
|
16
|
+
});
|
|
13
17
|
}
|
|
14
18
|
|
|
15
19
|
init() {
|
|
@@ -92,16 +96,16 @@ class Generator {
|
|
|
92
96
|
}
|
|
93
97
|
} else if (this.option == "key:generate") {
|
|
94
98
|
this.generateKeyConfigFile()
|
|
95
|
-
|
|
96
|
-
|
|
99
|
+
.then(resGenKey => resolve(resGenKey))
|
|
100
|
+
.catch(err => reject(err));
|
|
97
101
|
} else if (this.option == "add-on") {
|
|
98
102
|
if (this.argument == "init") {
|
|
99
103
|
this.makeAddOnInit()
|
|
100
104
|
.then(make => resolve(make))
|
|
101
|
-
.catch(err => reject(err));
|
|
105
|
+
.catch(err => reject(err));
|
|
102
106
|
} else {
|
|
103
107
|
resolve("\n[103m[90m Warning [0m[0m Using `add-on init` for initiate add-on.");
|
|
104
|
-
}
|
|
108
|
+
}
|
|
105
109
|
} else {
|
|
106
110
|
resolve("\n[101m Faltal [0m commnad it's not available.");
|
|
107
111
|
}
|
|
@@ -114,8 +118,8 @@ class Generator {
|
|
|
114
118
|
make(rq = null) {
|
|
115
119
|
return new Promise((resolve, reject) => {
|
|
116
120
|
try {
|
|
117
|
-
let tmpEndpointsPath = __dirname + '/
|
|
118
|
-
let tmpSpecPath = __dirname + '/
|
|
121
|
+
let tmpEndpointsPath = __dirname + '/_endpoints';
|
|
122
|
+
let tmpSpecPath = __dirname + '/_spec';
|
|
119
123
|
let endpointsPath = './src/endpoints/';
|
|
120
124
|
let testPath = './__tests__/unit/endpoints/';
|
|
121
125
|
// argument join `slash`
|
|
@@ -190,7 +194,65 @@ class Generator {
|
|
|
190
194
|
makeModel() {
|
|
191
195
|
return new Promise((resolve, reject) => {
|
|
192
196
|
try {
|
|
193
|
-
|
|
197
|
+
// declare path model file
|
|
198
|
+
let tmpModelsPath = __dirname;
|
|
199
|
+
// read global.config.js file for check pool_base for generate model file
|
|
200
|
+
this.fs.readFile("./global.config.js", 'utf8', (err, data) => {
|
|
201
|
+
if (err) {
|
|
202
|
+
throw err;
|
|
203
|
+
} else {
|
|
204
|
+
let buffer = Buffer.from(data);
|
|
205
|
+
let buf2str = buffer.toString();
|
|
206
|
+
let buf2json = JSON.parse(JSON.stringify(buf2str));
|
|
207
|
+
let pool_base = /global.pool_base\s+=\s+(?:"|')([^"]+)(?:"|')(?:\r|\n|$|;|\r)/i.exec(buf2json);
|
|
208
|
+
if (pool_base) {
|
|
209
|
+
// read app.config.js file for get db connect name
|
|
210
|
+
this.fs.readFile("./app.config.js", 'utf8', (appErr, appData) => {
|
|
211
|
+
if (appErr) {
|
|
212
|
+
throw appErr;
|
|
213
|
+
} else {
|
|
214
|
+
let appBuffer = Buffer.from(appData);
|
|
215
|
+
let appBuf2str = appBuffer.toString();
|
|
216
|
+
let appBuf2json = JSON.parse(JSON.stringify(appBuf2str));
|
|
217
|
+
let appBuf2eval = eval(appBuf2json);
|
|
218
|
+
// choose one of database connect name
|
|
219
|
+
inquirer.prompt([ {
|
|
220
|
+
type: "list",
|
|
221
|
+
name: "selectDbConnect",
|
|
222
|
+
message: "[93mPlease select database connect name:[0m",
|
|
223
|
+
choices: appBuf2eval.database_config.map(e => e.name),
|
|
224
|
+
} ]).then(dbSelected => {
|
|
225
|
+
// check pool_base
|
|
226
|
+
if (pool_base[ 1 ] == "basic") {
|
|
227
|
+
tmpModelsPath += '/_basic-models';
|
|
228
|
+
this.generateModel(tmpModelsPath, dbSelected.selectDbConnect)
|
|
229
|
+
.then(console.log)
|
|
230
|
+
.catch(console.log);
|
|
231
|
+
} else if (pool_base[ 1 ] == "sequelize") {
|
|
232
|
+
tmpModelsPath += '/_models';
|
|
233
|
+
this.generateModel(tmpModelsPath, dbSelected.selectDbConnect)
|
|
234
|
+
.then(console.log)
|
|
235
|
+
.catch(console.log);
|
|
236
|
+
} else {
|
|
237
|
+
resolve("\n[101m Faltal [0m The pool_base does not match the specific.");
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
} else {
|
|
243
|
+
resolve("\n[101m Faltal [0m The pool_base not found.");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
} catch (error) {
|
|
248
|
+
reject(error);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
generateModel(tmpModelsPath, dbSelected) {
|
|
254
|
+
return new Promise((resolve, reject) => {
|
|
255
|
+
try {
|
|
194
256
|
let modelPath = './src/models/';
|
|
195
257
|
// argument join `slash`
|
|
196
258
|
let arg = this.argument.replace(/^\/+|\/+$/g, '');
|
|
@@ -200,13 +262,16 @@ class Generator {
|
|
|
200
262
|
let subFolder = arg.join('/');
|
|
201
263
|
// models
|
|
202
264
|
let fullModels = modelPath + subFolder.concat('/') + models.concat('.js');
|
|
265
|
+
|
|
203
266
|
// check file exists
|
|
204
267
|
if (!this.fs.existsSync(fullModels)) {
|
|
205
268
|
// generater model
|
|
206
269
|
this.makeFolder(modelPath + subFolder)
|
|
207
270
|
.then(this.copy.bind(this, tmpModelsPath, fullModels))
|
|
208
271
|
.then(this.modelContentReplace.bind(this, fullModels, {
|
|
272
|
+
'modelNameUppercase': models,
|
|
209
273
|
'modelName': models.toLowerCase(),
|
|
274
|
+
'dbSelected': dbSelected,
|
|
210
275
|
}))
|
|
211
276
|
.then(logUpdate("\n[104m [37mProcessing[0m [0m The model `" + models + "` it's generating..."))
|
|
212
277
|
.then(generated => logUpdate(generated))
|
|
@@ -225,7 +290,7 @@ class Generator {
|
|
|
225
290
|
makeHelper() {
|
|
226
291
|
return new Promise((resolve, reject) => {
|
|
227
292
|
try {
|
|
228
|
-
let tmpHelpersPath = __dirname + '/
|
|
293
|
+
let tmpHelpersPath = __dirname + '/_helpers';
|
|
229
294
|
let helperPath = './src/helpers/';
|
|
230
295
|
// argument join `slash`
|
|
231
296
|
let arg = this.argument.replace(/^\/+|\/+$/g, '');
|
|
@@ -263,14 +328,10 @@ class Generator {
|
|
|
263
328
|
*/
|
|
264
329
|
return new Promise((resolve, reject) => {
|
|
265
330
|
try {
|
|
266
|
-
|
|
267
|
-
mkdirp(path
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
} else {
|
|
271
|
-
resolve(path);
|
|
272
|
-
}
|
|
273
|
-
})
|
|
331
|
+
const mkdirp = require("mkdirp");
|
|
332
|
+
mkdirp(path)
|
|
333
|
+
.then(p => resolve(p))
|
|
334
|
+
.catch(err => reject(err));
|
|
274
335
|
} catch (error) {
|
|
275
336
|
reject(error);
|
|
276
337
|
}
|
|
@@ -298,7 +359,7 @@ class Generator {
|
|
|
298
359
|
makeAddOnInit() {
|
|
299
360
|
return new Promise((resolve, reject) => {
|
|
300
361
|
try {
|
|
301
|
-
let tmpEndpointsPath = __dirname + '/
|
|
362
|
+
let tmpEndpointsPath = __dirname + '/_add-on';
|
|
302
363
|
let add_on_paste_point = "Add-on.js";
|
|
303
364
|
let folder_add_on = "./src/";
|
|
304
365
|
if (!this.fs.existsSync(folder_add_on + add_on_paste_point)) {
|
|
@@ -386,6 +447,11 @@ class Generator {
|
|
|
386
447
|
} else {
|
|
387
448
|
// content replace
|
|
388
449
|
let text = data.replace(new RegExp('{{modelName}}', 'g'), modelName);
|
|
450
|
+
text = text.replace(new RegExp('{{dbSelected}}', 'g'), textCondition.dbSelected);
|
|
451
|
+
// check add model name text uppercase
|
|
452
|
+
if (Object.keys(textCondition).length > 1) {
|
|
453
|
+
text = text.replace(new RegExp('{{modelNameUppercase}}', 'g'), textCondition.modelNameUppercase);
|
|
454
|
+
}
|
|
389
455
|
// writing the file
|
|
390
456
|
this.fs.writeFile(pathFile, text, 'utf8', (err) => {
|
|
391
457
|
if (err) {
|
|
@@ -411,7 +477,7 @@ class Generator {
|
|
|
411
477
|
let result = '';
|
|
412
478
|
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
413
479
|
let charactersLength = characters.length;
|
|
414
|
-
for (
|
|
480
|
+
for (var i = 0; i < length; i++) {
|
|
415
481
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
416
482
|
}
|
|
417
483
|
resolve(md5(result + secret));
|
|
@@ -445,7 +511,7 @@ class Generator {
|
|
|
445
511
|
} else {
|
|
446
512
|
resolve("\n[102m[90m Passed [0m[0m App secret it's new generated.");
|
|
447
513
|
}
|
|
448
|
-
});
|
|
514
|
+
});
|
|
449
515
|
});
|
|
450
516
|
}
|
|
451
517
|
});
|
|
@@ -458,7 +524,7 @@ class Generator {
|
|
|
458
524
|
help() {
|
|
459
525
|
return new Promise((resolve, reject) => {
|
|
460
526
|
try {
|
|
461
|
-
this.fs.readFile(__dirname + "/
|
|
527
|
+
this.fs.readFile(__dirname + "/_help", 'utf8', function (err, data) {
|
|
462
528
|
if (err) {
|
|
463
529
|
throw err;
|
|
464
530
|
}
|
|
@@ -474,11 +540,12 @@ class Generator {
|
|
|
474
540
|
return new Promise((resolve, reject) => {
|
|
475
541
|
try {
|
|
476
542
|
this.fs = require('fs');
|
|
543
|
+
this.cmd = require('node-cmd');
|
|
477
544
|
this.argv = argv;
|
|
478
|
-
this.option = argv[2];
|
|
479
|
-
this.argument = argv[3];
|
|
480
|
-
this.special = argv[4];
|
|
481
|
-
this.extra = argv[5];
|
|
545
|
+
this.option = argv[ 2 ];
|
|
546
|
+
this.argument = argv[ 3 ];
|
|
547
|
+
this.special = argv[ 4 ];
|
|
548
|
+
this.extra = argv[ 5 ];
|
|
482
549
|
resolve(this);
|
|
483
550
|
} catch (error) {
|
|
484
551
|
reject(err);
|
|
@@ -6,7 +6,6 @@ const _express_ = require("express");
|
|
|
6
6
|
global._app_ = _express_();
|
|
7
7
|
const cors = require("cors");
|
|
8
8
|
global.endpoint = _express_.Router();
|
|
9
|
-
global._mysql_ = require("mysql");
|
|
10
9
|
const cookieParser = require("cookie-parser");
|
|
11
10
|
const bodyParser = require("body-parser");
|
|
12
11
|
const expressSession = require("express-session");
|
|
@@ -15,7 +14,14 @@ const globalVariable = require(appRoot + "/global.config.js");
|
|
|
15
14
|
globalVariable.init();
|
|
16
15
|
// Local environments
|
|
17
16
|
global._config_ = require(appRoot + "/app.config");
|
|
18
|
-
const
|
|
17
|
+
const mySqlDbConnect = require("./databases/mysql");
|
|
18
|
+
const SequelizeDbConnect = require("./databases/sequelize");
|
|
19
|
+
// create global sequelize object
|
|
20
|
+
const { QueryTypes, DataTypes, Op } = require("sequelize");
|
|
21
|
+
global.QueryTypes = QueryTypes;
|
|
22
|
+
global.DataTypes = DataTypes;
|
|
23
|
+
global.Op = Op;
|
|
24
|
+
// engine import
|
|
19
25
|
const httpExpress = require("./services/http.express");
|
|
20
26
|
const fileWalk = require("./file-walk/file-walk");
|
|
21
27
|
// View engine
|
|
@@ -70,11 +76,12 @@ walker.on("end", () => {
|
|
|
70
76
|
// Initialize the application
|
|
71
77
|
init = async (jsfiles) => {
|
|
72
78
|
try {
|
|
73
|
-
await new Promise((resolve) => resolve(
|
|
74
|
-
await new Promise((resolve) => resolve(httpExpress.expressStart()));
|
|
79
|
+
await ((pool_base == "basic") ? new Promise((resolve) => resolve(mySqlDbConnect.connect())) : new Promise((resolve) => resolve(SequelizeDbConnect.connect())));
|
|
75
80
|
await new Promise((resolve) => resolve(authPassport.init()));
|
|
76
81
|
await new Promise((resolve) => resolve(fileWalk.fileWalk(jsfiles)));
|
|
82
|
+
await new Promise((resolve) => resolve(httpExpress.expressStart()));
|
|
77
83
|
} catch (error) {
|
|
84
|
+
console.log("[101m Compile failed [0m", error);
|
|
78
85
|
throw error;
|
|
79
86
|
}
|
|
80
87
|
}
|
|
@@ -10,8 +10,10 @@ module.exports = {
|
|
|
10
10
|
try {
|
|
11
11
|
// Create express server
|
|
12
12
|
const ExpressServer = _app_.listen(_config_.main_config.app_port, async () => {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
console.log('[102m[90m Passed [0m[0m Service is started at:');
|
|
14
|
+
console.log('- [91mLocal[0m: [36mhttp://' + _config_.main_config.app_host + ':' + ExpressServer.address().port + '[0m');
|
|
15
|
+
console.log('- [91mNetwork[0m: [36m' + _config_.main_config.client_host + '[0m');
|
|
16
|
+
this.authentication();
|
|
15
17
|
await this.badRequest()
|
|
16
18
|
.then(resolve(ExpressServer))
|
|
17
19
|
.catch(err => {
|
|
@@ -62,16 +64,16 @@ module.exports = {
|
|
|
62
64
|
var Beech = require("../../../lib/beech");
|
|
63
65
|
if (!passport_config.jwt_allow) {
|
|
64
66
|
// jwt is false
|
|
65
|
-
console.log("
|
|
67
|
+
console.log("- [91mJWT[0m: [90mOFF[0m");
|
|
66
68
|
return;
|
|
67
69
|
}
|
|
68
70
|
// jwt is true
|
|
69
|
-
console.log("
|
|
71
|
+
console.log("- [91mJWT[0m: [93mON[0m");
|
|
70
72
|
} else {
|
|
71
73
|
return;
|
|
72
74
|
}
|
|
73
75
|
// declare authentication endpoint name
|
|
74
|
-
const auth_endpoint = (passport_config.auth_endpoint) ? (passport_config.auth_endpoint[0] === "/" ? passport_config.auth_endpoint : "/" + passport_config.auth_endpoint) : "/authentication";
|
|
76
|
+
const auth_endpoint = (passport_config.auth_endpoint) ? (passport_config.auth_endpoint[ 0 ] === "/" ? passport_config.auth_endpoint : "/" + passport_config.auth_endpoint) : "/authentication";
|
|
75
77
|
// authentication endpoints
|
|
76
78
|
_app_.post(auth_endpoint, (req, res, next) => {
|
|
77
79
|
passport.authenticate('local', { session: false }, (err, user, opt) => {
|
|
@@ -86,7 +88,7 @@ module.exports = {
|
|
|
86
88
|
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
87
89
|
expiresIn: passport_config.token_expired
|
|
88
90
|
});
|
|
89
|
-
if (passport_config.app_secret_allow) {
|
|
91
|
+
if (passport_config.app_secret_allow) {
|
|
90
92
|
if (req.body.app_secret) {
|
|
91
93
|
if (_config_.main_config.app_secret == req.body.app_secret) {
|
|
92
94
|
res.status(200).json({
|
|
@@ -151,7 +153,7 @@ module.exports = {
|
|
|
151
153
|
]
|
|
152
154
|
}));
|
|
153
155
|
// google auth callback
|
|
154
|
-
const googleCallback = (passport_config.strategy.google.callbackURL) ? (passport_config.strategy.google.callbackURL[0] === "/" ? passport_config.strategy.google.callbackURL : "/" + passport_config.strategy.google.callbackURL) : "/google/callback";
|
|
156
|
+
const googleCallback = (passport_config.strategy.google.callbackURL) ? (passport_config.strategy.google.callbackURL[ 0 ] === "/" ? passport_config.strategy.google.callbackURL : "/" + passport_config.strategy.google.callbackURL) : "/google/callback";
|
|
155
157
|
_app_.get(auth_endpoint + googleCallback, passport.authenticate('google', { failureRedirect: passport_config.strategy.google.failureRedirect, failureMessage: true }), (req, res) => {
|
|
156
158
|
if (typeof req.user.user !== 'undefined') {
|
|
157
159
|
// declare user for sign JWT
|
|
@@ -169,8 +171,8 @@ module.exports = {
|
|
|
169
171
|
});
|
|
170
172
|
} else {
|
|
171
173
|
let condUser = {};
|
|
172
|
-
condUser[(passport_config.strategy.google.local_profile_fields.google_id) ? passport_config.strategy.google.local_profile_fields.google_id : "google_id"] = req.user.google.id;
|
|
173
|
-
Beech.findOne(
|
|
174
|
+
condUser[ (passport_config.strategy.google.local_profile_fields.google_id) ? passport_config.strategy.google.local_profile_fields.google_id : "google_id" ] = req.user.google.id;
|
|
175
|
+
Beech.findOne([], condUser, (err, result) => {
|
|
174
176
|
if (err) {
|
|
175
177
|
res.status(500).json({
|
|
176
178
|
code: 500,
|
|
@@ -179,7 +181,7 @@ module.exports = {
|
|
|
179
181
|
error: err
|
|
180
182
|
});
|
|
181
183
|
} else {
|
|
182
|
-
let user = JSON.parse(JSON.stringify(result[0]));
|
|
184
|
+
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
183
185
|
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
184
186
|
expiresIn: passport_config.token_expired
|
|
185
187
|
});
|
|
@@ -204,9 +206,9 @@ module.exports = {
|
|
|
204
206
|
*
|
|
205
207
|
*/
|
|
206
208
|
if (passport_config.strategy.facebook.allow) {
|
|
207
|
-
_app_.get(auth_endpoint + '/facebook', passport.authenticate('facebook', { scope: ['email', '
|
|
209
|
+
_app_.get(auth_endpoint + '/facebook', passport.authenticate('facebook', { scope: [ 'email', 'public_profile' ] }));
|
|
208
210
|
// facebook callback
|
|
209
|
-
const facebookCallback = (passport_config.strategy.facebook.callbackURL) ? (passport_config.strategy.facebook.callbackURL[0] === "/" ? passport_config.strategy.facebook.callbackURL : "/" + passport_config.strategy.facebook.callbackURL) : "/facebook/callback";
|
|
211
|
+
const facebookCallback = (passport_config.strategy.facebook.callbackURL) ? (passport_config.strategy.facebook.callbackURL[ 0 ] === "/" ? passport_config.strategy.facebook.callbackURL : "/" + passport_config.strategy.facebook.callbackURL) : "/facebook/callback";
|
|
210
212
|
_app_.get(auth_endpoint + facebookCallback, passport.authenticate('facebook', { failureRedirect: passport_config.strategy.facebook.failureRedirect, failureMessage: true }), (req, res) => {
|
|
211
213
|
if (typeof req.user.user !== 'undefined') {
|
|
212
214
|
// declare user for sign JWT
|
|
@@ -224,8 +226,8 @@ module.exports = {
|
|
|
224
226
|
});
|
|
225
227
|
} else {
|
|
226
228
|
let condUser = {};
|
|
227
|
-
condUser[(passport_config.strategy.facebook.local_profile_fields.facebook_id) ? passport_config.strategy.facebook.local_profile_fields.facebook_id : "facebook_id"] = req.user.facebook.id;
|
|
228
|
-
Beech.findOne(
|
|
229
|
+
condUser[ (passport_config.strategy.facebook.local_profile_fields.facebook_id) ? passport_config.strategy.facebook.local_profile_fields.facebook_id : "facebook_id" ] = req.user.facebook.id;
|
|
230
|
+
Beech.findOne([], condUser, (err, result) => {
|
|
229
231
|
if (err) {
|
|
230
232
|
res.status(500).json({
|
|
231
233
|
code: 500,
|
|
@@ -234,7 +236,7 @@ module.exports = {
|
|
|
234
236
|
error: err
|
|
235
237
|
});
|
|
236
238
|
} else {
|
|
237
|
-
let user = JSON.parse(JSON.stringify(result[0]));
|
|
239
|
+
let user = JSON.parse(JSON.stringify(result[ 0 ]));
|
|
238
240
|
const accessToken = jwt.sign(user, passport_config.secret, {
|
|
239
241
|
expiresIn: passport_config.token_expired
|
|
240
242
|
});
|