Skip to content

Commit

Permalink
Update CLI Stub
Browse files Browse the repository at this point in the history
  • Loading branch information
pphatdev committed Apr 17, 2024
1 parent 235976c commit 7516556
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 86 deletions.
101 changes: 65 additions & 36 deletions commands/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ if (commandLength >= 3) {
if ((options[0]) === '--controller') {

const controllerNames = options.slice(1, options.length)
controllerNames.forEach( name => {
const filename = `${name}`
createController(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 You have created controller file!\n`)
if (controllerNames.length > 0) {
controllerNames.forEach( name => {
const filename = `${name}`
createController(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 You have created controller file!\n`)
process.exit(0)
})
}
else {
console.log(`\x1b[41mcontroller name can't be blank!!\x1b[0m\n`)
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:controller controllername\x1b[0m\n\n`)
process.exit(0)
})
}
}


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

const modelNames = options.slice(1, options.length)
modelNames.forEach( name => {
const filename = `${name}`
createModel(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 You have created model file!\n`)
if (modelNames.length > 0) {
modelNames.forEach( name => {
const filename = `${name}`
createModel(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 You have created model file!\n`)
process.exit(0)
})
}
else {
console.log(`\x1b[41mmodel name can't be blank!!\x1b[0m\n`)
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:model modelname\x1b[0m\n\n`)
process.exit(0)
})
}
}


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

const modelNames = options.slice(1, options.length)
modelNames.forEach( name => {
const filename = `${name}`
createRoutes(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 You have created model file!\n`)
const routeNames = options.slice(1, options.length)
if (routeNames.length > 0) {
routeNames.forEach( name => {
const filename = `${name}`
createRoutes(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 You have created route file!\n`)
process.exit(0)
})
}
else {
console.log(`\x1b[41mroute name can't be blank!!\x1b[0m\n`)
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:route routename\x1b[0m\n\n`)
process.exit(0)
})
}
}

/**
* Create Controller, Create Route, Create Model
*/
if ((options[0]) === '--rcm') {

const modelNames = options.slice(1, options.length)
modelNames.forEach( name => {
const filename = `${name}`
createRoutes(filename)
createController(filename)
createModel(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 All file created!\n`)
const rcmNames = options.slice(1, options.length)
if (rcmNames.length > 0 ) {
rcmNames.forEach( name => {
const filename = `${name}`
createRoutes(filename)
createController(filename)
createModel(filename)
})

new Promise(resolve => setTimeout(resolve, 1000)).then(() => {
console.log(`✅🌈 All file created!\n`)
process.exit(0)
})
}
else
{
console.log(`\x1b[41mrcm name can't be blank!!\x1b[0m\n`)
console.log(`\x1b[33m[TRY]: \x1b[30mnpm run create:rcm rcmname\x1b[0m\n\n`)
process.exit(0)
})
}
}
}
74 changes: 24 additions & 50 deletions commands/stub/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,31 @@ const PAGE = new Pagination()
export const getData = async ( request ) =>
{
const { page, limit, search, sort } = request
const count = await client.query(`-- sql`)
const total = count.rows[0].count || 0
const pagination = PAGE.list({
const count = await client.query(`SELECT count(id) from public.[name]`)
const total = count.rows[0].count || 0
const query = PAGE.query({
table: 'public.[name]',
selectColumns: ["*"],
conditions: {
operator: 'WHERE',
value: ''
},
page: page,
limit: limit,
search: {
column: [], // search field ['name', 'id', 'etc...']
value: search, // search value
condition: "or", // or, and
withWere: true // include 'where'
column: [],
value: search,
operator: "or",
withWere: true
},
sort: {
column: [ ], // sort field ['name', 'id', 'etc...']
value: sort // desc, asc
column: [],
value: sort
},
})

const query = `select * from public.[name] ${ pagination } `

return await client.query(query).then(
async result => {
return await client.query(query, []).then(
result => {
const data = {
data: result.rows,
count: total,
Expand All @@ -45,7 +49,7 @@ export const getData = async ( request ) =>
export const getDataDetail = async ( { id } ) =>
{
return await client.query(
`select * from public.[name] where id=$1`, [id]
`SELECT * from public.[name] where id=$1`, [id]
).then(
async result => {
return response.success(
Expand All @@ -58,51 +62,21 @@ export const getDataDetail = async ( { id } ) =>
};


export const insetData = async ( request ) =>
{
const { name, email, password } = request;

return await client.query(
`INSERT INTO public.[name](...)`,
[name, email, password ]
).then(
result => {

if (result.rowCount < 0)
return result

return response.insetSuccess({ message: "Insert Success." })
}
).catch(
reason => {

if (reason.code == "23505")
return response.insetFailed({ message: reason.detail });

console.log(reason)
return reason
}
)
};


export const updateData = async ( request ) =>
{
const { a, b, c } = request;
return await client.query(
`UPDATE public.[name] SET ...`,
[ a, b, c ]
`UPDATE public.[name] SET "name"=$1 WHERE id=$2;`,
[name, id]
).then(
result => {

result =>
{
if (result.rowCount < 0)
return result

return response.insetSuccess({ message: "Update Success." })
}
).catch(
reason => {

reason =>
{
if (reason.code == "23505")
return response.insetFailed({ message: reason.detail });

Expand Down

0 comments on commit 7516556

Please sign in to comment.