frgen 0.0.1
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/LICENSE +24 -0
- package/README.md +3 -0
- package/lib/_virtual/_rollupPluginBabelHelpers.js +2 -0
- package/lib/main.js +2 -0
- package/lib/make_controller.js +2 -0
- package/lib/make_crud.js +2 -0
- package/lib/make_model.js +2 -0
- package/lib/make_resource.js +2 -0
- package/lib/utils.js +2 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";function t(t,r){var e=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=e){var n,o,i,a,c=[],u=!0,l=!1;try{if(i=(e=e.call(t)).next,0===r){if(Object(e)!==e)return;u=!1}else for(;!(u=(n=i.call(e)).done)&&(c.push(n.value),c.length!==r);u=!0);}catch(t){l=!0,o=t}finally{try{if(!u&&null!=e.return&&(a=e.return(),Object(a)!==a))return}finally{if(l)throw o}}return c}}function r(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable}))),e.push.apply(e,n)}return e}function e(){e=function(){return r};var t,r={},n=Object.prototype,o=n.hasOwnProperty,i=Object.defineProperty||function(t,r,e){t[r]=e.value},a="function"==typeof Symbol?Symbol:{},c=a.iterator||"@@iterator",u=a.asyncIterator||"@@asyncIterator",l=a.toStringTag||"@@toStringTag";function f(t,r,e){return Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}),t[r]}try{f({},"")}catch(t){f=function(t,r,e){return t[r]=e}}function s(t,r,e,n){var o=r&&r.prototype instanceof m?r:m,a=Object.create(o.prototype),c=new I(n||[]);return i(a,"_invoke",{value:S(t,e,c)}),a}function h(t,r,e){try{return{type:"normal",arg:t.call(r,e)}}catch(t){return{type:"throw",arg:t}}}r.wrap=s;var p="suspendedStart",y="suspendedYield",v="executing",d="completed",g={};function m(){}function b(){}function w(){}var x={};f(x,c,(function(){return this}));var O=Object.getPrototypeOf,j=O&&O(O(k([])));j&&j!==n&&o.call(j,c)&&(x=j);var L=w.prototype=m.prototype=Object.create(x);function E(t){["next","throw","return"].forEach((function(r){f(t,r,(function(t){return this._invoke(r,t)}))}))}function P(t,r){function e(n,i,a,c){var u=h(t[n],t,i);if("throw"!==u.type){var l=u.arg,f=l.value;return f&&"object"==typeof f&&o.call(f,"__await")?r.resolve(f.__await).then((function(t){e("next",t,a,c)}),(function(t){e("throw",t,a,c)})):r.resolve(f).then((function(t){l.value=t,a(l)}),(function(t){return e("throw",t,a,c)}))}c(u.arg)}var n;i(this,"_invoke",{value:function(t,o){function i(){return new r((function(r,n){e(t,o,r,n)}))}return n=n?n.then(i,i):i()}})}function S(r,e,n){var o=p;return function(i,a){if(o===v)throw Error("Generator is already running");if(o===d){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var u=_(c,n);if(u){if(u===g)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=v;var l=h(r,e,n);if("normal"===l.type){if(o=n.done?d:y,l.arg===g)continue;return{value:l.arg,done:n.done}}"throw"===l.type&&(o=d,n.method="throw",n.arg=l.arg)}}}function _(r,e){var n=e.method,o=r.iterator[n];if(o===t)return e.delegate=null,"throw"===n&&r.iterator.return&&(e.method="return",e.arg=t,_(r,e),"throw"===e.method)||"return"!==n&&(e.method="throw",e.arg=new TypeError("The iterator does not provide a '"+n+"' method")),g;var i=h(o,r.iterator,e.arg);if("throw"===i.type)return e.method="throw",e.arg=i.arg,e.delegate=null,g;var a=i.arg;return a?a.done?(e[r.resultName]=a.value,e.next=r.nextLoc,"return"!==e.method&&(e.method="next",e.arg=t),e.delegate=null,g):a:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,g)}function A(t){var r={tryLoc:t[0]};1 in t&&(r.catchLoc=t[1]),2 in t&&(r.finallyLoc=t[2],r.afterLoc=t[3]),this.tryEntries.push(r)}function T(t){var r=t.completion||{};r.type="normal",delete r.arg,t.completion=r}function I(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(A,this),this.reset(!0)}function k(r){if(r||""===r){var e=r[c];if(e)return e.call(r);if("function"==typeof r.next)return r;if(!isNaN(r.length)){var n=-1,i=function e(){for(;++n<r.length;)if(o.call(r,n))return e.value=r[n],e.done=!1,e;return e.value=t,e.done=!0,e};return i.next=i}}throw new TypeError(typeof r+" is not iterable")}return b.prototype=w,i(L,"constructor",{value:w,configurable:!0}),i(w,"constructor",{value:b,configurable:!0}),b.displayName=f(w,l,"GeneratorFunction"),r.isGeneratorFunction=function(t){var r="function"==typeof t&&t.constructor;return!!r&&(r===b||"GeneratorFunction"===(r.displayName||r.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,w):(t.__proto__=w,f(t,l,"GeneratorFunction")),t.prototype=Object.create(L),t},r.awrap=function(t){return{__await:t}},E(P.prototype),f(P.prototype,u,(function(){return this})),r.AsyncIterator=P,r.async=function(t,e,n,o,i){void 0===i&&(i=Promise);var a=new P(s(t,e,n,o),i);return r.isGeneratorFunction(e)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},E(L),f(L,l,"Generator"),f(L,c,(function(){return this})),f(L,"toString",(function(){return"[object Generator]"})),r.keys=function(t){var r=Object(t),e=[];for(var n in r)e.push(n);return e.reverse(),function t(){for(;e.length;){var n=e.pop();if(n in r)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=k,I.prototype={constructor:I,reset:function(r){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(T),!r)for(var e in this)"t"===e.charAt(0)&&o.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(r){if(this.done)throw r;var e=this;function n(n,o){return c.type="throw",c.arg=r,e.next=n,o&&(e.method="next",e.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=o.call(a,"catchLoc"),l=o.call(a,"finallyLoc");if(u&&l){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!l)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,r){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc<=this.prev&&o.call(n,"finallyLoc")&&this.prev<n.finallyLoc){var i=n;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=r&&r<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=r,i?(this.method="next",this.next=i.finallyLoc,g):this.complete(a)},complete:function(t,r){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&r&&(this.next=r),g},finish:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.finallyLoc===t)return this.complete(e.completion,e.afterLoc),T(e),g}},catch:function(t){for(var r=this.tryEntries.length-1;r>=0;--r){var e=this.tryEntries[r];if(e.tryLoc===t){var n=e.completion;if("throw"===n.type){var o=n.arg;T(e)}return o}}throw Error("illegal catch attempt")},delegateYield:function(r,e,n){return this.delegate={iterator:k(r),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=t),g}},r}function n(t,r){if("object"!=typeof t||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var n=e.call(t,r||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}function o(t){var r=n(t,"string");return"symbol"==typeof r?r:r+""}function i(t,r,e,n,o,i,a){try{var c=t[i](a),u=c.value}catch(t){return void e(t)}c.done?r(u):Promise.resolve(u).then(n,o)}function a(t,r,e){return(r=o(r))in t?Object.defineProperty(t,r,{value:e,enumerable:!0,configurable:!0,writable:!0}):t[r]=e,t}function c(t){if(Array.isArray(t))return t}function u(t,r){if(t){if("string"==typeof t)return l(t,r);var e=Object.prototype.toString.call(t).slice(8,-1);return"Object"===e&&t.constructor&&(e=t.constructor.name),"Map"===e||"Set"===e?Array.from(t):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?l(t,r):void 0}}function l(t,r){(null==r||r>t.length)&&(r=t.length);for(var e=0,n=new Array(r);e<r;e++)n[e]=t[e];return n}function f(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}Object.defineProperty(exports,"__esModule",{value:!0}),exports.arrayLikeToArray=l,exports.arrayWithHoles=c,exports.asyncToGenerator=function(t){return function(){var r=this,e=arguments;return new Promise((function(n,o){var a=t.apply(r,e);function c(t){i(a,n,o,c,u,"next",t)}function u(t){i(a,n,o,c,u,"throw",t)}c(void 0)}))}},exports.createForOfIteratorHelper=function(t,r){var e="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!e){if(Array.isArray(t)||(e=u(t))||r){e&&(t=e);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,a=!0,c=!1;return{s:function(){e=e.call(t)},n:function(){var t=e.next();return a=t.done,t},e:function(t){c=!0,i=t},f:function(){try{a||null==e.return||e.return()}finally{if(c)throw i}}}},exports.defineProperty=a,exports.iterableToArrayLimit=t,exports.nonIterableRest=f,exports.objectSpread2=function(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?r(Object(n),!0).forEach((function(r){a(t,r,n[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(r){Object.defineProperty(t,r,Object.getOwnPropertyDescriptor(n,r))}))}return t},exports.regeneratorRuntime=e,exports.slicedToArray=function(r,e){return c(r)||t(r,e)||u(r,e)||f()},exports.toPrimitive=n,exports.toPropertyKey=o,exports.unsupportedIterableToArray=u;
|
package/lib/main.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),r=require("./utils.js"),n=require("./make_model.js"),t=require("./make_resource.js"),a=require("./make_controller.js"),s=require("./make_crud.js"),c=process.argv[2],o=process.argv[3],u=process.argv[4],i=process.argv[5];c||(console.error("action undefined."),process.exit(1)),function(){var l=e.asyncToGenerator(e.regeneratorRuntime().mark((function l(){var k,p;return e.regeneratorRuntime().wrap((function(l){for(;;)switch(l.prev=l.next){case 0:return l.next=2,r.loadConfig();case 2:return k=l.sent,p=require("knex")(e.objectSpread2(e.objectSpread2({},k),{},{debug:!0})),l.prev=4,l.next=7,r.checkDatabaseConnection(p);case 7:if("make:model"!==c){l.next=12;break}return l.next=10,n(o,u,p);case 10:l.next=25;break;case 12:if("make:resource"!==c){l.next=17;break}return l.next=15,t(o,u,p);case 15:l.next=25;break;case 17:if("make:controller"!==c){l.next=22;break}return l.next=20,a(o,u,i,p);case 20:l.next=25;break;case 22:if("make:crud"!==c){l.next=25;break}return l.next=25,s(i,p);case 25:l.next=31;break;case 27:l.prev=27,l.t0=l.catch(4),console.log(l.t0),process.exit(1);case 31:return l.prev=31,p.destroy(),l.finish(31);case 34:case"end":return l.stop()}}),l,null,[[4,27,31,34]])})));return function(){return l.apply(this,arguments)}}()();
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),n=require("fs"),r=require("path"),t=require("./utils.js");function o(e,n){var r=n.type,o=n.nullable,c=["yup"],a=["integer","bigint","smallint","tinyint","numeric","decimal","float"].includes(r),i=["varchar","text","string","char","character varying","uuid","date","datetime","timestamp","timestamp with time zone"].includes(r),s=["boolean"].includes(r);return a?c.push("number()"):i?c.push("string()"):s?c.push("boolean()"):c.push("mix()"),o?c.push("optional()"):c.push("required()"),c.push("label('".concat(e.replaceAll("_"," "),"')")),"".concat(t.toCamelCase(e),":").concat(c.join("."))}var c=function(){var c=e.asyncToGenerator(e.regeneratorRuntime().mark((function c(a,i,s,u){var l,d,p,m,y,f,h,x,q;return e.regeneratorRuntime().wrap((function(c){for(;;)switch(c.prev=c.next){case 0:return c.prev=0,a||(console.error("class name undefined"),process.exit(1)),i||(console.error("table name undefined"),process.exit(1)),l="".concat(a,".ts"),d=r.join(process.cwd(),"src","app","http","controller",l),s&&("/"!==s.charAt(s.length-1)&&(s+="/"),d=r.join(process.cwd(),"src",s,l)),p=a.replace(/Controller$/,""),c.next=9,u.from(null!=i?i:p+"s").columnInfo();case 9:m=c.sent,y="",f="",Object.keys(m).length&&(h=Object.entries(m).filter((function(n){var r=e.slicedToArray(n,1)[0];return!["created_at","updated_at","createdAt","updatedAt"].includes(r)})).map((function(n){var r=e.slicedToArray(n,2),c=r[0],a=r[1];return[t.toCamelCase(c),o(c,a)]})),f="\n const validated = await req.validation((yup) => yup.object({\n ".concat(h.map((function(n){var r=e.slicedToArray(n,2);return r[0],r[1]})).join(",\n"),"\n }));\n "),y="\n const validated = await req.validation((yup) => yup.object({\n ".concat(h.filter((function(n){return"id"!==e.slicedToArray(n,1)[0]})).map((function(n){var r=e.slicedToArray(n,2);return r[0],r[1]})).join(",\n"),"\n }));\n ")),x='\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(a," = express.Router();\n\n").concat(a,'.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(a,'.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(a,'.post("/", async (req, res, next) => {\n try {\n ').concat(y,"\n const model = await ").concat(p,".query().insertAndFetch(").concat(""===y?"req.body":"validated",");\n res.json(new ").concat(p,"Resource(model, true));\n } catch (error) {\n next(error);\n }\n});\n\n").concat(a,'.put("/", async (req, res, next) => {\n try {\n ').concat(f,"\n const model = await ").concat(p,".query().findById(").concat(""===f?"req.body.id":"validated.id",');\n if (!model) {\n throw new NotFoundException("').concat(p,'");\n }\n await model.$query().updateAndFetch(').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(a,'.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(a,";\n"),q=r.dirname(d),n.existsSync(q)||n.mkdirSync(q,{recursive:!0}),n.writeFile(d,x,(function(e){e?console.error("Gagal membuat file:",e):console.log("File ".concat(l," berhasil dibuat di ").concat(d))})),c.next=22;break;case 19:c.prev=19,c.t0=c.catch(0),console.error("Gagal mendapatkan informasi kolom:",c.t0);case 22:return c.prev=22,c.finish(22);case 24:case"end":return c.stop()}}),c,null,[[0,19,22,24]])})));return function(e,n,r,t){return c.apply(this,arguments)}}();module.exports=c;
|
package/lib/make_crud.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),r=require("./make_controller.js"),t=require("./make_model.js"),a=require("./make_resource.js"),n=require("./utils.js"),s=function(){var s=e.asyncToGenerator(e.regeneratorRuntime().mark((function s(c,o){var i,u,l,m,p,f,v,_;return e.regeneratorRuntime().wrap((function(s){for(;;)switch(s.prev=s.next){case 0:return s.prev=0,s.next=3,o.select("*").from("information_schema.tables").whereNotIn("table_schema",["information_schema","pg_catalog"]).whereNotIn("table_name",["migrations","migrations_lock"]).where("table_type","BASE TABLE");case 3:i=s.sent,u=e.createForOfIteratorHelper(i),s.prev=5,u.s();case 7:if((l=u.n()).done){s.next=21;break}return m=l.value,p=m.table_name,f=n.capitalize(n.toCamelCase(n.removePluralSuffix(p))),v="".concat(f,"Resource"),_="".concat(f,"Controller"),s.next=15,t(f,p,o);case 15:return s.next=17,a(v,p,o);case 17:return s.next=19,r(_,p,c,o);case 19:s.next=7;break;case 21:s.next=26;break;case 23:s.prev=23,s.t0=s.catch(5),u.e(s.t0);case 26:return s.prev=26,u.f(),s.finish(26);case 29:s.next=34;break;case 31:s.prev=31,s.t1=s.catch(0),console.log(s.t1);case 34:return s.prev=34,s.finish(34);case 36:case"end":return s.stop()}}),s,null,[[0,31,34,36],[5,23,26,29]])})));return function(e,r){return s.apply(this,arguments)}}();module.exports=s;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),n=require("fs"),a=require("path"),t=require("./utils.js");function r(e,n){return o.apply(this,arguments)}function o(){return(o=e.asyncToGenerator(e.regeneratorRuntime().mark((function n(a,r){var o,c,i;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=3,r.select("tc.table_name","kcu.column_name","ccu.table_name as foreign_table_name","ccu.column_name as foreign_column_name").from("information_schema.table_constraints as tc").join("information_schema.key_column_usage as kcu","tc.constraint_name","kcu.constraint_name").join("information_schema.constraint_column_usage as ccu","ccu.constraint_name","tc.constraint_name").where("tc.constraint_type","FOREIGN KEY").andWhere("tc.table_name",a);case 3:return o=e.sent,e.next=6,r.select("tc.table_name","kcu.column_name","ccu.table_name as foreign_table_name","ccu.column_name as foreign_column_name").from("information_schema.table_constraints as tc").join("information_schema.key_column_usage as kcu","tc.constraint_name","kcu.constraint_name").join("information_schema.constraint_column_usage as ccu","ccu.constraint_name","tc.constraint_name").where("tc.constraint_type","FOREIGN KEY").andWhere("ccu.table_name",a);case 6:return c=e.sent,i={},o.forEach((function(e){var n=e.foreign_table_name;i[t.toCamelCase(t.removePluralSuffix(n))]={relation:"BelongsToOneRelation",relatedTable:e.foreign_table_name,foreignKey:e.column_name,localKey:e.foreign_column_name}})),c.forEach((function(e){var n=e.table_name;i[t.toCamelCase(t.removePluralSuffix(n))]={relation:"HasOneRelation",relatedTable:e.table_name,foreignKey:e.foreign_column_name,localKey:e.column_name},i[t.toCamelCase(n)]={relation:"HasManyRelation",relatedTable:e.table_name,foreignKey:e.foreign_column_name,localKey:e.column_name}})),e.abrupt("return",i);case 11:case"end":return e.stop()}}),n)})))).apply(this,arguments)}function c(n){return Array.from(new Set(Object.entries(n).map((function(n){var a=e.slicedToArray(n,2);a[0];var r=a[1].relatedTable;return"import ".concat(t.capitalize(t.toCamelCase(t.removePluralSuffix(r)))," from './").concat(t.capitalize(t.toCamelCase(t.removePluralSuffix(r))),"';")})))).join("\n")}function i(n,a){var r=Object.entries(n).map((function(n){var r=e.slicedToArray(n,2),o=r[0],c=r[1],i=c.relation,l=c.relatedTable,s=c.foreignKey,m=c.localKey;return"\n ".concat(o,": {\n relation: Model.").concat(i,",\n modelClass: ").concat(t.capitalize(t.toCamelCase(t.removePluralSuffix(l))),",\n join: {\n ").concat("ManyToManyRelation"===i?"\n from: '".concat(m,"',\n through: {\n from: '").concat(s,"',\n to: '").concat(s,"'\n },\n to: '").concat(m,"'"):"\n from: '".concat(a,".").concat(s,"',\n to: '").concat(l,".").concat(m,"'"),"\n }\n }")})).join(",");return"\nstatic get relationMappings() {\nreturn {".concat(r,"\n};\n}")}var l=function(){var o=e.asyncToGenerator(e.regeneratorRuntime().mark((function o(l,s,m){var u,f,_,p,d,b,g,y,h,v,C,x=arguments;return e.regeneratorRuntime().wrap((function(o){for(;;)switch(o.prev=o.next){case 0:return u=x.length>3&&void 0!==x[3]?x[3]:"public",o.prev=1,l||(console.error("class name undefined"),process.exit(1)),s||(console.error("table name undefined"),process.exit(1)),f="".concat(l,".ts"),_=a.join(process.cwd(),"src","app","model",f),o.next=8,m.from(null!=s?s:l.toLowerCase()+"s").columnInfo();case 8:return p=o.sent,o.next=11,r(s,m,u);case 11:d=o.sent,b=Object.entries(p).map((function(n){var a,r=e.slicedToArray(n,2),o=r[0],c=r[1],i=t.toCamelCase(o),l=(a=c.type,["integer","bigint","smallint","tinyint","numeric","decimal","float"].includes(a)?"number":["varchar","text","string","char","character varying","uuid"].includes(a)?"string":["boolean"].includes(a)?"boolean":["date","datetime","timestamp","timestamp with time zone"].includes(a)?"Date":"any");return" public ".concat(i,": ").concat(l,";")})).join("\n"),g='\n static get tableName() {\n return "'.concat(l.toLowerCase(),'s";\n }\n '),s&&(g='\n static get tableName() {\n return "'.concat(s,'";\n }\n ')),y=d?c(d):"",h=d?i(d,s):"",v='\n import Model from "@helper/Model";\n import { snakeCaseMappers } from "objection";\n '.concat(y,"\n \n export default class ").concat(l," extends Model {\n \n ").concat(b,"\n \n ").concat(g,'\n \n static get idColumn() {\n return "id";\n }\n \n static get columnNameMappers() {\n return snakeCaseMappers();\n }\n \n ').concat(h,"\n }"),C=a.dirname(_),n.existsSync(C)||n.mkdirSync(C,{recursive:!0}),n.writeFile(_,v,(function(e){e?console.error("Gagal membuat file:",e):console.log("File ".concat(f," berhasil dibuat di ").concat(_))})),o.next=26;break;case 23:o.prev=23,o.t0=o.catch(1),console.error("Gagal mendapatkan informasi kolom:",o.t0);case 26:return o.prev=26,o.finish(26);case 28:case"end":return o.stop()}}),o,null,[[1,23,26,28]])})));return function(e,n,a){return o.apply(this,arguments)}}();module.exports=l;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";var e=require("./_virtual/_rollupPluginBabelHelpers.js"),r=require("fs"),n=require("path"),t=require("./utils.js"),o=function(){var o=e.asyncToGenerator(e.regeneratorRuntime().mark((function o(a,c,s){var i,u,l,p,f,m;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,a||(console.error("class name undefined"),process.exit(1)),c||(console.error("table name undefined"),process.exit(1)),i="".concat(a.replace("Resource",""),"Resource.ts"),u=n.join(process.cwd(),"src","app","http","resource",i),e.next=7,s.from(c).columnInfo();case 7:l=e.sent,p=Object.keys(l).filter((function(e){return"created_at"!==e&&"updated_at"!==e})).map((function(e){return"".concat(t.toCamelCase(e),": this.").concat(t.toCamelCase(e))})).join(",\n"),f='\nimport Resource from "frexp/lib/Resource";\n\nexport default class '.concat(a," extends Resource {\n toArray() {\n return {\n").concat(p,"\n };\n }\n}"),m=n.dirname(u),r.existsSync(m)||r.mkdirSync(m,{recursive:!0}),r.writeFile(u,f,(function(e){e?console.error("Gagal membuat file:",e):console.log("File ".concat(i," berhasil dibuat di ").concat(u))})),e.next=18;break;case 15:e.prev=15,e.t0=e.catch(0),console.error("Gagal mendapatkan informasi kolom:",e.t0);case 18:return e.prev=18,e.finish(18);case 20:case"end":return e.stop()}}),o,null,[[0,15,18,20]])})));return function(e,r,n){return o.apply(this,arguments)}}();module.exports=o;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./_virtual/_rollupPluginBabelHelpers.js"),r=require("path"),t=function(){var r=e.asyncToGenerator(e.regeneratorRuntime().mark((function r(t){var n,a=arguments;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=a.length>1&&void 0!==a[1]?a[1]:"public",e.prev=1,e.next=4,t.schema.withSchema(n).raw("SELECT 1");case 4:console.log("connection success"),e.next=11;break;case 7:e.prev=7,e.t0=e.catch(1),console.error("Gagal terhubung ke database:",e.t0),process.exit(1);case 11:case"end":return e.stop()}}),r,null,[[1,7]])})));return function(e){return r.apply(this,arguments)}}(),n=function(){var t=e.asyncToGenerator(e.regeneratorRuntime().mark((function t(){var n,a,o,s,c=arguments;return e.regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=c.length>0&&void 0!==c[0]?c[0]:"development",e.prev=1,a=process.cwd(),o=r.resolve(a,"knexfile.js"),e.next=6,import(o);case 6:return s=e.sent,e.abrupt("return",s.default[n]);case 10:e.prev=10,e.t0=e.catch(1),console.log(e.t0);case 13:case"end":return e.stop()}}),t,null,[[1,10]])})));return function(){return t.apply(this,arguments)}}();exports.capitalize=function(e){return e.charAt(0).toUpperCase()+e.slice(1)},exports.checkDatabaseConnection=t,exports.loadConfig=n,exports.removePluralSuffix=function(e){if("ss"===e.toLowerCase().substring(e.length-2))return e;for(var r=0,t=["s","es","ies"];r<t.length;r++){var n=t[r];if(e.endsWith(n))return e.slice(0,-n.length)}return e},exports.toCamelCase=function(e){return e.replace(/_([a-z])/g,(function(e,r){return r.toUpperCase()}))};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "frgen",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "helper generator",
|
|
5
|
+
"main": "lib/",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
+
"build": "rollup -c",
|
|
10
|
+
"rebuild": "rollup -c"
|
|
11
|
+
},
|
|
12
|
+
"bin": {
|
|
13
|
+
"frgen": "./lib/main.js"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/fajarrh//express-knex-objection-starter.git"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"express",
|
|
21
|
+
"node",
|
|
22
|
+
"knex",
|
|
23
|
+
"postgres",
|
|
24
|
+
"generator",
|
|
25
|
+
"api",
|
|
26
|
+
"RESTful"
|
|
27
|
+
],
|
|
28
|
+
"author": "fajarrizkyhidayat",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/fajarrh//express-knex-objection-starter/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://fajarrh.github.io//express-knex-objection-starter/",
|
|
34
|
+
"files": [
|
|
35
|
+
"lib/**/*"
|
|
36
|
+
],
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@babel/core": "^7.24.4",
|
|
39
|
+
"@babel/plugin-transform-private-methods": "^7.25.9",
|
|
40
|
+
"@babel/preset-env": "^7.24.4",
|
|
41
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
42
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
43
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
44
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
45
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
46
|
+
"knex": "^3.1.0",
|
|
47
|
+
"pg": "^8.13.1"
|
|
48
|
+
}
|
|
49
|
+
}
|