@tachybase/database 0.23.8
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/.turbo/turbo-build.log +10 -0
- package/LICENSE +201 -0
- package/lib/collection-factory.d.ts +17 -0
- package/lib/collection-factory.js +52 -0
- package/lib/collection-group-manager.d.ts +26 -0
- package/lib/collection-group-manager.js +57 -0
- package/lib/collection-importer.d.ts +7 -0
- package/lib/collection-importer.js +74 -0
- package/lib/collection.d.ts +138 -0
- package/lib/collection.js +686 -0
- package/lib/database-utils/index.d.ts +9 -0
- package/lib/database-utils/index.js +71 -0
- package/lib/database-utils/traverseJSON.d.ts +10 -0
- package/lib/database-utils/traverseJSON.js +126 -0
- package/lib/database.d.ts +201 -0
- package/lib/database.js +765 -0
- package/lib/decorators/must-have-filter-decorator.d.ts +2 -0
- package/lib/decorators/must-have-filter-decorator.js +38 -0
- package/lib/decorators/target-collection-decorator.d.ts +2 -0
- package/lib/decorators/target-collection-decorator.js +47 -0
- package/lib/decorators/transaction-decorator.d.ts +1 -0
- package/lib/decorators/transaction-decorator.js +88 -0
- package/lib/eager-loading/eager-loading-tree.d.ts +31 -0
- package/lib/eager-loading/eager-loading-tree.js +439 -0
- package/lib/errors/identifier-error.d.ts +3 -0
- package/lib/errors/identifier-error.js +35 -0
- package/lib/errors/zero-column-table-error.d.ts +2 -0
- package/lib/errors/zero-column-table-error.js +31 -0
- package/lib/features/references-map.d.ts +18 -0
- package/lib/features/references-map.js +109 -0
- package/lib/features/referential-integrity-check.d.ts +8 -0
- package/lib/features/referential-integrity-check.js +83 -0
- package/lib/field-repository/array-field-repository.d.ts +28 -0
- package/lib/field-repository/array-field-repository.js +197 -0
- package/lib/fields/array-field.d.ts +11 -0
- package/lib/fields/array-field.js +57 -0
- package/lib/fields/belongs-to-field.d.ts +17 -0
- package/lib/fields/belongs-to-field.js +154 -0
- package/lib/fields/belongs-to-many-field.d.ts +20 -0
- package/lib/fields/belongs-to-many-field.js +187 -0
- package/lib/fields/boolean-field.d.ts +8 -0
- package/lib/fields/boolean-field.js +36 -0
- package/lib/fields/context-field.d.ts +14 -0
- package/lib/fields/context-field.js +70 -0
- package/lib/fields/date-field.d.ts +13 -0
- package/lib/fields/date-field.js +64 -0
- package/lib/fields/field.d.ts +43 -0
- package/lib/fields/field.js +156 -0
- package/lib/fields/has-inverse-field.d.ts +4 -0
- package/lib/fields/has-inverse-field.js +15 -0
- package/lib/fields/has-many-field.d.ts +68 -0
- package/lib/fields/has-many-field.js +156 -0
- package/lib/fields/has-one-field.d.ts +68 -0
- package/lib/fields/has-one-field.js +150 -0
- package/lib/fields/index.d.ts +46 -0
- package/lib/fields/index.js +65 -0
- package/lib/fields/json-field.d.ts +14 -0
- package/lib/fields/json-field.js +54 -0
- package/lib/fields/nanoid-field.d.ts +13 -0
- package/lib/fields/nanoid-field.js +58 -0
- package/lib/fields/number-field.d.ts +41 -0
- package/lib/fields/number-field.js +85 -0
- package/lib/fields/password-field.d.ts +21 -0
- package/lib/fields/password-field.js +95 -0
- package/lib/fields/radio-field.d.ts +16 -0
- package/lib/fields/radio-field.js +66 -0
- package/lib/fields/relation-field.d.ts +21 -0
- package/lib/fields/relation-field.js +79 -0
- package/lib/fields/set-field.d.ts +10 -0
- package/lib/fields/set-field.js +46 -0
- package/lib/fields/sort-field.d.ts +16 -0
- package/lib/fields/sort-field.js +187 -0
- package/lib/fields/string-field.d.ts +8 -0
- package/lib/fields/string-field.js +36 -0
- package/lib/fields/text-field.d.ts +8 -0
- package/lib/fields/text-field.js +36 -0
- package/lib/fields/time-field.d.ts +8 -0
- package/lib/fields/time-field.js +36 -0
- package/lib/fields/uid-field.d.ts +13 -0
- package/lib/fields/uid-field.js +63 -0
- package/lib/fields/uuid-field.d.ts +9 -0
- package/lib/fields/uuid-field.js +45 -0
- package/lib/fields/virtual-field.d.ts +8 -0
- package/lib/fields/virtual-field.js +36 -0
- package/lib/filter-match.d.ts +1 -0
- package/lib/filter-match.js +66 -0
- package/lib/filter-parser.d.ts +21 -0
- package/lib/filter-parser.js +230 -0
- package/lib/helpers.d.ts +3 -0
- package/lib/helpers.js +167 -0
- package/lib/index.d.ts +30 -0
- package/lib/index.js +114 -0
- package/lib/inherited-collection.d.ts +14 -0
- package/lib/inherited-collection.js +138 -0
- package/lib/inherited-map.d.ts +21 -0
- package/lib/inherited-map.js +113 -0
- package/lib/inherited-sync-runner.d.ts +4 -0
- package/lib/inherited-sync-runner.js +175 -0
- package/lib/listeners/adjacency-list.d.ts +2 -0
- package/lib/listeners/adjacency-list.js +42 -0
- package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +6 -0
- package/lib/listeners/append-child-collection-name-after-repository-find.js +66 -0
- package/lib/listeners/index.d.ts +2 -0
- package/lib/listeners/index.js +33 -0
- package/lib/magic-attribute-model.d.ts +8 -0
- package/lib/magic-attribute-model.js +203 -0
- package/lib/migration.d.ts +35 -0
- package/lib/migration.js +103 -0
- package/lib/mock-database.d.ts +25 -0
- package/lib/mock-database.js +126 -0
- package/lib/model-hook.d.ts +12 -0
- package/lib/model-hook.js +92 -0
- package/lib/model.d.ts +23 -0
- package/lib/model.js +152 -0
- package/lib/operators/array.d.ts +2 -0
- package/lib/operators/array.js +158 -0
- package/lib/operators/association.d.ts +2 -0
- package/lib/operators/association.js +35 -0
- package/lib/operators/boolean.d.ts +2 -0
- package/lib/operators/boolean.js +38 -0
- package/lib/operators/child-collection.d.ts +2 -0
- package/lib/operators/child-collection.js +67 -0
- package/lib/operators/date.d.ts +2 -0
- package/lib/operators/date.js +140 -0
- package/lib/operators/empty.d.ts +5 -0
- package/lib/operators/empty.js +89 -0
- package/lib/operators/eq.d.ts +2 -0
- package/lib/operators/eq.js +35 -0
- package/lib/operators/index.d.ts +5 -0
- package/lib/operators/index.js +56 -0
- package/lib/operators/jsonb.d.ts +2 -0
- package/lib/operators/jsonb.js +50 -0
- package/lib/operators/ne.d.ts +2 -0
- package/lib/operators/ne.js +40 -0
- package/lib/operators/notIn.d.ts +2 -0
- package/lib/operators/notIn.js +33 -0
- package/lib/operators/string.d.ts +2 -0
- package/lib/operators/string.js +109 -0
- package/lib/operators/utils.d.ts +4 -0
- package/lib/operators/utils.js +40 -0
- package/lib/options-parser.d.ts +37 -0
- package/lib/options-parser.js +332 -0
- package/lib/playground.d.ts +1 -0
- package/lib/playground.js +43 -0
- package/lib/query-interface/mysql-query-interface.d.ts +35 -0
- package/lib/query-interface/mysql-query-interface.js +122 -0
- package/lib/query-interface/postgres-query-interface.d.ts +32 -0
- package/lib/query-interface/postgres-query-interface.js +189 -0
- package/lib/query-interface/query-interface-builder.d.ts +2 -0
- package/lib/query-interface/query-interface-builder.js +46 -0
- package/lib/query-interface/query-interface.d.ts +42 -0
- package/lib/query-interface/query-interface.js +46 -0
- package/lib/query-interface/sqlite-query-interface.d.ts +35 -0
- package/lib/query-interface/sqlite-query-interface.js +131 -0
- package/lib/relation-repository/belongs-to-many-repository.d.ts +21 -0
- package/lib/relation-repository/belongs-to-many-repository.js +280 -0
- package/lib/relation-repository/belongs-to-repository.d.ts +6 -0
- package/lib/relation-repository/belongs-to-repository.js +39 -0
- package/lib/relation-repository/hasmany-repository.d.ts +10 -0
- package/lib/relation-repository/hasmany-repository.js +182 -0
- package/lib/relation-repository/hasone-repository.d.ts +6 -0
- package/lib/relation-repository/hasone-repository.js +39 -0
- package/lib/relation-repository/multiple-relation-repository.d.ts +20 -0
- package/lib/relation-repository/multiple-relation-repository.js +229 -0
- package/lib/relation-repository/relation-repository.d.ts +29 -0
- package/lib/relation-repository/relation-repository.js +202 -0
- package/lib/relation-repository/single-relation-repository.d.ts +25 -0
- package/lib/relation-repository/single-relation-repository.js +162 -0
- package/lib/relation-repository/types.d.ts +7 -0
- package/lib/relation-repository/types.js +15 -0
- package/lib/repositories/tree-repository/adjacency-list-repository.d.ts +18 -0
- package/lib/repositories/tree-repository/adjacency-list-repository.js +183 -0
- package/lib/repositories/view-repository.d.ts +3 -0
- package/lib/repositories/view-repository.js +32 -0
- package/lib/repository.d.ts +206 -0
- package/lib/repository.js +597 -0
- package/lib/sql-collection/index.d.ts +2 -0
- package/lib/sql-collection/index.js +23 -0
- package/lib/sql-collection/query-generator.d.ts +10 -0
- package/lib/sql-collection/query-generator.js +70 -0
- package/lib/sql-collection/sql-collection.d.ts +7 -0
- package/lib/sql-collection/sql-collection.js +69 -0
- package/lib/sql-collection/sql-model.d.ts +16 -0
- package/lib/sql-collection/sql-model.js +168 -0
- package/lib/sql-parser/index.d.ts +10 -0
- package/lib/sql-parser/index.js +10699 -0
- package/lib/sql-parser/postgres.d.ts +10 -0
- package/lib/sql-parser/postgres.js +31506 -0
- package/lib/sql-parser/sql.pegjs +1297 -0
- package/lib/sync-runner.d.ts +30 -0
- package/lib/sync-runner.js +277 -0
- package/lib/types.d.ts +49 -0
- package/lib/types.js +15 -0
- package/lib/update-associations.d.ts +60 -0
- package/lib/update-associations.js +405 -0
- package/lib/update-guard.d.ts +28 -0
- package/lib/update-guard.js +202 -0
- package/lib/utils.d.ts +7 -0
- package/lib/utils.js +127 -0
- package/lib/value-parsers/array-value-parser.d.ts +8 -0
- package/lib/value-parsers/array-value-parser.js +58 -0
- package/lib/value-parsers/base-value-parser.d.ts +12 -0
- package/lib/value-parsers/base-value-parser.js +63 -0
- package/lib/value-parsers/boolean-value-parser.d.ts +4 -0
- package/lib/value-parsers/boolean-value-parser.js +52 -0
- package/lib/value-parsers/date-value-parser.d.ts +5 -0
- package/lib/value-parsers/date-value-parser.js +88 -0
- package/lib/value-parsers/index.d.ts +12 -0
- package/lib/value-parsers/index.js +78 -0
- package/lib/value-parsers/json-value-parser.d.ts +4 -0
- package/lib/value-parsers/json-value-parser.js +47 -0
- package/lib/value-parsers/number-value-parser.d.ts +4 -0
- package/lib/value-parsers/number-value-parser.js +58 -0
- package/lib/value-parsers/string-value-parser.d.ts +8 -0
- package/lib/value-parsers/string-value-parser.js +64 -0
- package/lib/value-parsers/to-many-value-parser.d.ts +13 -0
- package/lib/value-parsers/to-many-value-parser.js +108 -0
- package/lib/value-parsers/to-one-value-parser.d.ts +4 -0
- package/lib/value-parsers/to-one-value-parser.js +48 -0
- package/lib/view/field-type-map.d.ts +97 -0
- package/lib/view/field-type-map.js +90 -0
- package/lib/view/view-inference.d.ts +32 -0
- package/lib/view/view-inference.js +146 -0
- package/lib/view-collection.d.ts +6 -0
- package/lib/view-collection.js +45 -0
- package/package.json +38 -0
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var postgres_query_interface_exports = {};
|
|
30
|
+
__export(postgres_query_interface_exports, {
|
|
31
|
+
default: () => PostgresQueryInterface
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(postgres_query_interface_exports);
|
|
34
|
+
var import_lodash = __toESM(require("lodash"));
|
|
35
|
+
var import_postgres = __toESM(require("../sql-parser/postgres"));
|
|
36
|
+
var import_query_interface = __toESM(require("./query-interface"));
|
|
37
|
+
const _PostgresQueryInterface = class _PostgresQueryInterface extends import_query_interface.default {
|
|
38
|
+
constructor(db) {
|
|
39
|
+
super(db);
|
|
40
|
+
}
|
|
41
|
+
async setAutoIncrementVal(options) {
|
|
42
|
+
const { tableInfo, columnName, seqName, currentVal, transaction } = options;
|
|
43
|
+
if (!seqName) {
|
|
44
|
+
throw new Error("seqName is required to set auto increment val in postgres");
|
|
45
|
+
}
|
|
46
|
+
await this.db.sequelize.query(
|
|
47
|
+
`alter table ${this.db.utils.quoteTable({
|
|
48
|
+
tableName: tableInfo.tableName,
|
|
49
|
+
schema: tableInfo.schema
|
|
50
|
+
})}
|
|
51
|
+
alter column "${columnName}" set default nextval('${seqName}')`,
|
|
52
|
+
{
|
|
53
|
+
transaction
|
|
54
|
+
}
|
|
55
|
+
);
|
|
56
|
+
if (currentVal) {
|
|
57
|
+
await this.db.sequelize.query(`select setval('${seqName}', ${currentVal})`, {
|
|
58
|
+
transaction
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
async getAutoIncrementInfo(options) {
|
|
63
|
+
const fieldName = options.fieldName || "id";
|
|
64
|
+
const tableInfo = options.tableInfo;
|
|
65
|
+
const sequenceNameResult = await this.db.sequelize.query(
|
|
66
|
+
`SELECT column_default
|
|
67
|
+
FROM information_schema.columns
|
|
68
|
+
WHERE table_name = '${tableInfo.tableName}'
|
|
69
|
+
and table_schema = '${tableInfo.schema || "public"}'
|
|
70
|
+
and "column_name" = '${fieldName}';`
|
|
71
|
+
);
|
|
72
|
+
const columnDefault = sequenceNameResult[0][0]["column_default"];
|
|
73
|
+
const regex = new RegExp(/nextval\('(.*)'::regclass\)/);
|
|
74
|
+
const match = regex.exec(columnDefault);
|
|
75
|
+
const sequenceName = match[1];
|
|
76
|
+
const sequenceCurrentValResult = await this.db.sequelize.query(
|
|
77
|
+
`select last_value
|
|
78
|
+
from ${sequenceName}`
|
|
79
|
+
);
|
|
80
|
+
const sequenceCurrentVal = parseInt(sequenceCurrentValResult[0][0]["last_value"]);
|
|
81
|
+
return {
|
|
82
|
+
seqName: sequenceName,
|
|
83
|
+
currentVal: sequenceCurrentVal
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async collectionTableExists(collection, options) {
|
|
87
|
+
const transaction = options == null ? void 0 : options.transaction;
|
|
88
|
+
const tableName = collection.model.tableName;
|
|
89
|
+
const schema = collection.collectionSchema() || "public";
|
|
90
|
+
const sql = `SELECT EXISTS(SELECT 1
|
|
91
|
+
FROM information_schema.tables
|
|
92
|
+
WHERE table_schema = '${schema}'
|
|
93
|
+
AND table_name = '${tableName}')`;
|
|
94
|
+
const results = await this.db.sequelize.query(sql, { type: "SELECT", transaction });
|
|
95
|
+
return results[0]["exists"];
|
|
96
|
+
}
|
|
97
|
+
async listViews() {
|
|
98
|
+
const sql = `
|
|
99
|
+
SELECT viewname as name, definition, schemaname as schema
|
|
100
|
+
FROM pg_views
|
|
101
|
+
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
|
|
102
|
+
ORDER BY viewname;
|
|
103
|
+
`;
|
|
104
|
+
return await this.db.sequelize.query(sql, { type: "SELECT" });
|
|
105
|
+
}
|
|
106
|
+
async viewDef(viewName) {
|
|
107
|
+
const [schema, name] = viewName.split(".");
|
|
108
|
+
const viewDefQuery = await this.db.sequelize.query(
|
|
109
|
+
`
|
|
110
|
+
select pg_get_viewdef(format('%I.%I', '${schema}', '${name}')::regclass, true) as definition
|
|
111
|
+
`,
|
|
112
|
+
{ type: "SELECT" }
|
|
113
|
+
);
|
|
114
|
+
return import_lodash.default.trim(viewDefQuery[0]["definition"]);
|
|
115
|
+
}
|
|
116
|
+
parseSQL(sql) {
|
|
117
|
+
return import_postgres.default.parse(sql);
|
|
118
|
+
}
|
|
119
|
+
async viewColumnUsage(options) {
|
|
120
|
+
const { viewName, schema = "public" } = options;
|
|
121
|
+
const sql = `
|
|
122
|
+
SELECT *
|
|
123
|
+
FROM information_schema.view_column_usage
|
|
124
|
+
WHERE view_schema = '${schema}'
|
|
125
|
+
AND view_name = '${viewName}';
|
|
126
|
+
`;
|
|
127
|
+
const columnUsages = await this.db.sequelize.query(sql, { type: "SELECT" });
|
|
128
|
+
const def = await this.viewDef(`${schema}.${viewName}`);
|
|
129
|
+
try {
|
|
130
|
+
const { ast } = this.parseSQL(def);
|
|
131
|
+
const columns = ast[0].columns;
|
|
132
|
+
const usages = columns.map((column) => {
|
|
133
|
+
const fieldAlias = column.as || column.expr.column;
|
|
134
|
+
const columnUsage = columnUsages.find((columnUsage2) => {
|
|
135
|
+
let columnExprTable = column.expr.table;
|
|
136
|
+
const from = ast[0].from;
|
|
137
|
+
if (columnExprTable === null && column.expr.type === "column_ref") {
|
|
138
|
+
columnExprTable = from[0].table;
|
|
139
|
+
} else {
|
|
140
|
+
const findAs = from.find((from2) => from2.as === columnExprTable);
|
|
141
|
+
if (findAs) {
|
|
142
|
+
columnExprTable = findAs.table;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return columnUsage2.column_name === column.expr.column && columnUsage2.table_name === columnExprTable;
|
|
146
|
+
});
|
|
147
|
+
return [
|
|
148
|
+
fieldAlias,
|
|
149
|
+
columnUsage ? {
|
|
150
|
+
...columnUsage
|
|
151
|
+
} : null
|
|
152
|
+
];
|
|
153
|
+
}).filter(([, columnUsage]) => columnUsage !== null);
|
|
154
|
+
return Object.fromEntries(usages);
|
|
155
|
+
} catch (e) {
|
|
156
|
+
console.log(e);
|
|
157
|
+
return {};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
async showTableDefinition(tableInfo) {
|
|
161
|
+
const showFunc = `
|
|
162
|
+
CREATE OR REPLACE FUNCTION show_create_table(p_schema text, p_table_name text)
|
|
163
|
+
RETURNS text AS
|
|
164
|
+
$BODY$
|
|
165
|
+
SELECT 'CREATE TABLE ' || quote_ident(p_schema) || '.' || quote_ident(p_table_name) || ' (' || E'\\n' || '' ||
|
|
166
|
+
string_agg(column_list.column_expr, ', ' || E'\\n' || '') ||
|
|
167
|
+
'' || E'\\n' || ');'
|
|
168
|
+
FROM (
|
|
169
|
+
SELECT ' ' || quote_ident(column_name) || ' ' || data_type ||
|
|
170
|
+
coalesce('(' || character_maximum_length || ')', '') ||
|
|
171
|
+
case when is_nullable = 'YES' then '' else ' NOT NULL' end as column_expr
|
|
172
|
+
FROM information_schema.columns
|
|
173
|
+
WHERE table_schema = p_schema AND table_name = p_table_name
|
|
174
|
+
ORDER BY ordinal_position) column_list;
|
|
175
|
+
$BODY$
|
|
176
|
+
LANGUAGE SQL STABLE;
|
|
177
|
+
`;
|
|
178
|
+
await this.db.sequelize.query(showFunc, { type: "RAW" });
|
|
179
|
+
const res = await this.db.sequelize.query(
|
|
180
|
+
`SELECT show_create_table('${tableInfo.schema || "public"}', '${tableInfo.tableName}')`,
|
|
181
|
+
{
|
|
182
|
+
type: "SELECT"
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
return res[0]["show_create_table"];
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
__name(_PostgresQueryInterface, "PostgresQueryInterface");
|
|
189
|
+
let PostgresQueryInterface = _PostgresQueryInterface;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var query_interface_builder_exports = {};
|
|
30
|
+
__export(query_interface_builder_exports, {
|
|
31
|
+
default: () => buildQueryInterface
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(query_interface_builder_exports);
|
|
34
|
+
var import_mysql_query_interface = __toESM(require("./mysql-query-interface"));
|
|
35
|
+
var import_postgres_query_interface = __toESM(require("./postgres-query-interface"));
|
|
36
|
+
var import_sqlite_query_interface = __toESM(require("./sqlite-query-interface"));
|
|
37
|
+
function buildQueryInterface(db) {
|
|
38
|
+
const map = {
|
|
39
|
+
mysql: import_mysql_query_interface.default,
|
|
40
|
+
mariadb: import_mysql_query_interface.default,
|
|
41
|
+
postgres: import_postgres_query_interface.default,
|
|
42
|
+
sqlite: import_sqlite_query_interface.default
|
|
43
|
+
};
|
|
44
|
+
return new map[db.options.dialect](db);
|
|
45
|
+
}
|
|
46
|
+
__name(buildQueryInterface, "buildQueryInterface");
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { QueryInterface as SequelizeQueryInterface, Transaction, Transactionable } from 'sequelize';
|
|
2
|
+
import { Collection } from '../collection';
|
|
3
|
+
import Database from '../database';
|
|
4
|
+
export type TableInfo = {
|
|
5
|
+
tableName: string;
|
|
6
|
+
schema?: string;
|
|
7
|
+
};
|
|
8
|
+
export default abstract class QueryInterface {
|
|
9
|
+
db: Database;
|
|
10
|
+
sequelizeQueryInterface: SequelizeQueryInterface;
|
|
11
|
+
protected constructor(db: Database);
|
|
12
|
+
abstract collectionTableExists(collection: Collection, options?: Transactionable): Promise<boolean>;
|
|
13
|
+
abstract listViews(): any;
|
|
14
|
+
abstract viewDef(viewName: string): Promise<string>;
|
|
15
|
+
abstract viewColumnUsage(options: {
|
|
16
|
+
viewName: string;
|
|
17
|
+
schema?: string;
|
|
18
|
+
}): Promise<{
|
|
19
|
+
[view_column_name: string]: {
|
|
20
|
+
column_name: string;
|
|
21
|
+
table_name: string;
|
|
22
|
+
table_schema?: string;
|
|
23
|
+
};
|
|
24
|
+
}>;
|
|
25
|
+
abstract parseSQL(sql: string): any;
|
|
26
|
+
abstract showTableDefinition(tableInfo: TableInfo): Promise<any>;
|
|
27
|
+
dropAll(options: any): Promise<void>;
|
|
28
|
+
abstract getAutoIncrementInfo(options: {
|
|
29
|
+
tableInfo: TableInfo;
|
|
30
|
+
fieldName: string;
|
|
31
|
+
}): Promise<{
|
|
32
|
+
seqName?: string;
|
|
33
|
+
currentVal: number;
|
|
34
|
+
}>;
|
|
35
|
+
abstract setAutoIncrementVal(options: {
|
|
36
|
+
tableInfo: TableInfo;
|
|
37
|
+
columnName: string;
|
|
38
|
+
seqName?: string;
|
|
39
|
+
currentVal: number;
|
|
40
|
+
transaction?: Transaction;
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var query_interface_exports = {};
|
|
20
|
+
__export(query_interface_exports, {
|
|
21
|
+
default: () => QueryInterface
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(query_interface_exports);
|
|
24
|
+
const _QueryInterface = class _QueryInterface {
|
|
25
|
+
constructor(db) {
|
|
26
|
+
this.db = db;
|
|
27
|
+
this.sequelizeQueryInterface = db.sequelize.getQueryInterface();
|
|
28
|
+
}
|
|
29
|
+
sequelizeQueryInterface;
|
|
30
|
+
async dropAll(options) {
|
|
31
|
+
if (options.drop !== true) return;
|
|
32
|
+
const views = await this.listViews();
|
|
33
|
+
for (const view of views) {
|
|
34
|
+
let removeSql;
|
|
35
|
+
if (view.schema) {
|
|
36
|
+
removeSql = `DROP VIEW IF EXISTS "${view.schema}"."${view.name}"`;
|
|
37
|
+
} else {
|
|
38
|
+
removeSql = `DROP VIEW IF EXISTS ${view.name}`;
|
|
39
|
+
}
|
|
40
|
+
await this.db.sequelize.query(removeSql, { transaction: options.transaction });
|
|
41
|
+
}
|
|
42
|
+
await this.db.sequelize.getQueryInterface().dropAllTables(options);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
__name(_QueryInterface, "QueryInterface");
|
|
46
|
+
let QueryInterface = _QueryInterface;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Transaction } from 'sequelize';
|
|
2
|
+
import { Collection } from '../collection';
|
|
3
|
+
import QueryInterface, { TableInfo } from './query-interface';
|
|
4
|
+
export default class SqliteQueryInterface extends QueryInterface {
|
|
5
|
+
constructor(db: any);
|
|
6
|
+
collectionTableExists(collection: Collection, options?: any): Promise<boolean>;
|
|
7
|
+
listViews(): Promise<[unknown[], unknown]>;
|
|
8
|
+
viewColumnUsage(options: {
|
|
9
|
+
viewName: string;
|
|
10
|
+
schema?: string;
|
|
11
|
+
}): Promise<{
|
|
12
|
+
[view_column_name: string]: {
|
|
13
|
+
column_name: string;
|
|
14
|
+
table_name: string;
|
|
15
|
+
table_schema?: string;
|
|
16
|
+
};
|
|
17
|
+
}>;
|
|
18
|
+
parseSQL(sql: string): any;
|
|
19
|
+
viewDef(viewName: string): Promise<string>;
|
|
20
|
+
showTableDefinition(tableInfo: TableInfo): Promise<any>;
|
|
21
|
+
getAutoIncrementInfo(options: {
|
|
22
|
+
tableInfo: TableInfo;
|
|
23
|
+
fieldName: string;
|
|
24
|
+
}): Promise<{
|
|
25
|
+
seqName?: string;
|
|
26
|
+
currentVal: number;
|
|
27
|
+
}>;
|
|
28
|
+
setAutoIncrementVal(options: {
|
|
29
|
+
tableInfo: TableInfo;
|
|
30
|
+
columnName: string;
|
|
31
|
+
seqName?: string;
|
|
32
|
+
currentVal: number;
|
|
33
|
+
transaction?: Transaction;
|
|
34
|
+
}): Promise<void>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var sqlite_query_interface_exports = {};
|
|
30
|
+
__export(sqlite_query_interface_exports, {
|
|
31
|
+
default: () => SqliteQueryInterface
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(sqlite_query_interface_exports);
|
|
34
|
+
var import_sql_parser = __toESM(require("../sql-parser"));
|
|
35
|
+
var import_query_interface = __toESM(require("./query-interface"));
|
|
36
|
+
const _SqliteQueryInterface = class _SqliteQueryInterface extends import_query_interface.default {
|
|
37
|
+
constructor(db) {
|
|
38
|
+
super(db);
|
|
39
|
+
}
|
|
40
|
+
async collectionTableExists(collection, options) {
|
|
41
|
+
const transaction = options == null ? void 0 : options.transaction;
|
|
42
|
+
const tableName = collection.model.tableName;
|
|
43
|
+
const sql = `SELECT name
|
|
44
|
+
FROM sqlite_master
|
|
45
|
+
WHERE type = 'table'
|
|
46
|
+
AND name = '${tableName}';`;
|
|
47
|
+
const results = await this.db.sequelize.query(sql, { type: "SELECT", transaction });
|
|
48
|
+
return results.length > 0;
|
|
49
|
+
}
|
|
50
|
+
async listViews() {
|
|
51
|
+
const sql = `
|
|
52
|
+
SELECT name, sql as definition
|
|
53
|
+
FROM sqlite_master
|
|
54
|
+
WHERE type = 'view'
|
|
55
|
+
ORDER BY name;
|
|
56
|
+
`;
|
|
57
|
+
return await this.db.sequelize.query(sql, {
|
|
58
|
+
type: "SELECT"
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async viewColumnUsage(options) {
|
|
62
|
+
try {
|
|
63
|
+
const { ast } = this.parseSQL(await this.viewDef(options.viewName));
|
|
64
|
+
const columns = ast.columns;
|
|
65
|
+
const results = [];
|
|
66
|
+
for (const column of columns) {
|
|
67
|
+
if (column.expr.type === "column_ref") {
|
|
68
|
+
results.push([
|
|
69
|
+
column.as || column.expr.column,
|
|
70
|
+
{
|
|
71
|
+
column_name: column.expr.column,
|
|
72
|
+
table_name: column.expr.table
|
|
73
|
+
}
|
|
74
|
+
]);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return Object.fromEntries(results);
|
|
78
|
+
} catch (e) {
|
|
79
|
+
this.db.logger.warn(e);
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
parseSQL(sql) {
|
|
84
|
+
return import_sql_parser.default.parse(sql);
|
|
85
|
+
}
|
|
86
|
+
async viewDef(viewName) {
|
|
87
|
+
const viewDefinition = await this.db.sequelize.query(
|
|
88
|
+
`SELECT sql
|
|
89
|
+
FROM sqlite_master
|
|
90
|
+
WHERE name = '${viewName}' AND type = 'view'`,
|
|
91
|
+
{
|
|
92
|
+
type: "SELECT"
|
|
93
|
+
}
|
|
94
|
+
);
|
|
95
|
+
const createView = viewDefinition[0]["sql"];
|
|
96
|
+
const regex = /(?<=AS\s)([\s\S]*)/i;
|
|
97
|
+
const match = createView.match(regex);
|
|
98
|
+
const sql = match[0];
|
|
99
|
+
return sql;
|
|
100
|
+
}
|
|
101
|
+
showTableDefinition(tableInfo) {
|
|
102
|
+
return Promise.resolve(void 0);
|
|
103
|
+
}
|
|
104
|
+
async getAutoIncrementInfo(options) {
|
|
105
|
+
const { tableInfo } = options;
|
|
106
|
+
const tableName = tableInfo.tableName;
|
|
107
|
+
const sql = `SELECT seq
|
|
108
|
+
FROM sqlite_sequence
|
|
109
|
+
WHERE name = '${tableName}';`;
|
|
110
|
+
const results = await this.db.sequelize.query(sql, { type: "SELECT" });
|
|
111
|
+
const row = results[0];
|
|
112
|
+
if (!row) {
|
|
113
|
+
return {
|
|
114
|
+
currentVal: 0
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
currentVal: row["seq"]
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
async setAutoIncrementVal(options) {
|
|
122
|
+
const { tableInfo, columnName, seqName, currentVal, transaction } = options;
|
|
123
|
+
const tableName = tableInfo.tableName;
|
|
124
|
+
const sql = `UPDATE sqlite_sequence
|
|
125
|
+
SET seq = ${currentVal}
|
|
126
|
+
WHERE name = '${tableName}';`;
|
|
127
|
+
await this.db.sequelize.query(sql, { transaction });
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
__name(_SqliteQueryInterface, "SqliteQueryInterface");
|
|
131
|
+
let SqliteQueryInterface = _SqliteQueryInterface;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Transaction } from 'sequelize';
|
|
2
|
+
import { AggregateOptions, CreateOptions, DestroyOptions, TargetKey } from '../repository';
|
|
3
|
+
import { MultipleRelationRepository } from './multiple-relation-repository';
|
|
4
|
+
import { AssociatedOptions, PrimaryKeyWithThroughValues } from './types';
|
|
5
|
+
type CreateBelongsToManyOptions = CreateOptions;
|
|
6
|
+
export declare class BelongsToManyRepository extends MultipleRelationRepository {
|
|
7
|
+
aggregate(options: AggregateOptions): Promise<any>;
|
|
8
|
+
create(options?: CreateBelongsToManyOptions): Promise<any>;
|
|
9
|
+
destroy(options?: TargetKey | TargetKey[] | DestroyOptions): Promise<boolean>;
|
|
10
|
+
add(options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
|
|
11
|
+
set(options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
|
|
12
|
+
toggle(options: TargetKey | {
|
|
13
|
+
tk?: TargetKey;
|
|
14
|
+
transaction?: Transaction;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
extendFindOptions(findOptions: any): any;
|
|
17
|
+
throughName(): any;
|
|
18
|
+
throughModel(): any;
|
|
19
|
+
protected setTargets(call: 'add' | 'set', options: TargetKey | TargetKey[] | PrimaryKeyWithThroughValues | PrimaryKeyWithThroughValues[] | AssociatedOptions): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|