rajt 0.0.32 → 0.0.33
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/package.json +1 -1
- package/src/auth/ability.ts +1 -1
- package/src/dynamodb/compact.ts +3 -2
- package/src/dynamodb/query-builder.ts +45 -19
package/package.json
CHANGED
package/src/auth/ability.ts
CHANGED
|
@@ -13,7 +13,7 @@ export class Ability {
|
|
|
13
13
|
static fromRoutes(actions: Routes) {
|
|
14
14
|
if (!actions?.length) return
|
|
15
15
|
|
|
16
|
-
const paths = actions?.map(a => Array.isArray(a) ? a[1] : a.path)
|
|
16
|
+
const paths = actions?.map(a => Array.isArray(a) ? a[0]+a[1] : a.method+a.path) || []
|
|
17
17
|
const items = new Set(paths)
|
|
18
18
|
|
|
19
19
|
if (items.size !== actions.length)
|
package/src/dynamodb/compact.ts
CHANGED
|
@@ -147,9 +147,10 @@ export default class Compact {
|
|
|
147
147
|
|
|
148
148
|
return val
|
|
149
149
|
}
|
|
150
|
-
const length = getLength(val, type)
|
|
151
150
|
|
|
152
|
-
|
|
151
|
+
const length = getLength(val, type)
|
|
152
|
+
if ([null, true, false].includes(val) || type != 'object' && length < 2)
|
|
153
|
+
return val
|
|
153
154
|
|
|
154
155
|
const index = seen.indexOf(val)
|
|
155
156
|
if (index !== -1)
|
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
import type { Condition, Operator } from './types'
|
|
2
2
|
|
|
3
3
|
export default class QueryBuilder {
|
|
4
|
-
#
|
|
4
|
+
#filters: Condition[] = []
|
|
5
|
+
#keyConditions: Condition[] = []
|
|
5
6
|
#limit?: number
|
|
6
7
|
#startKey?: Record<string, any>
|
|
7
8
|
#index?: string
|
|
9
|
+
#attrCounter = 1
|
|
10
|
+
#valCounter = 1
|
|
11
|
+
#fieldAttrMap: Record<string, string> = {}
|
|
12
|
+
#fieldValMap: Record<string, string> = {}
|
|
8
13
|
|
|
9
14
|
filter(field: string, operator: Operator, value: any = null) {
|
|
10
|
-
this.#
|
|
15
|
+
this.#filters.push({ type: 'filter', field, operator, value })
|
|
11
16
|
return this
|
|
12
17
|
}
|
|
13
18
|
|
|
14
19
|
keyCondition(field: string, operator: Operator | any, value?: any) {
|
|
15
20
|
const noVal = value === undefined
|
|
16
|
-
this.#
|
|
21
|
+
this.#keyConditions.push({ type: 'keyCondition', field, operator: noVal ? '=' : operator, value: noVal ? operator : value })
|
|
17
22
|
return this
|
|
18
23
|
}
|
|
19
24
|
|
|
@@ -32,22 +37,45 @@ export default class QueryBuilder {
|
|
|
32
37
|
return this
|
|
33
38
|
}
|
|
34
39
|
|
|
35
|
-
|
|
40
|
+
private attrName(field: string) {
|
|
41
|
+
if (!this.#fieldAttrMap[field])
|
|
42
|
+
this.#fieldAttrMap[field] = '#a'+ this.#attrCounter++
|
|
43
|
+
|
|
44
|
+
return this.#fieldAttrMap[field]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private valName(val: any) {
|
|
48
|
+
val = String(val)
|
|
49
|
+
if (!this.#fieldValMap[val])
|
|
50
|
+
this.#fieldValMap[val] = ':v'+ this.#valCounter++
|
|
51
|
+
|
|
52
|
+
return this.#fieldValMap[val]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
#resetCounters() {
|
|
56
|
+
this.#attrCounter = 0
|
|
57
|
+
this.#valCounter = 0
|
|
58
|
+
this.#fieldAttrMap = {}
|
|
59
|
+
this.#fieldValMap = {}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
buildExpression(conditions: Condition[]) {
|
|
36
63
|
const exprParts: string[] = []
|
|
37
64
|
const values: Record<string, any> = {}
|
|
38
65
|
const names: Record<string, string> = {}
|
|
39
66
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
const val = `:val${i}`
|
|
67
|
+
for (const cond of conditions) {
|
|
68
|
+
const attr = this.attrName(cond.field)
|
|
69
|
+
const val = Array.isArray(cond.value) ? '' : this.valName(cond.value)
|
|
44
70
|
names[attr] = cond.field
|
|
45
71
|
|
|
46
72
|
switch (cond.operator) {
|
|
47
73
|
case 'between': {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
74
|
+
const val0 = this.valName(cond.value[0])
|
|
75
|
+
const val1 = this.valName(cond.value[1])
|
|
76
|
+
exprParts.push(`${attr} BETWEEN ${val0} AND ${val1}`)
|
|
77
|
+
values[val0] = cond.value[0]
|
|
78
|
+
values[val1] = cond.value[1]
|
|
51
79
|
break
|
|
52
80
|
}
|
|
53
81
|
case 'begins_with': {
|
|
@@ -56,10 +84,10 @@ export default class QueryBuilder {
|
|
|
56
84
|
break
|
|
57
85
|
}
|
|
58
86
|
case 'in': {
|
|
59
|
-
const inVals = cond.value.map((v: any
|
|
60
|
-
const
|
|
61
|
-
values[
|
|
62
|
-
return
|
|
87
|
+
const inVals = cond.value.map((v: any) => {
|
|
88
|
+
const key = this.valName(v)
|
|
89
|
+
values[key] = v
|
|
90
|
+
return key
|
|
63
91
|
})
|
|
64
92
|
exprParts.push(`${attr} IN (${inVals.join(', ')})`)
|
|
65
93
|
break
|
|
@@ -92,8 +120,6 @@ export default class QueryBuilder {
|
|
|
92
120
|
values[val] = cond.value
|
|
93
121
|
}
|
|
94
122
|
}
|
|
95
|
-
|
|
96
|
-
i++
|
|
97
123
|
}
|
|
98
124
|
|
|
99
125
|
return {
|
|
@@ -104,7 +130,7 @@ export default class QueryBuilder {
|
|
|
104
130
|
}
|
|
105
131
|
|
|
106
132
|
get filters() {
|
|
107
|
-
const filter = this.buildExpression(
|
|
133
|
+
const filter = this.buildExpression(this.#filters)
|
|
108
134
|
const params: any = {}
|
|
109
135
|
|
|
110
136
|
if (this.#limit)
|
|
@@ -126,7 +152,7 @@ export default class QueryBuilder {
|
|
|
126
152
|
}
|
|
127
153
|
|
|
128
154
|
get conditions() {
|
|
129
|
-
const keys = this.buildExpression(
|
|
155
|
+
const keys = this.buildExpression(this.#keyConditions)
|
|
130
156
|
const filters = this.filters
|
|
131
157
|
|
|
132
158
|
const params: any = { ...filters }
|