frgen 0.1.2 → 0.1.3

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.
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),n=require("fs"),t=require("path"),r=require("./utils.js");function o(e,n){var t=n.type,o=n.nullable,a=["yup"],c=["integer","bigint","smallint","tinyint","numeric","decimal","float"].includes(t),s=["varchar","text","string","char","character varying","uuid","date","datetime","timestamp","timestamp with time zone"].includes(t),i=["boolean"].includes(t);return c?a.push("number()"):s?a.push("string()"):i?a.push("boolean()"):a.push("mixed()"),o?a.push("optional()"):a.push("required()"),a.push("label('".concat(e.replaceAll("_"," "),"')")),"".concat(r.toCamelCase(e),":").concat(a.join("."))}var a=function(){var a=e.asyncToGenerator(e.regeneratorRuntime().mark((function a(c,s,i,u){var l,d,m,p,y,h,f,x,b,q,w,v=arguments;return e.regeneratorRuntime().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return l=v.length>4&&void 0!==v[4]?v[4]:"public",a.prev=1,c||(console.error("class name undefined"),process.exit(1)),s||(console.error("table name undefined"),process.exit(1)),d="".concat(c,".ts"),m=t.join(process.cwd(),"src","app","http","controller",d),i&&"default"!==i&&("/"!==i.charAt(i.length-1)&&(i+="/"),m=t.join(process.cwd(),"src",i,d)),p=c.replace(/Controller$/,""),a.next=10,u.select("table_name","column_name","column_default","is_nullable","data_type","character_maximum_length").from("information_schema.columns").whereRaw("table_catalog = current_database()").where("table_name",null!=s?s:p+"s").where("table_schema",l);case 10:y=a.sent,h=Object.fromEntries(y.map((function(e){return[e.column_name,{defaultValue:e.column_default,type:e.data_type,maxLength:e.character_maximum_length,nullable:"YES"===e.is_nullable}]}))),f="",x="",Object.keys(h).length&&(b=Object.entries(h).filter((function(n){var t=e.slicedToArray(n,1)[0];return!["created_at","updated_at","createdAt","updatedAt"].includes(t)})).map((function(n){var t=e.slicedToArray(n,2),a=t[0],c=t[1];return[r.toCamelCase(a),o(a,c)]})),x="\n const validated = await req.validation((yup) => yup.object({\n ".concat(b.map((function(n){var t=e.slicedToArray(n,2);return t[0],t[1]})).join(",\n"),"\n }));\n "),f="\n const validated = await req.validation((yup) => yup.object({\n ".concat(b.filter((function(n){return"id"!==e.slicedToArray(n,1)[0]})).map((function(n){var t=e.slicedToArray(n,2);return t[0],t[1]})).join(",\n"),"\n }));\n ")),q='\nimport { NextFunction, Request, Response } from "express";\nimport { Controller, Get, Post, Put, Delete } from "@lib/Decorators";\nimport NotFoundException from "@exception/NotFoundException";\nimport '.concat(p,' from "@model/').concat(p,'";\nimport ').concat(p,'Resource from "@resource/').concat(p,'Resource";\n\n@Controller("').concat(r.toKebabCase(p),'")\nexport default class ').concat(c,' {\n\n @Get("/")\n async index(req: Request, res: Response, next: NextFunction){\n try {\n const results = await ').concat(p,'.query().page(req.getQuery("page", 0), req.getQuery("perPage", 10));\n res.json(').concat(p,'Resource.paginate(results));\n } catch (error) {\n next(error);\n }\n };\n\n @Get("/:id")\n async view(req: Request, res: Response, next: NextFunction){\n try {\n const model = await ').concat(p,'.query().findById(req.params.id);\n if (!model) {\n throw new NotFoundException("').concat(p,'")\n }\n res.json(new ').concat(p,'Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n\n @Post("/")\n async create(req: Request, res: Response, next: NextFunction){\n try {\n ').concat(f,"\n const model = await ").concat(p,".query().insertAndFetch(").concat(""===f?"req.body":"validated",");\n res.json(new ").concat(p,'Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n\n @Put("/")\n async update(req: Request, res: Response, next: NextFunction){\n try {\n ').concat(x,"\n const model = await ").concat(p,".query().findById(").concat(""===x?"req.body.id":"validated.id",');\n if (!model) {\n throw new NotFoundException("').concat(p,'");\n }\n await model.$query().updateAndFetch(').concat(""===x?"req.body":"validated",");\n res.json(new ").concat(p,'Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n\n @Delete("/:id")\n async destroy(req: Request, res: Response, next: NextFunction){\n try {\n const model = await ').concat(p,'.query().findById(+req.params.id);\n if (!model) {\n throw new NotFoundException("').concat(p,'");\n }\n await model.$query().delete();\n res.json(new ').concat(p,"Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n}\n"),w=t.dirname(m),n.existsSync(w)||n.mkdirSync(w,{recursive:!0}),n.writeFile(m,q,(function(e){e?console.error("Gagal membuat file:",e):console.log("File ".concat(d," berhasil dibuat di ").concat(m))})),a.next=24;break;case 21:a.prev=21,a.t0=a.catch(1),console.error("Gagal mendapatkan informasi kolom:",a.t0);case 24:return a.prev=24,a.finish(24);case 26:case"end":return a.stop()}}),a,null,[[1,21,24,26]])})));return function(e,n,t,r){return a.apply(this,arguments)}}();module.exports=a;
2
+ "use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),n=require("fs"),t=require("path"),r=require("./utils.js");function o(e,n){var t=n.type,o=n.nullable,a=["yup"],c=["integer","bigint","smallint","tinyint","numeric","decimal","float"].includes(t),s=["varchar","text","string","char","character varying","uuid","date","datetime","timestamp","timestamp with time zone"].includes(t),i=["boolean"].includes(t);return c?a.push("number()"):s?a.push("string()"):i?a.push("boolean()"):a.push("mixed()"),o?a.push("optional()"):a.push("required()"),a.push("label('".concat(e.replaceAll("_"," "),"')")),"".concat(r.toCamelCase(e),":").concat(a.join("."))}var a=function(){var a=e.asyncToGenerator(e.regeneratorRuntime().mark((function a(c,s,i,u){var l,d,m,p,y,h,f,x,b,q,w,v=arguments;return e.regeneratorRuntime().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return l=v.length>4&&void 0!==v[4]?v[4]:"public",a.prev=1,c||(console.error("class name undefined"),process.exit(1)),s||(console.error("table name undefined"),process.exit(1)),d="".concat(c,".ts"),m=t.join(process.cwd(),"src","app","http","controller",d),i&&"default"!==i&&("/"!==i.charAt(i.length-1)&&(i+="/"),m=t.join(process.cwd(),"src",i,d)),p=c.replace(/Controller$/,""),a.next=10,u.select("table_name","column_name","column_default","is_nullable","data_type","character_maximum_length").from("information_schema.columns").whereRaw("table_catalog = current_database()").where("table_name",null!=s?s:p+"s").where("table_schema",l);case 10:y=a.sent,h=Object.fromEntries(y.map((function(e){return[e.column_name,{defaultValue:e.column_default,type:e.data_type,maxLength:e.character_maximum_length,nullable:"YES"===e.is_nullable}]}))),f="",x="",Object.keys(h).length&&(b=Object.entries(h).filter((function(n){var t=e.slicedToArray(n,1)[0];return!["created_at","updated_at","createdAt","updatedAt"].includes(t)})).map((function(n){var t=e.slicedToArray(n,2),a=t[0],c=t[1];return[r.toCamelCase(a),o(a,c)]})),x="\n const validated = await req.validation((yup) => yup.object({\n ".concat(b.map((function(n){var t=e.slicedToArray(n,2);return t[0],t[1]})).join(",\n"),"\n }));\n "),f="\n const validated = await req.validation((yup) => yup.object({\n ".concat(b.filter((function(n){return"id"!==e.slicedToArray(n,1)[0]})).map((function(n){var t=e.slicedToArray(n,2);return t[0],t[1]})).join(",\n"),"\n }));\n ")),q='\nimport { NextFunction, Request, Response } from "express";\nimport { Controller, Get, Post, Put, Delete } from "@lib/Decorators";\nimport NotFoundException from "@exception/NotFoundException";\nimport '.concat(p,' from "@model/').concat(p,'";\nimport ').concat(p,'Resource from "@resource/').concat(p,'Resource";\n\n@Controller("/').concat(r.toKebabCase(p),'")\nexport class ').concat(c,' {\n\n @Get("/")\n async index(req: Request, res: Response, next: NextFunction){\n try {\n const results = await ').concat(p,'.query().page(req.getQuery("page", 0), req.getQuery("perPage", 10));\n res.json(').concat(p,'Resource.paginate(results));\n } catch (error) {\n next(error);\n }\n };\n\n @Get("/:id")\n async view(req: Request, res: Response, next: NextFunction){\n try {\n const model = await ').concat(p,'.query().findById(req.params.id);\n if (!model) {\n throw new NotFoundException("').concat(p,'")\n }\n res.json(new ').concat(p,'Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n\n @Post("/")\n async create(req: Request, res: Response, next: NextFunction){\n try {\n ').concat(f,"\n const model = await ").concat(p,".query().insertAndFetch(").concat(""===f?"req.body":"validated",");\n res.json(new ").concat(p,'Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n\n @Put("/")\n async update(req: Request, res: Response, next: NextFunction){\n try {\n ').concat(x,"\n const model = await ").concat(p,".query().findById(").concat(""===x?"req.body.id":"validated.id",');\n if (!model) {\n throw new NotFoundException("').concat(p,'");\n }\n await model.$query().updateAndFetch(').concat(""===x?"req.body":"validated",");\n res.json(new ").concat(p,'Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n\n @Delete("/:id")\n async destroy(req: Request, res: Response, next: NextFunction){\n try {\n const model = await ').concat(p,'.query().findById(+req.params.id);\n if (!model) {\n throw new NotFoundException("').concat(p,'");\n }\n await model.$query().delete();\n res.json(new ').concat(p,"Resource(model, true));\n } catch (error) {\n next(error);\n }\n };\n}\n"),w=t.dirname(m),n.existsSync(w)||n.mkdirSync(w,{recursive:!0}),n.writeFile(m,q,(function(e){e?console.error("Gagal membuat file:",e):console.log("File ".concat(d," berhasil dibuat di ").concat(m))})),a.next=24;break;case 21:a.prev=21,a.t0=a.catch(1),console.error("Gagal mendapatkan informasi kolom:",a.t0);case 24:return a.prev=24,a.finish(24);case 26:case"end":return a.stop()}}),a,null,[[1,21,24,26]])})));return function(e,n,t,r){return a.apply(this,arguments)}}();module.exports=a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frgen",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "helper generator",
5
5
  "main": "lib/",
6
6
  "types": "lib/index.d.ts",