@egi/smart-db 2.3.9 → 2.3.11
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 +1 @@
|
|
|
1
|
-
import fs from"fs";import _ from"lodash";import process from"process";import{SmartDbBetterSqlite3}from"../drivers/smart-db-better-sqlite3";import{SmartDbCoreTableModel}from"../models/smart-db-core-table-model";import{SqliteMasterModel}from"../models/sqlite-master-model";import{IN,NE}from"../smart-db-globals";function getType(e){let t;return t=e?e.match(/varchar/i)||e.match(/text/i)?"string":e.match(/integer\(1\)/i)?"boolean":e.match(/date/i)?"Date":e.match(/integer/i)||e.match(/number/i)?"number":e||"SqlValueType":"SqlValueType",t}function extractDbApi(e){console.log(`extract interface from '${e.db.getDbConnector()}' to '${e.modelDirectory}'`),fs.existsSync(e.modelDirectory)||fs.mkdirSync(e.modelDirectory,{recursive:!0});const t=e.db.getAllSync(SqliteMasterModel,{type:IN(["table","view"]),name:NE("sqlite_sequence")});if(!1===t)console.error(e.db.getLastError()),process.abort();else{let n=[];if(!e.includeCoreTables&&e.db.existsSync(SmartDbCoreTableModel)){const t=e.db.getAllSync(SmartDbCoreTableModel);if(!1===t)throw e.db.getLastError();t.forEach(e=>{n.push(e.name)})}const r=[];t.forEach(t=>{if(!n.includes(t.name)){const n=t.name[0].toUpperCase()+_.camelCase(t.name.substr(1))+"Model";r.push(n);const a=[];e.db.getTableInfo(t.name).then(r=>{const s=r.fields;let
|
|
1
|
+
import fs from"fs";import _ from"lodash";import process from"process";import{SmartDbBetterSqlite3}from"../drivers/smart-db-better-sqlite3";import{SmartDbCoreTableModel}from"../models/smart-db-core-table-model";import{SqliteMasterModel}from"../models/sqlite-master-model";import{IN,NE}from"../smart-db-globals";function getType(e){let t;return t=e?e.match(/varchar/i)||e.match(/text/i)?"string":e.match(/integer\(1\)/i)?"boolean":e.match(/date/i)?"Date":e.match(/integer/i)||e.match(/number/i)?"number":e||"SqlValueType":"SqlValueType",t}function extractDbApi(e){console.log(`extract interface from '${e.db.getDbConnector()}' to '${e.modelDirectory}'`),fs.existsSync(e.modelDirectory)||fs.mkdirSync(e.modelDirectory,{recursive:!0});const t=e.db.getAllSync(SqliteMasterModel,{type:IN(["table","view"]),name:NE("sqlite_sequence")});if(!1===t)console.error(e.db.getLastError()),process.abort();else{let n=[];if(!e.includeCoreTables&&e.db.existsSync(SmartDbCoreTableModel)){const t=e.db.getAllSync(SmartDbCoreTableModel);if(!1===t)throw e.db.getLastError();t.forEach(e=>{n.push(e.name)})}const r=[];t.forEach(t=>{if(!n.includes(t.name)){const n=t.name[0].toUpperCase()+_.camelCase(t.name.substr(1))+"Model";r.push(n);const a=[];e.db.getTableInfo(t.name).then(r=>{const s=r.fields;let i=!1,o="",l="/* eslint-disable @typescript-eslint/member-ordering */\n";if(l+="// noinspection JSUnusedGlobalSymbols\n\n",s){l+=`import {AbstractModel, GenericModelData, ModelAttributeMap} from "${e.abstractModelModule}";\n`,o+=`export interface ${n}Data extends GenericModelData {\n`;let r,c="",m="",b=!1;const p=[];s.forEach(e=>{const t=e.name.match(/([a-z0-9]{1,4})_/i);e.isPk&&(c=e.name,t&&(m=t[1])),t?p.includes(t[1])||p.push(t[1]):b=!0}),m||b||1!=p.length||(m=p[0]),m&&(r=new RegExp(`^${m}_(.*)$`));const d={};s.forEach(n=>{const s=getType(n.type);let c;i||"SqlValueType"!=s||(l+=`import {SqlValueType} from "${e.smartDbInterfaces}";\n`,i=!0);const m=r&&n.name.match(r);c=m?_.camelCase(m[1]):_.camelCase(n.name);let b=!0;c!=n.name&&(a.push({name:c,attribute:c,type:s,typeScriptStyle:b,alias:n.name}),b=!1),a.push({name:n.name,attribute:c,type:s,physical:void 0!==n.cid,typeScriptStyle:b}),d[c]=s,"user"==t.name&&"name"==c&&(o+=" // @ts-ignore"),o+=` ${c}?: ${s};\n`}),o+="}\n\n",o+=`export class ${n} extends AbstractModel<${n}, ${n}Data> {\n`,_.forEach(d,(e,t)=>{o+=` private _${t}?: ${e};\n`}),"user"==t.name&&(o+=" private _hash?: string;\n",o+=" private _privileges?: string[];\n"),o+="\n",o+=" static readonly attributeMap: ModelAttributeMap = {\n",a.forEach((e,t)=>{t>0&&(o+=",\n"),o+=` ${e.name}: {\n`,e.physical&&(o+=" physical: true,\n"),e.alias&&(o+=` alias: "${e.alias}",\n`),e.virtual&&(o+=" virtual: true,\n"),e.typeScriptStyle&&(o+=" typeScriptStyle: true,\n"),o+=` type: "${e.type}",\n`,o+=` attribute: "_${e.attribute}"\n`,o+=" }"}),o+="\n",o+=" };\n",o+="\n",o+=" static getClassName(): string {\n",o+=` return "${n}";\n`,o+=" }\n",o+="\n",o+=" static getTableName(): string {\n",o+=` return "${t.name}";\n`,o+=" }\n",o+="\n",o+=" static getPrimaryKey(): string {\n",o+=` return "${c}";\n`,o+=" }\n",o+="\n",o+=` static from(other: ${n} | ${n}Data): ${n} {\n`,o+=` let value: ${n} = null;\n`,o+=" if (other) {\n",o+=` value = new ${n}();\n`,o+=` if (other instanceof ${n}) {\n`,o+=" Object.assign(value, other);\n",o+=" } else {\n",o+=" value.assign(other);\n",o+=" }\n",o+=" }\n",o+=" return value;\n",o+=" }\n",o+="\n",o+=` constructor(data?: ${n} | ${n}Data) {\n`,o+=" super();\n",o+=" if (data) {\n",o+=" this.assign(data);\n",o+=" }\n",o+=" }\n",o+="\n",o+=` public clone(): ${n} {\n`,o+=` return ${n}.from(this);\n`,o+=" }\n",o+="\n",o+=" public getClassName(): string {\n",o+=` return "${n}";\n`,o+=" }\n",o+="\n",o+=" public getTableName(): string {\n",o+=` return "${t.name}";\n`,o+=" }\n",o+="\n",o+=" public getPrimaryKey(): string {\n",o+=` return "${c}";\n`,o+=" }\n",o+="\n",o+=" public getAttributeMap(): ModelAttributeMap {\n",o+=` return ${n}.attributeMap;\n`,o+=" }\n",a.forEach(e=>{let t;o+="\n",t=e.name.length<3||e.name.match(/_/)?"FunctionNamingConventionJS":"",""!==t&&(o+=` // noinspection ${t}\n`),o+=` get ${e.name}(): ${e.type} {\n`,o+=` return this._${e.attribute};\n`,o+=" }\n\n",""!==t&&(o+=` // noinspection ${t}\n`),o+=` set ${e.name}(${e.attribute}: ${e.type}) {\n`,o+=` this._${e.attribute} = ${e.attribute};\n`,o+=" }\n"}),"user"==t.name&&(o+="\n",o+=" get hash(): string[] {\n",o+=" return this._hash;\n",o+=" }\n\n",o+=" set privileges(hash: string) {\n",o+=" this._hash = hash;\n",o+=" }\n",o+="\n",o+=" get privileges(): string[] {\n",o+=" return this._privileges;\n",o+=" }\n\n",o+=" set privileges(p: string[]) {\n",o+=" this._privileges = p;\n",o+=" }\n"),o+="}\n",o=l+"\n"+o;const u=t.name.replace(/[^[a-z0-9]/g,"-")+"-model.ts";fs.writeFileSync(`${e.modelDirectory}/${u}`,o)}else console.log("error: table has no fields")}).catch(e=>{console.error(e),process.abort()})}});let a=`import {AbstractModel} from "${e.abstractModelModule}";\n`;_.forEach(r,e=>{a+=`import {${e}} from "./${_.kebabCase(e)}";\n`}),a+="\n",a+="interface SmartDbDictionaryEntry {\n",a+=" cls: "+r.map(e=>"typeof "+e).join(" |\n "),a+=";\n",a+="}\n\n",a+="export class SmartDbDictionary {\n",a+=" static models: { [relation: string]: SmartDbDictionaryEntry; } = {\n",_.forEach(r,e=>{a+=` ${e}: {\n`,a+=` cls: ${e}\n`,a+=" },\n"}),a=a.substring(0,a.length-2)+"\n",a+=" };\n",a+="}\n",fs.writeFileSync(e.modelDirectory+"/smart-db-dictionary.ts",a)}e.db.closeSync()}(()=>{if(4==process.argv.length){extractDbApi({modelDirectory:process.argv[3],smartDbInterfaces:"@egi/smart-db",abstractModelModule:"@egi/smart-db",db:new SmartDbBetterSqlite3(process.argv[2],{verbose:(e,...t)=>{console.log("DB LOG",e,...t)}})})}else if(2==process.argv.length&&fs.existsSync("src/helpers/extract-db-api.ts")){const e=`/tmp/smart-db-core.${process.pid}.sqlite3`,t={includeCoreTables:!0,modelDirectory:"src/models",smartDbInterfaces:"../smart-db-interfaces",abstractModelModule:"./abstract-models",db:new SmartDbBetterSqlite3(e,{verbose:(e,...t)=>{console.log("DB LOG",e,...t)}})};t.db.initDb({module:"smart-db-core",sqlFilesDirectory:"assets/sqlite3"}).then(()=>{extractDbApi(t),fs.unlinkSync(e),t.db.closeSync()})}else console.log("usage: extract-db-api {db-connector|path-to-db} {target-models-directory}")})();
|