dynoquery 0.1.9 → 0.1.11
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/README.md +10 -8
- package/dist/index-query.d.ts +5 -0
- package/dist/index-query.js +13 -0
- package/dist/index.js +3 -2
- package/dist/partition.d.ts +2 -1
- package/dist/partition.js +9 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,7 +33,8 @@ const db = new DynoQuery({
|
|
|
33
33
|
},
|
|
34
34
|
findBy: {
|
|
35
35
|
// TENANT#A#CAT#
|
|
36
|
-
Category: { indexName: 'GSI1', pkPrefix: 'CAT#' } // pkName defaults to GSI1PK, skName defaults to GSI1SK
|
|
36
|
+
Category: { indexName: 'GSI1', pkPrefix: 'CAT#' }, // pkName defaults to GSI1PK, skName defaults to GSI1SK
|
|
37
|
+
Date: { indexName: 'GSI2', pkPrefix: 'DATE#' }
|
|
37
38
|
}
|
|
38
39
|
});
|
|
39
40
|
|
|
@@ -68,13 +69,13 @@ async function example() {
|
|
|
68
69
|
await john.create('PROFILE', { name: 'John Doe', email: 'john@example.com' });
|
|
69
70
|
|
|
70
71
|
// Resulting GSI1PK: TENANT#A#CAT#USER
|
|
71
|
-
const cat = db.findByCategory('USER');
|
|
72
|
+
const cat = db.findByCategory('USER', '1');
|
|
73
|
+
const date = db.findByDate('2026-10-11', '2');
|
|
74
|
+
console.log(cat.getSkValue()); // '1'
|
|
72
75
|
await john.create('PROFILE', {
|
|
73
76
|
name: 'John Doe',
|
|
74
77
|
email: 'john@example.com',
|
|
75
|
-
|
|
76
|
-
GSI1SK: 'john@example.com'
|
|
77
|
-
});
|
|
78
|
+
}, [cat, date]);
|
|
78
79
|
|
|
79
80
|
// Update the item (updates both DB and partition cache)
|
|
80
81
|
await john.update('PROFILE', { theme: 'dark' });
|
|
@@ -111,7 +112,7 @@ A way to manage data within a specific partition.
|
|
|
111
112
|
- `getPkValue()`: Returns the generated partition key value.
|
|
112
113
|
- `get(sk)`: Fetches data for a specific sort key (returns a Promise).
|
|
113
114
|
- `getAll()`: Fetches all items in the partition and caches them. Returns the items.
|
|
114
|
-
- `create(sk, data)`: Creates an item in the partition.
|
|
115
|
+
- `create(sk, data, indices?)`: Creates an item in the partition. If `indices` (array of `IndexQuery`) are provided, it automatically adds the index PK and SK to the item.
|
|
115
116
|
- `update(sk, data)`: Updates an existing item (partial update).
|
|
116
117
|
- `delete(sk)`: Deletes an item.
|
|
117
118
|
- `deleteAll()`: Deletes all items in the partition.
|
|
@@ -119,8 +120,9 @@ A way to manage data within a specific partition.
|
|
|
119
120
|
### IndexQuery
|
|
120
121
|
A way to query Global Secondary Indexes.
|
|
121
122
|
- `getPkValue()`: Returns the generated partition key value for this index.
|
|
122
|
-
- `
|
|
123
|
-
- `
|
|
123
|
+
- `getSkValue()`: Returns the sort key value if it was provided when calling the index query method.
|
|
124
|
+
- `get(skValue | options)`: Query items in the index. Supports `skValue` (string) for `begins_with` search, or an options object with `skValue`, `limit`, and `scanIndexForward`. If `skValue` was provided when the `IndexQuery` was created, it will be used as the default if no `skValue` is passed here.
|
|
125
|
+
- `getAll()`: Fetches all items in the index for the given partition key. If `skValue` was provided when the `IndexQuery` was created, it will filter by it using `begins_with`.
|
|
124
126
|
- Automatically identifies the model name in results using `__model` (based on registered models) and provides `getPartition()` helper.
|
|
125
127
|
|
|
126
128
|
## License
|
package/dist/index-query.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export interface IndexQueryConfig {
|
|
|
6
6
|
skName?: string;
|
|
7
7
|
pkPrefix?: string;
|
|
8
8
|
pkValue: string;
|
|
9
|
+
skValue?: string;
|
|
9
10
|
}
|
|
10
11
|
export declare class IndexQuery {
|
|
11
12
|
protected db: DynoQuery;
|
|
@@ -14,6 +15,7 @@ export declare class IndexQuery {
|
|
|
14
15
|
protected pkName: string;
|
|
15
16
|
protected skName: string;
|
|
16
17
|
protected pkValue: string;
|
|
18
|
+
protected skValue?: string;
|
|
17
19
|
constructor(db: DynoQuery, config: IndexQueryConfig);
|
|
18
20
|
get<T = any>(skValueOrOptions?: string | {
|
|
19
21
|
skValue?: string;
|
|
@@ -22,5 +24,8 @@ export declare class IndexQuery {
|
|
|
22
24
|
}): Promise<T[]>;
|
|
23
25
|
getAll<T = any>(): Promise<T[]>;
|
|
24
26
|
getPkValue(): string;
|
|
27
|
+
getPkName(): string;
|
|
28
|
+
getSkName(): string;
|
|
29
|
+
getSkValue(): string | undefined;
|
|
25
30
|
private mapItemToModel;
|
|
26
31
|
}
|
package/dist/index-query.js
CHANGED
|
@@ -18,6 +18,7 @@ class IndexQuery {
|
|
|
18
18
|
this.indexName = config.indexName;
|
|
19
19
|
this.pkName = config.pkName || (this.indexName + "PK");
|
|
20
20
|
this.skName = config.skName || (this.indexName + "SK");
|
|
21
|
+
this.skValue = config.skValue;
|
|
21
22
|
const globalPrefix = db.getPkPrefix();
|
|
22
23
|
const indexPrefix = config.pkPrefix || "";
|
|
23
24
|
let finalPrefix = indexPrefix;
|
|
@@ -38,6 +39,9 @@ class IndexQuery {
|
|
|
38
39
|
else if (typeof skValueOrOptions === 'object') {
|
|
39
40
|
options = skValueOrOptions;
|
|
40
41
|
}
|
|
42
|
+
else if (this.skValue) {
|
|
43
|
+
options.skValue = this.skValue;
|
|
44
|
+
}
|
|
41
45
|
let keyCondition = "#pk = :pk";
|
|
42
46
|
const expressionAttributeNames = {
|
|
43
47
|
"#pk": this.pkName,
|
|
@@ -71,6 +75,15 @@ class IndexQuery {
|
|
|
71
75
|
getPkValue() {
|
|
72
76
|
return this.pkValue;
|
|
73
77
|
}
|
|
78
|
+
getPkName() {
|
|
79
|
+
return this.pkName;
|
|
80
|
+
}
|
|
81
|
+
getSkName() {
|
|
82
|
+
return this.skName;
|
|
83
|
+
}
|
|
84
|
+
getSkValue() {
|
|
85
|
+
return this.skValue;
|
|
86
|
+
}
|
|
74
87
|
mapItemToModel(item) {
|
|
75
88
|
const pkName = this.db.getPkName();
|
|
76
89
|
const pkValue = item[pkName];
|
package/dist/index.js
CHANGED
|
@@ -64,13 +64,14 @@ class DynoQuery {
|
|
|
64
64
|
const { IndexQuery } = require("./index-query");
|
|
65
65
|
Object.entries(findBy).forEach(([name, def]) => {
|
|
66
66
|
const methodName = `findBy${name}`;
|
|
67
|
-
this[methodName] = (id) => {
|
|
67
|
+
this[methodName] = (id, skValue) => {
|
|
68
68
|
return new IndexQuery(this, {
|
|
69
69
|
indexName: def.indexName,
|
|
70
70
|
pkName: def.pkName,
|
|
71
71
|
skName: def.skName,
|
|
72
72
|
pkPrefix: def.pkPrefix,
|
|
73
|
-
pkValue: id
|
|
73
|
+
pkValue: id,
|
|
74
|
+
skValue: skValue
|
|
74
75
|
});
|
|
75
76
|
};
|
|
76
77
|
});
|
package/dist/partition.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DynoQuery } from "./index";
|
|
2
|
+
import { IndexQuery } from "./index-query";
|
|
2
3
|
export interface PartitionConfig {
|
|
3
4
|
tableName?: string;
|
|
4
5
|
pk?: string;
|
|
@@ -21,7 +22,7 @@ export declare class Partition {
|
|
|
21
22
|
/**
|
|
22
23
|
* Create an item in this partition.
|
|
23
24
|
*/
|
|
24
|
-
create<T = any>(sk: string, data: T): Promise<void>;
|
|
25
|
+
create<T = any>(sk: string, data: T, indices?: IndexQuery[]): Promise<void>;
|
|
25
26
|
/**
|
|
26
27
|
* Update an existing item in this partition.
|
|
27
28
|
*/
|
package/dist/partition.js
CHANGED
|
@@ -79,9 +79,17 @@ class Partition {
|
|
|
79
79
|
/**
|
|
80
80
|
* Create an item in this partition.
|
|
81
81
|
*/
|
|
82
|
-
create(sk, data) {
|
|
82
|
+
create(sk, data, indices) {
|
|
83
83
|
return __awaiter(this, void 0, void 0, function* () {
|
|
84
84
|
const item = Object.assign({ [this.pkName]: this.pkValue, [this.skName]: sk }, data);
|
|
85
|
+
if (indices) {
|
|
86
|
+
indices.forEach((index) => {
|
|
87
|
+
item[index.getPkName()] = index.getPkValue();
|
|
88
|
+
if (index.getSkValue() !== undefined) {
|
|
89
|
+
item[index.getSkName()] = index.getSkValue();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
85
93
|
yield this.db.create({
|
|
86
94
|
TableName: this.tableName,
|
|
87
95
|
Item: item,
|