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
package/README.md
CHANGED
|
@@ -13,16 +13,6 @@
|
|
|
13
13
|
|
|
14
14
|
`Beech API` is a Very easy for using, very feather framework, easy to installation, easy to implementation, and high security.
|
|
15
15
|
|
|
16
|
-
## Tutorial
|
|
17
|
-
|
|
18
|
-
`Beech API` tutorial on [Youtube](https://www.youtube.com/channel/UCjBMmUfV6yF1dQkqXnCjn1g)
|
|
19
|
-
|
|
20
|
-
- [EP.0 Why Beech API ?, basic usage (old version)](https://youtu.be/gEw1Ay_WQR4)
|
|
21
|
-
- [EP.1 Setup enveronment & create project (new version 3.x.x)](https://youtu.be/Z7qaOJQ0a8g)
|
|
22
|
-
- EP.2 Beech API with CRUD (MySQL) `is comming soon..`
|
|
23
|
-
- EP.3 Beech API using security with Passport-JWT token
|
|
24
|
-
- EP.4 Beech API Unit testing
|
|
25
|
-
|
|
26
16
|
## Powered by Node.js & Express.js
|
|
27
17
|
|
|
28
18
|

|
|
@@ -30,14 +20,17 @@
|
|
|
30
20
|
## Environment
|
|
31
21
|
|
|
32
22
|
- [`Node.js`](https://nodejs.org) >= 14.19.0+ (recommended)
|
|
33
|
-
- `npm` >= 6.4.1+ or `yarn` >= 1.22.4+
|
|
34
23
|
|
|
35
24
|
## Installation
|
|
36
25
|
|
|
37
|
-
Beech API requires Node.js version
|
|
26
|
+
Beech API requires Node.js version 14.19.0 or above. You can manage multiple versions of Node on the same machine with [nvm](https://github.com/creationix/nvm) or [nvm-windows](https://github.com/coreybutler/nvm-windows). So, Let's go to install `beech-api`
|
|
38
27
|
|
|
39
28
|
```sh
|
|
40
|
-
|
|
29
|
+
// NPM
|
|
30
|
+
$ npm install beech-api --global
|
|
31
|
+
|
|
32
|
+
// Yarn
|
|
33
|
+
$ yarn global add beech-api
|
|
41
34
|
```
|
|
42
35
|
|
|
43
36
|
After installation, you will have access to the `beech-app` binary in your command line.
|
|
@@ -49,18 +42,35 @@ $ beech-app --version
|
|
|
49
42
|
|
|
50
43
|
## Creating a project
|
|
51
44
|
|
|
52
|
-
|
|
45
|
+
Create a new project run:
|
|
53
46
|
|
|
54
47
|
```sh
|
|
55
48
|
$ beech-app create hello-world
|
|
56
49
|
```
|
|
50
|
+
Run your project:
|
|
51
|
+
```sh
|
|
52
|
+
$ cd hello-world
|
|
53
|
+
|
|
54
|
+
$ npm start
|
|
55
|
+
// OR
|
|
56
|
+
$ yarn start
|
|
57
|
+
```
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
❓ **Note:** The Beech API it's start server at [http://localhost:9000](http://localhost:9000) you can change new a port in `app.config.js` file.
|
|
59
60
|
|
|
60
|
-
:grey_question: **Note:** The Beech API will be generate ``app_secret`` key in ``app.config.js`` file, You can manual generate by use command ``$ beech key:generate``
|
|
61
61
|
|
|
62
|
-
##
|
|
62
|
+
## Upgrade to latest version ##
|
|
63
|
+
The Beech API upgrade to latest version command avariable :
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
// Project upgrade
|
|
67
|
+
$ beech-app update
|
|
63
68
|
|
|
69
|
+
// Global upgrade
|
|
70
|
+
$ beech-app update -g, --global
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Beech CLI tool available ##
|
|
64
74
|
After installation, you will have access to the `beech` binary in your command line.
|
|
65
75
|
The `beech` command has a number of options and you can explore them all by running:
|
|
66
76
|
|
|
@@ -72,24 +82,78 @@ The `beech` command line available:
|
|
|
72
82
|
|
|
73
83
|
```
|
|
74
84
|
Usage:
|
|
75
|
-
$ beech [options] [arguments]
|
|
85
|
+
$ beech [options] [arguments] [special]
|
|
76
86
|
|
|
77
87
|
Options:
|
|
78
|
-
|
|
79
|
-
-v, --version
|
|
88
|
+
?, -h, --help Display this help message.
|
|
89
|
+
-v, --version Display the application version.
|
|
80
90
|
|
|
81
91
|
The following commands are available:
|
|
82
92
|
|
|
83
|
-
$ beech make <endpoint>
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
$ beech make <model> --model Create a new
|
|
87
|
-
$ beech make <helper> --helper
|
|
88
|
-
$ beech passport init
|
|
89
|
-
$ beech add-on init
|
|
93
|
+
$ beech make <endpoint> Create a new Endpoints and unit test file,
|
|
94
|
+
You might using [special] `-R, --require`
|
|
95
|
+
for choose Model(s) used to endpoint file.
|
|
96
|
+
$ beech make <model> -M, --model Create a new Models file.
|
|
97
|
+
$ beech make <helper> --helper Create a new Helpers file.
|
|
98
|
+
$ beech passport init Initialize authentication with passport-jwt.
|
|
99
|
+
$ beech add-on init Initialize add-on file.
|
|
100
|
+
$ beech key:generate, key:gen Re-Generate application key (Dangerous!).
|
|
101
|
+
$ beech hash:<text> Hash text for Access to Database connection.
|
|
90
102
|
```
|
|
103
|
+
❓ **Note:** Every to create new project will be generated new ``app_key`` in ``app.config.js`` file, If you can re-generate. Can use command ``$ beech key:generate`` or ``$ beech key:gen``
|
|
104
|
+
|
|
105
|
+
## Database connection ##
|
|
106
|
+
|
|
107
|
+
You might connection to Database with `database_config` object in `app.config.js` file. Anything can support to multiple Database connections.
|
|
108
|
+
|
|
109
|
+
The connection base on `pool_base` in `global.config.js` file.
|
|
91
110
|
|
|
92
|
-
|
|
111
|
+
- `basic` = Support only Raw Query with Only MySQL.
|
|
112
|
+
- `sequelize` = Support PDO, Raw Query with various Database Engine.
|
|
113
|
+
|
|
114
|
+
In case Access to Database must to Hash the `username` and `password` with Beech CLI like this.
|
|
115
|
+
|
|
116
|
+
```sh
|
|
117
|
+
// Hash database username
|
|
118
|
+
$ beech hash:root
|
|
119
|
+
Output: m42BVxQ6Q4kLdRX7xS_Hm7WbQiNqShJDvw9SfuLCgI431oafWBtQJoJDnoCL
|
|
120
|
+
|
|
121
|
+
// Hash database password
|
|
122
|
+
$ beech hash:password
|
|
123
|
+
Output: FjgcgJPylkV7EeQJjea_EeifPwaHVO9onD3T4ATk3YYAyvprdrQejtMGu3dcDS0ejA
|
|
124
|
+
|
|
125
|
+
```
|
|
126
|
+
Example:
|
|
127
|
+
|
|
128
|
+
📂 app.config.js
|
|
129
|
+
```js
|
|
130
|
+
// basic & sequelize (needed Hash)
|
|
131
|
+
|
|
132
|
+
...
|
|
133
|
+
|
|
134
|
+
database_config: [
|
|
135
|
+
{
|
|
136
|
+
dialect: "mysql",
|
|
137
|
+
name: "mysql_my_store_db",
|
|
138
|
+
host: "localhost",
|
|
139
|
+
username: "m42BVxQ6Q4kLdRX7xS_Hm7WbQiNqShJDvw9SfuLCgI431oafWBtQJoJDnoCL",
|
|
140
|
+
password: "FjgcgJPylkV7EeQJjea_EeifPwaHVO9onD3T4ATk3YYAyvprdrQejtMGu3dcDS0ejA",
|
|
141
|
+
database: "my_store_db",
|
|
142
|
+
port: "3306",
|
|
143
|
+
is_connect: true, // boolean, Turn ON/OFF to connect
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
...
|
|
147
|
+
|
|
148
|
+
],
|
|
149
|
+
|
|
150
|
+
...
|
|
151
|
+
```
|
|
152
|
+
❓ **Caution! :** Every re-new generate `app_key`. Must to new Hash your Access and change to ALL Database connections.
|
|
153
|
+
|
|
154
|
+
## Part of generate file
|
|
155
|
+
|
|
156
|
+
### # Generate Endpoints ###
|
|
93
157
|
|
|
94
158
|
The `endpoints` keep the endpoints basic request files currently support `GET`, `POST`, `PUT`, `PATCH` and `DELETE`.
|
|
95
159
|
|
|
@@ -98,21 +162,82 @@ So, you might create new endpoints with constant `endpoint` object variable in `
|
|
|
98
162
|
```sh
|
|
99
163
|
$ beech make endpointName
|
|
100
164
|
```
|
|
165
|
+
**Example ***(Basic)***** : Fruits endpoints.
|
|
101
166
|
|
|
102
|
-
|
|
167
|
+
📂 fruits-endpoints.js
|
|
168
|
+
```js
|
|
169
|
+
exports.init = () => {
|
|
170
|
+
|
|
171
|
+
// GET method
|
|
172
|
+
endpoint.get("/fruits", Credentials, (req, res) => {
|
|
173
|
+
// @response
|
|
174
|
+
res.json({
|
|
175
|
+
code: 200,
|
|
176
|
+
message: "Got a GET request.",
|
|
177
|
+
});
|
|
178
|
+
});
|
|
103
179
|
|
|
180
|
+
|
|
181
|
+
// POST method
|
|
182
|
+
endpoint.post("/fruits", Credentials, (req, res) => {
|
|
183
|
+
// @response
|
|
184
|
+
res.json({
|
|
185
|
+
code: 200,
|
|
186
|
+
result: {
|
|
187
|
+
id: req.body.id,
|
|
188
|
+
name: req.body.name,
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
// PUT method
|
|
195
|
+
endpoint.put("/fruits/:id", Credentials, (req, res) => {
|
|
196
|
+
// @response
|
|
197
|
+
res.json({
|
|
198
|
+
code: 200,
|
|
199
|
+
message: "Got a PUT request /fruits/" + req.params.id,
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
// DELETE method
|
|
205
|
+
endpoint.delete("/fruits/:id", Credentials, (req, res) => {
|
|
206
|
+
// @response
|
|
207
|
+
res.json({
|
|
208
|
+
code: 200,
|
|
209
|
+
message: "Got a DELETE request /fruits/" + req.params.id,
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
...
|
|
214
|
+
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**Example ***(Sequelize)***** : Fruits endpoints.
|
|
219
|
+
|
|
220
|
+
📂 fruits-endpoints.js
|
|
104
221
|
```js
|
|
105
|
-
//
|
|
222
|
+
// You can declare Base with Beech Core for initial default endpoint [GET, POST, PATCH, DELETE]
|
|
223
|
+
const { Base } = require("beech-api"); 👈
|
|
224
|
+
|
|
225
|
+
// Model schema & function
|
|
226
|
+
const { Fruits } = require("@/models/Fruits");
|
|
106
227
|
|
|
107
228
|
exports.init = () => {
|
|
108
229
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
230
|
+
// initialize Fruits Model
|
|
231
|
+
Base([Fruits]); 👈 // It's like magic creating endpoints for you (CRUD) ✨
|
|
232
|
+
|
|
233
|
+
// Now you can request /fruits with methods GET, POST, PATCH and DELETE
|
|
234
|
+
// (C) POST: /fruits with { body }
|
|
235
|
+
// (R) GET: /fruits /:limit?/:offset?
|
|
236
|
+
// (U) PATCH: /fruits/:id with { body }
|
|
237
|
+
// (D) DELETE: /fruits/:id
|
|
113
238
|
|
|
114
|
-
|
|
115
|
-
endpoint.
|
|
239
|
+
// Other GET method
|
|
240
|
+
endpoint.get('/example-fruits', (req, res) => {
|
|
116
241
|
...
|
|
117
242
|
});
|
|
118
243
|
|
|
@@ -121,9 +246,8 @@ $ beech make endpointName
|
|
|
121
246
|
}
|
|
122
247
|
```
|
|
123
248
|
|
|
124
|
-
:grey_question: **Note:** Inside the endpoints file must be export `init()` function for initialize the the endpoints.
|
|
125
249
|
|
|
126
|
-
|
|
250
|
+
### # Generate Models ###
|
|
127
251
|
|
|
128
252
|
The `models` keep the files of function(s) for retriving, inserting, updating and deleting with SQL data. for understanding you might make model name same your table name in `src/models` folder.
|
|
129
253
|
|
|
@@ -131,66 +255,96 @@ The `models` keep the files of function(s) for retriving, inserting, updating an
|
|
|
131
255
|
$ beech make modelName --model
|
|
132
256
|
```
|
|
133
257
|
|
|
134
|
-
**Example (
|
|
258
|
+
**Example ***(Basic)***** : Fruits model.
|
|
135
259
|
|
|
260
|
+
📂 Fruits.js
|
|
136
261
|
```js
|
|
137
|
-
|
|
262
|
+
module.exports = {
|
|
138
263
|
|
|
139
|
-
|
|
264
|
+
// Example basic function get data
|
|
265
|
+
getData() {
|
|
140
266
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
267
|
+
return {
|
|
268
|
+
id: 1,
|
|
269
|
+
name: "John Doe",
|
|
270
|
+
}
|
|
145
271
|
|
|
146
|
-
|
|
147
|
-
getFruits() {
|
|
148
|
-
// call example mysql `mysql.default_db` connection name
|
|
149
|
-
mysql.default_db.query("SELECT * FROM fruits", (err, results) => {
|
|
272
|
+
},
|
|
150
273
|
|
|
151
|
-
|
|
152
|
-
|
|
274
|
+
// Example basic function get data from MySQL table
|
|
275
|
+
getFruits() {
|
|
153
276
|
|
|
154
|
-
|
|
155
|
-
|
|
277
|
+
// call example mysql `mysql.default_db` connection name
|
|
278
|
+
mysql.default_db.query("SELECT * FROM fruits", (err, results) => {
|
|
279
|
+
|
|
280
|
+
if (err) { throw err }
|
|
281
|
+
return results;
|
|
282
|
+
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
}
|
|
156
286
|
|
|
157
|
-
|
|
287
|
+
};
|
|
158
288
|
```
|
|
159
289
|
|
|
160
|
-
**Example (
|
|
290
|
+
**Example ***(Sequelize)***** : Fruits model.
|
|
291
|
+
|
|
292
|
+
You can asign more DataTypes, Learn more : [Sequelize docs](https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types)
|
|
161
293
|
|
|
294
|
+
📂Fruits.js
|
|
162
295
|
```js
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
id:
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
296
|
+
const { Schema } = require("beech-api");
|
|
297
|
+
|
|
298
|
+
// Define table Schema with `Schema(sql.default_db)` connection name
|
|
299
|
+
const Fruits = Schema(sql.default_db).define("fruits", {
|
|
300
|
+
fruit_id: {
|
|
301
|
+
field: "id", // Ref: field `id` in fruits table
|
|
302
|
+
type: DataTypes.INTEGER,
|
|
303
|
+
autoIncrement: true,
|
|
304
|
+
primaryKey: true
|
|
305
|
+
},
|
|
306
|
+
fruitsName: DataTypes.STRING,
|
|
307
|
+
fruitsQty: DataTypes.INTEGER,
|
|
308
|
+
fruitsPrice: {
|
|
309
|
+
type: DataTypes.INTEGER,
|
|
310
|
+
allowNull: false, // Allow null feilds
|
|
311
|
+
},
|
|
312
|
+
createdAt: DataTypes.DATE,
|
|
313
|
+
updatedAt: DataTypes.DATE,
|
|
314
|
+
});
|
|
177
315
|
|
|
178
|
-
|
|
316
|
+
// Example Finder by id (ORM), Learn more: https://sequelize.org/docs/v6/core-concepts/model-querying-finders/
|
|
317
|
+
function exampleFindOneFruitsById(id) {
|
|
318
|
+
return Fruits.findOne({ where: { id: id } });
|
|
319
|
+
}
|
|
179
320
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
321
|
+
// Example Raw Query, Learn more: https://sequelize.org/docs/v6/core-concepts/raw-queries/
|
|
322
|
+
function exampleGetAllFruits(id) {
|
|
323
|
+
return Fruits.query("SELECT * FROM fruits");
|
|
324
|
+
}
|
|
184
325
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
326
|
+
// Example Raw Query with Model Instances. This allows you to easily map a query to a predefined model
|
|
327
|
+
function exampleGetAllFruitsWithModelInstance(id) {
|
|
328
|
+
return Fruits.query("SELECT * FROM fruits", {
|
|
329
|
+
model: Fruits, // When JOIN table needed register that table [Fruits, ...]
|
|
330
|
+
mapToModel: true // pass true here if you have any mapped fields
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
...
|
|
189
335
|
|
|
190
|
-
|
|
336
|
+
// Export Schema, Function, ...
|
|
337
|
+
module.exports = {
|
|
338
|
+
Fruits,
|
|
339
|
+
exampleFindFruitsById,
|
|
340
|
+
exampleGetAllFruits,
|
|
341
|
+
exampleGetAllFruitsWithModelInstance,
|
|
342
|
+
...
|
|
343
|
+
};
|
|
191
344
|
```
|
|
192
345
|
|
|
193
|
-
|
|
346
|
+
|
|
347
|
+
### # Generate Helpers ###
|
|
194
348
|
|
|
195
349
|
The `helpers` keep the files of functions for process specific something in the project. So, you might create the `helpers` in path `src/helpers` folder.
|
|
196
350
|
|
|
@@ -200,22 +354,21 @@ $ beech make helperName --helper
|
|
|
200
354
|
|
|
201
355
|
**Example:** Text editor helper.
|
|
202
356
|
|
|
357
|
+
📂 TextEditor.js
|
|
203
358
|
```js
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
module.exports = {
|
|
359
|
+
module.exports = {
|
|
207
360
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
361
|
+
textUpperCase(text) {
|
|
362
|
+
return text.toUpperCase();
|
|
363
|
+
},
|
|
364
|
+
|
|
365
|
+
textTrim(text) {
|
|
366
|
+
return text.trim();
|
|
367
|
+
},
|
|
215
368
|
|
|
216
|
-
|
|
369
|
+
...
|
|
217
370
|
|
|
218
|
-
|
|
371
|
+
};
|
|
219
372
|
```
|
|
220
373
|
|
|
221
374
|
## Authentication (passport-jwt)
|
|
@@ -230,6 +383,7 @@ $ beech passport init
|
|
|
230
383
|
|
|
231
384
|
After passport initialized the `passport.config.js` it's appeared
|
|
232
385
|
|
|
386
|
+
📂 passport.config.js
|
|
233
387
|
```js
|
|
234
388
|
module.exports = {
|
|
235
389
|
// allow using passport-jwt
|
|
@@ -255,19 +409,19 @@ module.exports = {
|
|
|
255
409
|
username_field: "",
|
|
256
410
|
password_field: "",
|
|
257
411
|
|
|
258
|
-
//
|
|
412
|
+
// JWT playload data, You can add it. Example: ["name", "email", ...]
|
|
259
413
|
fields: []
|
|
260
414
|
},
|
|
261
415
|
|
|
262
|
-
// allow using with
|
|
263
|
-
|
|
416
|
+
// allow using request with app_key (Every request needed app_key headers)
|
|
417
|
+
app_key_allow: false
|
|
264
418
|
|
|
265
419
|
...
|
|
266
420
|
|
|
267
421
|
};
|
|
268
422
|
```
|
|
269
423
|
|
|
270
|
-
Simple ``users`` table:
|
|
424
|
+
***Authentication structure :*** Simple ``users`` table:
|
|
271
425
|
```
|
|
272
426
|
==============================================================
|
|
273
427
|
| id | username | password | name | email |
|
|
@@ -276,38 +430,87 @@ Simple ``users`` table:
|
|
|
276
430
|
| 2 | johnson | secret | johnson BA. | john@bomb.com |
|
|
277
431
|
```
|
|
278
432
|
|
|
279
|
-
|
|
433
|
+
When you config passport with ```users``` table already. You will got Auth endpoint in available.
|
|
280
434
|
```js
|
|
281
|
-
|
|
435
|
+
POST: "/authentication" // Request token
|
|
436
|
+
POST: "/authentication/create" // Create new Auth data
|
|
437
|
+
PATCH: "/authentication/update/:id" // Update old Auth data
|
|
282
438
|
```
|
|
283
439
|
|
|
284
|
-
|
|
440
|
+
Example :
|
|
441
|
+
|
|
285
442
|
```js
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
443
|
+
// Request with body for gether Token
|
|
444
|
+
POST: "/authentication"
|
|
445
|
+
{
|
|
446
|
+
username: "bombkiml",
|
|
447
|
+
password: "secret"
|
|
448
|
+
}
|
|
293
449
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
450
|
+
|
|
451
|
+
// Request with body for Create Auth data
|
|
452
|
+
POST: "/authentication/create"
|
|
453
|
+
{
|
|
454
|
+
username: "add_new_username",
|
|
455
|
+
password: "add_new_secret",
|
|
456
|
+
name: "add_new_my_name",
|
|
457
|
+
email: "add_new_email"
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
// Request with body for Update Auth data
|
|
462
|
+
PATCH: "/authentication/update/1"
|
|
463
|
+
Bearer Authorization: your_token
|
|
464
|
+
{
|
|
465
|
+
username: "update_bombkiml",
|
|
466
|
+
password: "update_secret",
|
|
467
|
+
name: "update_my_name",
|
|
468
|
+
email: "my_update_email@bomb.com"
|
|
469
|
+
}
|
|
297
470
|
```
|
|
298
471
|
|
|
299
|
-
|
|
472
|
+
### Beech auth managements with User ###
|
|
473
|
+
You can easy management `users` data with Beech, Only ```Store, Update``` NO ```Delete```, Anything you can make DELETE endpoint by yourself
|
|
474
|
+
|
|
300
475
|
```js
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
password: "new_secret",
|
|
304
|
-
name: "bombkiml NC.",
|
|
305
|
-
email: "bombkiml@bomb.com"
|
|
306
|
-
}
|
|
476
|
+
const { Store, Update } = require("beech-api");
|
|
477
|
+
```
|
|
307
478
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
479
|
+
- ***Store*** users data with ``Store()``
|
|
480
|
+
```js
|
|
481
|
+
// prepare data for store users
|
|
482
|
+
var data = {
|
|
483
|
+
username: "bombkiml",
|
|
484
|
+
password: "secret",
|
|
485
|
+
name: "bombkiml nc.",
|
|
486
|
+
email: "bomb@bomb.com"
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
Store(data, (err, stored) => {
|
|
490
|
+
if (err) throw err;
|
|
491
|
+
|
|
492
|
+
// response affected data
|
|
493
|
+
console.log(stored.insertId, stored.affectedRows);
|
|
494
|
+
|
|
495
|
+
});
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
- ***Update*** users data with ``Update()``
|
|
499
|
+
```js
|
|
500
|
+
// prepare data for update users
|
|
501
|
+
var data = {
|
|
502
|
+
password: "new_secret",
|
|
503
|
+
name: "bombkiml NC.",
|
|
504
|
+
email: "bombkiml@bomb.com"
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
Update(data, id, (err, updated) => {
|
|
508
|
+
if (err) throw err;
|
|
509
|
+
|
|
510
|
+
// response affected data
|
|
511
|
+
console.log(updated.updateId, updated.affectedRows);
|
|
512
|
+
|
|
513
|
+
});
|
|
311
514
|
```
|
|
312
515
|
|
|
313
516
|
## Beech with Official Strategy
|
|
@@ -324,38 +527,36 @@ Before your application can make use of Sign In With Google, you must register y
|
|
|
324
527
|
|
|
325
528
|
Go to open file ``passport.config.js`` and go to ``google strategy`` then turn allow Google Strategy is ``allow: true`` something like this.
|
|
326
529
|
|
|
530
|
+
📂 passport.config.js
|
|
327
531
|
```js
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
...
|
|
532
|
+
...
|
|
331
533
|
|
|
332
|
-
|
|
534
|
+
strategy: {
|
|
333
535
|
|
|
334
|
-
|
|
536
|
+
google: {
|
|
335
537
|
|
|
336
|
-
|
|
337
|
-
|
|
538
|
+
// Allow using google strategy
|
|
539
|
+
allow: true,
|
|
338
540
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
}
|
|
541
|
+
// Authen profile store fields available: `google_id`, `name`, `email`, `photos`, `locate`
|
|
542
|
+
local_profile_fields: {
|
|
543
|
+
google_id: "google_id", // Google ID field, default field name: `google_id`
|
|
544
|
+
name: "your_name_field",
|
|
545
|
+
email: "your_email_field",
|
|
546
|
+
photos: "your_profile_url_field",
|
|
547
|
+
locate: "" // If you not store set to null or remove it.
|
|
548
|
+
},
|
|
549
|
+
// Google development Credentials OAuth 2.0 Client IDs
|
|
550
|
+
client_id: "GOOGLE_CLIENT_ID",
|
|
551
|
+
client_secret: "GOOGLE_CLIENT_SECRET",
|
|
552
|
+
// Callback endpoint default `/google/callback`
|
|
553
|
+
callbackURL: "",
|
|
554
|
+
// Failure redirect to your route
|
|
555
|
+
failureRedirect: "/login"
|
|
355
556
|
}
|
|
557
|
+
}
|
|
356
558
|
|
|
357
|
-
|
|
358
|
-
|
|
559
|
+
...
|
|
359
560
|
```
|
|
360
561
|
|
|
361
562
|
The above code is a configures and registers the Google Strategy.
|
|
@@ -373,7 +574,7 @@ Place a button on the application's login page, prompting the user to sign in wi
|
|
|
373
574
|
<a href="/authentication/google" class="button">Sign in with Google</a>
|
|
374
575
|
```
|
|
375
576
|
|
|
376
|
-
|
|
577
|
+
❓ **Note:** The URL "``/authentication``" will be follow by ``auth_endpoint`` when you custom it.
|
|
377
578
|
|
|
378
579
|
|
|
379
580
|
### Facebook Strategy
|
|
@@ -385,40 +586,41 @@ Before your application can make use of Facebook Login, you must register your a
|
|
|
385
586
|
|
|
386
587
|
Go to open file ``passport.config.js`` and go to ``facebook strategy`` then turn allow Facebook Strategy is ``allow: true`` something like this.
|
|
387
588
|
|
|
589
|
+
📂 passport.config.js
|
|
388
590
|
```js
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
...
|
|
591
|
+
...
|
|
392
592
|
|
|
393
|
-
|
|
593
|
+
strategy: {
|
|
394
594
|
|
|
395
|
-
|
|
595
|
+
facebook: {
|
|
396
596
|
|
|
397
|
-
|
|
398
|
-
|
|
597
|
+
// Allow using facebook strategy
|
|
598
|
+
allow: true,
|
|
399
599
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
600
|
+
// Authen profile store fields available: `facebook_id`, `name`, `email`, `photos`, `locate`
|
|
601
|
+
local_profile_fields: {
|
|
602
|
+
facebook_id: "facebook_id", // Facebook ID field, default field name: `facebook_id`
|
|
603
|
+
name: "your_name_field",
|
|
604
|
+
email: "your_email_field",
|
|
605
|
+
photos: "your_profile_url_field",
|
|
606
|
+
locate: "" // If you not store set to null or remove it.
|
|
607
|
+
},
|
|
608
|
+
// Facebook development Credentials OAuth 2.0
|
|
609
|
+
app_id: "FACEBOOK_APP_ID",
|
|
610
|
+
app_secret: "FACEBOOK_APP_SECRET",
|
|
611
|
+
|
|
612
|
+
// You can allow Permissions facebook profile fields. Learn more (https://developers.facebook.com/docs/graph-api/reference/v13.0/user#readperms)
|
|
613
|
+
// **Update 2024, Now! Facebook requests permission for show Email. Learn more (https://developers.facebook.com/docs/permissions)
|
|
614
|
+
profileFieldsAllow: [ 'id', 'displayName', 'name', 'photos', 'email', 'location' ], // Default allowed
|
|
615
|
+
|
|
616
|
+
// Callback endpoint default `/facebook/callback`
|
|
617
|
+
callbackURL: "",
|
|
618
|
+
// Failure redirect to your route
|
|
619
|
+
failureRedirect: "/login"
|
|
418
620
|
}
|
|
621
|
+
}
|
|
419
622
|
|
|
420
|
-
|
|
421
|
-
|
|
623
|
+
...
|
|
422
624
|
```
|
|
423
625
|
|
|
424
626
|
The above code is a configures and registers the Facebook Strategy.
|
|
@@ -437,8 +639,24 @@ Place a button on the application's login page, prompting the user to sign in wi
|
|
|
437
639
|
<a href="/authentication/facebook" class="button">Log In With Facebook</a>
|
|
438
640
|
```
|
|
439
641
|
|
|
440
|
-
|
|
642
|
+
❓ **Note:** The URL "``/authentication``" will be follow by ``auth_endpoint`` when you custom it.
|
|
441
643
|
|
|
644
|
+
## CORS Origin allowments
|
|
645
|
+
The origin array to the callback can be any value allowed for the origin option of the middleware. Certain CORS requests are considered `complex` and require an initial OPTIONS request (called the `pre-flight request`). You can allowed CORS origin inside file `beech.config.js`
|
|
646
|
+
|
|
647
|
+
📂 beech.config.js
|
|
648
|
+
```js
|
|
649
|
+
module.exports = {
|
|
650
|
+
defineConfig: {
|
|
651
|
+
server: {
|
|
652
|
+
origin: ["http://example.com", "http://my-webapp:8080", "https://cat.io"],
|
|
653
|
+
originSensitive: false, // Sensitive with contrasts wording
|
|
654
|
+
},
|
|
655
|
+
},
|
|
656
|
+
}
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
❓ **Note:** When you must to allowed all Origin. You can assign `*` or `[]` null value to `origin` variable.
|
|
442
660
|
|
|
443
661
|
|
|
444
662
|
## Databases managements
|
|
@@ -468,7 +686,7 @@ This will create following folders inside `databases` folder.
|
|
|
468
686
|
|
|
469
687
|
Before continuing further we will need to tell CLI how to connect to database. To do that let's open default config file `databases/config/database.json` It looks something like this:
|
|
470
688
|
|
|
471
|
-
```
|
|
689
|
+
```json
|
|
472
690
|
{
|
|
473
691
|
"development": {
|
|
474
692
|
"username": "root",
|
|
@@ -494,9 +712,9 @@ Before continuing further we will need to tell CLI how to connect to database. T
|
|
|
494
712
|
}
|
|
495
713
|
```
|
|
496
714
|
|
|
497
|
-
|
|
715
|
+
❓ **Note:** The database connect default port 3306 if you another port you can add object ``port`` in config.
|
|
498
716
|
|
|
499
|
-
|
|
717
|
+
❓ **Note:** If your database doesn't exists yet, you can just call `npx sequelize-cli db:create` command. With proper access it will create that database for you.
|
|
500
718
|
|
|
501
719
|
### # Creating first Migrations
|
|
502
720
|
|
|
@@ -572,11 +790,10 @@ Test using [Jest](https://jestjs.io/en/) for testing the project. Jest is a deli
|
|
|
572
790
|
|
|
573
791
|
So, When you make the new endpoints it's automatic create test file end with `.spec.js` in `__test__` folder with constant `baseUrl` variable and `axios` package.
|
|
574
792
|
|
|
575
|
-
Example endpoints testing
|
|
793
|
+
Example endpoints testing :
|
|
576
794
|
|
|
795
|
+
📂 fruits-endpoints.spec.js
|
|
577
796
|
```js
|
|
578
|
-
// fruits-endpoints.spec.js
|
|
579
|
-
|
|
580
797
|
const endpoint = baseUrl.concat("/fruits/fruits");
|
|
581
798
|
|
|
582
799
|
describe("Test endpoint : " + endpoint, () => {
|
|
@@ -617,10 +834,9 @@ $ pm2 start ./node_modules/beech-api/packages/cli/beech --name <serviceName> -i
|
|
|
617
834
|
|
|
618
835
|
Docker builds images automatically by reading the instructions from a Dockerfile -- a text file that contains all commands, in order, needed to build a given image. A Dockerfile adheres to a specific format and set of instructions which you can find at [Dockerfile reference](https://docs.docker.com/engine/reference/builder/).
|
|
619
836
|
|
|
837
|
+
📂 Dockerfile
|
|
620
838
|
```js
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
FROM node:12.18-alpine
|
|
839
|
+
FROM node:14.19-alpine
|
|
624
840
|
ENV NODE_ENV=production
|
|
625
841
|
WORKDIR /usr/src/api
|
|
626
842
|
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
|
|
@@ -638,7 +854,7 @@ The docker build command builds an image from a Dockerfile and a context. The bu
|
|
|
638
854
|
$ docker build -t <imageName> .
|
|
639
855
|
```
|
|
640
856
|
|
|
641
|
-
|
|
857
|
+
❓ **Note:** You can specify a repository and tag at which to save the new image : ``` $ docker build -t <imageName>:<tags> . ```
|
|
642
858
|
|
|
643
859
|
- **Run docker**
|
|
644
860
|
|
|
@@ -671,9 +887,8 @@ $ npm install line-api
|
|
|
671
887
|
|
|
672
888
|
Create file `Line.js` in `src/helpers` folder and copy code below:
|
|
673
889
|
|
|
890
|
+
📂 Line.js
|
|
674
891
|
```js
|
|
675
|
-
// Line.js
|
|
676
|
-
|
|
677
892
|
const Line = require("line-api");
|
|
678
893
|
|
|
679
894
|
module.exports = {
|