dynoquery 0.1.8 → 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
@@ -31,9 +31,9 @@ const db = new DynoQuery({
31
31
  models: {
32
32
  User: { pkPrefix: 'USER#' }, // TENANT#A#USER#
33
33
  },
34
- indexes: {
34
+ findBy: {
35
35
  // TENANT#A#CAT#
36
- ByCategory: { 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
37
  }
38
38
  });
39
39
 
@@ -44,7 +44,7 @@ async function example() {
44
44
 
45
45
  // Use registered index
46
46
  // Resulting GSI1PK: TENANT#A#CAT#1
47
- const categories = db.ByCategory('1');
47
+ const categories = db.findByCategory('1');
48
48
  const items = await categories.get('100');
49
49
  const allItems = await categories.getAll();
50
50
 
@@ -67,14 +67,14 @@ async function example() {
67
67
  // Create an item through partition
68
68
  await john.create('PROFILE', { name: 'John Doe', email: 'john@example.com' });
69
69
 
70
- // You can also use getPkValue() to get the generated PK for the partition or index
71
- // This is useful when you need to store it in another attribute (e.g., GSI)
72
- const cat = db.ByCategory('USER');
70
+ // Resulting GSI1PK: TENANT#A#CAT#USER
71
+ const cat = db.findByCategory('USER', '1');
72
+ console.log(cat.getSkValue()); // '1'
73
73
  await john.create('PROFILE', {
74
74
  name: 'John Doe',
75
75
  email: 'john@example.com',
76
76
  GSI1PK: cat.getPkValue(),
77
- GSI1SK: 'john@example.com'
77
+ GSI1SK: cat.getSkValue()
78
78
  });
79
79
 
80
80
  // Update the item (updates both DB and partition cache)
@@ -120,8 +120,9 @@ A way to manage data within a specific partition.
120
120
  ### IndexQuery
121
121
  A way to query Global Secondary Indexes.
122
122
  - `getPkValue()`: Returns the generated partition key value for this index.
123
- - `get(skValue | options)`: Query items in the index. Supports `skValue` (string) for `begins_with` search, or an options object with `skValue`, `limit`, and `scanIndexForward`.
124
- - `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`.
125
126
  - Automatically identifies the model name in results using `__model` (based on registered models) and provides `getPartition()` helper.
126
127
 
127
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.d.ts CHANGED
@@ -14,7 +14,7 @@ export interface DynoQueryConfig {
14
14
  models?: Record<string, {
15
15
  pkPrefix: string;
16
16
  }>;
17
- indexes?: Record<string, {
17
+ findBy?: Record<string, {
18
18
  indexName: string;
19
19
  pkName?: string;
20
20
  skName?: string;
package/dist/index.js CHANGED
@@ -41,7 +41,7 @@ const partition_1 = require("./partition");
41
41
  class DynoQuery {
42
42
  constructor(config = {}) {
43
43
  this.registeredModels = {};
44
- const { tableName, pkName, skName, pkPrefix, models, indexes } = config, clientConfig = __rest(config, ["tableName", "pkName", "skName", "pkPrefix", "models", "indexes"]);
44
+ const { tableName, pkName, skName, pkPrefix, models, findBy } = config, clientConfig = __rest(config, ["tableName", "pkName", "skName", "pkPrefix", "models", "findBy"]);
45
45
  this.client = new client_dynamodb_1.DynamoDBClient(clientConfig);
46
46
  this.docClient = lib_dynamodb_1.DynamoDBDocumentClient.from(this.client, {
47
47
  marshallOptions: {
@@ -60,16 +60,18 @@ class DynoQuery {
60
60
  };
61
61
  });
62
62
  }
63
- if (indexes) {
63
+ if (findBy) {
64
64
  const { IndexQuery } = require("./index-query");
65
- Object.entries(indexes).forEach(([name, def]) => {
66
- this[name] = (id) => {
65
+ Object.entries(findBy).forEach(([name, def]) => {
66
+ const methodName = `findBy${name}`;
67
+ this[methodName] = (id, skValue) => {
67
68
  return new IndexQuery(this, {
68
69
  indexName: def.indexName,
69
70
  pkName: def.pkName,
70
71
  skName: def.skName,
71
72
  pkPrefix: def.pkPrefix,
72
- pkValue: id
73
+ pkValue: id,
74
+ skValue: skValue
73
75
  });
74
76
  };
75
77
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynoquery",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/devspikejs/dynoquery.git"