frgen 0.0.4 → 0.0.5

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 a(e,n){var t=n.type,a=n.nullable,o=["yup"],c=["integer","bigint","smallint","tinyint","numeric","decimal","float"].includes(t),i=["varchar","text","string","char","character varying","uuid","date","datetime","timestamp","timestamp with time zone"].includes(t),s=["boolean"].includes(t);return c?o.push("number()"):i?o.push("string()"):s?o.push("boolean()"):o.push("mixed()"),a?o.push("optional()"):o.push("required()"),o.push("label('".concat(e.replaceAll("_"," "),"')")),"".concat(r.toCamelCase(e),":").concat(o.join("."))}var o=function(){var o=e.asyncToGenerator(e.regeneratorRuntime().mark((function o(c,i,s,l){var u,d,m,p,y,h,f,x,b,w,q,g=arguments;return e.regeneratorRuntime().wrap((function(o){for(;;)switch(o.prev=o.next){case 0:return u=g.length>4&&void 0!==g[4]?g[4]:"public",o.prev=1,c||(console.error("class name undefined"),process.exit(1)),i||(console.error("table name undefined"),process.exit(1)),d="".concat(c,".ts"),m=t.join(process.cwd(),"src","app","http","controller",d),s&&"default"!==s&&("/"!==s.charAt(s.length-1)&&(s+="/"),m=t.join(process.cwd(),"src",s,d)),p=c.replace(/Controller$/,""),o.next=10,l.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!=i?i:p+"s").where("table_schema",u);case 10:y=o.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),o=t[0],c=t[1];return[r.toCamelCase(o),a(o,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 ")),w='\nimport NotFoundException from "@exception/NotFoundException";\nimport '.concat(p,' from "@model/').concat(p,'";\nimport ').concat(p,'Resource from "@resource/').concat(p,'Resource";\nimport express from "express";\n\nconst ').concat(c," = express.Router();\n\n").concat(c,'.get("/", async (req, res, next) => {\n try {\n const results = await ').concat(p,'.query().page(req.getQuery("page", 0), req.getQuery("perPage", 10));\n res.json(results);\n } catch (error) {\n next(error);\n }\n});\n\n').concat(c,'.get("/:id", async (req, res, next) => {\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").concat(c,'.post("/", async (req, res, next) => {\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").concat(c,'.put("/", async (req, res, next) => {\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").concat(c,'.delete("/:id", async (req, res, next) => {\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\nexport default ").concat(c,";\n"),q=t.dirname(m),n.existsSync(q)||n.mkdirSync(q,{recursive:!0}),n.writeFile(m,w,(function(e){e?console.error("Gagal membuat file:",e):console.log("File ".concat(d," berhasil dibuat di ").concat(m))})),o.next=24;break;case 21:o.prev=21,o.t0=o.catch(1),console.error("Gagal mendapatkan informasi kolom:",o.t0);case 24:return o.prev=24,o.finish(24);case 26:case"end":return o.stop()}}),o,null,[[1,21,24,26]])})));return function(e,n,t,r){return o.apply(this,arguments)}}();module.exports=o;
2
+ "use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),n=require("fs"),t=require("path"),r=require("./utils.js");function a(e,n){var t=n.type,a=n.nullable,o=["yup"],c=["integer","bigint","smallint","tinyint","numeric","decimal","float"].includes(t),i=["varchar","text","string","char","character varying","uuid","date","datetime","timestamp","timestamp with time zone"].includes(t),s=["boolean"].includes(t);return c?o.push("number()"):i?o.push("string()"):s?o.push("boolean()"):o.push("mixed()"),a?o.push("optional()"):o.push("required()"),o.push("label('".concat(e.replaceAll("_"," "),"')")),"".concat(r.toCamelCase(e),":").concat(o.join("."))}var o=function(){var o=e.asyncToGenerator(e.regeneratorRuntime().mark((function o(c,i,s,l){var u,d,m,p,y,h,f,x,b,w,g,q=arguments;return e.regeneratorRuntime().wrap((function(o){for(;;)switch(o.prev=o.next){case 0:return u=q.length>4&&void 0!==q[4]?q[4]:"public",o.prev=1,c||(console.error("class name undefined"),process.exit(1)),i||(console.error("table name undefined"),process.exit(1)),d="".concat(c,".ts"),m=t.join(process.cwd(),"src","app","http","controller",d),s&&"default"!==s&&("/"!==s.charAt(s.length-1)&&(s+="/"),m=t.join(process.cwd(),"src",s,d)),p=c.replace(/Controller$/,""),o.next=10,l.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!=i?i:p+"s").where("table_schema",u);case 10:y=o.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),o=t[0],c=t[1];return[r.toCamelCase(o),a(o,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 ")),w='\nimport NotFoundException from "@exception/NotFoundException";\nimport '.concat(p,' from "@model/').concat(p,'";\nimport ').concat(p,'Resource from "@resource/').concat(p,'Resource";\nimport express from "express";\n\nconst ').concat(c," = express.Router();\n\n").concat(c,'.get("/", async (req, res, next) => {\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").concat(c,'.get("/:id", async (req, res, next) => {\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").concat(c,'.post("/", async (req, res, next) => {\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").concat(c,'.put("/", async (req, res, next) => {\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").concat(c,'.delete("/:id", async (req, res, next) => {\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\nexport default ").concat(c,";\n"),g=t.dirname(m),n.existsSync(g)||n.mkdirSync(g,{recursive:!0}),n.writeFile(m,w,(function(e){e?console.error("Gagal membuat file:",e):console.log("File ".concat(d," berhasil dibuat di ").concat(m))})),o.next=24;break;case 21:o.prev=21,o.t0=o.catch(1),console.error("Gagal mendapatkan informasi kolom:",o.t0);case 24:return o.prev=24,o.finish(24);case 26:case"end":return o.stop()}}),o,null,[[1,21,24,26]])})));return function(e,n,t,r){return o.apply(this,arguments)}}();module.exports=o;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frgen",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "helper generator",
5
5
  "main": "lib/",
6
6
  "types": "lib/index.d.ts",