@egi/smart-db 2.0.4 → 2.2.0
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/assets/mysql/{database-init.sql → smart-db-core-init.sql} +0 -0
- package/assets/sqlite3/{database-init.sql → smart-db-core-init.sql} +0 -0
- package/bin/extract-db-api +19 -0
- package/drivers/smart-db-better-sqlite3.d.ts +11 -9
- package/drivers/smart-db-better-sqlite3.js +1 -1
- package/drivers/smart-db-mysql.d.ts +9 -7
- package/drivers/smart-db-mysql.js +1 -1
- package/drivers/smart-db-mysql2.d.ts +9 -7
- package/drivers/smart-db-mysql2.js +1 -1
- package/drivers/smart-db-sqlite3.d.ts +10 -8
- package/drivers/smart-db-sqlite3.js +1 -1
- package/helpers/{extract-db-interface.d.ts → extract-db-api.d.ts} +0 -0
- package/helpers/extract-db-api.js +1 -0
- package/models/abstract-model.d.ts +22 -0
- package/models/abstract-model.js +1 -0
- package/{model → models}/smart-db-core-table-model.d.ts +13 -11
- package/models/smart-db-core-table-model.js +1 -0
- package/{model → models}/smart-db-dictionary.d.ts +0 -2
- package/models/smart-db-dictionary.js +1 -0
- package/{model → models}/smart-db-log-model.d.ts +27 -25
- package/models/smart-db-log-model.js +1 -0
- package/{model → models}/smart-db-version-model.d.ts +29 -27
- package/models/smart-db-version-model.js +1 -0
- package/{model → models}/smart-db-version-view-model.d.ts +31 -30
- package/models/smart-db-version-view-model.js +1 -0
- package/{model → models}/sqlite-master-model.d.ts +16 -9
- package/models/sqlite-master-model.js +1 -0
- package/{model → models}/sqlite-sequence-model.d.ts +5 -5
- package/models/sqlite-sequence-model.js +1 -0
- package/package.json +54 -26
- package/smart-db-api.d.ts +12 -0
- package/smart-db-api.js +1 -0
- package/smart-db-globals.d.ts +22 -0
- package/smart-db-globals.js +1 -0
- package/smart-db-interfaces.d.ts +170 -0
- package/smart-db-interfaces.js +1 -0
- package/smart-db-log.d.ts +46 -20
- package/smart-db-log.js +1 -1
- package/smart-db-sql-build-data.d.ts +9 -0
- package/smart-db-sql-build-data.js +1 -0
- package/smart-db-upgrade-manager.d.ts +2 -2
- package/smart-db-upgrade-manager.js +1 -1
- package/smart-db.d.ts +44 -51
- package/smart-db.js +1 -1
- package/bin/extract-db-interface +0 -18
- package/helpers/extract-db-interface.js +0 -1
- package/index.d.ts +0 -10
- package/index.js +0 -1
- package/model/abstract-model.d.ts +0 -44
- package/model/abstract-model.js +0 -1
- package/model/smart-db-core-table-model.js +0 -1
- package/model/smart-db-dictionary.js +0 -1
- package/model/smart-db-log-model.js +0 -1
- package/model/smart-db-version-model.js +0 -1
- package/model/smart-db-version-view-model.js +0 -1
- package/model/sqlite-master-model.js +0 -1
- package/model/sqlite-sequence-model.js +0 -1
- package/smart-db-interface.d.ts +0 -173
- package/smart-db-interface.js +0 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import { SmartDbVersionViewModel } from "./models/smart-db-version-view-model";
|
|
1
2
|
import { SmartDb } from "./smart-db";
|
|
2
|
-
import {
|
|
3
|
-
import { SmartDbOptions } from "./smart-db-interface";
|
|
3
|
+
import { SmartDbOptions } from "./smart-db-interfaces";
|
|
4
4
|
export declare class SmartDbUpgradeManager {
|
|
5
5
|
private db;
|
|
6
6
|
private log;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import*as fs from"fs";import _ from"lodash";import{SmartDbVersionViewModel}from"./models/smart-db-version-view-model";import{SmartDbLogLocation}from"./smart-db-log";export class SmartDbUpgradeManager{constructor(e){this.db=e,this.log=e.getLogger()}hasDatabaseModule(e){return new Promise((t,a)=>{this.db.exists(SmartDbVersionViewModel).then(o=>{o?this.db.getFirst(SmartDbVersionViewModel,{module:e}).then(e=>{t(!!e)}).catch(e=>{a(e)}):t(!1)}).catch(e=>{a(e)})})}prepareDatabase(e){return new Promise((t,a)=>{try{const o=fs.existsSync(e.sqlFilesDirectory)&&fs.statSync(e.sqlFilesDirectory);if(o&&o.isDirectory()){const o=e.sqlFilesDirectory+`/${e.module}-init.sql`;fs.existsSync(o)?this.hasDatabaseModule(e.module).then(r=>{r?(this.log.logInfo(SmartDbLogLocation.UpgradeManager,"prepareDatabase",`database module '${e.module}' exists`),this.upgradeDatabase(e).then(a=>{this.log.logInfo(SmartDbLogLocation.UpgradeManager,"prepareDatabase",`connected to module '${e.module}' version ${a.versionString}`),t(a)}).catch(e=>{a(e)})):(this.log.setDbLogging(!1),this.log.logInfo(SmartDbLogLocation.UpgradeManager,"prepareDatabase",`database '${e.module}' doesn't exists - begin creation`),this.executeSqlScript(o).then(()=>{this.log.setDbLogging(!0),this.log.logInfo(SmartDbLogLocation.UpgradeManager,"prepareDatabase","database module creation complete"),this.hasDatabaseModule(e.module).then(o=>{o?(this.log.logInfo(SmartDbLogLocation.UpgradeManager,"prepareDatabase","successfully initialized database for new module "+e.module),this.upgradeDatabase(e).then(a=>{this.log.logInfo(SmartDbLogLocation.UpgradeManager,"prepareDatabase",`connected to module '${e.module}' version ${a.versionString}`),t(a)}).catch(e=>{a(e)})):a("error running database initialization script")}).catch(e=>{a(e)})}).catch(e=>{a(e)}))}).catch(e=>{a(e)}):a(`missing mandatory '${e.module}-init.sql' file in ${o}`)}else a(`option 'sqlFilesDirectory' (${e.sqlFilesDirectory}) must point to an existing directory (absolute or relative to ${fs.realpathSync(".")})`)}catch(e){a(e)}})}executeSqlScript(e){const t=fs.readFileSync(e,"utf8");return this.db.exec(t)}upgradeDatabase(e){return new Promise((t,a)=>{this.db.getFirst(SmartDbVersionViewModel,{module:e.module}).then(o=>{o?fs.readdir(e.sqlFilesDirectory,(r,s)=>{if(r)a("unable to read database files from "+e.sqlFilesDirectory);else{let r=[];s.forEach(t=>{const a=new RegExp(`^${e.module}-update.([0-9]{3})\\..*\\.sql$`),s=t.match(a);if(s){parseInt(s[1],10)>o.sequence&&r.push(t)}}),r=_.sortBy(r),this.executeScriptSequentially(r,e).then(()=>{this.db.getFirst(SmartDbVersionViewModel,{module:e.module},"*","sequence DESC").then(e=>{e?t(SmartDbVersionViewModel.from(e)):a("unable to determine final smart db version")}).catch(e=>{a(e)})}).catch(e=>{a(e)})}}):a(`missing version entry for module ${e.module} - add the mandatory insert into smart_db_version statement to your database creation script!`)}).catch(e=>{a(e)})})}executeScriptSequentially(e,t){return new Promise(async(a,o)=>{e&&e.length>0?(this.log.logInfo(SmartDbLogLocation.UpgradeManager,"upgradeDatabase",`execute update script '${e[0]}' for module ${t.module}`),await this.executeSqlScript(`${t.sqlFilesDirectory}/${e[0]}`).then(()=>{this.log.logInfo(SmartDbLogLocation.UpgradeManager,"upgradeDatabase",`successfully executed update script '${e[0]}' for module ${t.module}`),1==e.length&&a(!0)}).catch(e=>{o(e)}),e.length>1&&this.executeScriptSequentially(e.splice(1),t).then(e=>{a(e)}).catch(e=>{o(e)})):a(!0)})}}
|
package/smart-db.d.ts
CHANGED
|
@@ -1,27 +1,13 @@
|
|
|
1
|
+
import { AbstractModel } from "./models/abstract-model";
|
|
2
|
+
import { SmartDbVersionViewModel } from "./models/smart-db-version-view-model";
|
|
3
|
+
import { GenericModelData, IndexedGenericModelData, SmartDbApi, SmartDbConnector, SmartDbDatabase, SmartDbOptions, SmartDbRunResult, SmartDbSqlOptions, SmartDbTableInfo, SqlFieldDescriptor, SqlLimit, SqlOrderBy, SqlUpdateValues, SqlValueType, SqlWhere } from "./smart-db-interfaces";
|
|
1
4
|
import { SmartDbLog } from "./smart-db-log";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { DatabaseRow, IndexedDatabaseRows, SmartDbInterface, SmartDbOptions, SmartDbRunResult, SmartDbSqlBuildData, SqlFieldDescriptor, SqlLimit, SmartDbSqlOptions, SqlOrderBy, SqlUpdateValues, SqlValueType, SqlWhere, SmartDbTableInfo } from "./smart-db-interface";
|
|
5
|
-
export interface SmartDbDatabase {
|
|
6
|
-
}
|
|
7
|
-
export interface SmartDbConnector {
|
|
8
|
-
}
|
|
9
|
-
export declare abstract class SmartDb implements SmartDbInterface {
|
|
10
|
-
static readonly DbDateRegexp: RegExp;
|
|
11
|
-
static readonly DbTimestampRegexp: RegExp;
|
|
12
|
-
protected db: SmartDbDatabase;
|
|
13
|
-
protected dbConnector: SmartDbConnector;
|
|
14
|
-
protected lastError: Error;
|
|
15
|
-
protected lastBuildData: SmartDbSqlBuildData;
|
|
16
|
-
protected smartDbLog: SmartDbLog;
|
|
17
|
-
private dictionaries;
|
|
18
|
-
protected constructor(dbConnector: SmartDbConnector);
|
|
19
|
-
static toDbDate(d: Date | number): string;
|
|
20
|
-
static toDbTimestamp(d: Date | number): string;
|
|
21
|
-
static toDate(d: Date | number | string): Date | null;
|
|
5
|
+
import { SmartDbSqlBuildData } from "./smart-db-sql-build-data";
|
|
6
|
+
export declare abstract class SmartDb implements SmartDbApi {
|
|
22
7
|
abstract getDatabaseType(): string;
|
|
23
|
-
abstract
|
|
24
|
-
abstract
|
|
8
|
+
abstract hasConcurrentTransactions(): boolean;
|
|
9
|
+
abstract exists<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): Promise<boolean>;
|
|
10
|
+
abstract existsSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): boolean;
|
|
25
11
|
abstract exec(script: string): Promise<void>;
|
|
26
12
|
abstract execSync(script: string): SmartDbDatabase | false;
|
|
27
13
|
abstract close(): Promise<void>;
|
|
@@ -29,42 +15,49 @@ export declare abstract class SmartDb implements SmartDbInterface {
|
|
|
29
15
|
abstract getDbQuote(): string;
|
|
30
16
|
abstract getTableInfo(table: string): Promise<SmartDbTableInfo>;
|
|
31
17
|
abstract getDbConnector(): string | SmartDbConnector;
|
|
32
|
-
|
|
18
|
+
protected abstract statementRun(buildData: SmartDbSqlBuildData): Promise<SmartDbRunResult>;
|
|
19
|
+
protected abstract statementRunSync(buildData: SmartDbSqlBuildData): SmartDbRunResult;
|
|
20
|
+
protected abstract statementGet(buildData: SmartDbSqlBuildData): Promise<any>;
|
|
21
|
+
protected abstract statementGetSync(buildData: SmartDbSqlBuildData): any;
|
|
22
|
+
protected abstract statementGetAll(buildData: SmartDbSqlBuildData): Promise<any[]>;
|
|
23
|
+
protected abstract statementGetAllSync(buildData: SmartDbSqlBuildData): any[];
|
|
24
|
+
protected db: SmartDbDatabase;
|
|
25
|
+
protected dbConnector: SmartDbConnector;
|
|
26
|
+
protected lastError: Error;
|
|
27
|
+
protected lastBuildData: SmartDbSqlBuildData;
|
|
28
|
+
protected smartDbLog: SmartDbLog;
|
|
29
|
+
private dictionaries;
|
|
30
|
+
protected constructor(dbConnector: SmartDbConnector);
|
|
31
|
+
initDb(appOptions: SmartDbOptions): Promise<SmartDbVersionViewModel[]>;
|
|
33
32
|
getLogger(): SmartDbLog;
|
|
34
33
|
getDb(): SmartDbDatabase;
|
|
35
34
|
getLastBuildData(): SmartDbSqlBuildData;
|
|
36
|
-
get<T extends AbstractModel>(modelClass: string | (new () => T), options: SmartDbSqlOptions): Promise<T |
|
|
37
|
-
getSync<T extends AbstractModel>(modelClass: string | (new () => T), options: SmartDbSqlOptions): T |
|
|
38
|
-
getFirst<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): Promise<T>;
|
|
39
|
-
getFirstSync<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): T | false;
|
|
40
|
-
getAll<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): Promise<T[]>;
|
|
41
|
-
getAllSync<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): T[] | false;
|
|
42
|
-
delete<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere): Promise<number>;
|
|
43
|
-
deleteSync<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere): number | false;
|
|
44
|
-
update<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues |
|
|
45
|
-
updateSync<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues |
|
|
46
|
-
insert<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues |
|
|
47
|
-
insertSync<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues |
|
|
35
|
+
get<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), options: SmartDbSqlOptions<T, D>): Promise<(T | D)[] | T | D | SqlValueType | IndexedGenericModelData<T, D> | string | string[]>;
|
|
36
|
+
getSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), options: SmartDbSqlOptions<T, D>): (T | D)[] | T | D | SqlValueType | IndexedGenericModelData<T, D> | string | string[] | false;
|
|
37
|
+
getFirst<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): Promise<T>;
|
|
38
|
+
getFirstSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): T | false;
|
|
39
|
+
getAll<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): Promise<T[]>;
|
|
40
|
+
getAllSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): T[] | false;
|
|
41
|
+
delete<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere): Promise<number>;
|
|
42
|
+
deleteSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere): number | false;
|
|
43
|
+
update<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T, where?: SqlWhere): Promise<number>;
|
|
44
|
+
updateSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T, where?: SqlWhere): number | false;
|
|
45
|
+
insert<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T): Promise<number>;
|
|
46
|
+
insertSync<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T): number | false;
|
|
48
47
|
getLastError(): Error;
|
|
49
|
-
buildSelectStatement(modelClass: string | (new () =>
|
|
48
|
+
buildSelectStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), options: SmartDbSqlOptions<T, D>): SmartDbSqlBuildData;
|
|
50
49
|
toDate(d: Date | number | string): Date | null;
|
|
51
50
|
toDbTimestamp(d: Date | number): string;
|
|
52
51
|
toDbDate(d: Date | number): string;
|
|
53
|
-
protected
|
|
54
|
-
protected
|
|
55
|
-
protected
|
|
56
|
-
protected
|
|
57
|
-
protected
|
|
58
|
-
protected
|
|
59
|
-
protected
|
|
60
|
-
protected
|
|
61
|
-
protected
|
|
62
|
-
protected buildInsertStatement(modelClass: string | (new () => AbstractModel), values: SqlUpdateValues | AbstractModel): SmartDbSqlBuildData;
|
|
63
|
-
protected prepareFieldValue(modelClass: string | (new () => AbstractModel), fieldOperation: SqlFieldDescriptor): string;
|
|
64
|
-
protected prepareField(modelClass: string | (new () => AbstractModel), field: SqlValueType | SqlFieldDescriptor, values?: SqlValueType[]): string;
|
|
65
|
-
protected buildFieldList(modelClass: string | (new () => AbstractModel), fields: (SqlValueType | SqlFieldDescriptor)[]): string[];
|
|
66
|
-
protected buildSelectSectionStatement(modelClass: string | (new () => AbstractModel), options: SmartDbSqlOptions): SmartDbSqlBuildData;
|
|
67
|
-
protected translateFieldName(modelClass: string | (new () => AbstractModel), field: string): string;
|
|
52
|
+
protected buildWhere<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where: SqlWhere, op?: string): SmartDbSqlBuildData;
|
|
53
|
+
protected buildDeleteStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), where?: SqlWhere): SmartDbSqlBuildData;
|
|
54
|
+
protected buildUpdateStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T, where?: SqlWhere): SmartDbSqlBuildData;
|
|
55
|
+
protected buildInsertStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), values: SqlUpdateValues | T): SmartDbSqlBuildData;
|
|
56
|
+
protected prepareFieldValue<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), fieldOperation: SqlFieldDescriptor): string;
|
|
57
|
+
protected prepareField<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), field: SqlValueType | SqlFieldDescriptor, values?: SqlValueType[]): string;
|
|
58
|
+
protected buildFieldList<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), fields: (SqlValueType | SqlFieldDescriptor)[]): string[];
|
|
59
|
+
protected buildSelectSectionStatement<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), options: SmartDbSqlOptions<T, D>): SmartDbSqlBuildData;
|
|
60
|
+
protected translateFieldName<T extends AbstractModel<T, D>, D extends GenericModelData>(modelClass: string | (new () => T), field: string): string;
|
|
68
61
|
protected makeDbValue(value: SqlValueType): SqlValueType;
|
|
69
62
|
protected makeArgumentDbValue(value: SqlValueType): SqlValueType;
|
|
70
63
|
protected saveExecute<T>(fn: () => T): T | false;
|
package/smart-db.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});var smart_db_log_1=require("./smart-db-log"),abstract_model_1=require("./model/abstract-model"),smart_db_upgrade_manager_1=require("./smart-db-upgrade-manager"),smart_db_log_model_1=require("./model/smart-db-log-model"),smart_db_dictionary_1=require("./model/smart-db-dictionary"),smart_db_interface_1=require("./smart-db-interface"),lodash_1=__importDefault(require("lodash")),fs_1=__importDefault(require("fs")),SmartDb=function(){function e(e){this.dictionaries=[],this.dbConnector=e,this.smartDbLog=new smart_db_log_1.SmartDbLog(this)}return e.toDbDate=function(e){var t;return e&&(lodash_1.default.isNumber(e)&&(e=new Date(e)),t=e.toISOString().substr(0,19).replace("T"," ")),t},e.toDbTimestamp=function(e){var t;return e&&(lodash_1.default.isNumber(e)&&(e=new Date(e)),t=e.toISOString().substr(0,23).replace("T"," ")),t},e.toDate=function(t){var a=null;return lodash_1.default.isDate(t)?a=t:(lodash_1.default.isNumber(t)||lodash_1.default.isString(t)&&(t.match(e.DbDateRegexp)||t.match(e.DbTimestampRegexp)))&&(a=new Date(t)),a},e.prototype.initDb=function(e){var t=this;return new Promise((function(a,r){t.dictionaries.push(smart_db_dictionary_1.SmartDbDictionary);var i=new smart_db_upgrade_manager_1.SmartDbUpgradeManager(t),n="assets/"+t.getDatabaseType();fs_1.default.existsSync(__dirname+"/"+n)&&(n=__dirname+"/"+n);var l={module:"smart-db-core",sqlFilesDirectory:n};i.prepareDatabase(l).then((function(n){t.exists(smart_db_log_model_1.SmartDbLogModel).then((function(e){t.smartDbLog.setDbLogging(e)}));var l=[],o=lodash_1.default.isArray(e)?e:[e];lodash_1.default.forEach(o,(function(e){l.push(i.prepareDatabase(e)),e.smartDbDictionary&&t.dictionaries.push(e.smartDbDictionary)})),Promise.all(l).then((function(e){e.push(n),a(e)}),(function(e){r(e)}))})).catch((function(e){r(e)}))}))},e.prototype.getLogger=function(){return this.smartDbLog},e.prototype.getDb=function(){return this.db},e.prototype.getLastBuildData=function(){return this.lastBuildData},e.prototype.get=function(e,t){var a=this;return new Promise((function(r,i){t||(t={});var n=a.buildSelectStatement(e,t);t.firstOnly||t.count?a.statementGet(n).then((function(i){r(a.prepareResultRow(e,i,t))})).catch((function(e){i(e)})):a.statementGetAll(n).then((function(i){r(a.prepareResultRows(e,i,t))})).catch((function(e){i(e)}))}))},e.prototype.getSync=function(e,t){var a=this;return this.saveExecute((function(){t||(t={});var r,i=a.buildSelectStatement(e,t);if(t.firstOnly||t.count){var n=a.statementGetSync(i);r=a.prepareResultRow(e,n,t)}else{var l=a.statementGetAllSync(i);r=a.prepareResultRows(e,l,t)}return r}))},e.prototype.getFirst=function(e,t,a,r){return this.get(e,{firstOnly:!0,where:t,fields:a,orderBy:r})},e.prototype.getFirstSync=function(e,t,a,r){return this.getSync(e,{firstOnly:!0,where:t,fields:a,orderBy:r})},e.prototype.getAll=function(e,t,a,r,i){return this.get(e,{where:t,fields:a,orderBy:r,limit:i})},e.prototype.getAllSync=function(e,t,a,r,i){return this.getSync(e,{where:t,fields:a,orderBy:r,limit:i})},e.prototype.delete=function(e,t){var a=this;return new Promise((function(r,i){var n=a.buildDeleteStatement(e,t);a.statementRun(n).then((function(e){e?r(e.changes):i(a.getLastError())})).catch((function(e){i(e)}))}))},e.prototype.deleteSync=function(e,t){var a=this;return this.saveExecute((function(){var r=a.buildDeleteStatement(e,t);return a.statementRunSync(r).changes}))},e.prototype.update=function(e,t,a){var r=this;return new Promise((function(i,n){var l=r.buildUpdateStatement(e,t,a);r.statementRun(l).then((function(e){e?i(e.changes):n(r.getLastError())})).catch((function(e){n(e)}))}))},e.prototype.updateSync=function(e,t,a){var r=this;return this.saveExecute((function(){var i=r.buildUpdateStatement(e,t,a);return r.statementRunSync(i).changes}))},e.prototype.insert=function(e,t){var a=this;return new Promise((function(r,i){var n=a.buildInsertStatement(e,t);a.statementRun(n).then((function(e){e?r(e.lastId):i(a.getLastError())})).catch((function(e){i(e)}))}))},e.prototype.insertSync=function(e,t){var a=this;return this.saveExecute((function(){var r=a.buildInsertStatement(e,t);return a.statementRunSync(r).lastId}))},e.prototype.getLastError=function(){return this.lastError},e.prototype.buildSelectStatement=function(e,t){var a=this;t||(t={});var r=this.buildSelectSectionStatement(e,t);if(lodash_1.default.forEach(["union","minus","intersect"],(function(e){var i=lodash_1.default.get(t,e);i&&(lodash_1.default.isArray(i)||(i=[i]),lodash_1.default.forEach(i,(function(t){var i=a.buildSelectSectionStatement(t.model,t);r.sql+=" "+e.toUpperCase()+" ",r.append(i)})))})),t.orderBy){r.sql+=" ORDER BY ";var i=lodash_1.default.isString(t.orderBy)?t.orderBy.split(/ *, */):t.orderBy;r.sql+=i.map((function(t){var r="",i=t.match(/^(\w*) (asc|desc)$/i);return i&&(t=i[1],r=i[2].toUpperCase()),t=a.translateFieldName(e,t),r&&(t+=" "+r),t})).join(", ")}return t.limit&&(t.limit.limit&&(r.sql+=" LIMIT "+t.limit.limit.toString()),t.limit.offset&&(r.sql+=" OFFSET "+t.limit.offset.toString())),this.lastBuildData=r,r},e.prototype.toDate=function(t){return e.toDate(t)},e.prototype.toDbTimestamp=function(t){return e.toDbTimestamp(t)},e.prototype.toDbDate=function(t){return e.toDbDate(t)},e.prototype.buildWhere=function(e,t,a){var r=this,i=new smart_db_interface_1.SmartDbSqlBuildData;return t&&lodash_1.default.keys(t).length>0&&(a||(a="AND",i.sql+=" WHERE "),i.sql+=lodash_1.default.map(t,(function(t,n){var l=n.toUpperCase();if("AND"==l||"OR"==l){var o="";return(lodash_1.default.isArray(t)?t:[t]).forEach((function(t,a){var n=r.buildWhere(e,t,l);a>0&&(o+=" "+l+" "),o+=n.sql,i.values=lodash_1.default.concat(i.values,n.values)})),o="("+o+")"}if("EXPRESSION"==l){var s=[];return lodash_1.default.forEach(t,(function(t){var a,n=r.prepareField(e,t.compare,i.values),l=r.prepareField(e,t.with,i.values);a=lodash_1.default.isString(t.operation)&&smart_db_interface_1.SqlOperationType[t.operation]?smart_db_interface_1.SqlOperationType[t.operation]:t.operation||smart_db_interface_1.SqlOperationType.EQ,s.push(n+" "+a+" "+l)})),s.join(" "+a+" ")}var u=!0,d=void 0;if(lodash_1.default.isArray(t)&&(t=smart_db_interface_1.IN(t)),lodash_1.default.isObject(t)){var f=t;switch(lodash_1.default.isString(f.operation)&&smart_db_interface_1.SqlOperationType[f.operation]&&(f.operation=smart_db_interface_1.SqlOperationType[f.operation]),f.operation){case smart_db_interface_1.SqlOperationType.IN:var p=new Array(f.value.length);p.fill("?"),d=smart_db_interface_1.SqlOperationType.IN+" ("+p.join(", ")+")",i.values=lodash_1.default.concat(i.values,f.value);break;case smart_db_interface_1.SqlOperationType.IS_NULL:case smart_db_interface_1.SqlOperationType.IS_NOT_NULL:d=f.operation;break;case smart_db_interface_1.SqlOperationType.LITERAL:n=f.key;var c=f.literalOperation||smart_db_interface_1.SqlOperationType.EQ;if(c==smart_db_interface_1.SqlOperationType.IN){var _=new Array(f.value.length);_.fill("?"),d=smart_db_interface_1.SqlOperationType.IN+" ("+_.join(", ")+")",i.values=lodash_1.default.concat(i.values,f.value)}else lodash_1.default.isUndefined(f.value)?d=c:(d=c+" ?",i.values.push(r.makeDbValue(f.value)));u=!1;break;default:d=f.operation+" ?",i.values.push(r.makeDbValue(f.value))}}else null===t?d=smart_db_interface_1.SqlOperationType.IS_NULL:void 0===t?(n="1",d="= 1",u=!1):(d=lodash_1.default.isString(t)&&t.match(/[%_]/)?smart_db_interface_1.SqlOperationType.LIKE+" ?":smart_db_interface_1.SqlOperationType.EQ+" ?",i.values.push(r.makeDbValue(t)));return u?r.translateFieldName(e,n)+" "+d:n+" "+d})).join(" "+a+" ")),i},e.prototype.buildDeleteStatement=function(e,t){var a=this.getTableName(e),r=new smart_db_interface_1.SmartDbSqlBuildData("DELETE FROM");return r.sql+=" "+a,t&&r.append(this.buildWhere(e,t)),this.lastBuildData=r,r},e.prototype.buildUpdateStatement=function(e,t,a){var r=this,i=this.getTableName(e),n=new smart_db_interface_1.SmartDbSqlBuildData("UPDATE");n.sql+=" "+i+" SET ";var l=[];return t instanceof abstract_model_1.AbstractModel&&(t=t.getPlainObject(abstract_model_1.FieldNamingStyle.Database)),lodash_1.default.forOwn(t,(function(t,a){l.push(r.translateFieldName(e,a)+" = ?"),n.values.push(r.makeDbValue(t))})),n.sql+=l.join(", "),a&&n.append(this.buildWhere(e,a)),this.lastBuildData=n,n},e.prototype.buildInsertStatement=function(e,t){var a=this,r=this.getTableName(e),i=new smart_db_interface_1.SmartDbSqlBuildData("INSERT");i.sql+=" INTO "+r;var n=[];t instanceof abstract_model_1.AbstractModel&&(t=t.getPlainObject(abstract_model_1.FieldNamingStyle.Database)),lodash_1.default.forOwn(t,(function(t,r){r=a.translateFieldName(e,r),n.push(r),i.values.push(a.makeDbValue(t))}));var l=new Array(n.length);return l.fill("?"),i.sql+=" ("+n.join(", ")+") VALUES ("+l.join(", ")+")",this.lastBuildData=i,i},e.prototype.prepareFieldValue=function(e,t){var a="<undefined>";switch(t.operation){case smart_db_interface_1.SqlFieldOperationType.FIELD:a=this.translateFieldName(e,t.value);break;case smart_db_interface_1.SqlFieldOperationType.VALUE:null===t.value?a="NULL":t.literal?a=t.value:lodash_1.default.isString(t.value)?!isNaN(parseFloat(t.value))&&isFinite(t.value)||(a="'"+t.value+"'"):lodash_1.default.isNumber(t.value)?a=t.value.toString():lodash_1.default.isBoolean(t.value)?a=t.value?"1":"0":lodash_1.default.isDate(t.value)?a="'"+t.value.toISOString().substr(0,23).replace("T"," ")+"'":console.error("unhandled field data type",typeof t.value,t.value);break;case smart_db_interface_1.SqlFieldOperationType.COUNT:a=lodash_1.default.isArray(t.value)?"COUNT("+t.value.join(",")+")":"COUNT("+(t.value||"")+")";break;case smart_db_interface_1.SqlFieldOperationType.COALESCE:var r=lodash_1.default.isArray(t.value)?t.value:[t.value];a="COALESCE("+this.buildFieldList(e,r).join(",")+")"}if(t.alias){var i=this.getDbQuote();a+=" as "+i+t.alias+i}return a},e.prototype.prepareField=function(e,t,a){var r;if(null===t)r="NULL";else if(lodash_1.default.isString(t)){var i=t.match(/^'(.*)'$/);i?a?(r="?",a.push(i[1])):r=t:r=this.translateFieldName(e,t)}else t.operation?r=this.prepareFieldValue(e,t):a?(r="?",a.push(t)):r=this.makeArgumentDbValue(t).toString();return r},e.prototype.buildFieldList=function(e,t){var a=this,r=[];return lodash_1.default.forEach(t,(function(t){r.push(a.prepareField(e,t))})),r},e.prototype.buildSelectSectionStatement=function(e,t){var a,r,i=this,n=this.getTableName(e);if(lodash_1.default.isArray(t.fields))a=t.fields;else if(lodash_1.default.isString(t.fields)){var l=t.fields.trim();a=""===l||"*"==l?[]:l.split(/,/)}else a=t.fields&&t.fields.operation?[t.fields]:[];a.length>0?r=this.buildFieldList(e,a).join(", "):r="*";t.distinct&&(r="DISTINCT "+r),t.count&&(r="COUNT("+r+")");var o=new smart_db_interface_1.SmartDbSqlBuildData("SELECT "+r+" FROM "+n);if(t.where&&o.append(this.buildWhere(e,t.where)),t.groupBy){o.sql+=" GROUP BY ";var s=lodash_1.default.isArray(t.groupBy)?t.groupBy:[t.groupBy];o.sql+=s.map((function(t){return i.translateFieldName(e,t)})).join(", ")}return o},e.prototype.translateFieldName=function(e,t){if(lodash_1.default.isString(e)){var a=e,r=!1;lodash_1.default.forEach(this.dictionaries,(function(t){return t.models&&t.models[a]&&(e=t.models[a].cls,r=!0),!r}))}if(!lodash_1.default.isString(e)){var i=e.attributeMap[t];if(i)i.alias&&(t=i.alias);else{var n=e.getTableName();this.lastError=new Error("unknown field '"+t+"' in table '"+n+"'"),this.smartDbLog.logError(smart_db_log_1.SmartDbLogType.Database,"translateFieldName",this.lastError)}}return t},e.prototype.makeDbValue=function(t){return lodash_1.default.isBoolean(t)?t=t?1:0:lodash_1.default.isDate(t)&&(t=e.toDbDate(t)),t},e.prototype.makeArgumentDbValue=function(t){return lodash_1.default.isBoolean(t)?t=t?1:0:lodash_1.default.isDate(t)?t="'"+e.toDbDate(t)+"'":lodash_1.default.isString(t)&&(t="'"+t+"'"),t},e.prototype.saveExecute=function(e){var t;try{t=e()}catch(e){this.lastError=e instanceof Error?e:new Error(e||"Unknown error"),this.smartDbLog.logFatal(smart_db_log_1.SmartDbLogType.Database,"saveExecute-catch",this.lastError),t=!1}return t},e.prototype.prepareResultRow=function(e,t,a){var r;if(a.indexedBy&&this.smartDbLog.logWarning(smart_db_log_1.SmartDbLogType.Database,"AbstractModel.get","option 'indexedBy' not supported without 'all'"),t)if(a.count)r=parseInt(lodash_1.default.values(t)[0],10);else if(a.collapseRow)r=lodash_1.default.values(t).join(",");else{r=e.from(t)}return r},e.prototype.prepareResultRows=function(e,t,a){var r;if(a.indexedBy||a.collapseRow||!lodash_1.default.isString(e)){var i=e;r=a.indexedBy?{}:new Array(t.length),lodash_1.default.forEach(t,(function(e,t){var n=i.from?i.from(e):null,l=null;if(a.indexedBy&&(l=n?n.getValue(a.indexedBy):e[a.indexedBy]),a.collapseRow){var o=lodash_1.default.values(e).join(",");l?r[l]=o:r[t]=o}else l?r[l]=n||e:r[t]=n||e}))}else r=t;return r},e.prototype.getTableName=function(e){var t;if(lodash_1.default.isString(e)){var a=e;lodash_1.default.forEach(this.dictionaries,(function(e){return e.models&&e.models[a]&&(t=e.models[a].cls.getTableName()),!t})),t||(t=a)}else{if(!e||!e.getTableName)throw new Error("unknown model: "+e);t=e.getTableName()}return t},e.DbDateRegexp=/^([0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9])(Z| GMT| GMT[-+][0-9]{1,2})?$/,e.DbTimestampRegexp=/^([0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9]{3})(Z| GMT| GMT[-+][0-9]{1,2})?$/,e}();exports.SmartDb=SmartDb;
|
|
1
|
+
import _ from"lodash";import{AbstractModel}from"./models/abstract-model";import{SmartDbDictionary}from"./models/smart-db-dictionary";import{SmartDbLogModel}from"./models/smart-db-log-model";import{IN,smartDbToDate,toSmartDbDate,toSmartDbTimestamp}from"./smart-db-globals";import{FieldNamingStyle,SqlFieldOperationType,SqlOperationType}from"./smart-db-interfaces";import{SmartDbLog,SmartDbLogLocation}from"./smart-db-log";import{SmartDbSqlBuildData}from"./smart-db-sql-build-data";import{SmartDbUpgradeManager}from"./smart-db-upgrade-manager";export class SmartDb{constructor(e){this.dictionaries=[],this.dbConnector=e,this.smartDbLog=new SmartDbLog(this)}initDb(e){return new Promise((t,a)=>{this.dictionaries.push(SmartDbDictionary);const r=new SmartDbUpgradeManager(this);let i;i=import.meta&&import.meta.url?import.meta.url.replace(/file:\/\//,"").replace(/\/[^/]*$/,""):__dirname;const s={module:"smart-db-core",sqlFilesDirectory:i+"/assets/"+this.getDatabaseType()};r.prepareDatabase(s).then(i=>{this.exists(SmartDbLogModel).then(s=>{this.smartDbLog.setDbLogging(s),r.prepareDatabase(e).then(e=>{t([i,e])},e=>{a(e)})})}).catch(e=>{a(e)})})}getLogger(){return this.smartDbLog}getDb(){return this.db}getLastBuildData(){return this.lastBuildData}get(e,t){return new Promise((a,r)=>{t||(t={});const i=this.buildSelectStatement(e,t);t.firstOnly||t.count?this.statementGet(i).then(r=>{a(this.prepareResultRow(e,r,t))}).catch(e=>{r(e)}):this.statementGetAll(i).then(r=>{a(this.prepareResultRows(e,r,t))}).catch(e=>{r(e)})})}getSync(e,t){return this.saveExecute(()=>{t||(t={});const a=this.buildSelectStatement(e,t);let r;if(t.firstOnly||t.count){const i=this.statementGetSync(a);r=this.prepareResultRow(e,i,t)}else{const i=this.statementGetAllSync(a);r=this.prepareResultRows(e,i,t)}return r})}getFirst(e,t,a,r){return this.get(e,{firstOnly:!0,where:t,fields:a,orderBy:r})}getFirstSync(e,t,a,r){return this.getSync(e,{firstOnly:!0,where:t,fields:a,orderBy:r})}getAll(e,t,a,r,i){return this.get(e,{where:t,fields:a,orderBy:r,limit:i})}getAllSync(e,t,a,r,i){return this.getSync(e,{where:t,fields:a,orderBy:r,limit:i})}delete(e,t){return new Promise((a,r)=>{const i=this.buildDeleteStatement(e,t);this.statementRun(i).then(e=>{e?a(e.changes):r(this.getLastError())}).catch(e=>{r(e)})})}deleteSync(e,t){return this.saveExecute(()=>{const a=this.buildDeleteStatement(e,t);return this.statementRunSync(a).changes})}update(e,t,a){return new Promise((r,i)=>{const s=this.buildUpdateStatement(e,t,a);this.statementRun(s).then(e=>{e?r(e.changes):i(this.getLastError())}).catch(e=>{i(e)})})}updateSync(e,t,a){return this.saveExecute(()=>{const r=this.buildUpdateStatement(e,t,a);return this.statementRunSync(r).changes})}insert(e,t){return new Promise((a,r)=>{const i=this.buildInsertStatement(e,t);this.statementRun(i).then(e=>{e?a(e.lastId):r(this.getLastError())}).catch(e=>{r(e)})})}insertSync(e,t){return this.saveExecute(()=>{const a=this.buildInsertStatement(e,t);return this.statementRunSync(a).lastId})}getLastError(){return this.lastError}buildSelectStatement(e,t){t||(t={});const a=this.buildSelectSectionStatement(e,t);if(_.forEach(["union","minus","intersect"],e=>{let r=_.get(t,e);r&&(_.isArray(r)||(r=[r]),_.forEach(r,t=>{const r=this.buildSelectSectionStatement(t.model,t);a.sql+=" "+e.toUpperCase()+" ",a.append(r)}))}),t.orderBy){a.sql+=" ORDER BY ";const r=_.isString(t.orderBy)?t.orderBy.split(/ *, */):t.orderBy;a.sql+=r.map(t=>{let a="";const r=t.match(/^(\w*) (asc|desc)$/i);return r&&(t=r[1],a=r[2].toUpperCase()),t=this.translateFieldName(e,t),a&&(t+=" "+a),t}).join(", ")}return t.limit&&(t.limit.limit&&(a.sql+=" LIMIT "+t.limit.limit.toString()),t.limit.offset&&(a.sql+=" OFFSET "+t.limit.offset.toString())),this.lastBuildData=a,a}toDate(e){return smartDbToDate(e)}toDbTimestamp(e){return toSmartDbTimestamp(e)}toDbDate(e){return toSmartDbDate(e)}buildWhere(e,t,a){const r=new SmartDbSqlBuildData;return t&&_.keys(t).length>0&&(a||(a="AND",r.sql+=" WHERE "),r.sql+=_.map(t,(t,i)=>{const s=i.toUpperCase();if("AND"==s||"OR"==s){let a="";return(_.isArray(t)?t:[t]).forEach((t,i)=>{const l=this.buildWhere(e,t,s);i>0&&(a+=" "+s+" "),a+=l.sql,r.values=_.concat(r.values,l.values)}),a="("+a+")",a}if("EXPRESSION"==s){let i=[];return _.forEach(t,t=>{const a=this.prepareField(e,t.compare,r.values),s=this.prepareField(e,t.with,r.values);let l;l=_.isString(t.operation)&&SqlOperationType[t.operation]?SqlOperationType[t.operation]:t.operation||SqlOperationType.EQ,i.push(`${a} ${l} ${s}`)}),i.join(" "+a+" ")}{let a,s,l=!0;if(_.isArray(t)&&(t=IN(t)),_.isObject(t)){const e=t;switch(_.isString(e.operation)&&SqlOperationType[e.operation]&&(e.operation=SqlOperationType[e.operation]),e.operation){case SqlOperationType.IN:const t=new Array(e.value.length);t.fill("?"),a=SqlOperationType.IN+" ("+t.join(", ")+")",r.values=_.concat(r.values,e.value);break;case SqlOperationType.IS_NULL:case SqlOperationType.IS_NOT_NULL:a=e.operation;break;case SqlOperationType.LITERAL:i=e.key;const s=e.literalOperation||SqlOperationType.EQ;if(s==SqlOperationType.IN){const t=new Array(e.value.length);t.fill("?"),a=SqlOperationType.IN+" ("+t.join(", ")+")",r.values=_.concat(r.values,e.value)}else _.isUndefined(e.value)?a=s:(a=s+" ?",r.values.push(this.makeDbValue(e.value)));l=!1;break;default:a=e.operation+" ?",r.values.push(this.makeDbValue(e.value))}}else null===t?a=SqlOperationType.IS_NULL:void 0===t?(i="1",a="= 1",l=!1):(a=_.isString(t)&&t.match(/[%_]/)?SqlOperationType.LIKE+" ?":SqlOperationType.EQ+" ?",r.values.push(this.makeDbValue(t)));return s=l?this.translateFieldName(e,i)+" "+a:i+" "+a,s}}).join(" "+a+" ")),r}buildDeleteStatement(e,t){const a=this.getTableName(e),r=new SmartDbSqlBuildData("DELETE FROM");return r.sql+=" "+a,t&&r.append(this.buildWhere(e,t)),this.lastBuildData=r,r}buildUpdateStatement(e,t,a){const r=this.getTableName(e),i=new SmartDbSqlBuildData("UPDATE");i.sql+=" "+r+" SET ";const s=[];return t instanceof AbstractModel&&(t=t.getPlainObject(FieldNamingStyle.Database)),_.forOwn(t,(t,a)=>{s.push(this.translateFieldName(e,a)+" = ?"),i.values.push(this.makeDbValue(t))}),i.sql+=s.join(", "),a&&i.append(this.buildWhere(e,a)),this.lastBuildData=i,i}buildInsertStatement(e,t){const a=this.getTableName(e),r=new SmartDbSqlBuildData("INSERT");r.sql+=" INTO "+a;const i=[];t instanceof AbstractModel&&(t=t.getPlainObject(FieldNamingStyle.Database)),_.forOwn(t,(t,a)=>{a=this.translateFieldName(e,a),i.push(a),r.values.push(this.makeDbValue(t))});const s=new Array(i.length);return s.fill("?"),r.sql+=" ("+i.join(", ")+") VALUES ("+s.join(", ")+")",this.lastBuildData=r,r}prepareFieldValue(e,t){let a="<undefined>";switch(t.operation){case SqlFieldOperationType.FIELD:a=this.translateFieldName(e,t.value);break;case SqlFieldOperationType.VALUE:null===t.value?a="NULL":t.literal?a=t.value:_.isString(t.value)?!isNaN(parseFloat(t.value))&&isFinite(t.value)||(a="'"+t.value+"'"):_.isNumber(t.value)?a=t.value.toString():_.isBoolean(t.value)?a=t.value?"1":"0":_.isDate(t.value)?a="'"+t.value.toISOString().substr(0,23).replace("T"," ")+"'":console.error("unhandled field data type",typeof t.value,t.value);break;case SqlFieldOperationType.COUNT:a=_.isArray(t.value)?"COUNT("+t.value.join(",")+")":"COUNT("+(t.value||"")+")";break;case SqlFieldOperationType.COALESCE:const r=_.isArray(t.value)?t.value:[t.value];a="COALESCE("+this.buildFieldList(e,r).join(",")+")"}if(t.alias){const e=this.getDbQuote();a+=` as ${e}${t.alias}${e}`}return a}prepareField(e,t,a){let r;if(null===t)r="NULL";else if(_.isString(t)){const i=t.match(/^'(.*)'$/);i?a?(r="?",a.push(i[1])):r=t:r=this.translateFieldName(e,t)}else t.operation?r=this.prepareFieldValue(e,t):a?(r="?",a.push(t)):r=this.makeArgumentDbValue(t).toString();return r}buildFieldList(e,t){const a=[];return _.forEach(t,t=>{a.push(this.prepareField(e,t))}),a}buildSelectSectionStatement(e,t){const a=this.getTableName(e);let r,i;if(_.isArray(t.fields))r=t.fields;else if(_.isString(t.fields)){const e=t.fields.trim();r=""===e||"*"==e?[]:e.split(/,/)}else r=t.fields&&t.fields.operation?[t.fields]:[];if(r.length>0){i=this.buildFieldList(e,r).join(", ")}else i="*";t.distinct&&(i="DISTINCT "+i),t.count&&(i=`COUNT(${i})`);const s=new SmartDbSqlBuildData(`SELECT ${i} FROM ${a}`);if(t.where&&s.append(this.buildWhere(e,t.where)),t.groupBy){s.sql+=" GROUP BY ";const a=_.isArray(t.groupBy)?t.groupBy:[t.groupBy];s.sql+=a.map(t=>this.translateFieldName(e,t)).join(", ")}return s}translateFieldName(e,t){if(_.isString(e)){const t=e;let a=!1;_.forEach(this.dictionaries,r=>(r.models&&r.models[t]&&(e=r.models[t].cls,a=!0),!a))}if(!_.isString(e)){const a=e.attributeMap[t];if(a)a.alias&&(t=a.alias);else{const a=e.getTableName();this.lastError=new Error(`unknown field '${t}' in table '${a}'`),this.smartDbLog.logError(SmartDbLogLocation.Database,"translateFieldName",this.lastError)}}return t}makeDbValue(e){return _.isBoolean(e)?e=e?1:0:_.isDate(e)&&(e=toSmartDbDate(e)),e}makeArgumentDbValue(e){return _.isBoolean(e)?e=e?1:0:_.isDate(e)?e=`'${toSmartDbDate(e)}'`:_.isString(e)&&(e=`'${e}'`),e}saveExecute(e){let t;try{t=e()}catch(e){this.lastError=e instanceof Error?e:new Error(e||"Unknown error"),this.smartDbLog.logFatal(SmartDbLogLocation.Database,"saveExecute-catch",this.lastError),t=!1}return t}prepareResultRow(e,t,a){let r;if(a.indexedBy&&this.smartDbLog.logWarning(SmartDbLogLocation.Database,"AbstractModel.get","option 'indexedBy' not supported without 'all'"),t)if(a.count)r=parseInt(_.values(t)[0],10);else if(a.collapseRow)r=_.values(t).join(",");else{r=e.from(t)}return r}prepareResultRows(e,t,a){let r;if(a.indexedBy||a.collapseRow||!_.isString(e)){const i=e;r=a.indexedBy?{}:new Array(t.length),_.forEach(t,(e,t)=>{const s=i.from?i.from(e):null;let l=null;if(a.indexedBy&&(l=s?s.getValue(a.indexedBy):e[a.indexedBy]),a.collapseRow){const a=_.values(e).join(",");l?r[l]=a:r[t]=a}else l?r[l]=s||e:r[t]=s||e})}else r=t;return r}getTableName(e){let t;if(_.isString(e)){let a=e;_.forEach(this.dictionaries,e=>(e.models&&e.models[a]&&(t=e.models[a].cls.getTableName()),!t)),t||(t=a)}else{if(!e||!e.getTableName)throw new Error("unknown model: "+e);t=e.getTableName()}return t}}
|
package/bin/extract-db-interface
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
#! /bin/bash
|
|
2
|
-
if [[ -L "$0" ]]; then
|
|
3
|
-
bin=$(dirname "$0")
|
|
4
|
-
link=$(readlink "$0")
|
|
5
|
-
ROOT=$(cd "$bin" > /dev/null && cd $(dirname "$link")/.. > /dev/null && pwd)
|
|
6
|
-
else
|
|
7
|
-
ROOT=$(cd `dirname "$0"`/.. > /dev/null && pwd)
|
|
8
|
-
fi
|
|
9
|
-
|
|
10
|
-
if [[ -f $ROOT/src/helpers/extract-db-interface.ts ]]; then
|
|
11
|
-
# development environment
|
|
12
|
-
ts-node "$ROOT/src/helpers/extract-db-interface.ts" "$@"
|
|
13
|
-
elif [[ -f $ROOT/helpers/extract-db-interface.js ]]; then
|
|
14
|
-
# distribution environment
|
|
15
|
-
node "$ROOT/helpers/extract-db-interface.js" "$@"
|
|
16
|
-
else
|
|
17
|
-
echo "missing smart-db-extract-interface script"
|
|
18
|
-
fi
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __spreadArrays=this&&this.__spreadArrays||function(){for(var e=0,t=0,r=arguments.length;t<r;t++)e+=arguments[t].length;var a=Array(e),n=0;for(t=0;t<r;t++)for(var s=arguments[t],l=0,o=s.length;l<o;l++,n++)a[n]=s[l];return a},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});var smart_db_interface_1=require("../smart-db-interface"),smart_db_core_table_model_1=require("../model/smart-db-core-table-model"),smart_db_better_sqlite3_1=require("../drivers/smart-db-better-sqlite3"),process_1=__importDefault(require("process")),lodash_1=__importDefault(require("lodash")),fs_1=__importDefault(require("fs"));function getType(e){return 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"}function extractDbInterface(e){console.log("extract interface from '"+e.db.getDbConnector()+"' to '"+e.modelDirectory+"'"),fs_1.default.existsSync(e.modelDirectory)||fs_1.default.mkdirSync(e.modelDirectory,{recursive:!0});var t=e.db.getAllSync("sqlite_master",{type:smart_db_interface_1.IN(["table","view"]),name:smart_db_interface_1.NE("sqlite_sequence")});if(!1===t)console.error(e.db.getLastError()),process_1.default.abort();else{var r=[];if(!e.includeCoreTables&&e.db.existsSync(smart_db_core_table_model_1.SmartDbCoreTableModel)){var a=e.db.getAllSync(smart_db_core_table_model_1.SmartDbCoreTableModel);if(!1===a)throw e.db.getLastError();a.forEach((function(e){r.push(e.name)}))}var n=[];t.forEach((function(t){if(!r.includes(t.name)){var a=t.name[0].toUpperCase()+lodash_1.default.camelCase(t.name.substr(1))+"Model";n.push(a);var s=[];e.db.getTableInfo(t.name).then((function(r){var n=r.fields,l=!1,o="",i="/* eslint-disable @typescript-eslint/member-ordering */\n";if(n){i+='import {AbstractModel, GenericModel, ModelAttributeMap} from "'+e.abstractModelModule+'";\n',o+="export interface "+a+"Data extends Record<string, any> {\n","user"==t.name&&n.push({name:"hash",type:"string",virtual:!0},{name:"privileges",type:"string[]",virtual:!0});var c,d="",u="",m=!1,b=[];n.forEach((function(e){var t=e.name.match(/([a-z0-9]{1,4})_/i);e.pk&&(d=e.name,t&&(u=t[1])),t?b.includes(t[1])||b.push(t[1]):m=!0})),u||m||1!=b.length||(u=b[0]),u&&(c=new RegExp("^"+u+"_(.*)$"));var f={};n.forEach((function(t){var r,a=getType(t.type);"SqlValueType"!=a||l||(i+='import {SqlValueType} from "'+e.smartDbInterface+'";\n',l=!0);var n=c&&t.name.match(c),d=!0;(r=n?lodash_1.default.camelCase(n[1]):lodash_1.default.camelCase(t.name))!=t.name&&(s.push({name:r,attribute:r,type:a,typeScriptStyle:d,alias:t.name}),d=!1),s.push({name:t.name,attribute:r,type:a,physical:void 0!==t.cid,typeScriptStyle:d}),f[r]=a,o+=" "+r+"?: "+a+";\n"})),o+="}\n\n",o+="export class "+a+" extends AbstractModel {\n",lodash_1.default.forEach(f,(function(e,t){o+=" private _"+t+"?: "+e+";\n"})),o+="\n",o+=" static readonly attributeMap: ModelAttributeMap = {\n",s.forEach((function(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 "'+a+'";\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 "'+d+'";\n',o+=" }\n",o+="\n",o+=" static from(other: AbstractModel | GenericModel): "+a+" {\n",o+=" let value: "+a+" = null;\n",o+=" if (other) {\n",o+=" value = new "+a+"();\n",o+=' if (other.getClassName && (<AbstractModel>other).getClassName() == "'+a+'") {\n',o+=" Object.assign(value, other);\n",o+=" } else {\n",o+=" value.assign(other as GenericModel);\n",o+=" }\n",o+=" }\n",o+=" return value;\n",o+=" }\n",o+="\n",o+=" public clone(): AbstractModel {\n",o+=" return "+a+".from(this);\n",o+=" }\n",o+="\n",o+=" public getClassName(): string {\n",o+=' return "'+a+'";\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 "'+d+'";\n',o+=" }\n",o+="\n",o+=" public getAttributeMap(): ModelAttributeMap {\n",o+=" return "+a+".attributeMap;\n",o+=" }\n",s.forEach((function(e){var t;o+="\n",""!==(t=e.name.length<3||e.name.match(/_/)?"FunctionNamingConventionJS":"")&&(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"})),o=i+"\n"+(o+="}\n");var _=t.name.replace(/[^[a-z0-9]/g,"-")+"-model.ts";fs_1.default.writeFileSync(e.modelDirectory+"/"+_,o)}else console.log("error: table has no fields")})).catch((function(e){console.error(e),process_1.default.abort()}))}}));var s='import {AbstractModel} from "'+e.abstractModelModule+'";\n';lodash_1.default.forEach(n,(function(e){s+="import {"+e+'} from "./'+lodash_1.default.kebabCase(e)+'";\n'})),s+="\n",s+="interface SmartDbDictionaryEntry {\n",s+=" cls: "+n.map((function(e){return"typeof "+e})).join(" |\n "),s+=";\n",s+="}\n\n",s+="export class SmartDbDictionary {\n",s+=" static models: { [relation: string]: SmartDbDictionaryEntry; } = {\n",lodash_1.default.forEach(n,(function(e){s+=" "+e+": {\n",s+=" cls: "+e+"\n",s+=" },\n"})),s=s.substring(0,s.length-2)+"\n",s+=" };\n\n",s+=" static createModel(relationName: string): AbstractModel {\n",s+=" const entry = SmartDbDictionary.models[relationName];\n",s+=" return entry && entry.cls && new (entry.cls)();\n",s+=" }\n",s+="}\n",fs_1.default.writeFileSync(e.modelDirectory+"/smart-db-dictionary.ts",s)}e.db.closeSync()}!function(){if(4==process_1.default.argv.length)extractDbInterface({modelDirectory:process_1.default.argv[3],smartDbInterface:"smart-db",abstractModelModule:"smart-db",db:new smart_db_better_sqlite3_1.SmartDbBetterSqlite3(process_1.default.argv[2],{verbose:function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];console.log.apply(console,__spreadArrays(["DB LOG",e],t))}})});else if(2==process_1.default.argv.length&&fs_1.default.existsSync("src/helpers/extract-db-interface.ts")){var e="/tmp/smart-db-core."+process_1.default.pid+".sqlite3",t={includeCoreTables:!0,modelDirectory:"src/model",smartDbInterface:"../smart-db-interface",abstractModelModule:"./abstract-model",db:new smart_db_better_sqlite3_1.SmartDbBetterSqlite3(e,{verbose:function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];console.log.apply(console,__spreadArrays(["DB LOG",e],t))}})};t.db.initDb({module:"smart-db-core",sqlFilesDirectory:"assets/sqlite3"}).then((function(r){extractDbInterface(t),fs_1.default.unlinkSync(e),t.db.closeSync()})),fs_1.default.existsSync("test/test.sqlite3")&&fs_1.default.unlinkSync("test/test.sqlite3");var r={modelDirectory:"test/model",smartDbInterface:"../../src/smart-db-interface",abstractModelModule:"../../src/model/abstract-model",db:new smart_db_better_sqlite3_1.SmartDbBetterSqlite3("test/db/sqlite3.db",{verbose:function(e){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];console.log.apply(console,__spreadArrays(["DB LOG",e],t))}})};r.db.initDb({module:"smart-db-test",sqlFilesDirectory:"test/db/sqlite3"}).then((function(e){extractDbInterface(r)}))}else console.log("usage: extract-db-interface {db-connector|path-to-db} {target-model-directory}")}();
|
package/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from "./smart-db";
|
|
2
|
-
export * from "./smart-db-log";
|
|
3
|
-
export * from "./smart-db-interface";
|
|
4
|
-
export * from "./smart-db-upgrade-manager";
|
|
5
|
-
export * from "./model/abstract-model";
|
|
6
|
-
export * from "./model/smart-db-version-model";
|
|
7
|
-
export * from "./model/smart-db-version-view-model";
|
|
8
|
-
export * from "./model/smart-db-log-model";
|
|
9
|
-
export * from "./model/sqlite-master-model";
|
|
10
|
-
export * from "./model/sqlite-sequence-model";
|
package/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";function __export(e){for(var r in e)exports.hasOwnProperty(r)||(exports[r]=e[r])}Object.defineProperty(exports,"__esModule",{value:!0}),__export(require("./smart-db")),__export(require("./smart-db-log")),__export(require("./smart-db-interface")),__export(require("./smart-db-upgrade-manager")),__export(require("./model/abstract-model")),__export(require("./model/smart-db-version-model")),__export(require("./model/smart-db-version-view-model")),__export(require("./model/smart-db-log-model")),__export(require("./model/sqlite-master-model")),__export(require("./model/sqlite-sequence-model"));
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { SqlValueType } from "../smart-db-interface";
|
|
2
|
-
export declare type GenericModel = Record<string, SqlValueType>;
|
|
3
|
-
export interface AttributeInfo {
|
|
4
|
-
attribute: string;
|
|
5
|
-
type: string;
|
|
6
|
-
alias?: string;
|
|
7
|
-
physical?: boolean;
|
|
8
|
-
virtual?: boolean;
|
|
9
|
-
alternative?: boolean;
|
|
10
|
-
typeScriptStyle?: boolean;
|
|
11
|
-
}
|
|
12
|
-
export interface ModelAttributeMap {
|
|
13
|
-
[key: string]: AttributeInfo;
|
|
14
|
-
}
|
|
15
|
-
export interface AbstractModelGlobals {
|
|
16
|
-
attributeMap: ModelAttributeMap;
|
|
17
|
-
getTableName: () => string;
|
|
18
|
-
from: (other: GenericModel | AbstractModel) => AbstractModel;
|
|
19
|
-
}
|
|
20
|
-
export declare enum FieldNamingStyle {
|
|
21
|
-
Database = 0,
|
|
22
|
-
TypeScript = 1,
|
|
23
|
-
All = 2
|
|
24
|
-
}
|
|
25
|
-
export declare abstract class AbstractModel {
|
|
26
|
-
static readonly attributeMap: ModelAttributeMap;
|
|
27
|
-
static getTableName(): string;
|
|
28
|
-
static from(other: GenericModel | AbstractModel): any;
|
|
29
|
-
abstract clone(): AbstractModel;
|
|
30
|
-
abstract getClassName(): string;
|
|
31
|
-
abstract getTableName(): string;
|
|
32
|
-
abstract getPrimaryKey(): string;
|
|
33
|
-
abstract getAttributeMap(): ModelAttributeMap;
|
|
34
|
-
assign(other: GenericModel, options?: {
|
|
35
|
-
only?: string[];
|
|
36
|
-
exclude?: string[];
|
|
37
|
-
}): void;
|
|
38
|
-
setValue(attribute: string, value: string | number | boolean | Date): void;
|
|
39
|
-
getValue(attribute: string): string | number | boolean;
|
|
40
|
-
hasAttribute(attribute: string): boolean;
|
|
41
|
-
clear(attributes: string | string[]): void;
|
|
42
|
-
getPlainObject(style: FieldNamingStyle, includeVirtuals?: boolean, fields?: string[]): GenericModel;
|
|
43
|
-
private toDbTimestamp;
|
|
44
|
-
}
|
package/model/abstract-model.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __importStar=this&&this.__importStar||function(t){if(t&&t.__esModule)return t;var e={};if(null!=t)for(var i in t)Object.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e.default=t,e};Object.defineProperty(exports,"__esModule",{value:!0});var FieldNamingStyle,_=__importStar(require("lodash"));!function(t){t[t.Database=0]="Database",t[t.TypeScript=1]="TypeScript",t[t.All=2]="All"}(FieldNamingStyle=exports.FieldNamingStyle||(exports.FieldNamingStyle={}));var AbstractModel=function(){function t(){}return t.getTableName=function(){return"overload this in child class"},t.from=function(t){return t},t.prototype.assign=function(e,i){var r=this;if("object"==typeof e){var a=this.getAttributeMap();Object.keys(a).forEach((function(a){var n=!0;if(i&&(i.only&&!_.includes(i.only,a)&&(n=!1),i.exclude&&_.includes(i.exclude,a)&&(n=!1)),n)if(e instanceof t&&e.hasAttribute(a))r.setValue(a,e.getValue(a));else if(e.hasOwnProperty(a)){var s=e[a];r.setValue(a,s)}}))}},t.prototype.setValue=function(t,e){var i=this.getAttributeMap()[t];if(!i)throw"unknown attribute "+t+" for "+this.getClassName();if(null===e)this[i.attribute]=null;else if(void 0===e)delete this[i.attribute];else{switch(i.type){case"SqlValueType":break;case"string":_.isNumber(e)?e=e.toString():_.isBoolean(e)?e=e?"true":"false":_.isDate(e)&&(e=this.toDbTimestamp(e));break;case"number":_.isString(e)?e=parseFloat(e):_.isBoolean(e)?e=e?1:0:_.isDate(e)&&(e=e.getTime());break;case"boolean":_.isString(e)?e=!e.match(/(F|N|false|no|0)/i):_.isNumber(e)?e=!!e:_.isDate(e)&&(e=!0);break;case"Date":_.isString(e)&&e.match(/^\d{4}-[01][0-9]-[0-3][0-9][ T][0-2][0-9]:[0-5][0-9]:[0-5][0-9]\s*$/)&&(e+=" GMT"),e=_.isNumber(e)||_.isString(e)||_.isDate(e)?new Date(e):new Date;break;default:throw"AbstractModel.setValue: unknown data type '"+i.type+"'"}this[i.attribute]=e}},t.prototype.getValue=function(t){return this[this.getAttributeMap()[t].attribute]},t.prototype.hasAttribute=function(t){return!!this.getAttributeMap()[t]},t.prototype.clear=function(t){var e=this;_.isArray(t)||(t=[t]);var i=this.getAttributeMap();t.forEach((function(t){var r=i[t];r&&delete e[r.attribute]}))},t.prototype.getPlainObject=function(t,e,i){var r=this,a=this.getAttributeMap(),n=[];void 0===e&&(e=!0),_.forOwn(a,(function(r,a){var s=t==FieldNamingStyle.All||e&&r.virtual;if(!s)if(t==FieldNamingStyle.Database)s=r.physical;else{if(t!=FieldNamingStyle.TypeScript)throw"unknown style "+t;s=r.typeScriptStyle}s&&(i&&!_.includes(i,a)||n.push(a))}));var s={};return n.forEach((function(t){var e=a[t];if(e){var i=r[e.attribute];void 0!==i&&(s[t]=i)}})),s},t.prototype.toDbTimestamp=function(t){var e;return t&&(_.isNumber(t)&&(t=new Date(t)),e=t.toISOString().substr(0,23).replace("T"," ")),e},t.attributeMap={},t}();exports.AbstractModel=AbstractModel;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __extends=this&&this.__extends||function(){var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(e,r)};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(exports,"__esModule",{value:!0});var abstract_model_1=require("./abstract-model"),SmartDbCoreTableModel=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return __extends(e,t),e.getClassName=function(){return"SmartDbCoreTableModel"},e.getTableName=function(){return"smart_db_core_table"},e.getPrimaryKey=function(){return"ctb_id"},e.from=function(t){var r=null;return t&&(r=new e,t.getClassName&&"SmartDbCoreTableModel"==t.getClassName()?Object.assign(r,t):r.assign(t)),r},e.prototype.clone=function(){return e.from(this)},e.prototype.getClassName=function(){return"SmartDbCoreTableModel"},e.prototype.getTableName=function(){return"smart_db_core_table"},e.prototype.getPrimaryKey=function(){return"ctb_id"},e.prototype.getAttributeMap=function(){return e.attributeMap},Object.defineProperty(e.prototype,"id",{get:function(){return this._id},set:function(t){this._id=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"ctb_id",{get:function(){return this._id},set:function(t){this._id=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"name",{get:function(){return this._name},set:function(t){this._name=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"ctb_name",{get:function(){return this._name},set:function(t){this._name=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"type",{get:function(){return this._type},set:function(t){this._type=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"ctb_type",{get:function(){return this._type},set:function(t){this._type=t},enumerable:!0,configurable:!0}),e.attributeMap={id:{alias:"ctb_id",typeScriptStyle:!0,type:"number",attribute:"_id"},ctb_id:{physical:!0,type:"number",attribute:"_id"},name:{alias:"ctb_name",typeScriptStyle:!0,type:"string",attribute:"_name"},ctb_name:{physical:!0,type:"string",attribute:"_name"},type:{alias:"ctb_type",typeScriptStyle:!0,type:"string",attribute:"_type"},ctb_type:{physical:!0,type:"string",attribute:"_type"}},e}(abstract_model_1.AbstractModel);exports.SmartDbCoreTableModel=SmartDbCoreTableModel;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var smart_db_core_table_model_1=require("./smart-db-core-table-model"),smart_db_log_model_1=require("./smart-db-log-model"),smart_db_version_model_1=require("./smart-db-version-model"),smart_db_version_view_model_1=require("./smart-db-version-view-model"),SmartDbDictionary=function(){function e(){}return e.createModel=function(r){var o=e.models[r];return o&&o.cls&&new o.cls},e.models={SmartDbCoreTableModel:{cls:smart_db_core_table_model_1.SmartDbCoreTableModel},SmartDbLogModel:{cls:smart_db_log_model_1.SmartDbLogModel},SmartDbVersionModel:{cls:smart_db_version_model_1.SmartDbVersionModel},SmartDbVersionViewModel:{cls:smart_db_version_view_model_1.SmartDbVersionViewModel}},e}();exports.SmartDbDictionary=SmartDbDictionary;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __extends=this&&this.__extends||function(){var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(e,r)};return function(e,r){function i(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(i.prototype=r.prototype,new i)}}();Object.defineProperty(exports,"__esModule",{value:!0});var abstract_model_1=require("./abstract-model"),SmartDbLogModel=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return __extends(e,t),e.getClassName=function(){return"SmartDbLogModel"},e.getTableName=function(){return"smart_db_log"},e.getPrimaryKey=function(){return"log_id"},e.from=function(t){var r=null;return t&&(r=new e,t.getClassName&&"SmartDbLogModel"==t.getClassName()?Object.assign(r,t):r.assign(t)),r},e.prototype.clone=function(){return e.from(this)},e.prototype.getClassName=function(){return"SmartDbLogModel"},e.prototype.getTableName=function(){return"smart_db_log"},e.prototype.getPrimaryKey=function(){return"log_id"},e.prototype.getAttributeMap=function(){return e.attributeMap},Object.defineProperty(e.prototype,"id",{get:function(){return this._id},set:function(t){this._id=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_id",{get:function(){return this._id},set:function(t){this._id=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"severity",{get:function(){return this._severity},set:function(t){this._severity=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_severity",{get:function(){return this._severity},set:function(t){this._severity=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"type",{get:function(){return this._type},set:function(t){this._type=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_type",{get:function(){return this._type},set:function(t){this._type=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"location",{get:function(){return this._location},set:function(t){this._location=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_location",{get:function(){return this._location},set:function(t){this._location=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"info",{get:function(){return this._info},set:function(t){this._info=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_info",{get:function(){return this._info},set:function(t){this._info=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"data",{get:function(){return this._data},set:function(t){this._data=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_data",{get:function(){return this._data},set:function(t){this._data=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"user",{get:function(){return this._user},set:function(t){this._user=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_user",{get:function(){return this._user},set:function(t){this._user=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"timestamp",{get:function(){return this._timestamp},set:function(t){this._timestamp=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"log_timestamp",{get:function(){return this._timestamp},set:function(t){this._timestamp=t},enumerable:!0,configurable:!0}),e.attributeMap={id:{alias:"log_id",typeScriptStyle:!0,type:"number",attribute:"_id"},log_id:{physical:!0,type:"number",attribute:"_id"},severity:{alias:"log_severity",typeScriptStyle:!0,type:"string",attribute:"_severity"},log_severity:{physical:!0,type:"string",attribute:"_severity"},type:{alias:"log_type",typeScriptStyle:!0,type:"string",attribute:"_type"},log_type:{physical:!0,type:"string",attribute:"_type"},location:{alias:"log_location",typeScriptStyle:!0,type:"string",attribute:"_location"},log_location:{physical:!0,type:"string",attribute:"_location"},info:{alias:"log_info",typeScriptStyle:!0,type:"string",attribute:"_info"},log_info:{physical:!0,type:"string",attribute:"_info"},data:{alias:"log_data",typeScriptStyle:!0,type:"string",attribute:"_data"},log_data:{physical:!0,type:"string",attribute:"_data"},user:{alias:"log_user",typeScriptStyle:!0,type:"string",attribute:"_user"},log_user:{physical:!0,type:"string",attribute:"_user"},timestamp:{alias:"log_timestamp",typeScriptStyle:!0,type:"Date",attribute:"_timestamp"},log_timestamp:{physical:!0,type:"Date",attribute:"_timestamp"}},e}(abstract_model_1.AbstractModel);exports.SmartDbLogModel=SmartDbLogModel;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __extends=this&&this.__extends||function(){var e=function(t,r){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(t,r)};return function(t,r){function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(exports,"__esModule",{value:!0});var abstract_model_1=require("./abstract-model"),SmartDbVersionModel=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return __extends(t,e),t.getClassName=function(){return"SmartDbVersionModel"},t.getTableName=function(){return"smart_db_version"},t.getPrimaryKey=function(){return"ver_id"},t.from=function(e){var r=null;return e&&(r=new t,e.getClassName&&"SmartDbVersionModel"==e.getClassName()?Object.assign(r,e):r.assign(e)),r},t.prototype.clone=function(){return t.from(this)},t.prototype.getClassName=function(){return"SmartDbVersionModel"},t.prototype.getTableName=function(){return"smart_db_version"},t.prototype.getPrimaryKey=function(){return"ver_id"},t.prototype.getAttributeMap=function(){return t.attributeMap},Object.defineProperty(t.prototype,"id",{get:function(){return this._id},set:function(e){this._id=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_id",{get:function(){return this._id},set:function(e){this._id=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"module",{get:function(){return this._module},set:function(e){this._module=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_module",{get:function(){return this._module},set:function(e){this._module=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"sequence",{get:function(){return this._sequence},set:function(e){this._sequence=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_sequence",{get:function(){return this._sequence},set:function(e){this._sequence=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"version",{get:function(){return this._version},set:function(e){this._version=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_version",{get:function(){return this._version},set:function(e){this._version=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"subVersion",{get:function(){return this._subVersion},set:function(e){this._subVersion=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_sub_version",{get:function(){return this._subVersion},set:function(e){this._subVersion=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"revision",{get:function(){return this._revision},set:function(e){this._revision=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_revision",{get:function(){return this._revision},set:function(e){this._revision=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"releaseType",{get:function(){return this._releaseType},set:function(e){this._releaseType=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_release_type",{get:function(){return this._releaseType},set:function(e){this._releaseType=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"installDate",{get:function(){return this._installDate},set:function(e){this._installDate=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_install_date",{get:function(){return this._installDate},set:function(e){this._installDate=e},enumerable:!0,configurable:!0}),t.attributeMap={id:{alias:"ver_id",typeScriptStyle:!0,type:"number",attribute:"_id"},ver_id:{physical:!0,type:"number",attribute:"_id"},module:{alias:"ver_module",typeScriptStyle:!0,type:"string",attribute:"_module"},ver_module:{physical:!0,type:"string",attribute:"_module"},sequence:{alias:"ver_sequence",typeScriptStyle:!0,type:"number",attribute:"_sequence"},ver_sequence:{physical:!0,type:"number",attribute:"_sequence"},version:{alias:"ver_version",typeScriptStyle:!0,type:"string",attribute:"_version"},ver_version:{physical:!0,type:"string",attribute:"_version"},subVersion:{alias:"ver_sub_version",typeScriptStyle:!0,type:"string",attribute:"_subVersion"},ver_sub_version:{physical:!0,type:"string",attribute:"_subVersion"},revision:{alias:"ver_revision",typeScriptStyle:!0,type:"string",attribute:"_revision"},ver_revision:{physical:!0,type:"string",attribute:"_revision"},releaseType:{alias:"ver_release_type",typeScriptStyle:!0,type:"string",attribute:"_releaseType"},ver_release_type:{physical:!0,type:"string",attribute:"_releaseType"},installDate:{alias:"ver_install_date",typeScriptStyle:!0,type:"Date",attribute:"_installDate"},ver_install_date:{physical:!0,type:"Date",attribute:"_installDate"}},t}(abstract_model_1.AbstractModel);exports.SmartDbVersionModel=SmartDbVersionModel;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __extends=this&&this.__extends||function(){var e=function(t,r){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(t,r)};return function(t,r){function n(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(exports,"__esModule",{value:!0});var abstract_model_1=require("./abstract-model"),SmartDbVersionViewModel=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return __extends(t,e),t.getClassName=function(){return"SmartDbVersionViewModel"},t.getTableName=function(){return"smart_db_version_view"},t.getPrimaryKey=function(){return""},t.from=function(e){var r=null;return e&&(r=new t,e.getClassName&&"SmartDbVersionViewModel"==e.getClassName()?Object.assign(r,e):r.assign(e)),r},t.prototype.clone=function(){return t.from(this)},t.prototype.getClassName=function(){return"SmartDbVersionViewModel"},t.prototype.getTableName=function(){return"smart_db_version_view"},t.prototype.getPrimaryKey=function(){return""},t.prototype.getAttributeMap=function(){return t.attributeMap},Object.defineProperty(t.prototype,"id",{get:function(){return this._id},set:function(e){this._id=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_id",{get:function(){return this._id},set:function(e){this._id=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"module",{get:function(){return this._module},set:function(e){this._module=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_module",{get:function(){return this._module},set:function(e){this._module=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"sequence",{get:function(){return this._sequence},set:function(e){this._sequence=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_sequence",{get:function(){return this._sequence},set:function(e){this._sequence=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"version",{get:function(){return this._version},set:function(e){this._version=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_version",{get:function(){return this._version},set:function(e){this._version=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"subVersion",{get:function(){return this._subVersion},set:function(e){this._subVersion=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_sub_version",{get:function(){return this._subVersion},set:function(e){this._subVersion=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"revision",{get:function(){return this._revision},set:function(e){this._revision=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_revision",{get:function(){return this._revision},set:function(e){this._revision=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"releaseType",{get:function(){return this._releaseType},set:function(e){this._releaseType=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_release_type",{get:function(){return this._releaseType},set:function(e){this._releaseType=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"installDate",{get:function(){return this._installDate},set:function(e){this._installDate=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_install_date",{get:function(){return this._installDate},set:function(e){this._installDate=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"versionString",{get:function(){return this._versionString},set:function(e){this._versionString=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"ver_version_string",{get:function(){return this._versionString},set:function(e){this._versionString=e},enumerable:!0,configurable:!0}),t.attributeMap={id:{alias:"ver_id",typeScriptStyle:!0,type:"number",attribute:"_id"},ver_id:{physical:!0,type:"number",attribute:"_id"},module:{alias:"ver_module",typeScriptStyle:!0,type:"string",attribute:"_module"},ver_module:{physical:!0,type:"string",attribute:"_module"},sequence:{alias:"ver_sequence",typeScriptStyle:!0,type:"number",attribute:"_sequence"},ver_sequence:{physical:!0,type:"number",attribute:"_sequence"},version:{alias:"ver_version",typeScriptStyle:!0,type:"string",attribute:"_version"},ver_version:{physical:!0,type:"string",attribute:"_version"},subVersion:{alias:"ver_sub_version",typeScriptStyle:!0,type:"string",attribute:"_subVersion"},ver_sub_version:{physical:!0,type:"string",attribute:"_subVersion"},revision:{alias:"ver_revision",typeScriptStyle:!0,type:"string",attribute:"_revision"},ver_revision:{physical:!0,type:"string",attribute:"_revision"},releaseType:{alias:"ver_release_type",typeScriptStyle:!0,type:"string",attribute:"_releaseType"},ver_release_type:{physical:!0,type:"string",attribute:"_releaseType"},installDate:{alias:"ver_install_date",typeScriptStyle:!0,type:"Date",attribute:"_installDate"},ver_install_date:{physical:!0,type:"Date",attribute:"_installDate"},versionString:{alias:"ver_version_string",typeScriptStyle:!0,type:"SqlValueType",attribute:"_versionString"},ver_version_string:{physical:!0,type:"SqlValueType",attribute:"_versionString"}},t}(abstract_model_1.AbstractModel);exports.SmartDbVersionViewModel=SmartDbVersionViewModel;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __extends=this&&this.__extends||function(){var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(e,r)};return function(e,r){function n(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(n.prototype=r.prototype,new n)}}();Object.defineProperty(exports,"__esModule",{value:!0});var abstract_model_1=require("./abstract-model"),SqliteMasterModel=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return __extends(e,t),e.getClassName=function(){return"SqliteMasterModel"},e.getTableName=function(){return"sqlite_master"},e.from=function(t){var r=new e;return r.assign(t),r},e.prototype.clone=function(){return e.from(this)},e.prototype.getClassName=function(){return"SqliteMasterModel"},e.prototype.getTableName=function(){return"sqlite_master"},e.prototype.getAttributeMap=function(){return e.attributeMap},e.prototype.getPrimaryKey=function(){return"name"},Object.defineProperty(e.prototype,"type",{get:function(){return this._type},set:function(t){this._type=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"name",{get:function(){return this._name},set:function(t){this._name=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"tbl_name",{get:function(){return this._tbl_name},set:function(t){this._tbl_name=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"tblName",{get:function(){return this._tbl_name},set:function(t){this._tbl_name=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"rootpage",{get:function(){return this._rootpage},set:function(t){this._rootpage=t},enumerable:!0,configurable:!0}),Object.defineProperty(e.prototype,"sql",{get:function(){return this._sql},set:function(t){this._sql=t},enumerable:!0,configurable:!0}),e.attributeMap={type:{type:"string",attribute:"_type"},name:{type:"string",attribute:"_name"},tbl_name:{type:"string",attribute:"_tbl_name"},tblName:{alias:"tbl_name",type:"string",attribute:"_tbl_name"},rootpage:{type:"number",attribute:"_rootpage"},sql:{type:"string",attribute:"_sql"}},e}(abstract_model_1.AbstractModel);exports.SqliteMasterModel=SqliteMasterModel;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __extends=this&&this.__extends||function(){var e=function(t,n){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(t,n)};return function(t,n){function r(){this.constructor=t}e(t,n),t.prototype=null===n?Object.create(n):(r.prototype=n.prototype,new r)}}();Object.defineProperty(exports,"__esModule",{value:!0});var abstract_model_1=require("./abstract-model"),SqliteSequenceModel=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return __extends(t,e),t.getClassName=function(){return"SqliteSequenceModel"},t.getTableName=function(){return"sqlite_sequence"},t.getPrimaryKey=function(){return""},t.from=function(e){var n=null;return e&&(n=new t,e.getClassName&&"SqliteSequenceModel"==e.getClassName()?Object.assign(n,e):n.assign(e)),n},t.prototype.clone=function(){return t.from(this)},t.prototype.getClassName=function(){return"SqliteSequenceModel"},t.prototype.getTableName=function(){return"sqlite_sequence"},t.prototype.getPrimaryKey=function(){return""},t.prototype.getAttributeMap=function(){return t.attributeMap},Object.defineProperty(t.prototype,"name",{get:function(){return this._name},set:function(e){this._name=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"seq",{get:function(){return this._seq},set:function(e){this._seq=e},enumerable:!0,configurable:!0}),t.attributeMap={name:{physical:!0,typeScriptStyle:!0,type:"SqlValueType",attribute:"_name"},seq:{physical:!0,typeScriptStyle:!0,type:"SqlValueType",attribute:"_seq"}},t}(abstract_model_1.AbstractModel);exports.SqliteSequenceModel=SqliteSequenceModel;
|
package/smart-db-interface.d.ts
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { AbstractModel } from "./model/abstract-model";
|
|
2
|
-
import { SmartDbDictionary } from "./model/smart-db-dictionary";
|
|
3
|
-
export interface SmartDbOptions {
|
|
4
|
-
module: string;
|
|
5
|
-
sqlFilesDirectory: string;
|
|
6
|
-
smartDbDictionary?: typeof SmartDbDictionary | any[];
|
|
7
|
-
skipAutoUpgrade?: boolean;
|
|
8
|
-
}
|
|
9
|
-
export declare enum SeverityCode {
|
|
10
|
-
Fatal = "F",
|
|
11
|
-
Error = "E",
|
|
12
|
-
Warning = "W",
|
|
13
|
-
Info = "I",
|
|
14
|
-
Debug = "D",
|
|
15
|
-
Local = "L"
|
|
16
|
-
}
|
|
17
|
-
export declare enum SeverityLevel {
|
|
18
|
-
None = 0,
|
|
19
|
-
Fatal = 1,
|
|
20
|
-
Error = 2,
|
|
21
|
-
Warning = 3,
|
|
22
|
-
Info = 4,
|
|
23
|
-
Debug = 5,
|
|
24
|
-
All = 9
|
|
25
|
-
}
|
|
26
|
-
export interface SmartDbRunResult {
|
|
27
|
-
lastId: number;
|
|
28
|
-
changes: number;
|
|
29
|
-
affected: number;
|
|
30
|
-
}
|
|
31
|
-
export interface SmartDbTableInfo {
|
|
32
|
-
fields: SmartDbFieldDescription[];
|
|
33
|
-
}
|
|
34
|
-
export interface SmartDbFieldDescription {
|
|
35
|
-
name: string;
|
|
36
|
-
type: string;
|
|
37
|
-
cid?: string;
|
|
38
|
-
virtual?: boolean;
|
|
39
|
-
pk?: boolean;
|
|
40
|
-
}
|
|
41
|
-
export interface SmartDbSqlComparison {
|
|
42
|
-
compare: string | SqlFieldDescriptor;
|
|
43
|
-
with: string | SqlFieldDescriptor;
|
|
44
|
-
operation?: SqlOperationType;
|
|
45
|
-
}
|
|
46
|
-
export interface SqlWhere {
|
|
47
|
-
[key: string]: SqlValueType | SqlValueType[] | SqlOperation | SqlWhere | SqlWhere[] | SmartDbSqlComparison[];
|
|
48
|
-
or?: SqlWhere | SqlWhere[];
|
|
49
|
-
and?: SqlWhere | SqlWhere[];
|
|
50
|
-
expression?: SmartDbSqlComparison[];
|
|
51
|
-
}
|
|
52
|
-
export declare type SqlUpdateValues = Record<string, SqlValueType>;
|
|
53
|
-
export declare type SqlOrderBy = string | string[];
|
|
54
|
-
declare type SqlGroupBy = string | string[];
|
|
55
|
-
export interface SqlLimit {
|
|
56
|
-
offset?: number;
|
|
57
|
-
limit?: number;
|
|
58
|
-
}
|
|
59
|
-
export interface SectionDescription {
|
|
60
|
-
model: (new () => AbstractModel) | string;
|
|
61
|
-
where?: SqlWhere;
|
|
62
|
-
fields?: string | SqlFieldDescriptor | (string | SqlFieldDescriptor)[] | null;
|
|
63
|
-
groupBy?: SqlGroupBy;
|
|
64
|
-
limit?: SqlLimit;
|
|
65
|
-
}
|
|
66
|
-
export interface SmartDbSqlOptions {
|
|
67
|
-
firstOnly?: boolean;
|
|
68
|
-
where?: SqlWhere;
|
|
69
|
-
fields?: string | SqlFieldDescriptor | (string | SqlFieldDescriptor)[] | null;
|
|
70
|
-
orderBy?: SqlOrderBy;
|
|
71
|
-
groupBy?: SqlGroupBy;
|
|
72
|
-
limit?: SqlLimit;
|
|
73
|
-
count?: boolean;
|
|
74
|
-
distinct?: boolean;
|
|
75
|
-
collapseRow?: boolean;
|
|
76
|
-
indexedBy?: string;
|
|
77
|
-
union?: SectionDescription | SectionDescription[];
|
|
78
|
-
minus?: SectionDescription | SectionDescription[];
|
|
79
|
-
intersect?: SectionDescription | SectionDescription[];
|
|
80
|
-
}
|
|
81
|
-
export declare enum SqlOperationType {
|
|
82
|
-
GT = ">",
|
|
83
|
-
GE = ">=",
|
|
84
|
-
LT = "<",
|
|
85
|
-
LE = "<=",
|
|
86
|
-
EQ = "=",
|
|
87
|
-
NE = "!=",
|
|
88
|
-
IN = "IN",
|
|
89
|
-
LIKE = "LIKE",
|
|
90
|
-
NOT_LIKE = "NOT LIKE",
|
|
91
|
-
IS_NULL = "IS NULL",
|
|
92
|
-
IS_NOT_NULL = "IS NOT NULL",
|
|
93
|
-
LITERAL = "LITERAL",
|
|
94
|
-
VALUE = "VALUE"
|
|
95
|
-
}
|
|
96
|
-
export declare enum SqlFieldOperationType {
|
|
97
|
-
VALUE = "VALUE",
|
|
98
|
-
FIELD = "FIELD",
|
|
99
|
-
COUNT = "COUNT",
|
|
100
|
-
COALESCE = "COALESCE"
|
|
101
|
-
}
|
|
102
|
-
export interface SqlFieldDescriptor {
|
|
103
|
-
operation: SqlFieldOperationType;
|
|
104
|
-
value: SqlFieldDescriptor | SqlValueType | (SqlFieldDescriptor | SqlValueType)[];
|
|
105
|
-
alias?: string;
|
|
106
|
-
literal?: boolean;
|
|
107
|
-
}
|
|
108
|
-
export interface SqlOperation {
|
|
109
|
-
operation: SqlOperationType;
|
|
110
|
-
value: SqlValueType | SqlValueType[];
|
|
111
|
-
key?: string;
|
|
112
|
-
literalOperation?: SqlOperationType;
|
|
113
|
-
}
|
|
114
|
-
export declare type SqlValueType = string | number | boolean | Date;
|
|
115
|
-
export interface DatabaseRow {
|
|
116
|
-
[field: string]: SqlValueType;
|
|
117
|
-
}
|
|
118
|
-
export interface KeyValueList {
|
|
119
|
-
[key: string]: SqlValueType;
|
|
120
|
-
}
|
|
121
|
-
export interface IndexedDatabaseRows<T extends AbstractModel> {
|
|
122
|
-
[key: string]: AbstractModel | T | DatabaseRow;
|
|
123
|
-
}
|
|
124
|
-
export declare const OP: (op: SqlOperationType, value?: string | number | boolean | Date | SqlValueType[], key?: string, literalOp?: SqlOperationType) => SqlOperation;
|
|
125
|
-
export declare const VALUE: (value: SqlValueType, alias?: string, literal?: boolean) => SqlFieldDescriptor;
|
|
126
|
-
export declare const FIELD: (value: string, alias?: string) => SqlFieldDescriptor;
|
|
127
|
-
export declare const COUNT: (value?: string, alias?: string) => SqlFieldDescriptor;
|
|
128
|
-
export declare const COALESCE: (values: (string | number | boolean | Date | SqlFieldDescriptor)[], alias?: string) => SqlFieldDescriptor;
|
|
129
|
-
export declare const NVL: (values: (string | number | boolean | Date | SqlFieldDescriptor)[], alias?: string) => SqlFieldDescriptor;
|
|
130
|
-
export declare const LITERAL: (key: string, value: SqlValueType, op?: SqlOperationType) => SqlOperation;
|
|
131
|
-
export declare const GT: (value: SqlValueType) => SqlOperation;
|
|
132
|
-
export declare const GE: (value: SqlValueType) => SqlOperation;
|
|
133
|
-
export declare const LT: (value: SqlValueType) => SqlOperation;
|
|
134
|
-
export declare const LE: (value: SqlValueType) => SqlOperation;
|
|
135
|
-
export declare const EQ: (value: SqlValueType) => SqlOperation;
|
|
136
|
-
export declare const NE: (value: any) => SqlOperation;
|
|
137
|
-
export declare const IN: (value: SqlValueType[]) => SqlOperation;
|
|
138
|
-
export declare const IS_NULL: () => SqlOperation;
|
|
139
|
-
export declare const IS_NOT_NULL: () => SqlOperation;
|
|
140
|
-
export interface SmartDbSqlBuildDataResults {
|
|
141
|
-
sql: string;
|
|
142
|
-
values: SqlValueType[];
|
|
143
|
-
}
|
|
144
|
-
export declare class SmartDbSqlBuildData {
|
|
145
|
-
sql: string;
|
|
146
|
-
values: SqlValueType[];
|
|
147
|
-
constructor(sql?: string, values?: SqlValueType[]);
|
|
148
|
-
append(data: SmartDbSqlBuildData): void;
|
|
149
|
-
toString(): string;
|
|
150
|
-
results(): SmartDbSqlBuildDataResults;
|
|
151
|
-
}
|
|
152
|
-
export interface SmartDbInterface {
|
|
153
|
-
exists<T extends AbstractModel>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): Promise<boolean>;
|
|
154
|
-
existsSync<T extends AbstractModel>(modelClass: string | (new () => T), type?: "view" | "table" | "index", indexTableName?: string): boolean;
|
|
155
|
-
get<T extends AbstractModel>(modelClass: string | (new () => T), options: SmartDbSqlOptions): Promise<T | T[] | string | string[] | DatabaseRow | SqlValueType | IndexedDatabaseRows<T>>;
|
|
156
|
-
getSync<T extends AbstractModel>(modelClass: string | (new () => T), options: SmartDbSqlOptions): T | T[] | string | string[] | DatabaseRow | SqlValueType | IndexedDatabaseRows<T> | false;
|
|
157
|
-
getFirst<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): Promise<T>;
|
|
158
|
-
getFirstSync<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy): T | false;
|
|
159
|
-
getAll<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): Promise<T[]>;
|
|
160
|
-
getAllSync<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere, fields?: string | string[] | null, orderBy?: SqlOrderBy, limit?: SqlLimit): T[] | false;
|
|
161
|
-
delete<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere): Promise<number>;
|
|
162
|
-
deleteSync<T extends AbstractModel>(modelClass: string | (new () => T), where?: SqlWhere): number | false;
|
|
163
|
-
update<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues, where?: SqlWhere): Promise<number>;
|
|
164
|
-
updateSync<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues, where?: SqlWhere): number | false;
|
|
165
|
-
insert<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues): Promise<number>;
|
|
166
|
-
insertSync<T extends AbstractModel>(modelClass: string | (new () => T), values: SqlUpdateValues): number | false;
|
|
167
|
-
getLastError(): any;
|
|
168
|
-
getLastBuildData(): SmartDbSqlBuildData;
|
|
169
|
-
toDate(d: Date | number | string): Date | null;
|
|
170
|
-
toDbTimestamp(d: Date | number): string;
|
|
171
|
-
toDbDate(d: Date | number): string;
|
|
172
|
-
}
|
|
173
|
-
export {};
|
package/smart-db-interface.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";var __importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t};Object.defineProperty(exports,"__esModule",{value:!0});var SeverityCode,SeverityLevel,SqlOperationType,SqlFieldOperationType,_=__importStar(require("lodash"));!function(e){e.Fatal="F",e.Error="E",e.Warning="W",e.Info="I",e.Debug="D",e.Local="L"}(SeverityCode=exports.SeverityCode||(exports.SeverityCode={})),function(e){e[e.None=0]="None",e[e.Fatal=1]="Fatal",e[e.Error=2]="Error",e[e.Warning=3]="Warning",e[e.Info=4]="Info",e[e.Debug=5]="Debug",e[e.All=9]="All"}(SeverityLevel=exports.SeverityLevel||(exports.SeverityLevel={})),function(e){e.GT=">",e.GE=">=",e.LT="<",e.LE="<=",e.EQ="=",e.NE="!=",e.IN="IN",e.LIKE="LIKE",e.NOT_LIKE="NOT LIKE",e.IS_NULL="IS NULL",e.IS_NOT_NULL="IS NOT NULL",e.LITERAL="LITERAL",e.VALUE="VALUE"}(SqlOperationType=exports.SqlOperationType||(exports.SqlOperationType={})),function(e){e.VALUE="VALUE",e.FIELD="FIELD",e.COUNT="COUNT",e.COALESCE="COALESCE"}(SqlFieldOperationType=exports.SqlFieldOperationType||(exports.SqlFieldOperationType={})),exports.OP=function(e,t,r,o){return r?{operation:e,value:t,key:r,literalOperation:o}:{operation:e,value:t}},exports.VALUE=function(e,t,r){return{operation:SqlFieldOperationType.VALUE,value:e,alias:t,literal:r}},exports.FIELD=function(e,t){return{operation:SqlFieldOperationType.FIELD,value:e,alias:t}},exports.COUNT=function(e,t){return{operation:SqlFieldOperationType.COUNT,value:e,alias:t}},exports.COALESCE=function(e,t){return{operation:SqlFieldOperationType.COALESCE,value:e,alias:t}},exports.NVL=function(e,t){return exports.COALESCE(e,t)},exports.LITERAL=function(e,t,r){return exports.OP(SqlOperationType.LITERAL,t,e,r||SqlOperationType.EQ)},exports.GT=function(e){return exports.OP(SqlOperationType.GT,e)},exports.GE=function(e){return exports.OP(SqlOperationType.GE,e)},exports.LT=function(e){return exports.OP(SqlOperationType.LT,e)},exports.LE=function(e){return exports.OP(SqlOperationType.LE,e)},exports.EQ=function(e){return exports.OP(SqlOperationType.EQ,e)},exports.NE=function(e){return exports.OP(SqlOperationType.NE,e)},exports.IN=function(e){return exports.OP(SqlOperationType.IN,e)},exports.IS_NULL=function(){return exports.OP(SqlOperationType.IS_NULL)},exports.IS_NOT_NULL=function(){return exports.OP(SqlOperationType.IS_NOT_NULL)};var SmartDbSqlBuildData=function(){function e(e,t){this.sql=e||"",this.values=t||[]}return e.prototype.append=function(e){this.sql=this.sql.trim()+" "+e.sql.trim(),this.values=_.concat(this.values,e.values)},e.prototype.toString=function(){return this.sql+" <= ("+this.values.join(", ")+")"},e.prototype.results=function(){return{sql:this.sql,values:this.values}},e}();exports.SmartDbSqlBuildData=SmartDbSqlBuildData;
|