@tachybase/database 1.3.39 → 1.3.41
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/lib/database.d.ts +2 -2
- package/lib/fields/sort-field.js +2 -2
- package/lib/filter-parser.js +2 -2
- package/lib/inherited-sync-runner.js +2 -2
- package/lib/relation-repository/multiple-relation-repository.js +1 -1
- package/lib/sql-parser/postgres.js +1 -1
- package/lib/sync-runner.js +10 -10
- package/lib/update-guard.js +3 -3
- package/package.json +5 -4
package/lib/database.d.ts
CHANGED
|
@@ -72,13 +72,13 @@ export declare const DialectVersionAccessors: {
|
|
|
72
72
|
};
|
|
73
73
|
postgres: {
|
|
74
74
|
sql: string;
|
|
75
|
-
get: (v: string) =>
|
|
75
|
+
get: (v: string) => string;
|
|
76
76
|
};
|
|
77
77
|
};
|
|
78
78
|
declare class DatabaseVersion {
|
|
79
79
|
db: Database;
|
|
80
80
|
constructor(db: Database);
|
|
81
|
-
satisfies(versions: any): Promise<
|
|
81
|
+
satisfies(versions: any): Promise<boolean>;
|
|
82
82
|
}
|
|
83
83
|
declare module 'sequelize' {
|
|
84
84
|
interface Sequelize {
|
package/lib/fields/sort-field.js
CHANGED
|
@@ -32,7 +32,7 @@ const _SortField = class _SortField extends import_field.Field {
|
|
|
32
32
|
this.setSortValue = /* @__PURE__ */ __name(async (instance, options) => {
|
|
33
33
|
const { name, scopeKey } = this.options;
|
|
34
34
|
const { model } = this.context.collection;
|
|
35
|
-
if ((0, import_lodash.isNumber)(instance.get(name)) && instance._previousDataValues[scopeKey]
|
|
35
|
+
if ((0, import_lodash.isNumber)(instance.get(name)) && instance._previousDataValues[scopeKey] === instance[scopeKey]) {
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
38
38
|
const where = {};
|
|
@@ -50,7 +50,7 @@ const _SortField = class _SortField extends import_field.Field {
|
|
|
50
50
|
}, "setSortValue");
|
|
51
51
|
this.onScopeChange = /* @__PURE__ */ __name(async (instance, options) => {
|
|
52
52
|
const { scopeKey } = this.options;
|
|
53
|
-
if (scopeKey && !instance.isNewRecord && instance._previousDataValues[scopeKey]
|
|
53
|
+
if (scopeKey && !instance.isNewRecord && instance._previousDataValues[scopeKey] !== instance[scopeKey]) {
|
|
54
54
|
await this.setSortValue(instance, options);
|
|
55
55
|
}
|
|
56
56
|
}, "onScopeChange");
|
package/lib/filter-parser.js
CHANGED
|
@@ -49,7 +49,7 @@ const _FilterParser = class _FilterParser {
|
|
|
49
49
|
for (const key of Object.keys(filter)) {
|
|
50
50
|
if (key.endsWith(".$exists") || key.endsWith(".$notExists")) {
|
|
51
51
|
const keyArr = key.split(".");
|
|
52
|
-
if (keyArr[keyArr.length - 2]
|
|
52
|
+
if (keyArr[keyArr.length - 2] === "id") {
|
|
53
53
|
continue;
|
|
54
54
|
}
|
|
55
55
|
keyArr.splice(keyArr.length - 1, 0, "id");
|
|
@@ -84,7 +84,7 @@ const _FilterParser = class _FilterParser {
|
|
|
84
84
|
if (skipPrefix && key.startsWith(skipPrefix)) {
|
|
85
85
|
continue;
|
|
86
86
|
}
|
|
87
|
-
if ((key
|
|
87
|
+
if ((key === "$or" || key === "$and") && Array.isArray(value) && value.length === 0) {
|
|
88
88
|
continue;
|
|
89
89
|
}
|
|
90
90
|
debug('handle filter key "%s: "%s"', key, value);
|
|
@@ -39,7 +39,7 @@ const _InheritedSyncRunner = class _InheritedSyncRunner {
|
|
|
39
39
|
const db = inheritedCollection.context.database;
|
|
40
40
|
const dialect = db.sequelize.getDialect();
|
|
41
41
|
const queryInterface = db.sequelize.getQueryInterface();
|
|
42
|
-
if (dialect
|
|
42
|
+
if (dialect !== "postgres") {
|
|
43
43
|
throw new Error("Inherit model is only supported on postgres");
|
|
44
44
|
}
|
|
45
45
|
const parents = inheritedCollection.parents;
|
|
@@ -119,7 +119,7 @@ const _InheritedSyncRunner = class _InheritedSyncRunner {
|
|
|
119
119
|
const idColumnQuery = await queryInterface.sequelize.query(idColumnSql, {
|
|
120
120
|
transaction
|
|
121
121
|
});
|
|
122
|
-
if (idColumnQuery[0].length
|
|
122
|
+
if (idColumnQuery[0].length === 0) {
|
|
123
123
|
continue;
|
|
124
124
|
}
|
|
125
125
|
await queryInterface.sequelize.query(
|
|
@@ -156,7 +156,7 @@ const _MultipleRelationRepository = class _MultipleRelationRepository extends (_
|
|
|
156
156
|
async findOne(options) {
|
|
157
157
|
const transaction2 = await this.getTransaction(options, false);
|
|
158
158
|
const rows = await this.find({ ...options, limit: 1, transaction: transaction2 });
|
|
159
|
-
return rows.length
|
|
159
|
+
return rows.length === 1 ? rows[0] : null;
|
|
160
160
|
}
|
|
161
161
|
async remove(options) {
|
|
162
162
|
const transaction2 = await this.getTransaction(options);
|
|
@@ -3091,7 +3091,7 @@ function peg$parse(input, options) {
|
|
|
3091
3091
|
let table = t && t.table || null;
|
|
3092
3092
|
if (Array.isArray(v)) {
|
|
3093
3093
|
v.forEach((row, idx) => {
|
|
3094
|
-
if (row.value.length
|
|
3094
|
+
if (row.value.length !== c.length) {
|
|
3095
3095
|
throw new Error(`Error: column count doesn't match value count at row ${idx + 1}`);
|
|
3096
3096
|
}
|
|
3097
3097
|
});
|
package/lib/sync-runner.js
CHANGED
|
@@ -82,17 +82,17 @@ const _SyncRunner = class _SyncRunner {
|
|
|
82
82
|
}
|
|
83
83
|
try {
|
|
84
84
|
const columnsBePrimaryKey = Object.keys(columns).filter((key) => {
|
|
85
|
-
return columns[key].primaryKey
|
|
85
|
+
return columns[key].primaryKey === true;
|
|
86
86
|
}).sort();
|
|
87
87
|
const columnsWillBePrimaryKey = Object.keys(this.rawAttributes).filter((key) => {
|
|
88
|
-
return this.rawAttributes[key].primaryKey
|
|
88
|
+
return this.rawAttributes[key].primaryKey === true;
|
|
89
89
|
}).map((key) => {
|
|
90
90
|
return this.rawAttributes[key].field;
|
|
91
91
|
}).sort();
|
|
92
|
-
if (columnsWillBePrimaryKey.length
|
|
92
|
+
if (columnsWillBePrimaryKey.length === 0) {
|
|
93
93
|
return;
|
|
94
94
|
}
|
|
95
|
-
if (JSON.stringify(columnsBePrimaryKey)
|
|
95
|
+
if (JSON.stringify(columnsBePrimaryKey) !== JSON.stringify(columnsWillBePrimaryKey)) {
|
|
96
96
|
await this.queryInterface.addConstraint(this.tableName, {
|
|
97
97
|
type: "primary key",
|
|
98
98
|
fields: columnsWillBePrimaryKey,
|
|
@@ -124,7 +124,7 @@ const _SyncRunner = class _SyncRunner {
|
|
|
124
124
|
const columnDefaultValue = columns[columnName].defaultValue;
|
|
125
125
|
if (columnDefaultValue === null && attributeDefaultValue === void 0) continue;
|
|
126
126
|
if (columnDefaultValue === "NULL" && attributeDefaultValue === null) continue;
|
|
127
|
-
if (columnDefaultValue
|
|
127
|
+
if (columnDefaultValue !== attributeDefaultValue) {
|
|
128
128
|
const changeAttribute = {
|
|
129
129
|
...currentAttribute,
|
|
130
130
|
defaultValue: attributeDefaultValue
|
|
@@ -153,10 +153,10 @@ const _SyncRunner = class _SyncRunner {
|
|
|
153
153
|
const existsIndexes = await this.queryInterface.showIndex(this.collection.getTableNameWithSchema(), options);
|
|
154
154
|
const existsUniqueIndexes = existsIndexes.filter((index) => index.unique);
|
|
155
155
|
const uniqueAttributes = Object.keys(this.rawAttributes).filter((key) => {
|
|
156
|
-
return this.rawAttributes[key].unique
|
|
156
|
+
return this.rawAttributes[key].unique === true;
|
|
157
157
|
});
|
|
158
158
|
for (const existUniqueIndex of existsUniqueIndexes) {
|
|
159
|
-
const isSingleField = existUniqueIndex.fields.length
|
|
159
|
+
const isSingleField = existUniqueIndex.fields.length === 1;
|
|
160
160
|
if (!isSingleField) continue;
|
|
161
161
|
const columnName = existUniqueIndex.fields[0].attribute;
|
|
162
162
|
const currentAttribute = this.findAttributeByColumnName(columnName);
|
|
@@ -180,7 +180,7 @@ const _SyncRunner = class _SyncRunner {
|
|
|
180
180
|
}
|
|
181
181
|
for (const uniqueAttribute of uniqueAttributes) {
|
|
182
182
|
const indexExists = existsUniqueIndexes.find((index) => {
|
|
183
|
-
return index.fields.length
|
|
183
|
+
return index.fields.length === 1 && index.fields[0].attribute === this.rawAttributes[uniqueAttribute].field;
|
|
184
184
|
});
|
|
185
185
|
if (!indexExists) {
|
|
186
186
|
await this.queryInterface.addIndex(this.tableName, [this.rawAttributes[uniqueAttribute].field], {
|
|
@@ -226,7 +226,7 @@ const _SyncRunner = class _SyncRunner {
|
|
|
226
226
|
}
|
|
227
227
|
findAttributeByColumnName(columnName) {
|
|
228
228
|
return Object.values(this.rawAttributes).find((attribute) => {
|
|
229
|
-
return attribute.field
|
|
229
|
+
return attribute.field === columnName;
|
|
230
230
|
});
|
|
231
231
|
}
|
|
232
232
|
async performSync(options) {
|
|
@@ -259,7 +259,7 @@ const _SyncRunner = class _SyncRunner {
|
|
|
259
259
|
}
|
|
260
260
|
async handleSchema(options) {
|
|
261
261
|
const _schema = this.model._schema;
|
|
262
|
-
if (_schema && _schema
|
|
262
|
+
if (_schema && _schema !== "public") {
|
|
263
263
|
await this.sequelize.query(`CREATE SCHEMA IF NOT EXISTS "${_schema}";`, {
|
|
264
264
|
raw: true,
|
|
265
265
|
transaction: options == null ? void 0 : options.transaction
|
package/lib/update-guard.js
CHANGED
|
@@ -53,7 +53,7 @@ const _UpdateGuard = class _UpdateGuard {
|
|
|
53
53
|
this.model = model;
|
|
54
54
|
}
|
|
55
55
|
setAssociationKeysToBeUpdate(associationKeysToBeUpdate) {
|
|
56
|
-
if (this.action
|
|
56
|
+
if (this.action === "create") {
|
|
57
57
|
this.associationKeysToBeUpdate = Object.keys(this.model.associations);
|
|
58
58
|
} else {
|
|
59
59
|
this.associationKeysToBeUpdate = associationKeysToBeUpdate;
|
|
@@ -112,7 +112,7 @@ const _UpdateGuard = class _UpdateGuard {
|
|
|
112
112
|
const listOfAssociation = /* @__PURE__ */ __name((list, association) => {
|
|
113
113
|
if (list) {
|
|
114
114
|
list = list.filter((whiteListKey) => whiteListKey.startsWith(`${association}.`)).map((whiteListKey) => whiteListKey.replace(`${association}.`, ""));
|
|
115
|
-
if (list.length
|
|
115
|
+
if (list.length === 0) {
|
|
116
116
|
return void 0;
|
|
117
117
|
}
|
|
118
118
|
return list;
|
|
@@ -130,7 +130,7 @@ const _UpdateGuard = class _UpdateGuard {
|
|
|
130
130
|
if (associationKeysToBeUpdate.includes(association)) {
|
|
131
131
|
return value;
|
|
132
132
|
}
|
|
133
|
-
const associationKeyName = associationObj.associationType
|
|
133
|
+
const associationKeyName = associationObj.associationType === "BelongsTo" || associationObj.associationType === "HasOne" ? associationObj.targetKey : associationObj.target.primaryKeyAttribute;
|
|
134
134
|
if (value[associationKeyName]) {
|
|
135
135
|
return import_lodash.default.pick(value, [associationKeyName, ...Object.keys(associationObj.target.associations)]);
|
|
136
136
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tachybase/database",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.41",
|
|
4
4
|
"description": "",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -23,12 +23,13 @@
|
|
|
23
23
|
"semver": "7.7.2",
|
|
24
24
|
"sequelize": "^6.37.5",
|
|
25
25
|
"umzug": "^3.8.2",
|
|
26
|
-
"@tachybase/
|
|
27
|
-
"@tachybase/
|
|
26
|
+
"@tachybase/logger": "1.3.41",
|
|
27
|
+
"@tachybase/utils": "1.3.41"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@types/flat": "^5.0.5",
|
|
31
31
|
"@types/glob": "^7.2.0",
|
|
32
|
-
"@types/lodash": "^4.17.20"
|
|
32
|
+
"@types/lodash": "^4.17.20",
|
|
33
|
+
"@types/semver": "7.7.0"
|
|
33
34
|
}
|
|
34
35
|
}
|