create-sip 0.11.0 → 0.12.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 +57 -0
- package/expressapi/op +10 -13
- package/expressapi/templates/controllerTemplate.js +80 -0
- package/package.json +1 -1
- package/expressapi/tools/genconf.js +0 -12
- package/expressapi/tools/genkey.js +0 -23
package/README.md
CHANGED
|
@@ -21,3 +21,60 @@ npm create sip@latest
|
|
|
21
21
|
|
|
22
22
|
* Write project name.
|
|
23
23
|
* Select project type.
|
|
24
|
+
|
|
25
|
+
## Express API
|
|
26
|
+
|
|
27
|
+
Key generation:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
node op key:generate
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Afte project generation, you have an config/default.json file. If you want generate a new file, delete the config/default.json file and run next command:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
node op conf:generate
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Create model:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
node op create model <name>
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Create controller:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
node op create controller <name>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Exmple:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
node op create model employee
|
|
55
|
+
node op create controller employee
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Database
|
|
59
|
+
|
|
60
|
+
Default database type is sqlite :memory:. If you want to use another database, edit the config/default.json file. For example SQLite:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{
|
|
64
|
+
"app": {
|
|
65
|
+
"port": 8000,
|
|
66
|
+
"key": "",
|
|
67
|
+
"log": "console.log"
|
|
68
|
+
},
|
|
69
|
+
"db": {
|
|
70
|
+
"dialect": "sqlite",
|
|
71
|
+
"host": "127.0.0.1",
|
|
72
|
+
"name": "",
|
|
73
|
+
"user": "",
|
|
74
|
+
"pass": "",
|
|
75
|
+
"path": "database.sqlite"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Must be set the database path in the config/default.json file.
|
package/expressapi/op
CHANGED
|
@@ -3,14 +3,13 @@ import fs from 'fs-extra';
|
|
|
3
3
|
|
|
4
4
|
import fsp from 'fs/promises'
|
|
5
5
|
import { generateApiKey } from 'generate-api-key'
|
|
6
|
-
import path, { dirname } from 'path';
|
|
7
|
-
import url from 'url';
|
|
8
6
|
|
|
9
|
-
console.log(process.argv.length)
|
|
10
7
|
console.log(process.argv[2])
|
|
11
8
|
|
|
12
9
|
if(process.argv.length == 3 && process.argv[2] == 'key:generate') {
|
|
13
10
|
startGenerateKey();
|
|
11
|
+
} else if(process.argv.length == 3 && process.argv[2] == 'conf:generate') {
|
|
12
|
+
startGenerateConf();
|
|
14
13
|
} else if(process.argv.length < 4) {
|
|
15
14
|
console.log('Usage:');
|
|
16
15
|
console.log('node op <command> <type> <name>');
|
|
@@ -29,11 +28,6 @@ if(process.argv.length == 3 && process.argv[2] == 'key:generate') {
|
|
|
29
28
|
copyController();
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
|
-
if(process.argv[2] === 'key:generate') {
|
|
33
|
-
console.log('Genearete key ... ');
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
31
|
}
|
|
38
32
|
|
|
39
33
|
async function copyController() {
|
|
@@ -87,11 +81,6 @@ function capitalizeFirstLetter(word) {
|
|
|
87
81
|
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
88
82
|
}
|
|
89
83
|
|
|
90
|
-
function generateKey(size = 32, format = 'base64') {
|
|
91
|
-
const buffer = crypto.randomBytes(size);
|
|
92
|
-
return buffer.toString(format);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
84
|
function startGenerateKey() {
|
|
96
85
|
console.log('Generate key...');
|
|
97
86
|
const fileName = 'config/default.json'
|
|
@@ -105,3 +94,11 @@ function startGenerateKey() {
|
|
|
105
94
|
.then(body => fs.writeFile(fileName, body, 'utf8'))
|
|
106
95
|
.catch(error => console.log(error))
|
|
107
96
|
}
|
|
97
|
+
|
|
98
|
+
function startGenerateConf() {
|
|
99
|
+
console.log('Generate conf...');
|
|
100
|
+
const sourceFileName = 'config/default.json.example';
|
|
101
|
+
const destinationFileName = 'config/default.json';
|
|
102
|
+
fs.copyFile(sourceFileName, destinationFileName)
|
|
103
|
+
.catch(error => console.log(error));
|
|
104
|
+
}
|
|
@@ -19,6 +19,86 @@ const ThingController = {
|
|
|
19
19
|
success: true,
|
|
20
20
|
data: things
|
|
21
21
|
})
|
|
22
|
+
},
|
|
23
|
+
async show(req, res) {
|
|
24
|
+
try {
|
|
25
|
+
ThingController.tryShow(req, res)
|
|
26
|
+
}catch(error) {
|
|
27
|
+
res.status(500)
|
|
28
|
+
res.json({
|
|
29
|
+
success: false,
|
|
30
|
+
message: 'Error! The query is failed!'
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
async tryShow(req, res) {
|
|
35
|
+
const thing = await Thing.findByPk(req.params.id)
|
|
36
|
+
res.status(200)
|
|
37
|
+
res.json({
|
|
38
|
+
success: true,
|
|
39
|
+
data: thing
|
|
40
|
+
})
|
|
41
|
+
},
|
|
42
|
+
async create(req, res) {
|
|
43
|
+
try {
|
|
44
|
+
ThingController.tryCreate(req, res)
|
|
45
|
+
}catch(error) {
|
|
46
|
+
res.status(500)
|
|
47
|
+
res.json({
|
|
48
|
+
success: false,
|
|
49
|
+
message: 'Error! The query is failed!'
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
async tryCreate(req, res) {
|
|
54
|
+
const thing = await Thing.create(req.body)
|
|
55
|
+
res.status(201)
|
|
56
|
+
res.json({
|
|
57
|
+
success: true,
|
|
58
|
+
data: thing
|
|
59
|
+
})
|
|
60
|
+
},
|
|
61
|
+
async update(req, res) {
|
|
62
|
+
try {
|
|
63
|
+
ThingController.tryUpdate(req, res)
|
|
64
|
+
}catch(error) {
|
|
65
|
+
res.status(500)
|
|
66
|
+
res.json({
|
|
67
|
+
success: false,
|
|
68
|
+
message: 'Error! The query is failed!'
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
async tryUpdate(req, res) {
|
|
73
|
+
const thing = await Thing.update(req.body, {
|
|
74
|
+
where: { id: req.params.id }
|
|
75
|
+
})
|
|
76
|
+
res.status(200)
|
|
77
|
+
res.json({
|
|
78
|
+
success: true,
|
|
79
|
+
data: thing
|
|
80
|
+
})
|
|
81
|
+
},
|
|
82
|
+
async destroy(req, res) {
|
|
83
|
+
try {
|
|
84
|
+
ThingController.tryDestroy(req, res)
|
|
85
|
+
}catch(error) {
|
|
86
|
+
res.status(500)
|
|
87
|
+
res.json({
|
|
88
|
+
success: false,
|
|
89
|
+
message: 'Error! The query is failed!'
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
async tryDestroy(req, res) {
|
|
94
|
+
const thing = await Thing.destroy({
|
|
95
|
+
where: { id: req.params.id }
|
|
96
|
+
})
|
|
97
|
+
res.status(200)
|
|
98
|
+
res.json({
|
|
99
|
+
success: true,
|
|
100
|
+
data: thing
|
|
101
|
+
})
|
|
22
102
|
}
|
|
23
103
|
}
|
|
24
104
|
|
package/package.json
CHANGED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import fs from 'fs/promises'
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import url from 'url';
|
|
4
|
-
|
|
5
|
-
const __filename = url.fileURLToPath(import.meta.url);
|
|
6
|
-
const __dirname = path.dirname(__filename);
|
|
7
|
-
const sourceFileName = path.join(__dirname, '../config/default.json.example');
|
|
8
|
-
const destinationFileName = path.join(__dirname, '../config/default.json');
|
|
9
|
-
|
|
10
|
-
fs.copyFile(sourceFileName, destinationFileName)
|
|
11
|
-
.catch(error => console.log(error));
|
|
12
|
-
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import fs from 'fs/promises'
|
|
2
|
-
import { generateApiKey } from 'generate-api-key'
|
|
3
|
-
import path, { dirname } from 'path';
|
|
4
|
-
import url from 'url';
|
|
5
|
-
|
|
6
|
-
const __filename = url.fileURLToPath(import.meta.url);
|
|
7
|
-
const __dirname = dirname(__filename);
|
|
8
|
-
const fileName = path.join(__dirname, '../config/default.json');
|
|
9
|
-
|
|
10
|
-
function generateKey(size = 32, format = 'base64') {
|
|
11
|
-
const buffer = crypto.randomBytes(size);
|
|
12
|
-
return buffer.toString(format);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
fs.readFile(fileName)
|
|
16
|
-
.then(body => JSON.parse(body))
|
|
17
|
-
.then(json => {
|
|
18
|
-
json.app.key = generateApiKey({method: 'bytes', length: 32})
|
|
19
|
-
return json
|
|
20
|
-
})
|
|
21
|
-
.then(json => JSON.stringify(json, null, 4))
|
|
22
|
-
.then(body => fs.writeFile(fileName, body, 'utf8'))
|
|
23
|
-
.catch(error => console.log(error))
|