dynoquery 0.1.9 → 0.1.10

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 CHANGED
@@ -68,12 +68,13 @@ async function example() {
68
68
  await john.create('PROFILE', { name: 'John Doe', email: 'john@example.com' });
69
69
 
70
70
  // Resulting GSI1PK: TENANT#A#CAT#USER
71
- const cat = db.findByCategory('USER');
71
+ const cat = db.findByCategory('USER', '1');
72
+ console.log(cat.getSkValue()); // '1'
72
73
  await john.create('PROFILE', {
73
74
  name: 'John Doe',
74
75
  email: 'john@example.com',
75
76
  GSI1PK: cat.getPkValue(),
76
- GSI1SK: 'john@example.com'
77
+ GSI1SK: cat.getSkValue()
77
78
  });
78
79
 
79
80
  // Update the item (updates both DB and partition cache)
@@ -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
- - `get(skValue | options)`: Query items in the index. Supports `skValue` (string) for `begins_with` search, or an options object with `skValue`, `limit`, and `scanIndexForward`.
123
- - `getAll()`: Fetches all items in the index for the given partition key.
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
@@ -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,6 @@ export declare class IndexQuery {
22
24
  }): Promise<T[]>;
23
25
  getAll<T = any>(): Promise<T[]>;
24
26
  getPkValue(): string;
27
+ getSkValue(): string | undefined;
25
28
  private mapItemToModel;
26
29
  }
@@ -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,9 @@ class IndexQuery {
71
75
  getPkValue() {
72
76
  return this.pkValue;
73
77
  }
78
+ getSkValue() {
79
+ return this.skValue;
80
+ }
74
81
  mapItemToModel(item) {
75
82
  const pkName = this.db.getPkName();
76
83
  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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynoquery",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/devspikejs/dynoquery.git"