create-sip 0.10.9 → 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/config/default.json +15 -0
- package/expressapi/op +104 -0
- package/expressapi/package.json +1 -0
- package/expressapi/templates/controllerTemplate.js +105 -0
- package/expressapi/templates/modelTemplate.js +18 -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
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import replace from 'replace';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
|
|
4
|
+
import fsp from 'fs/promises'
|
|
5
|
+
import { generateApiKey } from 'generate-api-key'
|
|
6
|
+
|
|
7
|
+
console.log(process.argv[2])
|
|
8
|
+
|
|
9
|
+
if(process.argv.length == 3 && process.argv[2] == 'key:generate') {
|
|
10
|
+
startGenerateKey();
|
|
11
|
+
} else if(process.argv.length == 3 && process.argv[2] == 'conf:generate') {
|
|
12
|
+
startGenerateConf();
|
|
13
|
+
} else if(process.argv.length < 4) {
|
|
14
|
+
console.log('Usage:');
|
|
15
|
+
console.log('node op <command> <type> <name>');
|
|
16
|
+
console.log('Example:');
|
|
17
|
+
console.log('node op create model something');
|
|
18
|
+
console.log('node op create controller something');
|
|
19
|
+
console.log('node op key:generate');
|
|
20
|
+
process.exit(1);
|
|
21
|
+
} else {
|
|
22
|
+
if(process.argv[2] === 'create') {
|
|
23
|
+
console.log('Create a new ' + process.argv[3] + '...');
|
|
24
|
+
if(process.argv[3] === 'model') {
|
|
25
|
+
copyModel();
|
|
26
|
+
}
|
|
27
|
+
if(process.argv[3] === 'controller') {
|
|
28
|
+
copyController();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function copyController() {
|
|
34
|
+
|
|
35
|
+
const className = process.argv[4]
|
|
36
|
+
const lowerName = className.toLowerCase()
|
|
37
|
+
|
|
38
|
+
const src = 'templates/controllerTemplate.js'
|
|
39
|
+
const dest = `app/controllers/${lowerName}controller.js`
|
|
40
|
+
await fs.copy(src, dest)
|
|
41
|
+
|
|
42
|
+
replace({
|
|
43
|
+
regex: 'Thing',
|
|
44
|
+
replacement: capitalizeFirstLetter(className),
|
|
45
|
+
paths: [dest]
|
|
46
|
+
})
|
|
47
|
+
replace({
|
|
48
|
+
regex: 'thing',
|
|
49
|
+
replacement: className,
|
|
50
|
+
paths: [dest]
|
|
51
|
+
})
|
|
52
|
+
replace({
|
|
53
|
+
regex: 'things',
|
|
54
|
+
replacement: className + 's',
|
|
55
|
+
paths: [dest]
|
|
56
|
+
})
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function copyModel() {
|
|
60
|
+
|
|
61
|
+
const className = process.argv[4]
|
|
62
|
+
const lowerName = className.toLowerCase()
|
|
63
|
+
|
|
64
|
+
const src = 'templates/modelTemplate.js'
|
|
65
|
+
const dest = `app/models/${lowerName}.js`
|
|
66
|
+
await fs.copy(src, dest)
|
|
67
|
+
|
|
68
|
+
replace({
|
|
69
|
+
regex: 'Thing',
|
|
70
|
+
replacement: capitalizeFirstLetter(className),
|
|
71
|
+
paths: [dest]
|
|
72
|
+
})
|
|
73
|
+
replace({
|
|
74
|
+
regex: 'thing',
|
|
75
|
+
replacement: className,
|
|
76
|
+
paths: [dest]
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function capitalizeFirstLetter(word) {
|
|
81
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function startGenerateKey() {
|
|
85
|
+
console.log('Generate key...');
|
|
86
|
+
const fileName = 'config/default.json'
|
|
87
|
+
fsp.readFile(fileName)
|
|
88
|
+
.then(body => JSON.parse(body))
|
|
89
|
+
.then(json => {
|
|
90
|
+
json.app.key = generateApiKey({method: 'bytes', length: 32})
|
|
91
|
+
return json
|
|
92
|
+
})
|
|
93
|
+
.then(json => JSON.stringify(json, null, 4))
|
|
94
|
+
.then(body => fs.writeFile(fileName, body, 'utf8'))
|
|
95
|
+
.catch(error => console.log(error))
|
|
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
|
+
}
|
package/expressapi/package.json
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import Thing from '../models/thing.js'
|
|
2
|
+
|
|
3
|
+
const ThingController = {
|
|
4
|
+
async index(req, res) {
|
|
5
|
+
try {
|
|
6
|
+
ThingController.tryIndex(req, res)
|
|
7
|
+
}catch(error) {
|
|
8
|
+
res.status(500)
|
|
9
|
+
res.json({
|
|
10
|
+
success: false,
|
|
11
|
+
message: 'Error! The query is failed!'
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
async tryIndex(req, res) {
|
|
16
|
+
const things = await Thing.findAll()
|
|
17
|
+
res.status(200)
|
|
18
|
+
res.json({
|
|
19
|
+
success: true,
|
|
20
|
+
data: things
|
|
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
|
+
})
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export default ThingController
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DataTypes } from 'sequelize'
|
|
2
|
+
import sequelize from '../database/database.js'
|
|
3
|
+
|
|
4
|
+
const Thing = sequelize.define('thing', {
|
|
5
|
+
id: {
|
|
6
|
+
type: DataTypes.INTEGER,
|
|
7
|
+
autoIncrement: true,
|
|
8
|
+
primaryKey: true
|
|
9
|
+
},
|
|
10
|
+
name: { type: DataTypes.STRING, allowNull: false }
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
sequelize.sync({
|
|
15
|
+
force: false
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
export default Thing
|
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))
|