beech-api 3.2.11 → 3.4.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 +500 -262
- package/index.js +3 -0
- package/package.json +22 -3
- package/packages/cli/bin/beech.js +54 -1
- package/packages/cli/core/auth/Credentials.js +23 -0
- package/packages/cli/core/auth/Passport.js +204 -0
- package/packages/cli/core/configure/app.config.js +19 -16
- package/packages/cli/core/configure/passport.config.js +84 -0
- package/packages/cli/core/databases/mysql.connection.js +13 -5
- package/packages/cli/core/generator/add-on +9 -0
- package/packages/cli/core/generator/endpoints +5 -5
- package/packages/cli/core/generator/help +3 -0
- package/packages/cli/core/generator/index.js +114 -7
- package/packages/cli/core/generator/package +2 -16
- package/packages/cli/core/index.js +31 -10
- package/packages/cli/core/services/http.express.js +210 -53
- package/packages/lib/beech.js +72 -0
- package/packages/lib/salt.js +3 -0
- package/packages/package.json +17 -0
package/README.md
CHANGED
|
@@ -1,297 +1,535 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://github.com/bombkiml)
|
|
2
|
+
|
|
2
3
|
# Beech API framework
|
|
4
|
+
|
|
3
5
|
[](https://github.com/bombkiml/beech-api/releases/)
|
|
4
6
|
[](https://github.com/bombkiml/beech-api/blob/master/README.md)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
7
|
+
|
|
8
|
+
## What is Beech API ?
|
|
9
|
+
|
|
10
|
+
`Beech API` is a Node.js framework it's help you with very easy create API project under [Node.js](https://nodejs.org)
|
|
11
|
+
|
|
12
|
+
## Why Beech API ?
|
|
13
|
+
|
|
14
|
+
`Beech API` is a Very easy for using, very feather framework, easy to installation, easy to implementation, and high security.
|
|
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
|
+
## Powered by Node.js & Express.js
|
|
27
|
+
|
|
28
|
+

|
|
29
|
+
|
|
30
|
+
## Environment
|
|
31
|
+
|
|
32
|
+
- [`Node.js`](https://nodejs.org) >= 10.13.0+ (recommended)
|
|
33
|
+
- `npm` >= 6.4.1+ or `yarn` >= 1.22.4+
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Beech API requires Node.js version 8.9 or above (10.13.0+ recommended). 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
|
+
|
|
39
|
+
```sh
|
|
40
|
+
$ npm install beech-api -g
|
|
41
|
+
# OR
|
|
42
|
+
$ yarn global add beech-api
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
After installation, you will have access to the `beech-app` binary in your command line.
|
|
46
|
+
You can check you have the right version with this command:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
$ beech-app --version
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Creating a project
|
|
53
|
+
|
|
54
|
+
create a new project run:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
$ beech-app create hello-world
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
:grey_question: **Note:** The Beech API it's start server at [http://127.0.0.1:9000](http://127.0.0.1:9000) you can change new a port in `app.config.js` file.
|
|
61
|
+
|
|
62
|
+
: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``
|
|
63
|
+
|
|
64
|
+
## Part of generate file
|
|
65
|
+
|
|
66
|
+
After installation, you will have access to the `beech` binary in your command line.
|
|
67
|
+
The `beech` command has a number of options and you can explore them all by running:
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
$ beech --help
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The `beech` command line available:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
Usage:
|
|
77
|
+
$ beech [options] [arguments] <special>
|
|
78
|
+
|
|
79
|
+
Options:
|
|
80
|
+
?|-h, --help Display this help message
|
|
81
|
+
-v, --version Display this application version
|
|
82
|
+
|
|
83
|
+
The following commands are available:
|
|
84
|
+
|
|
85
|
+
$ beech make <endpoint> Create a new endpoints and unit test file,
|
|
86
|
+
You might using <special> `--require=Model1,Model2,..`
|
|
87
|
+
for require model file(s) in generate processing
|
|
88
|
+
$ beech make <model> --model Create a new models file
|
|
89
|
+
$ beech passport init Initialize authentication with passport-jwt
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Endpoints
|
|
93
|
+
|
|
94
|
+
The `endpoints` keep the endpoints basic request files currently support `GET`, `POST`, `PUT`, `PATCH` and `DELETE`.
|
|
95
|
+
|
|
96
|
+
So, you might create new endpoints with constant `endpoint` object variable in `src/endpoints/` folder and file neme must be end with `-endpoints.js`
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
// fruits-endpoints.js
|
|
100
|
+
|
|
101
|
+
exports.init = () => {
|
|
102
|
+
|
|
103
|
+
/@GET/
|
|
104
|
+
endpoint.get('/fruits', (req, res) => {
|
|
105
|
+
...
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
/@POST/
|
|
109
|
+
endpoint.post('/fruits', (req, res) => {
|
|
110
|
+
...
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
:grey_question: **Note:** Inside the endpoints file must be export `init()` function for initialize the the endpoints.
|
|
117
|
+
|
|
118
|
+
## Models
|
|
119
|
+
|
|
120
|
+
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.
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
// Fruits.js
|
|
124
|
+
|
|
125
|
+
module.exports = {
|
|
126
|
+
|
|
127
|
+
// Example basic function get data
|
|
128
|
+
getData() {
|
|
129
|
+
return { ... }
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
// Example basic function get data from MySQL (must be return promise)
|
|
133
|
+
getFruits() {
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
try {
|
|
136
|
+
// call mysql `default_db` connection
|
|
137
|
+
mysql.default_db.query("SELECT * FROM fruits", (err, results) => {
|
|
138
|
+
if (err) { reject(err) }
|
|
139
|
+
resolve(results);
|
|
140
|
+
});
|
|
141
|
+
} catch (error) {
|
|
142
|
+
reject(error);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
};
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Helpers
|
|
151
|
+
|
|
152
|
+
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.
|
|
153
|
+
|
|
154
|
+
Example text editor helper:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
// TextEditor.js
|
|
158
|
+
|
|
159
|
+
module.exports = {
|
|
160
|
+
|
|
161
|
+
textUpperCase(text) {
|
|
162
|
+
return text.toUpperCase();
|
|
163
|
+
},
|
|
29
164
|
|
|
165
|
+
textTrim(text) {
|
|
166
|
+
return text.trim();
|
|
167
|
+
},
|
|
168
|
+
...
|
|
169
|
+
|
|
170
|
+
};
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Authentication (passport-jwt)
|
|
174
|
+
|
|
175
|
+
Passport is authentication middleware for Node. It is designed to serve a singular purpose: authenticate requests. When writing modules, encapsulation is a virtue, so Passport delegates all other functionality to the application. This separation of concerns keeps code clean and maintainable, and makes Passport extremely easy to integrate into an application.
|
|
176
|
+
|
|
177
|
+
Passport-jwt initiation :
|
|
178
|
+
|
|
179
|
+
```
|
|
180
|
+
$ beech passport init
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
After passport initialized the `passport.config.js` it's appeared
|
|
184
|
+
|
|
185
|
+
```js
|
|
186
|
+
module.exports = {
|
|
187
|
+
// allow using passport-jwt
|
|
188
|
+
jwt_allow: true,
|
|
189
|
+
|
|
190
|
+
// custom authenticaiton endpoint name, default `/authentication`
|
|
191
|
+
auth_endpoint: "",
|
|
192
|
+
|
|
193
|
+
// your jwt secret key
|
|
194
|
+
secret: "your_jwt_secret",
|
|
195
|
+
|
|
196
|
+
// token expiry time (seconds), default 86400 sec. it's expired in 24 hr.
|
|
197
|
+
token_expired: 86400,
|
|
198
|
+
|
|
199
|
+
model: {
|
|
200
|
+
// your mysql connection name inside `app.config.js` file (users table storage)
|
|
201
|
+
name: "default_db",
|
|
202
|
+
|
|
203
|
+
// table name of users store, default table `users`
|
|
204
|
+
table: "",
|
|
205
|
+
|
|
206
|
+
// secret user store field for authenticate, default field `username` and `password`
|
|
207
|
+
username_field: "",
|
|
208
|
+
password_field: "",
|
|
209
|
+
|
|
210
|
+
// show fields, default show fields ["id", "name", "email"]
|
|
211
|
+
fields: []
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
// allow using with app_secret requset
|
|
215
|
+
app_secret_allow: false
|
|
216
|
+
};
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Simple ``users`` table:
|
|
220
|
+
```
|
|
221
|
+
==============================================================
|
|
222
|
+
| id | username | password | name | email |
|
|
223
|
+
--------------------------------------------------------------
|
|
224
|
+
| 1 | bombkiml | secret | bombkiml NC. | bomb@bomb.com |
|
|
225
|
+
| 2 | johnson | secret | johnson BA. | john@bomb.com |
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
You can easy management `users` data with Beech helper just define below:
|
|
229
|
+
```js
|
|
230
|
+
const Beech = require("beech-api").User;
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
- Store users with ``store()``
|
|
234
|
+
```js
|
|
235
|
+
// store users
|
|
236
|
+
var data = {
|
|
237
|
+
username: "bombkiml",
|
|
238
|
+
password: "secret",
|
|
239
|
+
name: "bombkiml nc.",
|
|
240
|
+
email: "bomb@bomb.com"
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
Beech.store(data, (err) => {
|
|
244
|
+
if (err) throw err;
|
|
245
|
+
});
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
- Update users with ``update()``
|
|
249
|
+
```js
|
|
250
|
+
// update users
|
|
251
|
+
var data = {
|
|
252
|
+
password: "new_secret",
|
|
253
|
+
name: "bombkiml NC.",
|
|
254
|
+
email: "bombkiml@bomb.com"
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
Beech.update(data, id, (err) => {
|
|
258
|
+
if (err) throw err;
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
## API with Official strategies
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
## Databases managements
|
|
274
|
+
|
|
275
|
+
### # Migrations & Seeder
|
|
276
|
+
|
|
277
|
+
Just like you use Git / SVN to manage changes in your source code, you can use migrations to keep track of changes to the database. With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes in order to get back to the old state.
|
|
278
|
+
|
|
279
|
+
You will need [Sequelize CLI.](https://github.com/sequelize/cli) The CLI ships support for [migrations](https://sequelize.org/v5/manual/migrations.html) and project.
|
|
280
|
+
|
|
281
|
+
### # Usage
|
|
282
|
+
|
|
283
|
+
To create an empty project you will need to execute `init` command
|
|
284
|
+
|
|
285
|
+
```sh
|
|
286
|
+
$ npx sequelize-cli init
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
This will create following folders inside `databases` folder.
|
|
290
|
+
|
|
291
|
+
- `config`, contains config file, which tells CLI how to connect with database.
|
|
292
|
+
- `models`, contains all models for your project.
|
|
293
|
+
- `migrations`, contains all migration files.
|
|
294
|
+
- `seeders`, contains all seed files.
|
|
295
|
+
|
|
296
|
+
### # Configuration
|
|
297
|
+
|
|
298
|
+
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:
|
|
299
|
+
|
|
300
|
+
```
|
|
301
|
+
{
|
|
302
|
+
"development": {
|
|
303
|
+
"username": "root",
|
|
304
|
+
"password": null,
|
|
305
|
+
"database": "database_development",
|
|
306
|
+
"host": "127.0.0.1",
|
|
307
|
+
"dialect": "mysql"
|
|
308
|
+
},
|
|
309
|
+
"test": {
|
|
310
|
+
"username": "root",
|
|
311
|
+
"password": null,
|
|
312
|
+
"database": "database_test",
|
|
313
|
+
"host": "127.0.0.1",
|
|
314
|
+
"dialect": "mysql"
|
|
315
|
+
},
|
|
316
|
+
"production": {
|
|
317
|
+
"username": "root",
|
|
318
|
+
"password": null,
|
|
319
|
+
"database": "database_production",
|
|
320
|
+
"host": "127.0.0.1",
|
|
321
|
+
"dialect": "mysql"
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
:grey_question: **Note:** The database connect default port 3306 if you another port you can add object ``port`` in config.
|
|
327
|
+
|
|
328
|
+
:grey_question: **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.
|
|
329
|
+
|
|
330
|
+
### # Creating first Migrations
|
|
331
|
+
|
|
332
|
+
Create `model` use `model:generate` command. This command requires two options.
|
|
333
|
+
|
|
334
|
+
- `name`, Name of the model
|
|
335
|
+
- `attributes`, List of model attributes
|
|
336
|
+
|
|
337
|
+
Let's create a model named example `User`.
|
|
338
|
+
|
|
339
|
+
```sh
|
|
340
|
+
$ npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### # Migrations Up and Down
|
|
344
|
+
|
|
345
|
+
Until this step, we haven't inserted anything into the database. We have just created required model and migration files for our first model User.
|
|
346
|
+
|
|
347
|
+
- **Migrate Up** : you can create that table in database you need to run db:migrate command.
|
|
348
|
+
|
|
30
349
|
```sh
|
|
31
|
-
$
|
|
350
|
+
$ npx sequelize-cli db:migrate
|
|
32
351
|
```
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
create a new project run:
|
|
36
|
-
|
|
352
|
+
|
|
353
|
+
- **Migrate Down** : you can use `db:migrate:undo`, this command will revert most recent migration.
|
|
37
354
|
```sh
|
|
38
|
-
$
|
|
355
|
+
$ npx sequelize-cli db:migrate:undo
|
|
39
356
|
```
|
|
40
357
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
358
|
+
### # Creating First Seeder
|
|
359
|
+
|
|
360
|
+
To manage all data migrations you can use seeders. Seed files are some change in data that can be used to populate database table with sample data or test data.
|
|
361
|
+
|
|
362
|
+
Let's create a seed file which will add a demo user to our User table.
|
|
363
|
+
|
|
364
|
+
```sh
|
|
365
|
+
$ npx sequelize-cli seed:generate --name user
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
### # Seeder Up and Down
|
|
369
|
+
|
|
370
|
+
In last step you have create a seed file. It's still not committed to database. To do that we need to run a simple command.
|
|
371
|
+
|
|
372
|
+
- **Seed Up** : you can execute that seed file and you will have a user inserted into `User` table.
|
|
373
|
+
|
|
48
374
|
```sh
|
|
49
|
-
$
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
The ``beech`` command line available:
|
|
53
|
-
|
|
375
|
+
$ npx sequelize-cli db:seed:all
|
|
54
376
|
```
|
|
55
|
-
Usage:
|
|
56
|
-
$ beech [options] [arguments] <special>
|
|
57
|
-
|
|
58
|
-
Options:
|
|
59
|
-
?|-h, --help Display this help message
|
|
60
|
-
-v, --version Display this application version
|
|
61
|
-
|
|
62
|
-
The following commands are available:
|
|
63
|
-
|
|
64
|
-
$ beech make <endpoint> Create a new endpoints and unit test file,
|
|
65
|
-
You might using <special> `--require=Model1,Model2,..`
|
|
66
|
-
for require model file(s) in generate processing
|
|
67
|
-
$ beech make <model> --model Create a new models file
|
|
68
|
-
```
|
|
69
|
-
#
|
|
70
|
-
### Endpoints
|
|
71
|
-
The ``endpoints`` keep the endpoints basic request files currently support ``GET``, ``POST``, ``PUT``, ``PATCH`` and ``DELETE``.
|
|
72
|
-
|
|
73
|
-
So, you might create new endpoints with constant ``endpoint`` object variable in ``src/endpoints/`` folder and file neme must be end with ``-endpoints.js``
|
|
74
|
-
|
|
75
|
-
```js
|
|
76
|
-
// fruits-endpoints.js
|
|
77
|
-
|
|
78
|
-
exports.init = () => {
|
|
79
|
-
|
|
80
|
-
/@GET/
|
|
81
|
-
endpoint.get('/fruits', (req, res) => {
|
|
82
|
-
...
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
/@POST/
|
|
86
|
-
endpoint.post('/fruits', (req, res) => {
|
|
87
|
-
...
|
|
88
|
-
});
|
|
89
377
|
|
|
90
|
-
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
:grey_question: Tips: Inside the endpoints file must be export ``init()`` function for initialize the the endpoints. |
|
|
94
|
-
------------ |
|
|
378
|
+
- **Seed Down** : seeders can be undone if they are using any storage. There are two commands available for that:
|
|
95
379
|
|
|
96
|
-
|
|
97
|
-
### Models
|
|
98
|
-
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.
|
|
99
|
-
|
|
100
|
-
```js
|
|
101
|
-
// Fruits.js
|
|
102
|
-
|
|
103
|
-
module.exports = {
|
|
104
|
-
|
|
105
|
-
// Example basic function get data
|
|
106
|
-
getData() {
|
|
107
|
-
return { ... }
|
|
108
|
-
},
|
|
109
|
-
|
|
110
|
-
// Example basic function get data from MySQL (must be return promise)
|
|
111
|
-
getFruits() {
|
|
112
|
-
return new Promise((resolve, reject) => {
|
|
113
|
-
try {
|
|
114
|
-
// call mysql `default_db` connection
|
|
115
|
-
mysql.default_db.query("SELECT * FROM fruits", (err, results) => {
|
|
116
|
-
if (err) { reject(err) }
|
|
117
|
-
resolve(results);
|
|
118
|
-
});
|
|
119
|
-
} catch (error) {
|
|
120
|
-
reject(error);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
};
|
|
126
|
-
```
|
|
380
|
+
If you wish to undo most recent seed
|
|
127
381
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
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.
|
|
131
|
-
|
|
132
|
-
Example text editor helper:
|
|
133
|
-
|
|
134
|
-
```js
|
|
135
|
-
// TextEditor.js
|
|
136
|
-
|
|
137
|
-
module.exports = {
|
|
138
|
-
|
|
139
|
-
textUpperCase(text) {
|
|
140
|
-
return text.toUpperCase();
|
|
141
|
-
},
|
|
142
|
-
textTrim(text) {
|
|
143
|
-
return text.trim();
|
|
144
|
-
},
|
|
145
|
-
...
|
|
146
|
-
|
|
147
|
-
};
|
|
382
|
+
```sh
|
|
383
|
+
$ npx sequelize-cli db:seed:undo
|
|
148
384
|
```
|
|
149
385
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
Free ``helpers``you can make [LINE Notify](https://github.com/Yuhsak/line-api#readme) by using [line-api](https://notify-bot.line.me/en/) package with create the helper function following.
|
|
153
|
-
|
|
154
|
-
Installation the package:
|
|
155
|
-
|
|
386
|
+
If you wish to undo a specific seed
|
|
387
|
+
|
|
156
388
|
```
|
|
157
|
-
$
|
|
389
|
+
$ npx sequelize-cli db:seed:undo --seed <seederName>
|
|
158
390
|
```
|
|
159
|
-
|
|
160
|
-
Create file ``Line.js`` in ``src/helpers`` folder and copy code below:
|
|
161
|
-
|
|
162
|
-
```js
|
|
163
|
-
// Line.js
|
|
164
|
-
|
|
165
|
-
const Line = require('line-api');
|
|
166
391
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
});
|
|
172
|
-
notify.send({
|
|
173
|
-
message: message
|
|
174
|
-
})
|
|
175
|
-
.then(console.log)
|
|
176
|
-
}
|
|
177
|
-
};
|
|
392
|
+
If you wish to undo all seeds
|
|
393
|
+
|
|
394
|
+
```sh
|
|
395
|
+
$ npx sequelize-cli db:seed:undo:all
|
|
178
396
|
```
|
|
397
|
+
|
|
398
|
+
## Testing
|
|
399
|
+
|
|
400
|
+
Test using [Jest](https://jestjs.io/en/) for testing the project. Jest is a delightful JavaScript Testing Framework with a focus on simplicity. Learn more [Jest docs](https://jestjs.io/docs/en/getting-started.html)
|
|
401
|
+
|
|
402
|
+
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.
|
|
403
|
+
|
|
404
|
+
Example endpoints testing
|
|
405
|
+
|
|
406
|
+
```js
|
|
407
|
+
// fruits-endpoints.spec.js
|
|
408
|
+
|
|
409
|
+
const endpoint = baseUrl.concat("/fruits/fruits");
|
|
410
|
+
|
|
411
|
+
describe("Test endpoint : " + endpoint, () => {
|
|
412
|
+
it("Truthy!", () => {
|
|
413
|
+
expect("/fruits/fruits").toBeTruthy();
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it("Respond with basic GET status code 200", (done) => {
|
|
417
|
+
axios.get(endpoint).then((res) => {
|
|
418
|
+
expect(200).toEqual(res.data.code);
|
|
419
|
+
done();
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
## Implementation
|
|
179
426
|
|
|
180
|
-
|
|
427
|
+
### # Implement with [PM2](https://pm2.keymetrics.io/)
|
|
428
|
+
[PM2](https://pm2.keymetrics.io/) is a daemon process manager that will help you manage and keep your application online. Getting started with PM2 is straightforward, it is offered as a simple and intuitive CLI, installable via [NPM](https://www.npmjs.com/).
|
|
429
|
+
|
|
430
|
+
```sh
|
|
431
|
+
# Start service as standalone
|
|
432
|
+
$ pm2 start ./node_modules/beech-api/packages/cli/beech --name <serviceName>
|
|
433
|
+
|
|
434
|
+
# OR
|
|
435
|
+
|
|
436
|
+
# Start service as cluster mode
|
|
437
|
+
$ pm2 start ./node_modules/beech-api/packages/cli/beech --name <serviceName> -i <instances>
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### # Implement with [Docker](https://www.docker.com)
|
|
181
441
|
|
|
182
|
-
|
|
183
|
-
### Databases managements
|
|
184
|
-
#### # Migrations & Seeder
|
|
185
|
-
Just like you use Git / SVN to manage changes in your source code, you can use migrations to keep track of changes to the database. With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes in order to get back to the old state.
|
|
442
|
+
[Docker](https://www.docker.com) is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.
|
|
186
443
|
|
|
187
|
-
|
|
444
|
+
- **Create Dockerfile**
|
|
188
445
|
|
|
189
|
-
|
|
190
|
-
|
|
446
|
+
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/).
|
|
447
|
+
|
|
448
|
+
```js
|
|
449
|
+
// Dockerfile
|
|
450
|
+
|
|
451
|
+
FROM node:12.18-alpine
|
|
452
|
+
ENV NODE_ENV=production
|
|
453
|
+
WORKDIR /usr/src/api
|
|
454
|
+
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
|
|
455
|
+
RUN npm install --production --silent && mv node_modules .
|
|
456
|
+
COPY . .
|
|
457
|
+
EXPOSE 9000
|
|
458
|
+
CMD ["node", "./node_modules/beech-api/packages/cli/beech"]
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
- **Docker build image**
|
|
462
|
+
|
|
463
|
+
The docker build command builds an image from a Dockerfile and a context. The build’s context is the set of files at a specified location ```PATH``` or ```URL```. The PATH is a directory on your local filesystem. The URL is a Git repository location.
|
|
464
|
+
|
|
465
|
+
```sh
|
|
466
|
+
$ docker build -t <imageName> .
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
:grey_question: Tips: You can specify a repository and tag at which to save the new image : ``` $ docker build -t <imageName>:<tags> . ``` |
|
|
470
|
+
------------ |
|
|
471
|
+
|
|
472
|
+
- **Run docker**
|
|
473
|
+
|
|
474
|
+
After create ``image`` you can run docker engine following :
|
|
475
|
+
|
|
476
|
+
- **Docker Container (Standalone)**
|
|
191
477
|
```sh
|
|
192
|
-
$
|
|
478
|
+
$ docker run -d -p 9000:9000 --name <containerName> <imageName>
|
|
193
479
|
```
|
|
194
480
|
|
|
195
|
-
|
|
481
|
+
- **Create Docker Swarm (Cluster)**
|
|
482
|
+
```sh
|
|
483
|
+
# initiate swarm
|
|
484
|
+
$ docker swarm init
|
|
485
|
+
# run docker service
|
|
486
|
+
$ docker service create --replicas <instances> --name <containerName> --publish 9000:9000 <imageName>
|
|
487
|
+
```
|
|
196
488
|
|
|
197
|
-
- ``config``, contains config file, which tells CLI how to connect with database.
|
|
198
|
-
- ``models``, contains all models for your project.
|
|
199
|
-
- ``migrations``, contains all migration files.
|
|
200
|
-
- ``seeders``, contains all seed files.
|
|
201
489
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
490
|
+
## Bonus
|
|
491
|
+
|
|
492
|
+
Free `helpers` you can make [LINE Notify](https://github.com/Yuhsak/line-api#readme) by using [line-api](https://notify-bot.line.me/en/) package with create the helper function following.
|
|
493
|
+
|
|
494
|
+
Installation the package:
|
|
495
|
+
|
|
496
|
+
```
|
|
497
|
+
$ npm install line-api
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
Create file `Line.js` in `src/helpers` folder and copy code below:
|
|
501
|
+
|
|
502
|
+
```js
|
|
503
|
+
// Line.js
|
|
504
|
+
|
|
505
|
+
const Line = require("line-api");
|
|
506
|
+
|
|
507
|
+
module.exports = {
|
|
508
|
+
|
|
509
|
+
notify(message, token) {
|
|
510
|
+
const notify = new Line.Notify({
|
|
511
|
+
token: token
|
|
512
|
+
});
|
|
513
|
+
notify
|
|
514
|
+
.send({
|
|
515
|
+
message: message
|
|
516
|
+
})
|
|
517
|
+
.then(console.log);
|
|
228
518
|
}
|
|
229
|
-
```
|
|
230
|
-
The ``sequelize`` command line available:
|
|
231
|
-
```
|
|
232
|
-
$ npx sequelize [command]
|
|
233
|
-
|
|
234
|
-
Commands:
|
|
235
|
-
sequelize db:migrate Run pending migrations
|
|
236
|
-
sequelize db:migrate:schema:timestamps:add Update migration table to have timestamps
|
|
237
|
-
sequelize db:migrate:status List the status of all migrations
|
|
238
|
-
sequelize db:migrate:undo Reverts a migration
|
|
239
|
-
sequelize db:migrate:undo:all Revert all migrations ran
|
|
240
|
-
sequelize db:seed Run specified seeder
|
|
241
|
-
sequelize db:seed:undo Deletes data from the database
|
|
242
|
-
sequelize db:seed:all Run every seeder
|
|
243
|
-
sequelize db:seed:undo:all Deletes data from the database
|
|
244
|
-
sequelize db:create Create database specified by configuration
|
|
245
|
-
sequelize db:drop Drop database specified by configuration
|
|
246
|
-
sequelize init Initializes project
|
|
247
|
-
sequelize init:config Initializes configuration
|
|
248
|
-
sequelize init:migrations Initializes migrations
|
|
249
|
-
sequelize init:models Initializes models
|
|
250
|
-
sequelize init:seeders Initializes seeders
|
|
251
|
-
sequelize migration:generate Generates a new migration file [aliases: migration:create]
|
|
252
|
-
sequelize model:generate Generates a model and its migration [aliases: model:create]
|
|
253
|
-
sequelize seed:generate Generates a new seed file [aliases: seed:create]
|
|
254
|
-
|
|
255
|
-
Options:
|
|
256
|
-
--version Show version number [boolean]
|
|
257
|
-
--help Show help [boolean]
|
|
258
|
-
```
|
|
259
519
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
Test using [Jest](https://jestjs.io/en/) for testing the project. Jest is a delightful JavaScript Testing Framework with a focus on simplicity. Learn more [Jest docs](https://jestjs.io/docs/en/getting-started.html)
|
|
263
|
-
|
|
264
|
-
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.
|
|
265
|
-
|
|
266
|
-
Example endpoints testing
|
|
267
|
-
|
|
268
|
-
```js
|
|
269
|
-
// fruits-endpoints.spec.js
|
|
270
|
-
|
|
271
|
-
const endpoint = baseUrl.concat('/fruits/fruits');
|
|
272
|
-
|
|
273
|
-
describe('Test endpoint : ' + endpoint, () => {
|
|
274
|
-
it('Truthy!', () => {
|
|
275
|
-
expect('/fruits/fruits').toBeTruthy();
|
|
276
|
-
});
|
|
520
|
+
};
|
|
521
|
+
```
|
|
277
522
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
expect(200).toEqual(res.data.code);
|
|
282
|
-
done();
|
|
283
|
-
})
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
```
|
|
523
|
+
Enjoy.
|
|
524
|
+
|
|
525
|
+
## Development
|
|
288
526
|
|
|
289
|
-
#
|
|
290
|
-
### Development
|
|
291
527
|
Want to contribute or join for great job!, You can contact to me via
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
528
|
+
|
|
529
|
+
- GitHub: [bombkiml/beech-api - issues](https://github.com/bombkiml/beech-api/issues)
|
|
530
|
+
- E-mail: nattapat.jquery@gmail.com
|
|
531
|
+
- Facebook: [https://www.facebook.com/bombkiml](https://www.facebook.com/bombkiml)
|
|
532
|
+
|
|
533
|
+
## License
|
|
534
|
+
|
|
297
535
|
The Beech API framework is open-sourced software licensed under the [MIT license.](https://opensource.org/licenses/MIT)
|