betterddb 0.5.1 → 0.5.3
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/builders/query-builder.js +10 -12
- package/package.json +1 -1
- package/src/builders/query-builder.ts +15 -14
- package/test/get.test.ts +5 -0
- package/test/query.test.ts +10 -0
|
@@ -12,6 +12,11 @@ class QueryBuilder {
|
|
|
12
12
|
this.expressionAttributeNames = {};
|
|
13
13
|
this.expressionAttributeValues = {};
|
|
14
14
|
this.ascending = true;
|
|
15
|
+
const keys = this.parent.getKeys();
|
|
16
|
+
let pkName = keys.primary.name;
|
|
17
|
+
let builtKey = this.parent.buildKey(this.key);
|
|
18
|
+
this.expressionAttributeNames['#pk'] = pkName;
|
|
19
|
+
this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
|
|
15
20
|
}
|
|
16
21
|
usingIndex(indexName) {
|
|
17
22
|
if (!this.parent.getKeys().gsis) {
|
|
@@ -21,6 +26,10 @@ class QueryBuilder {
|
|
|
21
26
|
throw new Error('index does not exist');
|
|
22
27
|
}
|
|
23
28
|
this.index = this.parent.getKeys().gsis[indexName];
|
|
29
|
+
const pkName = this.index.primary.name;
|
|
30
|
+
const builtKey = this.parent.buildIndexes(this.key);
|
|
31
|
+
this.expressionAttributeNames['#pk'] = pkName;
|
|
32
|
+
this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
|
|
24
33
|
return this;
|
|
25
34
|
}
|
|
26
35
|
sortAscending() {
|
|
@@ -119,18 +128,7 @@ class QueryBuilder {
|
|
|
119
128
|
*/
|
|
120
129
|
async execute() {
|
|
121
130
|
var _a, _b, _c;
|
|
122
|
-
|
|
123
|
-
let pkName = keys.primary.name;
|
|
124
|
-
let builtKey = this.parent.buildKey(this.key);
|
|
125
|
-
if (this.index) {
|
|
126
|
-
pkName = this.index.primary.name;
|
|
127
|
-
builtKey = this.parent.buildIndexes(this.key);
|
|
128
|
-
}
|
|
129
|
-
if (!this.expressionAttributeNames['#pk']) {
|
|
130
|
-
this.expressionAttributeNames['#pk'] = pkName;
|
|
131
|
-
this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
|
|
132
|
-
this.keyConditions.unshift(`#pk = :pk_value`);
|
|
133
|
-
}
|
|
131
|
+
this.keyConditions.unshift(`#pk = :pk_value`);
|
|
134
132
|
const keyConditionExpression = this.keyConditions.join(' AND ');
|
|
135
133
|
const params = {
|
|
136
134
|
TableName: this.parent.getTableName(),
|
package/package.json
CHANGED
|
@@ -13,7 +13,14 @@ export class QueryBuilder<T> {
|
|
|
13
13
|
private lastKey?: Record<string, any>;
|
|
14
14
|
private ascending: boolean = true;
|
|
15
15
|
|
|
16
|
-
constructor(private parent: BetterDDB<T>, private key: Partial<T>, ) {
|
|
16
|
+
constructor(private parent: BetterDDB<T>, private key: Partial<T>, ) {
|
|
17
|
+
const keys = this.parent.getKeys();
|
|
18
|
+
let pkName = keys.primary.name;
|
|
19
|
+
let builtKey = this.parent.buildKey(this.key) as Record<string, any>;
|
|
20
|
+
|
|
21
|
+
this.expressionAttributeNames['#pk'] = pkName;
|
|
22
|
+
this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
|
|
23
|
+
}
|
|
17
24
|
|
|
18
25
|
public usingIndex(indexName: string): this {
|
|
19
26
|
if (!this.parent.getKeys().gsis) {
|
|
@@ -24,6 +31,12 @@ export class QueryBuilder<T> {
|
|
|
24
31
|
}
|
|
25
32
|
|
|
26
33
|
this.index = this.parent.getKeys().gsis![indexName];
|
|
34
|
+
|
|
35
|
+
const pkName = this.index.primary.name;
|
|
36
|
+
const builtKey = this.parent.buildIndexes(this.key);
|
|
37
|
+
this.expressionAttributeNames['#pk'] = pkName;
|
|
38
|
+
this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
|
|
39
|
+
|
|
27
40
|
return this;
|
|
28
41
|
}
|
|
29
42
|
|
|
@@ -136,19 +149,7 @@ export class QueryBuilder<T> {
|
|
|
136
149
|
* Executes the query and returns a Promise that resolves with an array of items.
|
|
137
150
|
*/
|
|
138
151
|
public async execute(): Promise<PaginatedResult<T>> {
|
|
139
|
-
|
|
140
|
-
let pkName = keys.primary.name;
|
|
141
|
-
let builtKey = this.parent.buildKey(this.key) as Record<string, any>;
|
|
142
|
-
if (this.index) {
|
|
143
|
-
pkName = this.index.primary.name;
|
|
144
|
-
builtKey = this.parent.buildIndexes(this.key);
|
|
145
|
-
}
|
|
146
|
-
if (!this.expressionAttributeNames['#pk']) {
|
|
147
|
-
this.expressionAttributeNames['#pk'] = pkName;
|
|
148
|
-
this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
|
|
149
|
-
this.keyConditions.unshift(`#pk = :pk_value`);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
+
this.keyConditions.unshift(`#pk = :pk_value`);
|
|
152
153
|
const keyConditionExpression = this.keyConditions.join(' AND ');
|
|
153
154
|
|
|
154
155
|
const params: QueryCommandInput = {
|
package/test/get.test.ts
CHANGED
|
@@ -77,4 +77,9 @@ describe('BetterDDB - Get Operation', () => {
|
|
|
77
77
|
expect(user).not.toBeNull();
|
|
78
78
|
expect(user?.id).toBe('user-123');
|
|
79
79
|
});
|
|
80
|
+
|
|
81
|
+
it('should retrieve an item using GetBuilder that does not exist', async () => {
|
|
82
|
+
const user = await userDdb.get({ id: 'user-123', email: 'jane@example.com' }).execute();
|
|
83
|
+
expect(user).toBeNull();
|
|
84
|
+
});
|
|
80
85
|
});
|
package/test/query.test.ts
CHANGED
|
@@ -179,4 +179,14 @@ describe('BetterDDB - Query Operation', () => {
|
|
|
179
179
|
expect(result.name).toContain('B');
|
|
180
180
|
});
|
|
181
181
|
});
|
|
182
|
+
|
|
183
|
+
it('should query items using QueryBuilder with where clause on an index', async () => {
|
|
184
|
+
// Query the GSI for email "alice@example.com". Two items match.
|
|
185
|
+
// Then apply two filter conditions: name begins_with "Alice" and name contains "B" should only match one.
|
|
186
|
+
const results = await userDdb.query({ email: 'alice@example.com' })
|
|
187
|
+
.usingIndex('EmailIndex')
|
|
188
|
+
.where('begins_with', { email: 'alice' })
|
|
189
|
+
.execute();
|
|
190
|
+
expect(results.items.length).toEqual(2);
|
|
191
|
+
});
|
|
182
192
|
});
|