Skip to content

Commit 7516556

Browse files
committed
Update CLI Stub
1 parent 235976c commit 7516556

File tree

2 files changed

+89
-86
lines changed

2 files changed

+89
-86
lines changed

commands/cli/index.js

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,22 @@ if (commandLength >= 3) {
6262
if ((options[0]) === '--controller') {
6363

6464
const controllerNames = options.slice(1, options.length)
65-
controllerNames.forEach( name => {
66-
const filename = `${name}`
67-
createController(filename)
68-
})
69-
70-
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
71-
console.log(`✅🌈 You have created controller file!\n`)
65+
if (controllerNames.length > 0) {
66+
controllerNames.forEach( name => {
67+
const filename = `${name}`
68+
createController(filename)
69+
})
70+
71+
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
72+
console.log(`✅🌈 You have created controller file!\n`)
73+
process.exit(0)
74+
})
75+
}
76+
else {
77+
console.log(`\x1b[41mcontroller name can't be blank!!\x1b[0m\n`)
78+
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:controller controllername\x1b[0m\n\n`)
7279
process.exit(0)
73-
})
80+
}
7481
}
7582

7683

@@ -80,15 +87,22 @@ if (commandLength >= 3) {
8087
if ((options[0]) === '--model') {
8188

8289
const modelNames = options.slice(1, options.length)
83-
modelNames.forEach( name => {
84-
const filename = `${name}`
85-
createModel(filename)
86-
})
87-
88-
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
89-
console.log(`✅🌈 You have created model file!\n`)
90+
if (modelNames.length > 0) {
91+
modelNames.forEach( name => {
92+
const filename = `${name}`
93+
createModel(filename)
94+
})
95+
96+
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
97+
console.log(`✅🌈 You have created model file!\n`)
98+
process.exit(0)
99+
})
100+
}
101+
else {
102+
console.log(`\x1b[41mmodel name can't be blank!!\x1b[0m\n`)
103+
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:model modelname\x1b[0m\n\n`)
90104
process.exit(0)
91-
})
105+
}
92106
}
93107

94108

@@ -97,34 +111,49 @@ if (commandLength >= 3) {
97111
*/
98112
if ((options[0]) === '--route') {
99113

100-
const modelNames = options.slice(1, options.length)
101-
modelNames.forEach( name => {
102-
const filename = `${name}`
103-
createRoutes(filename)
104-
})
105-
106-
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
107-
console.log(`✅🌈 You have created model file!\n`)
114+
const routeNames = options.slice(1, options.length)
115+
if (routeNames.length > 0) {
116+
routeNames.forEach( name => {
117+
const filename = `${name}`
118+
createRoutes(filename)
119+
})
120+
121+
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
122+
console.log(`✅🌈 You have created route file!\n`)
123+
process.exit(0)
124+
})
125+
}
126+
else {
127+
console.log(`\x1b[41mroute name can't be blank!!\x1b[0m\n`)
128+
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:route routename\x1b[0m\n\n`)
108129
process.exit(0)
109-
})
130+
}
110131
}
111132

112133
/**
113134
* Create Controller, Create Route, Create Model
114135
*/
115136
if ((options[0]) === '--rcm') {
116137

117-
const modelNames = options.slice(1, options.length)
118-
modelNames.forEach( name => {
119-
const filename = `${name}`
120-
createRoutes(filename)
121-
createController(filename)
122-
createModel(filename)
123-
})
124-
125-
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
126-
console.log(`✅🌈 All file created!\n`)
138+
const rcmNames = options.slice(1, options.length)
139+
if (rcmNames.length > 0 ) {
140+
rcmNames.forEach( name => {
141+
const filename = `${name}`
142+
createRoutes(filename)
143+
createController(filename)
144+
createModel(filename)
145+
})
146+
147+
new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
148+
console.log(`✅🌈 All file created!\n`)
149+
process.exit(0)
150+
})
151+
}
152+
else
153+
{
154+
console.log(`\x1b[41mrcm name can't be blank!!\x1b[0m\n`)
155+
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:rcm rcmname\x1b[0m\n\n`)
127156
process.exit(0)
128-
})
157+
}
129158
}
130159
}

commands/stub/model.stub

Lines changed: 24 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@ const PAGE = new Pagination()
88
export const getData = async ( request ) =>
99
{
1010
const { page, limit, search, sort } = request
11-
const count = await client.query(`-- sql`)
12-
const total = count.rows[0].count || 0
13-
const pagination = PAGE.list({
11+
const count = await client.query(`SELECT count(id) from public.[name]`)
12+
const total = count.rows[0].count || 0
13+
const query = PAGE.query({
14+
table: 'public.[name]',
15+
selectColumns: ["*"],
16+
conditions: {
17+
operator: 'WHERE',
18+
value: ''
19+
},
1420
page: page,
1521
limit: limit,
1622
search: {
17-
column: [], // search field ['name', 'id', 'etc...']
18-
value: search, // search value
19-
condition: "or", // or, and
20-
withWere: true // include 'where'
23+
column: [],
24+
value: search,
25+
operator: "or",
26+
withWere: true
2127
},
2228
sort: {
23-
column: [ ], // sort field ['name', 'id', 'etc...']
24-
value: sort // desc, asc
29+
column: [],
30+
value: sort
2531
},
2632
})
2733

28-
const query = `select * from public.[name] ${ pagination } `
29-
30-
return await client.query(query).then(
31-
async result => {
34+
return await client.query(query, []).then(
35+
result => {
3236
const data = {
3337
data: result.rows,
3438
count: total,
@@ -45,7 +49,7 @@ export const getData = async ( request ) =>
4549
export const getDataDetail = async ( { id } ) =>
4650
{
4751
return await client.query(
48-
`select * from public.[name] where id=$1`, [id]
52+
`SELECT * from public.[name] where id=$1`, [id]
4953
).then(
5054
async result => {
5155
return response.success(
@@ -58,51 +62,21 @@ export const getDataDetail = async ( { id } ) =>
5862
};
5963

6064

61-
export const insetData = async ( request ) =>
62-
{
63-
const { name, email, password } = request;
64-
65-
return await client.query(
66-
`INSERT INTO public.[name](...)`,
67-
[name, email, password ]
68-
).then(
69-
result => {
70-
71-
if (result.rowCount < 0)
72-
return result
73-
74-
return response.insetSuccess({ message: "Insert Success." })
75-
}
76-
).catch(
77-
reason => {
78-
79-
if (reason.code == "23505")
80-
return response.insetFailed({ message: reason.detail });
81-
82-
console.log(reason)
83-
return reason
84-
}
85-
)
86-
};
87-
88-
8965
export const updateData = async ( request ) =>
9066
{
91-
const { a, b, c } = request;
9267
return await client.query(
93-
`UPDATE public.[name] SET ...`,
94-
[ a, b, c ]
68+
`UPDATE public.[name] SET "name"=$1 WHERE id=$2;`,
69+
[name, id]
9570
).then(
96-
result => {
97-
71+
result =>
72+
{
9873
if (result.rowCount < 0)
9974
return result
100-
10175
return response.insetSuccess({ message: "Update Success." })
10276
}
10377
).catch(
104-
reason => {
105-
78+
reason =>
79+
{
10680
if (reason.code == "23505")
10781
return response.insetFailed({ message: reason.detail });
10882

0 commit comments

Comments
 (0)