fib-orm 1.4.1 → 2.0.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 +74 -0
- package/lib/cli/commands/dumpModel.d.ts +2 -0
- package/lib/cli/commands/dumpModel.js +106 -0
- package/lib/cli/commands/escape.d.ts +2 -0
- package/lib/cli/commands/escape.js +68 -0
- package/lib/cli/commands/spinner.d.ts +2 -0
- package/lib/cli/commands/spinner.js +36 -0
- package/lib/cli/commands/upgrade.d.ts +2 -0
- package/lib/cli/commands/upgrade.js +65 -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 +26 -0
- package/lib/cli/helpers/package.js +56 -0
- package/lib/cli/index.d.ts +2 -0
- package/lib/cli/index.js +18 -0
- package/lib/codepoints-map.json +762 -0
- package/lib/db-driver/Typo.d.ts +112 -0
- package/lib/db-driver/Typo.js +1 -0
- package/lib/db-driver/built-ins/base.class.d.ts +96 -0
- package/lib/db-driver/built-ins/base.class.js +186 -0
- package/lib/db-driver/built-ins/driver-dm.d.ts +18 -0
- package/lib/db-driver/built-ins/driver-dm.js +53 -0
- package/lib/db-driver/built-ins/driver-mongodb.d.ts +24 -0
- package/lib/db-driver/built-ins/driver-mongodb.js +46 -0
- package/lib/db-driver/built-ins/driver-mysql.d.ts +18 -0
- package/lib/db-driver/built-ins/driver-mysql.js +37 -0
- package/lib/db-driver/built-ins/driver-postgresql.d.ts +22 -0
- package/lib/db-driver/built-ins/driver-postgresql.js +76 -0
- package/lib/db-driver/built-ins/driver-redis.d.ts +12 -0
- package/lib/db-driver/built-ins/driver-redis.js +35 -0
- package/lib/db-driver/built-ins/driver-sqlite.d.ts +17 -0
- package/lib/db-driver/built-ins/driver-sqlite.js +34 -0
- package/lib/db-driver/index.d.ts +3 -0
- package/lib/db-driver/index.js +4 -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 +3 -0
- package/lib/knex/sandbox.d.ts +4 -0
- package/lib/knex/sandbox.js +22 -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 +176 -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 +17 -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 +15 -0
- package/lib/orm/Associations/_utils.js +52 -0
- package/lib/orm/ChainFind.d.ts +3 -0
- package/lib/orm/ChainFind.js +327 -0
- package/lib/orm/ChainInstance.d.ts +6 -0
- package/lib/orm/ChainInstance.js +73 -0
- package/lib/orm/Debug.d.ts +2 -0
- package/lib/orm/Debug.js +17 -0
- package/lib/orm/Drivers/DB/base.d.ts +3 -0
- package/lib/orm/Drivers/DB/base.js +3 -0
- package/lib/orm/Drivers/DB/dm.d.ts +11 -0
- package/lib/orm/Drivers/DB/dm.js +23 -0
- package/lib/orm/Drivers/DB/mysql.d.ts +11 -0
- package/lib/orm/Drivers/DB/mysql.js +23 -0
- package/lib/orm/Drivers/DB/postgres.d.ts +11 -0
- package/lib/orm/Drivers/DB/postgres.js +22 -0
- package/lib/orm/Drivers/DB/sqlite3.d.ts +16 -0
- package/lib/orm/Drivers/DB/sqlite3.js +39 -0
- package/lib/orm/Drivers/DML/_ddl-sql.d.ts +3 -0
- package/lib/orm/Drivers/DML/_ddl-sql.js +51 -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 +38 -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 +2 -0
- package/lib/orm/Drivers/DML/dm.d.ts +2 -0
- package/lib/orm/Drivers/DML/dm.js +293 -0
- package/lib/orm/Drivers/DML/index.d.ts +4 -0
- package/lib/orm/Drivers/DML/index.js +6 -0
- package/lib/orm/Drivers/DML/mysql.d.ts +2 -0
- package/lib/orm/Drivers/DML/mysql.js +251 -0
- package/lib/orm/Drivers/DML/postgres.d.ts +3 -0
- package/lib/orm/Drivers/DML/postgres.js +276 -0
- package/lib/orm/Drivers/DML/sqlite.d.ts +2 -0
- package/lib/orm/Drivers/DML/sqlite.js +266 -0
- package/lib/orm/Error.d.ts +11 -0
- package/lib/orm/Error.js +40 -0
- package/lib/orm/Helpers.d.ts +35 -0
- package/lib/orm/Helpers.js +233 -0
- package/lib/orm/Hook.d.ts +13 -0
- package/lib/orm/Hook.js +99 -0
- package/lib/orm/Instance.d.ts +2 -0
- package/lib/orm/Instance.js +697 -0
- package/lib/orm/LazyLoad.d.ts +4 -0
- package/lib/orm/LazyLoad.js +76 -0
- package/lib/orm/Model.d.ts +7 -0
- package/lib/orm/Model.js +971 -0
- package/lib/orm/ORM.d.ts +37 -0
- package/lib/orm/ORM.js +241 -0
- package/lib/orm/Patch/fib-cache.d.ts +40 -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 +13 -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 +119 -0
- package/lib/orm/Singleton.d.ts +5 -0
- package/lib/orm/Singleton.js +72 -0
- package/lib/orm/Typo/Adapter.d.ts +5 -0
- package/lib/orm/Typo/Adapter.js +1 -0
- package/lib/orm/Typo/DMLDriver.d.ts +210 -0
- package/lib/orm/Typo/DMLDriver.js +1 -0
- package/lib/orm/Typo/Db.d.ts +38 -0
- package/lib/orm/Typo/Db.js +1 -0
- package/lib/orm/Typo/Error.d.ts +31 -0
- package/lib/orm/Typo/Error.js +1 -0
- package/lib/orm/Typo/ORM.d.ts +233 -0
- package/lib/orm/Typo/ORM.js +3 -0
- package/lib/orm/Typo/Validators.d.ts +32 -0
- package/lib/orm/Typo/Validators.js +2 -0
- package/lib/orm/Typo/_common.d.ts +38 -0
- package/lib/orm/Typo/_common.js +1 -0
- package/lib/orm/Typo/assoc.d.ts +221 -0
- package/lib/orm/Typo/assoc.js +1 -0
- package/lib/orm/Typo/hook.d.ts +42 -0
- package/lib/orm/Typo/hook.js +1 -0
- package/lib/orm/Typo/instance.d.ts +157 -0
- package/lib/orm/Typo/instance.js +1 -0
- package/lib/orm/Typo/model.d.ts +313 -0
- package/lib/orm/Typo/model.js +6 -0
- package/lib/orm/Typo/patch.d.ts +31 -0
- package/lib/orm/Typo/patch.js +1 -0
- package/lib/orm/Typo/property.d.ts +57 -0
- package/lib/orm/Typo/property.js +1 -0
- package/lib/orm/Typo/query.d.ts +216 -0
- package/lib/orm/Typo/query.js +1 -0
- package/lib/orm/Typo/settings.d.ts +35 -0
- package/lib/orm/Typo/settings.js +5 -0
- package/lib/orm/Typo/synchronous.d.ts +47 -0
- package/lib/orm/Typo/synchronous.js +1 -0
- package/lib/orm/Utilities.d.ts +179 -0
- package/lib/orm/Utilities.js +975 -0
- package/lib/orm/Validators.d.ts +34 -0
- package/lib/orm/Validators.js +100 -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 +68 -0
- package/lib/orm/index.js +157 -0
- package/lib/orm-core/callback.d.ts +12 -0
- package/lib/orm-core/callback.js +1 -0
- package/lib/orm-core/dialect.d.ts +80 -0
- package/lib/orm-core/dialect.js +1 -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 +6 -0
- package/lib/orm-core/sync.d.ts +12 -0
- package/lib/orm-core/sync.js +1 -0
- package/lib/orm-core/utils.d.ts +1 -0
- package/lib/orm-core/utils.js +5 -0
- package/lib/orm-property/Property.d.ts +78 -0
- package/lib/orm-property/Property.js +1 -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 +22 -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 +237 -0
- package/lib/orm-property/transformers/index.d.ts +4 -0
- package/lib/orm-property/transformers/index.js +5 -0
- package/lib/orm-property/transformers/mysql.d.ts +102 -0
- package/lib/orm-property/transformers/mysql.js +261 -0
- package/lib/orm-property/transformers/postgresql.d.ts +54 -0
- package/lib/orm-property/transformers/postgresql.js +189 -0
- package/lib/orm-property/transformers/sqlite.d.ts +19 -0
- package/lib/orm-property/transformers/sqlite.js +171 -0
- package/lib/sql-ddl-sync/Dialects/dm.d.ts +40 -0
- package/lib/sql-ddl-sync/Dialects/dm.js +296 -0
- package/lib/sql-ddl-sync/Dialects/index.d.ts +7 -0
- package/lib/sql-ddl-sync/Dialects/index.js +7 -0
- package/lib/sql-ddl-sync/Dialects/mysql.d.ts +39 -0
- package/lib/sql-ddl-sync/Dialects/mysql.js +240 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.d.ts +40 -0
- package/lib/sql-ddl-sync/Dialects/postgresql.js +286 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.d.ts +44 -0
- package/lib/sql-ddl-sync/Dialects/sqlite.js +256 -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 +1 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.d.ts +13 -0
- package/lib/sql-ddl-sync/Typo/DbIndex.js +1 -0
- package/lib/sql-ddl-sync/Typo/Dialect.d.ts +81 -0
- package/lib/sql-ddl-sync/Typo/Dialect.js +1 -0
- package/lib/sql-ddl-sync/Typo/Driver.d.ts +53 -0
- package/lib/sql-ddl-sync/Typo/Driver.js +1 -0
- package/lib/sql-ddl-sync/Typo/Query.d.ts +7 -0
- package/lib/sql-ddl-sync/Typo/Query.js +1 -0
- package/lib/sql-ddl-sync/Typo/SQL.d.ts +31 -0
- package/lib/sql-ddl-sync/Typo/SQL.js +1 -0
- package/lib/sql-ddl-sync/Typo/_common.d.ts +29 -0
- package/lib/sql-ddl-sync/Typo/_common.js +1 -0
- package/lib/sql-ddl-sync/Utils.d.ts +40 -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 +371 -0
- package/lib/sql-query/Comparators.d.ts +3 -0
- package/lib/sql-query/Comparators.js +94 -0
- package/lib/sql-query/Create.d.ts +35 -0
- package/lib/sql-query/Create.js +77 -0
- package/lib/sql-query/Dialects/dm.d.ts +3 -0
- package/lib/sql-query/Dialects/dm.js +74 -0
- package/lib/sql-query/Dialects/index.d.ts +10 -0
- package/lib/sql-query/Dialects/index.js +12 -0
- package/lib/sql-query/Dialects/mssql.d.ts +3 -0
- package/lib/sql-query/Dialects/mssql.js +73 -0
- package/lib/sql-query/Dialects/mysql.d.ts +3 -0
- package/lib/sql-query/Dialects/mysql.js +100 -0
- package/lib/sql-query/Dialects/postgresql.d.ts +3 -0
- package/lib/sql-query/Dialects/postgresql.js +76 -0
- package/lib/sql-query/Dialects/sqlite.d.ts +3 -0
- package/lib/sql-query/Dialects/sqlite.js +61 -0
- package/lib/sql-query/Helpers.d.ts +38 -0
- package/lib/sql-query/Helpers.js +259 -0
- package/lib/sql-query/Insert.d.ts +12 -0
- package/lib/sql-query/Insert.js +33 -0
- package/lib/sql-query/Query.d.ts +29 -0
- package/lib/sql-query/Query.js +119 -0
- package/lib/sql-query/Remove.d.ts +16 -0
- package/lib/sql-query/Remove.js +93 -0
- package/lib/sql-query/Select.d.ts +62 -0
- package/lib/sql-query/Select.js +533 -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 +1 -0
- package/lib/sql-query/Typo/Comparators.d.ts +140 -0
- package/lib/sql-query/Typo/Comparators.js +1 -0
- package/lib/sql-query/Typo/Dialect.d.ts +39 -0
- package/lib/sql-query/Typo/Dialect.js +1 -0
- package/lib/sql-query/Typo/Field.d.ts +8 -0
- package/lib/sql-query/Typo/Field.js +1 -0
- package/lib/sql-query/Typo/Helper.d.ts +4 -0
- package/lib/sql-query/Typo/Helper.js +1 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.d.ts +80 -0
- package/lib/sql-query/Typo/Query-ChainBuilder.js +1 -0
- package/lib/sql-query/Typo/Query.d.ts +34 -0
- package/lib/sql-query/Typo/Query.js +16 -0
- package/lib/sql-query/Typo/Sql.d.ts +143 -0
- package/lib/sql-query/Typo/Sql.js +1 -0
- package/lib/sql-query/Typo/SubQuery.d.ts +39 -0
- package/lib/sql-query/Typo/SubQuery.js +1 -0
- package/lib/sql-query/Typo/index.d.ts +9 -0
- package/lib/sql-query/Typo/index.js +1 -0
- package/lib/sql-query/Update.d.ts +15 -0
- package/lib/sql-query/Update.js +38 -0
- package/lib/sql-query/Where.d.ts +4 -0
- package/lib/sql-query/Where.js +280 -0
- package/package.json +72 -36
- package/.travis.yml +0 -14
- package/@types/index.d.ts +0 -193
- 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,327 @@
|
|
|
1
|
+
const util = require("util");
|
|
2
|
+
const Utilities = require("./Utilities");
|
|
3
|
+
const ChainInstance = require("./ChainInstance");
|
|
4
|
+
const _dml_helpers_1 = require("./Drivers/DML/_dml-helpers");
|
|
5
|
+
const MODEL_FUNCS = [
|
|
6
|
+
"hasOne", "hasMany",
|
|
7
|
+
"drop", "sync", "get", "clear", "create",
|
|
8
|
+
"exists", "settings", "aggregate"
|
|
9
|
+
];
|
|
10
|
+
const ChainFind = function (Model, opts) {
|
|
11
|
+
const merges = opts.merge = Utilities.combineMergeInfoToArray(opts.merge);
|
|
12
|
+
const chainRunSync = function () {
|
|
13
|
+
let conditions = util.omit(opts.conditions, Model.virtualProperties);
|
|
14
|
+
conditions = Utilities.transformPropertyNames(conditions, opts.properties);
|
|
15
|
+
Utilities.filterWhereConditionsInput(conditions, { properties: Model.allProperties });
|
|
16
|
+
const order = Utilities.transformOrderPropertyNames(opts.order, opts.properties);
|
|
17
|
+
let foundItems;
|
|
18
|
+
const vFields = Object.entries(Model.virtualProperties).map(([k, p]) => p.mapsTo || k);
|
|
19
|
+
const { tableConditions, topConditions } = Utilities.extractSelectTopConditions(conditions, vFields);
|
|
20
|
+
const __pointTypeMapsTo = (0, _dml_helpers_1.pickPointTypeFields)(opts.driver, Model.allProperties);
|
|
21
|
+
foundItems = opts.driver.find(opts.only, opts.table, tableConditions, {
|
|
22
|
+
limit: Utilities.coercePositiveInt(opts.limit, undefined),
|
|
23
|
+
order: order,
|
|
24
|
+
merge: merges,
|
|
25
|
+
offset: Utilities.coercePositiveInt(opts.offset, undefined),
|
|
26
|
+
exists: opts.exists.map(existCond => {
|
|
27
|
+
var _a;
|
|
28
|
+
const maybeAssoc = (_a = Object.values(Model.associations).find(({ association }) => association.mergeTable === existCond.table)) === null || _a === void 0 ? void 0 : _a.association;
|
|
29
|
+
const mergeProps = (maybeAssoc === null || maybeAssoc === void 0 ? void 0 : maybeAssoc.props) || null;
|
|
30
|
+
if (mergeProps) {
|
|
31
|
+
existCond.conditions = util.omit(existCond.conditions, vFields);
|
|
32
|
+
existCond.conditions = Utilities.transformPropertyNames(existCond.conditions, mergeProps);
|
|
33
|
+
Utilities.filterWhereConditionsInput(existCond.conditions, { properties: mergeProps });
|
|
34
|
+
}
|
|
35
|
+
return existCond;
|
|
36
|
+
}),
|
|
37
|
+
topConditions,
|
|
38
|
+
selectVirtualFields: vFields,
|
|
39
|
+
generateSqlSelect: opts.generateSqlSelect,
|
|
40
|
+
__pointTypeMapsTo
|
|
41
|
+
});
|
|
42
|
+
if (foundItems.length === 0) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* only valid for related table based on single foreign key
|
|
47
|
+
* @param err
|
|
48
|
+
* @param items
|
|
49
|
+
*/
|
|
50
|
+
const eagerLoadSync = function (items) {
|
|
51
|
+
const associationKey_Item_Map = {};
|
|
52
|
+
const self_key_field = opts.keys[0];
|
|
53
|
+
const keys = items.map(function (item) {
|
|
54
|
+
const key = item[self_key_field];
|
|
55
|
+
associationKey_Item_Map[key] = item;
|
|
56
|
+
return key;
|
|
57
|
+
});
|
|
58
|
+
Utilities.parallelQueryIfPossible(false, opts.__eager, (association) => {
|
|
59
|
+
let instances = [];
|
|
60
|
+
instances = opts.driver.eagerQuery(association, opts, keys);
|
|
61
|
+
const association_name = association.name;
|
|
62
|
+
for (let idx = 0, instance, item = null; instance = instances[idx]; idx++) {
|
|
63
|
+
item = associationKey_Item_Map[instance.$p];
|
|
64
|
+
// Create the association arrays
|
|
65
|
+
if (!item[association_name])
|
|
66
|
+
item[association_name] = [];
|
|
67
|
+
item[association_name].push(instance);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
const items = foundItems.map((dataItem) => {
|
|
72
|
+
const newInstanceSync = opts.newInstanceSync;
|
|
73
|
+
return newInstanceSync(dataItem);
|
|
74
|
+
});
|
|
75
|
+
if (opts.__eager && opts.__eager.length)
|
|
76
|
+
eagerLoadSync(items);
|
|
77
|
+
return items;
|
|
78
|
+
};
|
|
79
|
+
const chainRun = function (done) {
|
|
80
|
+
const syncResponse = Utilities.catchBlocking(chainRunSync);
|
|
81
|
+
Utilities.takeAwayResult(syncResponse, { no_throw: true, callback: done, use_tick: true });
|
|
82
|
+
};
|
|
83
|
+
const chain = {
|
|
84
|
+
model: null,
|
|
85
|
+
options: null,
|
|
86
|
+
whereExists: function () {
|
|
87
|
+
if (arguments.length && Array.isArray(arguments[0])) {
|
|
88
|
+
opts.exists = arguments[0];
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
opts.exists = Array.prototype.slice.apply(arguments);
|
|
92
|
+
}
|
|
93
|
+
return this;
|
|
94
|
+
},
|
|
95
|
+
only: function () {
|
|
96
|
+
if (arguments.length && Array.isArray(arguments[0])) {
|
|
97
|
+
opts.only = arguments[0];
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
opts.only = Array.prototype.slice.apply(arguments);
|
|
101
|
+
}
|
|
102
|
+
return this;
|
|
103
|
+
},
|
|
104
|
+
omit: function () {
|
|
105
|
+
let omit = null;
|
|
106
|
+
if (arguments.length && Array.isArray(arguments[0])) {
|
|
107
|
+
omit = arguments[0];
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
omit = Array.prototype.slice.apply(arguments);
|
|
111
|
+
}
|
|
112
|
+
this.only(util.difference(Object.keys(opts.properties), omit));
|
|
113
|
+
return this;
|
|
114
|
+
},
|
|
115
|
+
limit: function (limit) {
|
|
116
|
+
opts.limit = limit;
|
|
117
|
+
return this;
|
|
118
|
+
},
|
|
119
|
+
skip: function (offset) {
|
|
120
|
+
return this.offset(offset);
|
|
121
|
+
},
|
|
122
|
+
offset: function (offset) {
|
|
123
|
+
opts.offset = offset;
|
|
124
|
+
return this;
|
|
125
|
+
},
|
|
126
|
+
order: function (...orders) {
|
|
127
|
+
if (!Array.isArray(opts.order)) {
|
|
128
|
+
opts.order = [];
|
|
129
|
+
}
|
|
130
|
+
opts.order = opts.order.concat(Utilities.standardizeOrder(orders));
|
|
131
|
+
return this;
|
|
132
|
+
},
|
|
133
|
+
orderRaw: function (str, args) {
|
|
134
|
+
if (!Array.isArray(opts.order)) {
|
|
135
|
+
opts.order = [];
|
|
136
|
+
}
|
|
137
|
+
args = args || [];
|
|
138
|
+
opts.order.push([str, args]);
|
|
139
|
+
return this;
|
|
140
|
+
},
|
|
141
|
+
eager: function () {
|
|
142
|
+
// This will allow params such as ("abc", "def") or (["abc", "def"])
|
|
143
|
+
var associations = util.flatten(arguments);
|
|
144
|
+
// TODO: Implement eager loading for Mongo and delete this.
|
|
145
|
+
if (opts.driver.config.protocol == "mongodb:") {
|
|
146
|
+
throw new Error("MongoDB does not currently support eager loading");
|
|
147
|
+
}
|
|
148
|
+
opts.__eager = opts.associations.filter(function (association) {
|
|
149
|
+
return ~associations.indexOf(association.name);
|
|
150
|
+
});
|
|
151
|
+
return this;
|
|
152
|
+
},
|
|
153
|
+
/* methods to ChainInstance :start */
|
|
154
|
+
each: function (cb) {
|
|
155
|
+
return ChainInstance(this, cb);
|
|
156
|
+
},
|
|
157
|
+
/* methods to ChainInstance :end */
|
|
158
|
+
/* sync, callback-as-run style methods :start */
|
|
159
|
+
all: null,
|
|
160
|
+
allSync: null,
|
|
161
|
+
where: null,
|
|
162
|
+
whereSync: null,
|
|
163
|
+
find: function (...args) {
|
|
164
|
+
var cb = null;
|
|
165
|
+
opts.conditions = opts.conditions || {};
|
|
166
|
+
if (typeof util.last(args) === "function") {
|
|
167
|
+
cb = args.pop();
|
|
168
|
+
}
|
|
169
|
+
if (typeof args[0] === "object") {
|
|
170
|
+
util.extend(opts.conditions, args[0]);
|
|
171
|
+
}
|
|
172
|
+
else if (typeof args[0] === "string") {
|
|
173
|
+
opts.conditions.__sql = (opts.conditions.__sql || []);
|
|
174
|
+
opts.conditions.__sql.push(args);
|
|
175
|
+
}
|
|
176
|
+
if (cb) {
|
|
177
|
+
chainRun(cb);
|
|
178
|
+
}
|
|
179
|
+
return this;
|
|
180
|
+
},
|
|
181
|
+
findSync: function (...args) {
|
|
182
|
+
opts.conditions = opts.conditions || {};
|
|
183
|
+
if (typeof args[0] === "object") {
|
|
184
|
+
util.extend(opts.conditions, args[0]);
|
|
185
|
+
}
|
|
186
|
+
else if (typeof args[0] === "string") {
|
|
187
|
+
opts.conditions.__sql = (opts.conditions.__sql || []);
|
|
188
|
+
opts.conditions.__sql.push(args);
|
|
189
|
+
}
|
|
190
|
+
return chainRunSync();
|
|
191
|
+
},
|
|
192
|
+
count: function (cb) {
|
|
193
|
+
process.nextTick(() => {
|
|
194
|
+
const syncResult = Utilities.catchBlocking(chain.countSync);
|
|
195
|
+
Utilities.takeAwayResult(syncResult, { callback: cb });
|
|
196
|
+
});
|
|
197
|
+
return this;
|
|
198
|
+
},
|
|
199
|
+
countSync: function () {
|
|
200
|
+
const data = opts.driver.count(opts.table, Utilities.transformPropertyNames(opts.conditions, opts.properties), {
|
|
201
|
+
merge: merges
|
|
202
|
+
});
|
|
203
|
+
if (data.length === 0)
|
|
204
|
+
return 0;
|
|
205
|
+
return data[0].c;
|
|
206
|
+
},
|
|
207
|
+
remove: function (cb) {
|
|
208
|
+
process.nextTick(() => {
|
|
209
|
+
const syncResult = Utilities.catchBlocking(chain.removeSync);
|
|
210
|
+
Utilities.takeAwayResult(syncResult, { callback: cb });
|
|
211
|
+
});
|
|
212
|
+
return this;
|
|
213
|
+
},
|
|
214
|
+
// TODO: add test case about `.removeSync()`
|
|
215
|
+
removeSync: function () {
|
|
216
|
+
Utilities.disAllowOpForVModel(Model, 'modelChain.remove');
|
|
217
|
+
const keys = opts.keyProperties.map((x) => x.mapsTo);
|
|
218
|
+
const data = opts.driver.find(keys, opts.table, Utilities.transformPropertyNames(opts.conditions, opts.properties), {
|
|
219
|
+
limit: opts.limit,
|
|
220
|
+
order: Utilities.transformOrderPropertyNames(opts.order, opts.properties),
|
|
221
|
+
merge: merges,
|
|
222
|
+
offset: opts.offset,
|
|
223
|
+
exists: opts.exists,
|
|
224
|
+
__pointTypeMapsTo: [],
|
|
225
|
+
});
|
|
226
|
+
if (data.length === 0) {
|
|
227
|
+
return null;
|
|
228
|
+
}
|
|
229
|
+
const conditions = {};
|
|
230
|
+
let or;
|
|
231
|
+
conditions.or = [];
|
|
232
|
+
for (let i = 0; i < data.length; i++) {
|
|
233
|
+
or = {};
|
|
234
|
+
for (let j = 0; j < keys.length; j++) {
|
|
235
|
+
or[keys[j]] = data[i][keys[j]];
|
|
236
|
+
}
|
|
237
|
+
conditions.or.push(or);
|
|
238
|
+
}
|
|
239
|
+
return opts.driver.remove(opts.table, conditions);
|
|
240
|
+
},
|
|
241
|
+
first: function (cb) {
|
|
242
|
+
return this.run(function (err, items) {
|
|
243
|
+
return cb(err, items && items.length > 0 ? items[0] : null);
|
|
244
|
+
});
|
|
245
|
+
},
|
|
246
|
+
firstSync: function () {
|
|
247
|
+
const items = this.runSync();
|
|
248
|
+
return items && items.length > 0 ? items[0] : null;
|
|
249
|
+
},
|
|
250
|
+
last: function (cb) {
|
|
251
|
+
return this.run(function (err, items) {
|
|
252
|
+
return cb(err, items && items.length > 0 ? items[items.length - 1] : null);
|
|
253
|
+
});
|
|
254
|
+
},
|
|
255
|
+
lastSync: function () {
|
|
256
|
+
const items = this.runSync();
|
|
257
|
+
return items && items.length > 0 ? items[items.length - 1] : null;
|
|
258
|
+
},
|
|
259
|
+
run: function (cb) {
|
|
260
|
+
chainRun(cb);
|
|
261
|
+
return this;
|
|
262
|
+
},
|
|
263
|
+
runSync: function () {
|
|
264
|
+
return chainRunSync();
|
|
265
|
+
},
|
|
266
|
+
/* sync, callback-as-run style methods :end */
|
|
267
|
+
};
|
|
268
|
+
chain.allSync = chain.whereSync = chain.findSync;
|
|
269
|
+
chain.all = chain.where = chain.find;
|
|
270
|
+
if (opts.associations) {
|
|
271
|
+
for (let i = 0; i < opts.associations.length; i++) {
|
|
272
|
+
addChainMethod(chain, opts.associations[i], opts);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
for (let k in Model) {
|
|
276
|
+
if (MODEL_FUNCS.indexOf(k) >= 0) {
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
if (typeof Model[k] !== "function" || chain[k]) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
chain[k] = Model[k];
|
|
283
|
+
}
|
|
284
|
+
chain.model = Model;
|
|
285
|
+
chain.options = opts;
|
|
286
|
+
return chain;
|
|
287
|
+
};
|
|
288
|
+
function addChainMethod(chain, association, opts) {
|
|
289
|
+
chain[association.hasAccessor] = function (value) {
|
|
290
|
+
if (!opts.exists) {
|
|
291
|
+
opts.exists = [];
|
|
292
|
+
}
|
|
293
|
+
var conditions = {};
|
|
294
|
+
var assocIds = Object.keys(association.mergeAssocId);
|
|
295
|
+
var ids = association.model.id;
|
|
296
|
+
function mergeConditions(source) {
|
|
297
|
+
for (let i = 0, cond_item; i < assocIds.length; i++) {
|
|
298
|
+
if (typeof cond_item === "undefined") {
|
|
299
|
+
conditions[assocIds[i]] = source[ids[i]];
|
|
300
|
+
}
|
|
301
|
+
else if ((cond_item = conditions[assocIds[i]])
|
|
302
|
+
&& Array.isArray(cond_item)) {
|
|
303
|
+
cond_item.push(source[ids[i]]);
|
|
304
|
+
conditions[assocIds[i]] = cond_item;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
conditions[assocIds[i]] = [cond_item, source[ids[i]]];
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (Array.isArray(value)) {
|
|
312
|
+
for (let i = 0; i < value.length; i++) {
|
|
313
|
+
mergeConditions(value[i]);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
mergeConditions(value);
|
|
318
|
+
}
|
|
319
|
+
opts.exists.push({
|
|
320
|
+
table: association.mergeTable,
|
|
321
|
+
link: [Object.keys(association.mergeId), association.model.id],
|
|
322
|
+
conditions: conditions
|
|
323
|
+
});
|
|
324
|
+
return chain;
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
module.exports = ChainFind;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
module.exports = function ChainInstance(chain, chain_cb) {
|
|
2
|
+
let instances = null;
|
|
3
|
+
let loading = false;
|
|
4
|
+
const queue = [];
|
|
5
|
+
const load = function () {
|
|
6
|
+
loading = true;
|
|
7
|
+
chain.run(function (err, items) {
|
|
8
|
+
instances = items;
|
|
9
|
+
return next();
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
const pushIterateeQueue = function (hwd) {
|
|
13
|
+
return function (cb) {
|
|
14
|
+
if (!loading) {
|
|
15
|
+
load();
|
|
16
|
+
}
|
|
17
|
+
queue.push({ hwd: hwd, args: [cb] });
|
|
18
|
+
return calls;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
const next = function () {
|
|
22
|
+
if (queue.length === 0)
|
|
23
|
+
return;
|
|
24
|
+
const item = queue.shift();
|
|
25
|
+
return item.hwd.apply(calls, item.args);
|
|
26
|
+
};
|
|
27
|
+
const calls = {
|
|
28
|
+
_each: pushIterateeQueue(function (cb) {
|
|
29
|
+
instances.forEach(cb);
|
|
30
|
+
return next();
|
|
31
|
+
}),
|
|
32
|
+
filter: pushIterateeQueue(function (cb) {
|
|
33
|
+
instances = instances.filter(cb);
|
|
34
|
+
return next();
|
|
35
|
+
}),
|
|
36
|
+
sort: pushIterateeQueue(function (cb) {
|
|
37
|
+
instances.sort(cb);
|
|
38
|
+
return next();
|
|
39
|
+
}),
|
|
40
|
+
count: pushIterateeQueue(function (cb) {
|
|
41
|
+
cb(instances.length);
|
|
42
|
+
return next();
|
|
43
|
+
}),
|
|
44
|
+
get: pushIterateeQueue(function (cb) {
|
|
45
|
+
cb(instances);
|
|
46
|
+
return next();
|
|
47
|
+
}),
|
|
48
|
+
save: pushIterateeQueue(function (cb) {
|
|
49
|
+
const saveNext = function (i) {
|
|
50
|
+
if (i >= instances.length) {
|
|
51
|
+
if (typeof cb === "function") {
|
|
52
|
+
cb();
|
|
53
|
+
}
|
|
54
|
+
return next();
|
|
55
|
+
}
|
|
56
|
+
return instances[i].save(function (err) {
|
|
57
|
+
if (err) {
|
|
58
|
+
if (typeof cb === "function") {
|
|
59
|
+
cb(err);
|
|
60
|
+
}
|
|
61
|
+
return next();
|
|
62
|
+
}
|
|
63
|
+
return saveNext(i + 1);
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
return saveNext(0);
|
|
67
|
+
})
|
|
68
|
+
};
|
|
69
|
+
if (typeof chain_cb === "function") {
|
|
70
|
+
return calls._each(chain_cb);
|
|
71
|
+
}
|
|
72
|
+
return calls;
|
|
73
|
+
};
|
package/lib/orm/Debug.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
exports.sql = void 0;
|
|
3
|
+
const util = require("util");
|
|
4
|
+
const tty = require("tty");
|
|
5
|
+
function sql(driver, sql) {
|
|
6
|
+
var fmt;
|
|
7
|
+
if (tty.isatty(process.stdout.fd)) {
|
|
8
|
+
fmt = "\033[32;1m(orm/%s) \033[34m%s\033[0m\n";
|
|
9
|
+
sql = sql.replace(/`(.+?)`/g, function (m) { return "\033[31m" + m + "\033[34m"; });
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
fmt = "[SQL/%s] %s\n";
|
|
13
|
+
}
|
|
14
|
+
process.stdout.write(util.format(fmt, driver, sql));
|
|
15
|
+
}
|
|
16
|
+
exports.sql = sql;
|
|
17
|
+
;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="@fibjs/types" />
|
|
2
|
+
/// <reference types="@fibjs/types" />
|
|
3
|
+
import type { FxOrmCommon } from '../../Typo/_common';
|
|
4
|
+
import type { FxOrmDb } from '../../Typo/Db';
|
|
5
|
+
declare const Driver: typeof import("../../../db-driver/built-ins/driver-dm").default;
|
|
6
|
+
export declare class Database extends Driver implements FxOrmDb.Database<Class_DbConnection> {
|
|
7
|
+
eventor: Class_EventEmitter;
|
|
8
|
+
connect(cb?: FxOrmCommon.GenericCallback<Class_DbConnection>): Class_DbConnection;
|
|
9
|
+
query<T = any>(sql: string, cb?: FxOrmCommon.GenericCallback<T>): any;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
exports.Database = void 0;
|
|
3
|
+
const index_1 = require("../../../db-driver/index");
|
|
4
|
+
const getEventEmitter = require("./base");
|
|
5
|
+
const Utilities = require("../../Utilities");
|
|
6
|
+
const Driver = index_1.Driver.getDriver('dm');
|
|
7
|
+
class Database extends Driver {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
this.eventor = getEventEmitter();
|
|
11
|
+
}
|
|
12
|
+
connect(cb) {
|
|
13
|
+
const exposedErrResults = Utilities.catchBlocking(() => super.open());
|
|
14
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
15
|
+
return this.connection;
|
|
16
|
+
}
|
|
17
|
+
query(sql, cb) {
|
|
18
|
+
const exposedErrResults = Utilities.catchBlocking(() => this.execute(sql));
|
|
19
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
20
|
+
return exposedErrResults.result;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.Database = Database;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="@fibjs/types" />
|
|
2
|
+
/// <reference types="@fibjs/types" />
|
|
3
|
+
import type { FxOrmCommon } from '../../Typo/_common';
|
|
4
|
+
import type { FxOrmDb } from '../../Typo/Db';
|
|
5
|
+
declare const Driver: typeof import("../../../db-driver/built-ins/driver-mysql").default;
|
|
6
|
+
export declare class Database extends Driver implements FxOrmDb.Database<Class_MySQL> {
|
|
7
|
+
eventor: Class_EventEmitter;
|
|
8
|
+
connect(cb?: FxOrmCommon.GenericCallback<Class_MySQL>): Class_MySQL;
|
|
9
|
+
query<T = any>(sql: string, cb?: FxOrmCommon.GenericCallback<T>): any;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
exports.Database = void 0;
|
|
3
|
+
const index_1 = require("../../../db-driver/index");
|
|
4
|
+
const getEventEmitter = require("./base");
|
|
5
|
+
const Utilities = require("../../Utilities");
|
|
6
|
+
const Driver = index_1.Driver.getDriver('mysql');
|
|
7
|
+
class Database extends Driver {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
this.eventor = getEventEmitter();
|
|
11
|
+
}
|
|
12
|
+
connect(cb) {
|
|
13
|
+
const exposedErrResults = Utilities.catchBlocking(() => super.open());
|
|
14
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
15
|
+
return this.connection;
|
|
16
|
+
}
|
|
17
|
+
query(sql, cb) {
|
|
18
|
+
const exposedErrResults = Utilities.catchBlocking(() => this.execute(sql));
|
|
19
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
20
|
+
return exposedErrResults.result;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.Database = Database;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="@fibjs/types" />
|
|
2
|
+
/// <reference types="@fibjs/types" />
|
|
3
|
+
import type { FxOrmCommon } from '../../Typo/_common';
|
|
4
|
+
import type { FxOrmDb } from '../../Typo/Db';
|
|
5
|
+
declare const Driver: typeof import("../../../db-driver/built-ins/driver-postgresql").default;
|
|
6
|
+
export default class PostgresqlDatabase extends Driver implements FxOrmDb.Database<Class_DbConnection> {
|
|
7
|
+
eventor: Class_EventEmitter;
|
|
8
|
+
connect(cb?: FxOrmCommon.GenericCallback<Class_DbConnection>): Class_DbConnection;
|
|
9
|
+
query<T = any>(sql: string, cb?: FxOrmCommon.GenericCallback<T>): any;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
const index_1 = require("../../../db-driver/index");
|
|
3
|
+
const getEventEmitter = require("./base");
|
|
4
|
+
const Utilities = require("../../Utilities");
|
|
5
|
+
const Driver = index_1.Driver.getDriver('postgresql');
|
|
6
|
+
class PostgresqlDatabase extends Driver {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.eventor = getEventEmitter();
|
|
10
|
+
}
|
|
11
|
+
connect(cb) {
|
|
12
|
+
const exposedErrResults = Utilities.catchBlocking(() => super.open());
|
|
13
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
14
|
+
return this.connection;
|
|
15
|
+
}
|
|
16
|
+
query(sql, cb) {
|
|
17
|
+
const exposedErrResults = Utilities.catchBlocking(() => this.execute(sql));
|
|
18
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
19
|
+
return exposedErrResults.result;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.default = PostgresqlDatabase;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="@fibjs/types" />
|
|
2
|
+
/// <reference types="@fibjs/types" />
|
|
3
|
+
import type { FxOrmCommon } from '../../Typo/_common';
|
|
4
|
+
import type { FxOrmDb } from '../../Typo/Db';
|
|
5
|
+
declare const Driver: typeof import("../../../db-driver/built-ins/driver-sqlite").default;
|
|
6
|
+
export declare class Database extends Driver implements FxOrmDb.DatabaseBase_SQLite {
|
|
7
|
+
eventor: Class_EventEmitter;
|
|
8
|
+
conn: Class_SQLite;
|
|
9
|
+
get use_memory(): boolean;
|
|
10
|
+
constructor(opts: any);
|
|
11
|
+
connect(cb?: FxOrmCommon.GenericCallback<Class_SQLite>): Class_SQLite;
|
|
12
|
+
query<T = any>(sql: string, cb?: FxOrmCommon.GenericCallback<T>): any;
|
|
13
|
+
all<T = any>(sql: string, cb?: FxOrmCommon.GenericCallback<T>): any;
|
|
14
|
+
get<T = any>(sql: string, cb?: FxOrmCommon.GenericCallback<T>): any;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
exports.Database = void 0;
|
|
3
|
+
const index_1 = require("../../../db-driver/index");
|
|
4
|
+
const getEventEmitter = require("./base");
|
|
5
|
+
const Utilities = require("../../Utilities");
|
|
6
|
+
function isMemoryMode(href) {
|
|
7
|
+
return href === ':memory:';
|
|
8
|
+
}
|
|
9
|
+
const Driver = index_1.Driver.getDriver('sqlite');
|
|
10
|
+
class Database extends Driver {
|
|
11
|
+
get use_memory() {
|
|
12
|
+
return isMemoryMode(this.config.href);
|
|
13
|
+
}
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
super(opts);
|
|
16
|
+
this.eventor = getEventEmitter();
|
|
17
|
+
this.config.slashes = false;
|
|
18
|
+
}
|
|
19
|
+
connect(cb) {
|
|
20
|
+
const exposedErrResults = Utilities.catchBlocking(() => super.open());
|
|
21
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
22
|
+
return this.connection;
|
|
23
|
+
}
|
|
24
|
+
query(sql, cb) {
|
|
25
|
+
const exposedErrResults = Utilities.catchBlocking(() => this.execute(sql));
|
|
26
|
+
Utilities.takeAwayResult(exposedErrResults, { no_throw: !!cb, callback: cb });
|
|
27
|
+
return exposedErrResults.result;
|
|
28
|
+
}
|
|
29
|
+
all(sql, cb) {
|
|
30
|
+
return this.query(sql, cb);
|
|
31
|
+
}
|
|
32
|
+
get(sql, cb) {
|
|
33
|
+
const results = this.all(sql)[0];
|
|
34
|
+
if (typeof cb === 'function')
|
|
35
|
+
return cb(null, results);
|
|
36
|
+
return results;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.Database = Database;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
exports.doDrop = exports.doSync = void 0;
|
|
3
|
+
const _merge = require("lodash.merge");
|
|
4
|
+
const index_1 = require("../../../sql-ddl-sync/index");
|
|
5
|
+
const Utilities = require("../../Utilities");
|
|
6
|
+
function setIndex(p, v, k) {
|
|
7
|
+
v.index = true;
|
|
8
|
+
p[k] = v;
|
|
9
|
+
}
|
|
10
|
+
;
|
|
11
|
+
const doSync = function (opts) {
|
|
12
|
+
const syncInstance = new index_1.Sync(Object.assign(Object.assign({}, opts.allow_drop_column && { suppressColumnDrop: false }), { dbdriver: this.db, debug: function (text) {
|
|
13
|
+
process.env.DEBUG_SQLDDLSYNC && global.console.log("> %s", text);
|
|
14
|
+
} }));
|
|
15
|
+
if (opts.repair_column)
|
|
16
|
+
syncInstance.strategy = 'mixed';
|
|
17
|
+
if (this.customTypes) {
|
|
18
|
+
for (let k in this.customTypes) {
|
|
19
|
+
syncInstance.defineType(k, this.customTypes[k]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
syncInstance.defineCollection(opts.table, opts.allProperties, {
|
|
23
|
+
comment: opts === null || opts === void 0 ? void 0 : opts.tableComment
|
|
24
|
+
});
|
|
25
|
+
for (let i = 0, props = {}; i < opts.many_associations.length; i++) {
|
|
26
|
+
props = {};
|
|
27
|
+
_merge(props, opts.many_associations[i].mergeId);
|
|
28
|
+
_merge(props, opts.many_associations[i].mergeAssocId);
|
|
29
|
+
Object.entries(props).forEach(([k, v]) => setIndex(props, v, k));
|
|
30
|
+
_merge(props, opts.many_associations[i].props);
|
|
31
|
+
syncInstance.defineCollection(opts.many_associations[i].mergeTable, props);
|
|
32
|
+
}
|
|
33
|
+
syncInstance.sync();
|
|
34
|
+
return this;
|
|
35
|
+
};
|
|
36
|
+
exports.doSync = doSync;
|
|
37
|
+
const doDrop = function (opts) {
|
|
38
|
+
let drop_queries = [], pending, err;
|
|
39
|
+
drop_queries.push(`DROP TABLE IF EXISTS ${this.query.escapeId(opts.table)}`);
|
|
40
|
+
for (let i = 0; i < opts.many_associations.length; i++) {
|
|
41
|
+
drop_queries.push(`DROP TABLE IF EXISTS ${this.query.escapeId(opts.many_associations[i].mergeTable)}`);
|
|
42
|
+
}
|
|
43
|
+
pending = drop_queries.length;
|
|
44
|
+
for (let i = 0; i < drop_queries.length; i++) {
|
|
45
|
+
err = Utilities.catchBlocking(this.execQuery, [drop_queries[i]], { thisArg: this }).error;
|
|
46
|
+
if (err || --pending === 0)
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
return this;
|
|
50
|
+
};
|
|
51
|
+
exports.doDrop = doDrop;
|