@cheetah.js/orm 0.1.92 → 0.1.95
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.
|
@@ -30,16 +30,16 @@ class SqlConditionBuilder {
|
|
|
30
30
|
return sqlParts;
|
|
31
31
|
}
|
|
32
32
|
processEntry(key, value, alias, model) {
|
|
33
|
-
this.trackLastNonOperatorKey(key);
|
|
33
|
+
this.trackLastNonOperatorKey(key, model);
|
|
34
34
|
const relationship = this.findRelationship(key, model);
|
|
35
35
|
if (relationship) {
|
|
36
36
|
return this.handleRelationship(relationship, value, alias);
|
|
37
37
|
}
|
|
38
38
|
if (this.isScalarValue(value)) {
|
|
39
|
-
return this.handleScalarValue(key, value, alias);
|
|
39
|
+
return this.handleScalarValue(key, value, alias, model);
|
|
40
40
|
}
|
|
41
41
|
if (this.isArrayValue(key, value)) {
|
|
42
|
-
return this.buildInCondition(key, value, alias);
|
|
42
|
+
return this.buildInCondition(key, value, alias, model);
|
|
43
43
|
}
|
|
44
44
|
return this.handleObjectValue(key, value, alias, model);
|
|
45
45
|
}
|
|
@@ -50,11 +50,11 @@ class SqlConditionBuilder {
|
|
|
50
50
|
}
|
|
51
51
|
return '';
|
|
52
52
|
}
|
|
53
|
-
handleScalarValue(key, value, alias) {
|
|
53
|
+
handleScalarValue(key, value, alias, model) {
|
|
54
54
|
if (key === '$eq') {
|
|
55
|
-
return this.buildSimpleCondition(this.lastKeyNotOperator, value, alias, '=');
|
|
55
|
+
return this.buildSimpleCondition(this.lastKeyNotOperator, value, alias, '=', model);
|
|
56
56
|
}
|
|
57
|
-
return this.buildSimpleCondition(key, value, alias);
|
|
57
|
+
return this.buildSimpleCondition(key, value, alias, '=', model);
|
|
58
58
|
}
|
|
59
59
|
handleObjectValue(key, value, alias, model) {
|
|
60
60
|
if (this.isLogicalOperator(key)) {
|
|
@@ -80,23 +80,23 @@ class SqlConditionBuilder {
|
|
|
80
80
|
buildOperatorCondition(key, operator, value, alias, model) {
|
|
81
81
|
switch (operator) {
|
|
82
82
|
case '$eq':
|
|
83
|
-
return this.buildSimpleCondition(key, value, alias, '=');
|
|
83
|
+
return this.buildSimpleCondition(key, value, alias, '=', model);
|
|
84
84
|
case '$ne':
|
|
85
|
-
return this.buildSimpleCondition(key, value, alias, '!=');
|
|
85
|
+
return this.buildSimpleCondition(key, value, alias, '!=', model);
|
|
86
86
|
case '$in':
|
|
87
|
-
return this.buildInCondition(key, value, alias);
|
|
87
|
+
return this.buildInCondition(key, value, alias, model);
|
|
88
88
|
case '$nin':
|
|
89
|
-
return this.buildNotInCondition(key, value, alias);
|
|
89
|
+
return this.buildNotInCondition(key, value, alias, model);
|
|
90
90
|
case '$like':
|
|
91
|
-
return this.buildLikeCondition(key, value, alias);
|
|
91
|
+
return this.buildLikeCondition(key, value, alias, model);
|
|
92
92
|
case '$gt':
|
|
93
|
-
return this.buildComparisonCondition(key, value, alias, '>');
|
|
93
|
+
return this.buildComparisonCondition(key, value, alias, '>', model);
|
|
94
94
|
case '$gte':
|
|
95
|
-
return this.buildComparisonCondition(key, value, alias, '>=');
|
|
95
|
+
return this.buildComparisonCondition(key, value, alias, '>=', model);
|
|
96
96
|
case '$lt':
|
|
97
|
-
return this.buildComparisonCondition(key, value, alias, '<');
|
|
97
|
+
return this.buildComparisonCondition(key, value, alias, '<', model);
|
|
98
98
|
case '$lte':
|
|
99
|
-
return this.buildComparisonCondition(key, value, alias, '<=');
|
|
99
|
+
return this.buildComparisonCondition(key, value, alias, '<=', model);
|
|
100
100
|
case '$and':
|
|
101
101
|
case '$or':
|
|
102
102
|
return this.buildNestedLogicalCondition(operator, value, alias, model);
|
|
@@ -104,23 +104,28 @@ class SqlConditionBuilder {
|
|
|
104
104
|
return '';
|
|
105
105
|
}
|
|
106
106
|
}
|
|
107
|
-
buildSimpleCondition(key, value, alias, operator
|
|
107
|
+
buildSimpleCondition(key, value, alias, operator, model) {
|
|
108
|
+
const column = this.resolveColumnName(key, model);
|
|
108
109
|
const formattedValue = this.formatValue(value);
|
|
109
|
-
return `${alias}.${
|
|
110
|
+
return `${alias}.${column} ${operator} ${formattedValue}`;
|
|
110
111
|
}
|
|
111
|
-
buildInCondition(key, values, alias) {
|
|
112
|
+
buildInCondition(key, values, alias, model) {
|
|
113
|
+
const column = this.resolveColumnName(key, model);
|
|
112
114
|
const formattedValues = values.map(val => this.formatValue(val)).join(', ');
|
|
113
|
-
return `${alias}.${
|
|
115
|
+
return `${alias}.${column} IN (${formattedValues})`;
|
|
114
116
|
}
|
|
115
|
-
buildNotInCondition(key, values, alias) {
|
|
117
|
+
buildNotInCondition(key, values, alias, model) {
|
|
118
|
+
const column = this.resolveColumnName(key, model);
|
|
116
119
|
const formattedValues = values.map(val => this.formatValue(val)).join(', ');
|
|
117
|
-
return `${alias}.${
|
|
120
|
+
return `${alias}.${column} NOT IN (${formattedValues})`;
|
|
118
121
|
}
|
|
119
|
-
buildLikeCondition(key, value, alias) {
|
|
120
|
-
|
|
122
|
+
buildLikeCondition(key, value, alias, model) {
|
|
123
|
+
const column = this.resolveColumnName(key, model);
|
|
124
|
+
return `${alias}.${column} LIKE '${value}'`;
|
|
121
125
|
}
|
|
122
|
-
buildComparisonCondition(key, value, alias, operator) {
|
|
123
|
-
|
|
126
|
+
buildComparisonCondition(key, value, alias, operator, model) {
|
|
127
|
+
const column = this.resolveColumnName(key, model);
|
|
128
|
+
return `${alias}.${column} ${operator} ${value}`;
|
|
124
129
|
}
|
|
125
130
|
buildNestedLogicalCondition(operator, value, alias, model) {
|
|
126
131
|
const conditions = value.map((cond) => this.build(cond, alias, model));
|
|
@@ -155,10 +160,29 @@ class SqlConditionBuilder {
|
|
|
155
160
|
extractLogicalOperator(key) {
|
|
156
161
|
return key.toUpperCase().replace('$', '');
|
|
157
162
|
}
|
|
158
|
-
trackLastNonOperatorKey(key) {
|
|
163
|
+
trackLastNonOperatorKey(key, model) {
|
|
159
164
|
if (!this.OPERATORS.includes(key)) {
|
|
160
165
|
this.lastKeyNotOperator = key;
|
|
161
166
|
}
|
|
162
167
|
}
|
|
168
|
+
resolveColumnName(property, model) {
|
|
169
|
+
if (property.startsWith('$')) {
|
|
170
|
+
return property;
|
|
171
|
+
}
|
|
172
|
+
const entity = this.entityStorage.get(model);
|
|
173
|
+
if (!entity) {
|
|
174
|
+
return property;
|
|
175
|
+
}
|
|
176
|
+
const column = entity.properties?.[property]?.options.columnName;
|
|
177
|
+
if (column) {
|
|
178
|
+
return column;
|
|
179
|
+
}
|
|
180
|
+
return this.resolveRelationColumn(property, entity) ?? property;
|
|
181
|
+
}
|
|
182
|
+
resolveRelationColumn(property, entity) {
|
|
183
|
+
const relation = entity.relations?.find(rel => rel.propertyKey === property);
|
|
184
|
+
const column = relation?.columnName;
|
|
185
|
+
return typeof column === 'string' ? column : undefined;
|
|
186
|
+
}
|
|
163
187
|
}
|
|
164
188
|
exports.SqlConditionBuilder = SqlConditionBuilder;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cheetah.js/orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.95",
|
|
4
4
|
"description": "A simple ORM for Cheetah.js",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"bun",
|
|
56
56
|
"value-object"
|
|
57
57
|
],
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "d9b887ebe3cf2b0bb4a2fa1b9e02ef5085342c2f"
|
|
59
59
|
}
|