dynoquery 0.1.16 → 0.1.18

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
@@ -66,7 +66,7 @@ async function userExample() {
66
66
 
67
67
  #### Second-Level Objects (Single-Row Operations)
68
68
 
69
- For a more flexible way to work with individual rows, you can use `draft()` and `get()` to obtain an `Item` object, then call `create()`, `update()`, or `save()` on it directly.
69
+ For a more flexible way to work with individual rows, you can use `draft()` and `get()` to obtain an `Item` object, then call `create()`, `update()`, or `save()` on it directly. You can also use a shorthand by passing a second argument to your model function (as shown in the examples below).
70
70
 
71
71
  ```typescript
72
72
  const john = db.User('john@example.com');
@@ -94,7 +94,10 @@ await johnFriend1.update({ Name: 'Alice', rank: 1 });
94
94
  const johnPref = john.draft('PREF', { theme: 'dark' });
95
95
  await johnPref.save();
96
96
 
97
- ````
97
+ // 6. Shorthand access (returns an Item object directly)
98
+ const johnMeta = db.User('john@example.com', 'METADATA');
99
+ await johnMeta.update({ name: 'John Doe' });
100
+ ```
98
101
 
99
102
  ### 2. Global Secondary Indexes (findBy)
100
103
 
@@ -153,7 +156,7 @@ await db.Product('p123').update('INFO', { price: 45 }, [electronics]);
153
156
  #### Using Item.create() and Item.update()
154
157
  ```typescript
155
158
  const electronics = db.findByCategory('ELECTRONICS', 'RANK#1');
156
- const mouse = db.Product('p123').draft('INFO');
159
+ const mouse = db.Product('p123', 'INFO');
157
160
 
158
161
  // Pass data and indices directly to create()
159
162
  await mouse.create({
@@ -170,7 +173,7 @@ You can also use `setIndex()` to attach indices to an item so they are used auto
170
173
 
171
174
  ```typescript
172
175
  const electronics = db.findByCategory('ELECTRONICS', 'RANK#1');
173
- const mouse = db.Product('p123').draft('INFO');
176
+ const mouse = db.Product('p123', 'INFO');
174
177
 
175
178
  mouse.setIndex(electronics);
176
179
  mouse.price = 50;
@@ -232,14 +235,16 @@ if (token) {
232
235
  - `delete(params)`: Low-level DeleteCommand wrapper.
233
236
  - `query(params)`: Low-level QueryCommand wrapper.
234
237
  - `scan(params)`: Low-level ScanCommand wrapper.
238
+ - `[ModelName](id, skValue?)`: Returns a `Partition` instance for the given ID. If `skValue` is provided, returns an `Item` object directly.
239
+ - `findBy[IndexName](id, skValue?)`: Returns an `IndexQuery` instance.
235
240
 
236
241
  ### Partition
237
- - `get(sk)`: Fetches data for a specific Sort Key value (returns a Promise).
242
+ - `get(skValue)`: Fetches data for a specific Sort Key value (returns a Promise).
238
243
  - `getAll(options?)`: Fetches items in the partition. Options: `{ limit, exclusiveStartKey }`.
239
- - `create(sk, data, indices?)`: Creates an item. `indices` is an array of `IndexQuery` for GSI population.
240
- - `update(sk, data)`: Partial update of an item.
241
- - `delete(sk)`: Deletes an item.
242
- - `draft(sk, data?)`: Returns an `Item` object initialized with `data` (optional).
244
+ - `create(skValue, data, indices?)`: Creates an item. `indices` is an array of `IndexQuery` for GSI population.
245
+ - `update(skValue, data)`: Partial update of an item.
246
+ - `delete(skValue)`: Deletes an item.
247
+ - `draft(skValue, data?)`: Returns an `Item` object initialized with `data` (optional).
243
248
  - `deleteAll()`: Deletes all items in the partition.
244
249
  - `getLastEvaluatedKey()`: Returns the pagination token from the last `getAll()`.
245
250
 
package/dist/index.js CHANGED
@@ -55,8 +55,12 @@ class DynoQuery {
55
55
  if (models) {
56
56
  this.registeredModels = models;
57
57
  Object.entries(models).forEach(([name, def]) => {
58
- this[name] = (id) => {
59
- return new partition_1.Partition(this, { pkPrefix: this.globalPkPrefix + def.pkPrefix }, id);
58
+ this[name] = (id, skValue) => {
59
+ const partition = new partition_1.Partition(this, { pkPrefix: this.globalPkPrefix + def.pkPrefix }, id);
60
+ if (skValue) {
61
+ return partition.draft(skValue);
62
+ }
63
+ return partition;
60
64
  };
61
65
  });
62
66
  }
@@ -34,7 +34,7 @@ export declare class Partition {
34
34
  /**
35
35
  * Create an item in this partition.
36
36
  */
37
- create<T = any>(sk: string, data: T, indices?: IndexQuery[]): Promise<T>;
37
+ create<T = any>(skValue: string, data: T, indices?: IndexQuery[]): Promise<T>;
38
38
  /**
39
39
  * Internal method to get raw data for a specific SK.
40
40
  */
@@ -42,21 +42,21 @@ export declare class Partition {
42
42
  /**
43
43
  * Update an existing item in this partition.
44
44
  */
45
- update<T = any>(sk: string, data: Partial<T>, indices?: IndexQuery[]): Promise<T>;
45
+ update<T = any>(skValue: string, data: Partial<T>, indices?: IndexQuery[]): Promise<T>;
46
46
  /**
47
47
  * Delete an item by its SK within this partition.
48
48
  */
49
- delete(sk: string): Promise<void>;
49
+ delete(skValue: string): Promise<void>;
50
50
  /**
51
51
  * Get data for a specific SK and return it wrapped in a Item object.
52
52
  */
53
- get<T = any>(sk: string): Promise<T | null>;
53
+ get<T = any>(skValue: string): Promise<T | null>;
54
54
  /**
55
55
  * Pre-draft an item for creation. Returns an Item object.
56
56
  * @param sk The sort key value
57
57
  * @param data Initial data for the row
58
58
  */
59
- draft<T = any>(sk: string, data?: any): T;
59
+ draft<T = any>(skValue: string, data?: any): T;
60
60
  getPkValue(): string;
61
61
  getLastEvaluatedKey(): any;
62
62
  /**
package/dist/partition.js CHANGED
@@ -129,9 +129,9 @@ class Partition {
129
129
  /**
130
130
  * Create an item in this partition.
131
131
  */
132
- create(sk, data, indices) {
132
+ create(skValue, data, indices) {
133
133
  return __awaiter(this, void 0, void 0, function* () {
134
- const item = Object.assign({ [this.pkName]: this.pkValue, [this.skName]: sk }, data);
134
+ const item = Object.assign({ [this.pkName]: this.pkValue, [this.skName]: skValue }, data);
135
135
  if (indices) {
136
136
  indices.forEach((index) => {
137
137
  item[index.getPkName()] = index.getPkValue();
@@ -144,17 +144,17 @@ class Partition {
144
144
  TableName: this.tableName,
145
145
  Item: item,
146
146
  });
147
- this.cache[sk] = item;
148
- return new Item(this, sk, item);
147
+ this.cache[skValue] = item;
148
+ return new Item(this, skValue, item);
149
149
  });
150
150
  }
151
151
  /**
152
152
  * Internal method to get raw data for a specific SK.
153
153
  */
154
- _getRaw(sk) {
154
+ _getRaw(skValue) {
155
155
  return __awaiter(this, void 0, void 0, function* () {
156
- if (this.cache[sk] !== undefined) {
157
- return this.cache[sk] || null;
156
+ if (this.cache[skValue] !== undefined) {
157
+ return this.cache[skValue] || null;
158
158
  }
159
159
  if (this.isLoaded) {
160
160
  return null;
@@ -163,12 +163,12 @@ class Partition {
163
163
  TableName: this.tableName,
164
164
  Key: {
165
165
  [this.pkName]: this.pkValue,
166
- [this.skName]: sk,
166
+ [this.skName]: skValue,
167
167
  },
168
168
  });
169
169
  const data = response.Item || null;
170
170
  if (data) {
171
- this.cache[sk] = data;
171
+ this.cache[skValue] = data;
172
172
  }
173
173
  return data;
174
174
  });
@@ -176,35 +176,35 @@ class Partition {
176
176
  /**
177
177
  * Update an existing item in this partition.
178
178
  */
179
- update(sk, data, indices) {
179
+ update(skValue, data, indices) {
180
180
  return __awaiter(this, void 0, void 0, function* () {
181
- const current = (yield this._getRaw(sk)) || {};
181
+ const current = (yield this._getRaw(skValue)) || {};
182
182
  const updated = Object.assign(Object.assign({}, current), data);
183
- return yield this.create(sk, updated, indices);
183
+ return yield this.create(skValue, updated, indices);
184
184
  });
185
185
  }
186
186
  /**
187
187
  * Delete an item by its SK within this partition.
188
188
  */
189
- delete(sk) {
189
+ delete(skValue) {
190
190
  return __awaiter(this, void 0, void 0, function* () {
191
191
  yield this.db.delete({
192
192
  TableName: this.tableName,
193
193
  Key: {
194
194
  [this.pkName]: this.pkValue,
195
- [this.skName]: sk,
195
+ [this.skName]: skValue,
196
196
  },
197
197
  });
198
- delete this.cache[sk];
198
+ delete this.cache[skValue];
199
199
  });
200
200
  }
201
201
  /**
202
202
  * Get data for a specific SK and return it wrapped in a Item object.
203
203
  */
204
- get(sk) {
204
+ get(skValue) {
205
205
  return __awaiter(this, void 0, void 0, function* () {
206
- const data = yield this._getRaw(sk);
207
- return data ? new Item(this, sk, data) : null;
206
+ const data = yield this._getRaw(skValue);
207
+ return data ? new Item(this, skValue, data) : null;
208
208
  });
209
209
  }
210
210
  /**
@@ -212,8 +212,8 @@ class Partition {
212
212
  * @param sk The sort key value
213
213
  * @param data Initial data for the row
214
214
  */
215
- draft(sk, data = {}) {
216
- return new Item(this, sk, data);
215
+ draft(skValue, data = {}) {
216
+ return new Item(this, skValue, data);
217
217
  }
218
218
  getPkValue() {
219
219
  return this.pkValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dynoquery",
3
- "version": "0.1.16",
3
+ "version": "0.1.18",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/devspikejs/dynoquery.git"