fib-orm 1.4.2 → 2.1.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/License +1 -1
- package/README.md +53 -0
- package/lib/cli/commands/ddlFrom.d.ts +2 -0
- package/lib/cli/commands/ddlFrom.js +75 -0
- package/lib/cli/commands/dumpModel.d.ts +2 -0
- package/lib/cli/commands/dumpModel.js +107 -0
- package/lib/cli/commands/escape.d.ts +2 -0
- package/lib/cli/commands/escape.js +69 -0
- package/lib/cli/commands/spinner.d.ts +2 -0
- package/lib/cli/commands/spinner.js +37 -0
- package/lib/cli/commands/upgrade.d.ts +2 -0
- package/lib/cli/commands/upgrade.js +66 -0
- package/lib/cli/helpers/ddl.d.ts +17 -0
- package/lib/cli/helpers/ddl.js +55 -0
- package/lib/cli/helpers/package.d.ts +25 -0
- package/lib/cli/helpers/package.js +56 -0
- package/lib/cli/index.d.ts +2 -0
- package/lib/cli/index.js +19 -0
- package/lib/codepoints-map.json +762 -0
- package/lib/db-driver/Typo.d.ts +111 -0
- package/lib/db-driver/Typo.js +2 -0
- package/lib/db-driver/built-ins/base.class.d.ts +91 -0
- package/lib/db-driver/built-ins/base.class.js +187 -0
- package/lib/db-driver/built-ins/driver-dm.d.ts +17 -0
- package/lib/db-driver/built-ins/driver-dm.js +54 -0
- package/lib/db-driver/built-ins/driver-mongodb.d.ts +23 -0
- package/lib/db-driver/built-ins/driver-mongodb.js +47 -0
- package/lib/db-driver/built-ins/driver-mysql.d.ts +17 -0
- package/lib/db-driver/built-ins/driver-mysql.js +38 -0
- package/lib/db-driver/built-ins/driver-postgresql.d.ts +21 -0
- package/lib/db-driver/built-ins/driver-postgresql.js +77 -0
- package/lib/db-driver/built-ins/driver-redis.d.ts +11 -0
- package/lib/db-driver/built-ins/driver-redis.js +36 -0
- package/lib/db-driver/built-ins/driver-sqlite.d.ts +16 -0
- package/lib/db-driver/built-ins/driver-sqlite.js +35 -0
- package/lib/db-driver/index.d.ts +3 -0
- package/lib/db-driver/index.js +5 -0
- package/lib/db-driver/utils.d.ts +14 -0
- package/lib/db-driver/utils.js +187 -0
- package/lib/knex/deps/tarn/dist/PendingOperation.d.ts +12 -0
- package/lib/knex/deps/tarn/dist/PendingOperation.js +48 -0
- package/lib/knex/deps/tarn/dist/Pool.d.ts +97 -0
- package/lib/knex/deps/tarn/dist/Pool.js +516 -0
- package/lib/knex/deps/tarn/dist/PromiseInspection.d.ts +16 -0
- package/lib/knex/deps/tarn/dist/PromiseInspection.js +21 -0
- package/lib/knex/deps/tarn/dist/Resource.d.ts +9 -0
- package/lib/knex/deps/tarn/dist/Resource.js +19 -0
- package/lib/knex/deps/tarn/dist/TimeoutError.d.ts +2 -0
- package/lib/knex/deps/tarn/dist/TimeoutError.js +5 -0
- package/lib/knex/deps/tarn/dist/tarn.d.ts +3 -0
- package/lib/knex/deps/tarn/dist/tarn.js +10 -0
- package/lib/knex/deps/tarn/dist/utils.d.ts +14 -0
- package/lib/knex/deps/tarn/dist/utils.js +60 -0
- package/lib/knex/knex.d.ts +2 -0
- package/lib/knex/knex.js +4 -0
- package/lib/knex/sandbox.d.ts +4 -0
- package/lib/knex/sandbox.js +23 -0
- package/lib/orm/Adapters.d.ts +3 -0
- package/lib/orm/Adapters.js +44 -0
- package/lib/orm/AggregateFunctions.d.ts +3 -0
- package/lib/orm/AggregateFunctions.js +177 -0
- package/lib/orm/Associations/Extend.d.ts +22 -0
- package/lib/orm/Associations/Extend.js +274 -0
- package/lib/orm/Associations/Many.d.ts +16 -0
- package/lib/orm/Associations/Many.js +569 -0
- package/lib/orm/Associations/One.d.ts +16 -0
- package/lib/orm/Associations/One.js +310 -0
- package/lib/orm/Associations/_utils.d.ts +14 -0
- package/lib/orm/Associations/_utils.js +53 -0
- package/lib/orm/ChainFind.d.ts +3 -0
- package/lib/orm/ChainFind.js +328 -0
- package/lib/orm/ChainInstance.d.ts +6 -0
- package/lib/orm/ChainInstance.js +74 -0
- package/lib/orm/Debug.d.ts +2 -0
- package/lib/orm/Debug.js +17 -0
- package/lib/orm/Drivers/DB/base.d.ts +2 -0
- package/lib/orm/Drivers/DB/base.js +4 -0
- package/lib/orm/Drivers/DB/dm.d.ts +9 -0
- package/lib/orm/Drivers/DB/dm.js +24 -0
- package/lib/orm/Drivers/DB/mysql.d.ts +9 -0
- package/lib/orm/Drivers/DB/mysql.js +24 -0
- package/lib/orm/Drivers/DB/postgres.d.ts +9 -0
- package/lib/orm/Drivers/DB/postgres.js +23 -0
- package/lib/orm/Drivers/DB/sqlite3.d.ts +14 -0
- package/lib/orm/Drivers/DB/sqlite3.js +40 -0
- package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
- package/lib/orm/Drivers/DML/_ddl-sql.js +52 -0
- package/lib/orm/Drivers/DML/_dml-helpers.d.ts +19 -0
- package/lib/orm/Drivers/DML/_dml-helpers.js +51 -0
- package/lib/orm/Drivers/DML/_shared.d.ts +9 -0
- package/lib/orm/Drivers/DML/_shared.js +39 -0
- package/lib/orm/Drivers/DML/_utils.d.ts +8 -0
- package/lib/orm/Drivers/DML/_utils.js +67 -0
- package/lib/orm/Drivers/DML/constants.d.ts +3 -0
- package/lib/orm/Drivers/DML/constants.js +3 -0
- package/lib/orm/Drivers/DML/dm.d.ts +2 -0
- package/lib/orm/Drivers/DML/dm.js +294 -0
- package/lib/orm/Drivers/DML/index.d.ts +4 -0
- package/lib/orm/Drivers/DML/index.js +7 -0
- package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
- package/lib/orm/Drivers/DML/mysql.js +252 -0
- package/lib/orm/Drivers/DML/postgres.d.ts +2 -0
- package/lib/orm/Drivers/DML/postgres.js +277 -0
- package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
- package/lib/orm/Drivers/DML/sqlite.js +267 -0
- package/lib/orm/Error.d.ts +11 -0
- package/lib/orm/Error.js +41 -0
- package/lib/orm/Helpers.d.ts +35 -0
- package/lib/orm/Helpers.js +234 -0
- package/lib/orm/Hook.d.ts +13 -0
- package/lib/orm/Hook.js +100 -0
- package/lib/orm/Instance.d.ts +2 -0
- package/lib/orm/Instance.js +698 -0
- package/lib/orm/LazyLoad.d.ts +4 -0
- package/lib/orm/LazyLoad.js +76 -0
- package/lib/orm/Model.d.ts +6 -0
- package/lib/orm/Model.js +974 -0
- package/lib/orm/ORM.d.ts +37 -0
- package/lib/orm/ORM.js +242 -0
- package/lib/orm/Patch/fib-cache.d.ts +39 -0
- package/lib/orm/Patch/fib-cache.js +2 -0
- package/lib/orm/Patch/plugin.d.ts +4 -0
- package/lib/orm/Patch/plugin.js +14 -0
- package/lib/orm/Property.d.ts +9 -0
- package/lib/orm/Property.js +71 -0
- package/lib/orm/Settings.d.ts +42 -0
- package/lib/orm/Settings.js +120 -0
- package/lib/orm/Singleton.d.ts +5 -0
- package/lib/orm/Singleton.js +73 -0
- package/lib/orm/Typo/Adapter.d.ts +5 -0
- package/lib/orm/Typo/Adapter.js +2 -0
- package/lib/orm/Typo/DMLDriver.d.ts +209 -0
- package/lib/orm/Typo/DMLDriver.js +2 -0
- package/lib/orm/Typo/Db.d.ts +35 -0
- package/lib/orm/Typo/Db.js +2 -0
- package/lib/orm/Typo/Error.d.ts +29 -0
- package/lib/orm/Typo/Error.js +2 -0
- package/lib/orm/Typo/ORM.d.ts +231 -0
- package/lib/orm/Typo/ORM.js +4 -0
- package/lib/orm/Typo/Validators.d.ts +31 -0
- package/lib/orm/Typo/Validators.js +3 -0
- package/lib/orm/Typo/_common.d.ts +37 -0
- package/lib/orm/Typo/_common.js +2 -0
- package/lib/orm/Typo/assoc.d.ts +221 -0
- package/lib/orm/Typo/assoc.js +2 -0
- package/lib/orm/Typo/hook.d.ts +42 -0
- package/lib/orm/Typo/hook.js +2 -0
- package/lib/orm/Typo/instance.d.ts +156 -0
- package/lib/orm/Typo/instance.js +2 -0
- package/lib/orm/Typo/model.d.ts +311 -0
- package/lib/orm/Typo/model.js +7 -0
- package/lib/orm/Typo/patch.d.ts +31 -0
- package/lib/orm/Typo/patch.js +2 -0
- package/lib/orm/Typo/property.d.ts +57 -0
- package/lib/orm/Typo/property.js +2 -0
- package/lib/orm/Typo/query.d.ts +216 -0
- package/lib/orm/Typo/query.js +2 -0
- package/lib/orm/Typo/settings.d.ts +35 -0
- package/lib/orm/Typo/settings.js +6 -0
- package/lib/orm/Typo/synchronous.d.ts +47 -0
- package/lib/orm/Typo/synchronous.js +2 -0
- package/lib/orm/Utilities.d.ts +178 -0
- package/lib/orm/Utilities.js +975 -0
- package/lib/orm/Validators.d.ts +33 -0
- package/lib/orm/Validators.js +101 -0
- package/lib/orm/Where/common.d.ts +1 -0
- package/lib/orm/Where/common.js +7 -0
- package/lib/orm/Where/filterDate.d.ts +11 -0
- package/lib/orm/Where/filterDate.js +58 -0
- package/lib/orm/index.d.ts +67 -0
- package/lib/orm/index.js +158 -0
- package/lib/orm-core/callback.d.ts +12 -0
- package/lib/orm-core/callback.js +2 -0
- package/lib/orm-core/dialect.d.ts +80 -0
- package/lib/orm-core/dialect.js +2 -0
- package/lib/orm-core/error.d.ts +16 -0
- package/lib/orm-core/error.js +29 -0
- package/lib/orm-core/index.d.ts +6 -0
- package/lib/orm-core/index.js +7 -0
- package/lib/orm-core/sync.d.ts +12 -0
- package/lib/orm-core/sync.js +2 -0
- package/lib/orm-core/utils.d.ts +1 -0
- package/lib/orm-core/utils.js +6 -0
- package/lib/orm-property/Property.d.ts +78 -0
- package/lib/orm-property/Property.js +2 -0
- package/lib/orm-property/Utils.d.ts +9 -0
- package/lib/orm-property/Utils.js +13 -0
- package/lib/orm-property/customTypes.d.ts +2 -0
- package/lib/orm-property/customTypes.js +13 -0
- package/lib/orm-property/index.d.ts +10 -0
- package/lib/orm-property/index.js +23 -0
- package/lib/orm-property/transformer.d.ts +5 -0
- package/lib/orm-property/transformer.js +10 -0
- package/lib/orm-property/transformers/dm.d.ts +29 -0
- package/lib/orm-property/transformers/dm.js +238 -0
- package/lib/orm-property/transformers/index.d.ts +4 -0
- package/lib/orm-property/transformers/index.js +6 -0
- package/lib/orm-property/transformers/mysql.d.ts +101 -0
- package/lib/orm-property/transformers/mysql.js +262 -0
- package/lib/orm-property/transformers/postgresql.d.ts +54 -0
- package/lib/orm-property/transformers/postgresql.js +190 -0
- package/lib/orm-property/transformers/sqlite.d.ts +19 -0
- package/lib/orm-property/transformers/sqlite.js +172 -0
- package/lib/sql-ddl-sync/Dialects/dm.d.ts +39 -0
- package/lib/sql-ddl-sync/Dialects/dm.js +297 -0
- package/lib/sql-ddl-sync/Dialects/index.d.ts +6 -0
- package/lib/sql-ddl-sync/Dialects/index.js +8 -0
- package/lib/sql-ddl-sync/Dialects/mysql.d.ts +38 -0
- package/lib/sql-ddl-sync/Dialects/mysql.js +241 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +39 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.js +287 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +43 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.js +257 -0
- package/lib/sql-ddl-sync/SQL.d.ts +12 -0
- package/lib/sql-ddl-sync/SQL.js +108 -0
- package/lib/sql-ddl-sync/Typo/Collection.d.ts +13 -0
- package/lib/sql-ddl-sync/Typo/Collection.js +2 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.js +2 -0
- package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
- package/lib/sql-ddl-sync/Typo/Dialect.js +2 -0
- package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
- package/lib/sql-ddl-sync/Typo/Driver.js +2 -0
- package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
- package/lib/sql-ddl-sync/Typo/Query.js +2 -0
- package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
- package/lib/sql-ddl-sync/Typo/SQL.js +2 -0
- package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
- package/lib/sql-ddl-sync/Typo/_common.js +2 -0
- package/lib/sql-ddl-sync/Utils.d.ts +39 -0
- package/lib/sql-ddl-sync/Utils.js +217 -0
- package/lib/sql-ddl-sync/index.d.ts +87 -0
- package/lib/sql-ddl-sync/index.js +372 -0
- package/lib/sql-query/Comparators.d.ts +3 -0
- package/lib/sql-query/Comparators.js +95 -0
- package/lib/sql-query/Create.d.ts +35 -0
- package/lib/sql-query/Create.js +78 -0
- package/lib/sql-query/Dialects/dm.d.ts +3 -0
- package/lib/sql-query/Dialects/dm.js +75 -0
- package/lib/sql-query/Dialects/index.d.ts +10 -0
- package/lib/sql-query/Dialects/index.js +13 -0
- package/lib/sql-query/Dialects/mssql.d.ts +3 -0
- package/lib/sql-query/Dialects/mssql.js +74 -0
- package/lib/sql-query/Dialects/mysql.d.ts +3 -0
- package/lib/sql-query/Dialects/mysql.js +101 -0
- package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
- package/lib/sql-query/Dialects/postgresql.js +77 -0
- package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
- package/lib/sql-query/Dialects/sqlite.js +62 -0
- package/lib/sql-query/Helpers.d.ts +37 -0
- package/lib/sql-query/Helpers.js +260 -0
- package/lib/sql-query/Insert.d.ts +12 -0
- package/lib/sql-query/Insert.js +34 -0
- package/lib/sql-query/Query.d.ts +28 -0
- package/lib/sql-query/Query.js +120 -0
- package/lib/sql-query/Remove.d.ts +16 -0
- package/lib/sql-query/Remove.js +94 -0
- package/lib/sql-query/Select.d.ts +62 -0
- package/lib/sql-query/Select.js +534 -0
- package/lib/sql-query/Set.d.ts +4 -0
- package/lib/sql-query/Set.js +16 -0
- package/lib/sql-query/Typo/Aggregation.d.ts +32 -0
- package/lib/sql-query/Typo/Aggregation.js +2 -0
- package/lib/sql-query/Typo/Comparators.d.ts +140 -0
- package/lib/sql-query/Typo/Comparators.js +2 -0
- package/lib/sql-query/Typo/Dialect.d.ts +39 -0
- package/lib/sql-query/Typo/Dialect.js +2 -0
- package/lib/sql-query/Typo/Field.d.ts +8 -0
- package/lib/sql-query/Typo/Field.js +2 -0
- package/lib/sql-query/Typo/Helper.d.ts +4 -0
- package/lib/sql-query/Typo/Helper.js +2 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.js +2 -0
- package/lib/sql-query/Typo/Query.d.ts +34 -0
- package/lib/sql-query/Typo/Query.js +17 -0
- package/lib/sql-query/Typo/Sql.d.ts +143 -0
- package/lib/sql-query/Typo/Sql.js +2 -0
- package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
- package/lib/sql-query/Typo/SubQuery.js +2 -0
- package/lib/sql-query/Typo/index.d.ts +9 -0
- package/lib/sql-query/Typo/index.js +2 -0
- package/lib/sql-query/Update.d.ts +15 -0
- package/lib/sql-query/Update.js +39 -0
- package/lib/sql-query/Where.d.ts +4 -0
- package/lib/sql-query/Where.js +280 -0
- package/package.json +71 -36
- package/.travis.yml +0 -14
- package/@types/index.d.ts +0 -254
- package/@types/orm_mirror/orm.d.ts +0 -275
- package/@types/orm_mirror/sql-query.d.ts +0 -79
- package/Readme.md +0 -843
- package/lib/index.js +0 -13
- package/lib/modules/mysql.js +0 -35
- package/lib/modules/sqlite3.js +0 -27
- package/lib/patch.js +0 -271
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Driver = void 0;
|
|
4
|
+
const util = require("util");
|
|
5
|
+
const mysql_1 = require("../DB/mysql");
|
|
6
|
+
const shared = require("./_shared");
|
|
7
|
+
const DDL = require("./_ddl-sql");
|
|
8
|
+
const Query_1 = require("../../../sql-query/Query");
|
|
9
|
+
const Sync = require("../../../sql-ddl-sync/index");
|
|
10
|
+
const utils = require("./_utils");
|
|
11
|
+
const Utilities = require("../../Utilities");
|
|
12
|
+
const _dml_helpers_1 = require("./_dml-helpers");
|
|
13
|
+
exports.Driver = function (config, connection, opts) {
|
|
14
|
+
this.dialect = 'mysql';
|
|
15
|
+
this.opts = opts || {};
|
|
16
|
+
this.customTypes = {};
|
|
17
|
+
Object.defineProperty(this, 'db', {
|
|
18
|
+
value: connection || new mysql_1.Database(config),
|
|
19
|
+
writable: false
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, 'sqlDriver', {
|
|
22
|
+
value: this.db,
|
|
23
|
+
writable: false
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, 'config', {
|
|
26
|
+
value: this.db.config,
|
|
27
|
+
writable: false
|
|
28
|
+
});
|
|
29
|
+
if (!this.config.timezone)
|
|
30
|
+
this.config.timezone = "local";
|
|
31
|
+
Object.defineProperty(this, 'ddlSync', {
|
|
32
|
+
value: Sync.dialect(this.dialect),
|
|
33
|
+
writable: false
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, 'query', {
|
|
36
|
+
value: new Query_1.Query({ dialect: this.dialect, timezone: this.config.timezone }),
|
|
37
|
+
writable: false
|
|
38
|
+
});
|
|
39
|
+
utils.getKnexInstance(this);
|
|
40
|
+
this.aggregate_functions = ["ABS", "CEIL", "FLOOR", "ROUND",
|
|
41
|
+
"AVG", "MIN", "MAX",
|
|
42
|
+
"LOG", "LOG2", "LOG10", "EXP", "POWER",
|
|
43
|
+
"ACOS", "ASIN", "ATAN", "COS", "SIN", "TAN",
|
|
44
|
+
"CONV", ["RANDOM", "RAND"], "RADIANS", "DEGREES",
|
|
45
|
+
"SUM", "COUNT",
|
|
46
|
+
"DISTINCT"];
|
|
47
|
+
};
|
|
48
|
+
util.extend(exports.Driver.prototype, shared, DDL);
|
|
49
|
+
exports.Driver.prototype.ping = function (cb) {
|
|
50
|
+
this.db.ping();
|
|
51
|
+
if (cb)
|
|
52
|
+
setImmediate(cb);
|
|
53
|
+
return this;
|
|
54
|
+
};
|
|
55
|
+
exports.Driver.prototype.on = function (ev, cb) {
|
|
56
|
+
if (ev == "error") {
|
|
57
|
+
this.db.eventor.on("error", cb);
|
|
58
|
+
this.db.eventor.on("unhandledError", cb);
|
|
59
|
+
}
|
|
60
|
+
return this;
|
|
61
|
+
};
|
|
62
|
+
exports.Driver.prototype.connect = function (cb) {
|
|
63
|
+
const syncResponse = Utilities.catchBlocking(() => {
|
|
64
|
+
return this.db.connect();
|
|
65
|
+
});
|
|
66
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
67
|
+
return syncResponse.result;
|
|
68
|
+
};
|
|
69
|
+
exports.Driver.prototype.reconnect = function (cb) {
|
|
70
|
+
const connOpts = this.config.href || this.config;
|
|
71
|
+
this.db = new mysql_1.Database(connOpts);
|
|
72
|
+
const syncResponse = Utilities.catchBlocking(() => {
|
|
73
|
+
return this.connect();
|
|
74
|
+
});
|
|
75
|
+
if (typeof cb === 'function')
|
|
76
|
+
cb(null, syncResponse.result);
|
|
77
|
+
return syncResponse.result;
|
|
78
|
+
};
|
|
79
|
+
exports.Driver.prototype.close = function (cb) {
|
|
80
|
+
const errResults = Utilities.catchBlocking(() => this.db.close());
|
|
81
|
+
Utilities.takeAwayResult(errResults, { no_throw: !!cb, callback: cb });
|
|
82
|
+
return errResults.result;
|
|
83
|
+
};
|
|
84
|
+
exports.Driver.prototype.getQuery = function () {
|
|
85
|
+
return this.query;
|
|
86
|
+
};
|
|
87
|
+
exports.Driver.prototype.execSimpleQuery = function (query, cb) {
|
|
88
|
+
if (this.opts.debug) {
|
|
89
|
+
require("../../Debug").sql('mysql', query);
|
|
90
|
+
}
|
|
91
|
+
return this.db.query(query, cb);
|
|
92
|
+
};
|
|
93
|
+
exports.Driver.prototype.find = function (selectFields, table, conditions, opts) {
|
|
94
|
+
const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
|
|
95
|
+
const __pointTypeMapsTo = opts.__pointTypeMapsTo || [];
|
|
96
|
+
(0, _dml_helpers_1.filterFieldsOnFind)({
|
|
97
|
+
dmlDriver: this,
|
|
98
|
+
pointPropertiesMapsTo: __pointTypeMapsTo,
|
|
99
|
+
}, {
|
|
100
|
+
selectFields,
|
|
101
|
+
});
|
|
102
|
+
const ctx = {
|
|
103
|
+
table,
|
|
104
|
+
fromTuple: from_tuple,
|
|
105
|
+
selectFields,
|
|
106
|
+
selectVirtualFields: opts.selectVirtualFields || []
|
|
107
|
+
};
|
|
108
|
+
let q = this.query.select();
|
|
109
|
+
q = typeof (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect) === 'function' ? (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect.call(this, ctx, q)) || q : Utilities.DEFAULT_GENERATE_SQL_QUERY_SELECT.call(this, ctx, q);
|
|
110
|
+
if (opts.offset) {
|
|
111
|
+
q.offset(opts.offset);
|
|
112
|
+
}
|
|
113
|
+
if (typeof opts.limit == "number") {
|
|
114
|
+
q.limit(opts.limit);
|
|
115
|
+
}
|
|
116
|
+
else if (opts.offset) {
|
|
117
|
+
// OFFSET cannot be used without LIMIT so we use the biggest BIGINT number possible
|
|
118
|
+
q.limit('18446744073709551615');
|
|
119
|
+
}
|
|
120
|
+
utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions, opts.topConditions]);
|
|
121
|
+
utils.buildOrderToQuery.apply(this, [q, opts.order]);
|
|
122
|
+
utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
|
|
123
|
+
utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
|
|
124
|
+
const results = this.execSimpleQuery(q.build());
|
|
125
|
+
if (__pointTypeMapsTo.length > 0 && Array.isArray(results)) {
|
|
126
|
+
results.forEach(item => {
|
|
127
|
+
__pointTypeMapsTo.forEach(field => {
|
|
128
|
+
item[field] = (0, _dml_helpers_1.safeParseJson)(item[field]);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return results;
|
|
133
|
+
};
|
|
134
|
+
exports.Driver.prototype.count = function (table, conditions, opts, cb) {
|
|
135
|
+
const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
|
|
136
|
+
const q = this.query.select()
|
|
137
|
+
.from(from_tuple)
|
|
138
|
+
.count(null, 'c');
|
|
139
|
+
utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions]);
|
|
140
|
+
utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
|
|
141
|
+
utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
|
|
142
|
+
return this.execSimpleQuery(q.build(), cb);
|
|
143
|
+
};
|
|
144
|
+
exports.Driver.prototype.insert = function (table, data, keyProperties, cb) {
|
|
145
|
+
const q = this.query.insert()
|
|
146
|
+
.into(table)
|
|
147
|
+
.set(data)
|
|
148
|
+
.build();
|
|
149
|
+
const syncResponse = Utilities.catchBlocking(() => {
|
|
150
|
+
const info = this.execSimpleQuery(q);
|
|
151
|
+
const ids = {};
|
|
152
|
+
if (keyProperties) {
|
|
153
|
+
if (keyProperties.length == 1 && info.hasOwnProperty("insertId") && info.insertId !== 0) {
|
|
154
|
+
ids[keyProperties[0].name] = info.insertId;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
for (let i = 0, prop; i < keyProperties.length; i++) {
|
|
158
|
+
prop = keyProperties[i];
|
|
159
|
+
ids[prop.name] = data[prop.mapsTo];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return ids;
|
|
164
|
+
});
|
|
165
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
166
|
+
return syncResponse.result;
|
|
167
|
+
};
|
|
168
|
+
exports.Driver.prototype.update = function (table, changes, conditions, cb) {
|
|
169
|
+
var q = this.query.update()
|
|
170
|
+
.into(table)
|
|
171
|
+
.set(changes)
|
|
172
|
+
.where(conditions)
|
|
173
|
+
.build();
|
|
174
|
+
return this.execSimpleQuery(q, cb);
|
|
175
|
+
};
|
|
176
|
+
exports.Driver.prototype.remove = function (table, conditions, cb) {
|
|
177
|
+
var q = this.query.remove()
|
|
178
|
+
.from(table)
|
|
179
|
+
.where(conditions)
|
|
180
|
+
.build();
|
|
181
|
+
return this.execSimpleQuery(q, cb);
|
|
182
|
+
};
|
|
183
|
+
exports.Driver.prototype.clear = function (table, cb) {
|
|
184
|
+
var q = "TRUNCATE TABLE " + this.query.escapeId(table);
|
|
185
|
+
return this.execSimpleQuery(q, cb);
|
|
186
|
+
};
|
|
187
|
+
exports.Driver.prototype.valueToProperty = function (value, property) {
|
|
188
|
+
var customType;
|
|
189
|
+
switch (property.type) {
|
|
190
|
+
case "date":
|
|
191
|
+
if (util.isNumber(value) || util.isString(value))
|
|
192
|
+
value = new Date(value);
|
|
193
|
+
break;
|
|
194
|
+
case "boolean":
|
|
195
|
+
value = !!value;
|
|
196
|
+
break;
|
|
197
|
+
case "object":
|
|
198
|
+
if (typeof value == "object" && !Buffer.isBuffer(value)) {
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
value = JSON.parse(value);
|
|
203
|
+
}
|
|
204
|
+
catch (e) {
|
|
205
|
+
value = null;
|
|
206
|
+
}
|
|
207
|
+
break;
|
|
208
|
+
case "binary": {
|
|
209
|
+
// compat with standard behavior of `JSON.stringify`/`JSON.parse`
|
|
210
|
+
if (value && value.type === 'Buffer' && Array.isArray(value.data)) {
|
|
211
|
+
value = Buffer.from(value.data);
|
|
212
|
+
}
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
default:
|
|
216
|
+
customType = this.customTypes[property.type];
|
|
217
|
+
if (customType && 'valueToProperty' in customType) {
|
|
218
|
+
value = customType.valueToProperty(value, property);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return value;
|
|
222
|
+
};
|
|
223
|
+
exports.Driver.prototype.propertyToValue = function (value, property) {
|
|
224
|
+
switch (property.type) {
|
|
225
|
+
case "date":
|
|
226
|
+
if (util.isNumber(value) || util.isString(value))
|
|
227
|
+
value = new Date(value);
|
|
228
|
+
break;
|
|
229
|
+
case "boolean":
|
|
230
|
+
value = (value) ? 1 : 0;
|
|
231
|
+
break;
|
|
232
|
+
case "object":
|
|
233
|
+
if (value !== null) {
|
|
234
|
+
value = JSON.stringify(value);
|
|
235
|
+
}
|
|
236
|
+
break;
|
|
237
|
+
case "point":
|
|
238
|
+
return function () { return 'POINT(' + value.x + ', ' + value.y + ')'; };
|
|
239
|
+
case "binary": {
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
default:
|
|
243
|
+
const customType = this.customTypes[property.type];
|
|
244
|
+
if (customType && 'propertyToValue' in customType) {
|
|
245
|
+
value = customType.propertyToValue(value);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return value;
|
|
249
|
+
};
|
|
250
|
+
Object.defineProperty(exports.Driver.prototype, "isSql", {
|
|
251
|
+
value: true
|
|
252
|
+
});
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/// <reference lib="es2015" />
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Driver = void 0;
|
|
5
|
+
const util = require("util");
|
|
6
|
+
const shared = require("./_shared");
|
|
7
|
+
const DDL = require("./_ddl-sql");
|
|
8
|
+
const Sync = require("../../../sql-ddl-sync/index");
|
|
9
|
+
const Query_1 = require("../../../sql-query/Query");
|
|
10
|
+
const utils = require("./_utils");
|
|
11
|
+
const Utilities = require("../../Utilities");
|
|
12
|
+
const postgres_1 = require("../DB/postgres");
|
|
13
|
+
exports.Driver = function (config, connection, opts) {
|
|
14
|
+
this.dialect = 'postgresql';
|
|
15
|
+
this.opts = opts || {};
|
|
16
|
+
this.customTypes = {};
|
|
17
|
+
Object.defineProperty(this, 'db', {
|
|
18
|
+
value: connection || new postgres_1.default(config),
|
|
19
|
+
writable: false
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, 'sqlDriver', {
|
|
22
|
+
value: this.db,
|
|
23
|
+
writable: false
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, 'config', {
|
|
26
|
+
value: this.db.config,
|
|
27
|
+
writable: false
|
|
28
|
+
});
|
|
29
|
+
if (!this.config.timezone)
|
|
30
|
+
this.config.timezone = "local";
|
|
31
|
+
Object.defineProperty(this, 'ddlSync', {
|
|
32
|
+
value: Sync.dialect(this.dialect),
|
|
33
|
+
writable: false
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, 'query', {
|
|
36
|
+
value: new Query_1.Query({ dialect: this.dialect, timezone: this.config.timezone }),
|
|
37
|
+
writable: false
|
|
38
|
+
});
|
|
39
|
+
utils.getKnexInstance(this);
|
|
40
|
+
this.aggregate_functions = [
|
|
41
|
+
"ABS", "CEIL", "FLOOR", "ROUND",
|
|
42
|
+
"AVG", "MIN", "MAX",
|
|
43
|
+
"LOG", "EXP", "POWER",
|
|
44
|
+
"ACOS", "ASIN", "ATAN", "COS", "SIN", "TAN",
|
|
45
|
+
"RANDOM", "RADIANS", "DEGREES",
|
|
46
|
+
"SUM", "COUNT",
|
|
47
|
+
"DISTINCT"
|
|
48
|
+
];
|
|
49
|
+
};
|
|
50
|
+
util.extend(exports.Driver.prototype, shared, DDL);
|
|
51
|
+
exports.Driver.prototype.ping = function (cb) {
|
|
52
|
+
this.execSimpleQuery("SELECT * FROM pg_stat_activity LIMIT 1", function () {
|
|
53
|
+
return cb === null || cb === void 0 ? void 0 : cb();
|
|
54
|
+
});
|
|
55
|
+
return this;
|
|
56
|
+
};
|
|
57
|
+
exports.Driver.prototype.on = function (ev, cb) {
|
|
58
|
+
if (ev == "error") {
|
|
59
|
+
this.db.eventor.on("error", cb);
|
|
60
|
+
this.db.eventor.on("unhandledError", cb);
|
|
61
|
+
}
|
|
62
|
+
return this;
|
|
63
|
+
};
|
|
64
|
+
exports.Driver.prototype.connect = function (cb) {
|
|
65
|
+
const syncResponse = Utilities.catchBlocking(() => {
|
|
66
|
+
return this.db.connect();
|
|
67
|
+
});
|
|
68
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
69
|
+
return syncResponse.result;
|
|
70
|
+
};
|
|
71
|
+
exports.Driver.prototype.reconnect = function (cb) {
|
|
72
|
+
const connOpts = this.config.href || this.config;
|
|
73
|
+
this.db = new postgres_1.default(connOpts);
|
|
74
|
+
const syncResponse = Utilities.catchBlocking(() => {
|
|
75
|
+
return this.connect();
|
|
76
|
+
});
|
|
77
|
+
if (typeof cb === 'function')
|
|
78
|
+
cb(null, syncResponse.result);
|
|
79
|
+
return syncResponse.result;
|
|
80
|
+
};
|
|
81
|
+
exports.Driver.prototype.close = function (cb) {
|
|
82
|
+
const errResults = Utilities.catchBlocking(() => this.db.close());
|
|
83
|
+
Utilities.takeAwayResult(errResults, { no_throw: !!cb, callback: cb });
|
|
84
|
+
return errResults.result;
|
|
85
|
+
};
|
|
86
|
+
exports.Driver.prototype.getQuery = function () {
|
|
87
|
+
return this.query;
|
|
88
|
+
};
|
|
89
|
+
exports.Driver.prototype.execSimpleQuery = function (query, cb) {
|
|
90
|
+
if (this.opts.debug) {
|
|
91
|
+
require("../../Debug").sql('postgresql', query);
|
|
92
|
+
}
|
|
93
|
+
return this.db.query(query, cb);
|
|
94
|
+
};
|
|
95
|
+
exports.Driver.prototype.find = function (selectFields, table, conditions, opts) {
|
|
96
|
+
const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
|
|
97
|
+
const ctx = {
|
|
98
|
+
table,
|
|
99
|
+
fromTuple: from_tuple,
|
|
100
|
+
selectFields,
|
|
101
|
+
selectVirtualFields: opts.selectVirtualFields || []
|
|
102
|
+
};
|
|
103
|
+
let q = this.query.select();
|
|
104
|
+
q = typeof (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect) === 'function' ? (opts === null || opts === void 0 ? void 0 : opts.generateSqlSelect.call(this, ctx, q)) || q : Utilities.DEFAULT_GENERATE_SQL_QUERY_SELECT.call(this, ctx, q);
|
|
105
|
+
if (opts.offset) {
|
|
106
|
+
q.offset(opts.offset);
|
|
107
|
+
}
|
|
108
|
+
if (typeof opts.limit == "number") {
|
|
109
|
+
q.limit(opts.limit);
|
|
110
|
+
}
|
|
111
|
+
if (opts.order) {
|
|
112
|
+
for (let i = 0; i < opts.order.length; i++) {
|
|
113
|
+
q.order(opts.order[i][0], opts.order[i][1]);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions, opts.topConditions]);
|
|
117
|
+
utils.buildOrderToQuery.apply(this, [q, opts.order]);
|
|
118
|
+
utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
|
|
119
|
+
utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
|
|
120
|
+
return this.execSimpleQuery(q.build());
|
|
121
|
+
};
|
|
122
|
+
exports.Driver.prototype.count = function (table, conditions, opts, cb) {
|
|
123
|
+
const { from_tuple, pure_table, alias } = Utilities.parseTableInputForSelect(table);
|
|
124
|
+
const q = this.query.select().from(from_tuple).count(null, 'c');
|
|
125
|
+
utils.buildBaseConditionsToQuery.apply(this, [q, pure_table, conditions]);
|
|
126
|
+
utils.buildMergeToQuery.apply(this, [q, opts.merge, conditions]);
|
|
127
|
+
utils.buildExistsToQuery.apply(this, [q, alias, opts.exists]);
|
|
128
|
+
return this.execSimpleQuery(q.build(), cb);
|
|
129
|
+
};
|
|
130
|
+
exports.Driver.prototype.insert = function (table, data, keyProperties, cb) {
|
|
131
|
+
const q = this.query.insert().into(table).set(data).build();
|
|
132
|
+
const syncResponse = Utilities.catchBlocking(() => {
|
|
133
|
+
const results = this.execSimpleQuery(q + " RETURNING *");
|
|
134
|
+
const ids = {};
|
|
135
|
+
if (keyProperties) {
|
|
136
|
+
for (let i = 0; i < keyProperties.length; i++) {
|
|
137
|
+
const prop = keyProperties[i];
|
|
138
|
+
// Zero is a valid value for an ID column
|
|
139
|
+
ids[prop.name] = results[0][prop.mapsTo] !== undefined ? results[0][prop.mapsTo] : null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return ids;
|
|
143
|
+
});
|
|
144
|
+
Utilities.takeAwayResult(syncResponse, { callback: cb });
|
|
145
|
+
return syncResponse.result;
|
|
146
|
+
};
|
|
147
|
+
exports.Driver.prototype.update = function (table, changes, conditions, cb) {
|
|
148
|
+
const q = this.query.update().into(table).set(changes).where(conditions).build();
|
|
149
|
+
return this.execSimpleQuery(q, cb);
|
|
150
|
+
};
|
|
151
|
+
exports.Driver.prototype.remove = function (table, conditions, cb) {
|
|
152
|
+
const q = this.query.remove().from(table).where(conditions).build();
|
|
153
|
+
return this.execSimpleQuery(q, cb);
|
|
154
|
+
};
|
|
155
|
+
exports.Driver.prototype.clear = function (table, cb) {
|
|
156
|
+
const q = "TRUNCATE TABLE " + this.query.escapeId(table);
|
|
157
|
+
return this.execSimpleQuery(q, cb);
|
|
158
|
+
};
|
|
159
|
+
exports.Driver.prototype.valueToProperty = function (value, property) {
|
|
160
|
+
let customType, v;
|
|
161
|
+
switch (property.type) {
|
|
162
|
+
case "object":
|
|
163
|
+
if (typeof value == "object" && !Buffer.isBuffer(value)) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
try {
|
|
167
|
+
value = JSON.parse(value);
|
|
168
|
+
}
|
|
169
|
+
catch (e) {
|
|
170
|
+
value = null;
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
case "binary": {
|
|
174
|
+
// compat with standard behavior of `JSON.stringify`/`JSON.parse`
|
|
175
|
+
if (value && value.type === 'Buffer' && Array.isArray(value.data)) {
|
|
176
|
+
value = Buffer.from(value.data);
|
|
177
|
+
}
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
case "point":
|
|
181
|
+
if (typeof value == "string") {
|
|
182
|
+
const m = value.match(/\((\-?[\d\.]+)[\s,]+(\-?[\d\.]+)\)/);
|
|
183
|
+
if (m) {
|
|
184
|
+
value = { x: parseFloat(m[1]), y: parseFloat(m[2]) };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
break;
|
|
188
|
+
case "date":
|
|
189
|
+
if ((util.isNumber(value) || util.isString(value)))
|
|
190
|
+
value = new Date(value);
|
|
191
|
+
if (util.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
|
|
192
|
+
const tz = convertTimezone(this.config.timezone);
|
|
193
|
+
// shift local to UTC
|
|
194
|
+
value.setTime(value.getTime() - (value.getTimezoneOffset() * 60000));
|
|
195
|
+
if (tz !== false) {
|
|
196
|
+
// shift UTC to timezone
|
|
197
|
+
value.setTime(value.getTime() - (tz * 60000));
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
break;
|
|
201
|
+
case "number":
|
|
202
|
+
if (typeof value == 'string') {
|
|
203
|
+
switch (value.trim()) {
|
|
204
|
+
case 'Infinity':
|
|
205
|
+
case '-Infinity':
|
|
206
|
+
case 'NaN':
|
|
207
|
+
value = Number(value);
|
|
208
|
+
break;
|
|
209
|
+
default:
|
|
210
|
+
v = parseFloat(value);
|
|
211
|
+
if (Number.isFinite(v)) {
|
|
212
|
+
value = v;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
case "integer":
|
|
218
|
+
if (typeof value == 'string') {
|
|
219
|
+
v = parseInt(value);
|
|
220
|
+
if (Number.isFinite(v)) {
|
|
221
|
+
value = v;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
default:
|
|
226
|
+
customType = this.customTypes[property.type];
|
|
227
|
+
if (customType && 'valueToProperty' in customType) {
|
|
228
|
+
value = customType.valueToProperty(value, property);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return value;
|
|
232
|
+
};
|
|
233
|
+
exports.Driver.prototype.propertyToValue = function (value, property) {
|
|
234
|
+
let customType;
|
|
235
|
+
switch (property.type) {
|
|
236
|
+
case "object":
|
|
237
|
+
if (value !== null) {
|
|
238
|
+
if (!Buffer.isBuffer(value))
|
|
239
|
+
value = JSON.stringify(value).replace(/\\/g, '\\\\');
|
|
240
|
+
}
|
|
241
|
+
break;
|
|
242
|
+
case "date":
|
|
243
|
+
if ((util.isNumber(value) || util.isString(value)))
|
|
244
|
+
value = new Date(value);
|
|
245
|
+
if (util.isDate(value) && this.config.timezone && this.config.timezone != 'local') {
|
|
246
|
+
var tz = convertTimezone(this.config.timezone);
|
|
247
|
+
// shift local to UTC
|
|
248
|
+
value.setTime(value.getTime() + (value.getTimezoneOffset() * 60000));
|
|
249
|
+
if (tz !== false) {
|
|
250
|
+
// shift UTC to timezone
|
|
251
|
+
value.setTime(value.getTime() + (tz * 60000));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
break;
|
|
255
|
+
case "point":
|
|
256
|
+
return function () { return 'POINT(' + value.x + ', ' + value.y + ')'; };
|
|
257
|
+
default:
|
|
258
|
+
customType = this.customTypes[property.type];
|
|
259
|
+
if (customType && 'propertyToValue' in customType) {
|
|
260
|
+
value = customType.propertyToValue(value);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
return value;
|
|
264
|
+
};
|
|
265
|
+
Object.defineProperty(exports.Driver.prototype, "isSql", {
|
|
266
|
+
value: true
|
|
267
|
+
});
|
|
268
|
+
function convertTimezone(tz) {
|
|
269
|
+
if (tz == "Z") {
|
|
270
|
+
return 0;
|
|
271
|
+
}
|
|
272
|
+
var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
|
|
273
|
+
if (m) {
|
|
274
|
+
return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
|
|
275
|
+
}
|
|
276
|
+
return false;
|
|
277
|
+
}
|