@egi/smart-db
Version:
Unified Smart DB Access
1 lines • 1.68 kB
JavaScript
import _ from"lodash";import Sqlite3 from"sqlite3";import{SqliteMasterModel}from"../models/sqlite-master-model";import{SmartDb}from"../smart-db";export class SmartDbSqlite3 extends SmartDb{constructor(t,r){_.isString(t)?(super(t),this.db=new Sqlite3.Database(t,r&&r.mode,r&&r.callback)):(super(null),this.db=t)}close(){return new Promise(((t,r)=>{try{this.smartDbLog.setDb(null),this.db.close((r=>{this.isReady=!1,r?(this.lastError=r,t(!1)):t(!0)}))}catch(t){this.lastError=t,r(t)}}))}commit(){return this.exec("commit")}exec(t){return new Promise(((r,e)=>{try{this.db.exec(t,(t=>{t?(this.lastError=t,e(t)):r()}))}catch(t){this.lastError=t,e(t)}}))}exists(t,r,e){return new Promise(((s,a)=>{try{const i=_.isString(t)?t:t.getTableName();this.getFirst(SqliteMasterModel,{name:i,type:r,tblName:e}).then((t=>{s(!!t)})).catch((t=>{a(t)}))}catch(t){this.lastError=t,a(t)}}))}getDatabaseType(){return"sqlite3"}getTableInfo(t){throw new Error("Method not implemented (getTableInfo)")}rollback(){return this.exec("rollback")}statementRun(t){return new Promise(((r,e)=>{try{const s=this;this.db.run(t.sql,t.values,(function(t){t?(s.lastError=t,e(t)):r({changes:this.changes,affected:this.changes,lastId:this.lastID})}))}catch(t){this.lastError=t,e(t)}}))}statementGet(t){return new Promise(((r,e)=>{try{const s=this;this.db.prepare(t.sql,(function(a){a?e(a):this.get(t.values,(function(t,a){t?(s.lastError=t,e(t)):r(a)}))}))}catch(t){this.lastError=t,e(t)}}))}statementGetAll(t){return new Promise(((r,e)=>{try{const s=this;this.db.prepare(t.sql,(function(a){a?(s.lastError=a,e(a)):this.all(t.values,(function(t,a){t?(s.lastError=t,e(t)):r(a)}))}))}catch(t){this.lastError=t,e(t)}}))}}