betterddb 0.6.0 → 0.6.4

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.
Files changed (49) hide show
  1. package/.eslintrc.cjs +41 -0
  2. package/lib/betterddb.js +29 -17
  3. package/lib/builders/batch-get-builder.js +6 -2
  4. package/lib/builders/create-builder.js +14 -6
  5. package/lib/builders/delete-builder.js +8 -5
  6. package/lib/builders/get-builder.js +11 -6
  7. package/lib/builders/query-builder.js +38 -31
  8. package/lib/builders/scan-builder.js +17 -11
  9. package/lib/builders/update-builder.js +27 -18
  10. package/lib/operator.js +10 -10
  11. package/lib/{betterddb.d.ts → types/betterddb.d.ts} +13 -9
  12. package/lib/types/betterddb.d.ts.map +1 -0
  13. package/lib/{builders → types/builders}/batch-get-builder.d.ts +2 -1
  14. package/lib/types/builders/batch-get-builder.d.ts.map +1 -0
  15. package/lib/{builders → types/builders}/create-builder.d.ts +3 -2
  16. package/lib/types/builders/create-builder.d.ts.map +1 -0
  17. package/lib/{builders → types/builders}/delete-builder.d.ts +3 -2
  18. package/lib/types/builders/delete-builder.d.ts.map +1 -0
  19. package/lib/{builders → types/builders}/get-builder.d.ts +3 -2
  20. package/lib/types/builders/get-builder.d.ts.map +1 -0
  21. package/lib/{builders → types/builders}/query-builder.d.ts +4 -3
  22. package/lib/types/builders/query-builder.d.ts.map +1 -0
  23. package/lib/{builders → types/builders}/scan-builder.d.ts +4 -3
  24. package/lib/types/builders/scan-builder.d.ts.map +1 -0
  25. package/lib/{builders → types/builders}/update-builder.d.ts +3 -2
  26. package/lib/types/builders/update-builder.d.ts.map +1 -0
  27. package/lib/types/index.d.ts +2 -0
  28. package/lib/types/index.d.ts.map +1 -0
  29. package/lib/types/operator.d.ts +3 -0
  30. package/lib/types/operator.d.ts.map +1 -0
  31. package/lib/types/{paginated-result.d.ts → types/paginated-result.d.ts} +1 -0
  32. package/lib/types/types/paginated-result.d.ts.map +1 -0
  33. package/package.json +15 -5
  34. package/prettier.config.js +6 -0
  35. package/src/betterddb.ts +46 -37
  36. package/src/builders/batch-get-builder.ts +11 -6
  37. package/src/builders/create-builder.ts +42 -27
  38. package/src/builders/delete-builder.ts +28 -17
  39. package/src/builders/get-builder.ts +26 -19
  40. package/src/builders/query-builder.ts +64 -44
  41. package/src/builders/scan-builder.ts +18 -14
  42. package/src/builders/update-builder.ts +53 -27
  43. package/src/index.ts +1 -1
  44. package/src/operator.ts +21 -21
  45. package/src/types/paginated-result.ts +1 -1
  46. package/test/update.test.ts +6 -0
  47. package/tsconfig.json +25 -11
  48. package/lib/index.d.ts +0 -1
  49. package/lib/operator.d.ts +0 -2
@@ -3,17 +3,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UpdateBuilder = void 0;
4
4
  const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
5
5
  class UpdateBuilder {
6
+ parent;
7
+ key;
8
+ actions = {};
9
+ condition;
10
+ // When using transaction mode, we store extra transaction items.
11
+ extraTransactItems = [];
6
12
  // Reference to the parent BetterDDB instance and key.
7
13
  constructor(parent, key) {
8
14
  this.parent = parent;
9
15
  this.key = key;
10
- this.actions = {};
11
- // When using transaction mode, we store extra transaction items.
12
- this.extraTransactItems = [];
13
16
  }
14
17
  // Chainable methods:
15
18
  set(attrs) {
16
- this.actions.set = { ...this.actions.set, ...attrs };
19
+ const partialSchema = this.parent.getSchema().partial();
20
+ const validated = partialSchema.parse(attrs);
21
+ this.actions.set = { ...this.actions.set, ...validated };
17
22
  return this;
18
23
  }
19
24
  remove(attrs) {
@@ -21,7 +26,9 @@ class UpdateBuilder {
21
26
  return this;
22
27
  }
23
28
  add(attrs) {
24
- this.actions.add = { ...this.actions.add, ...attrs };
29
+ const partialSchema = this.parent.getSchema().partial();
30
+ const validated = partialSchema.parse(attrs);
31
+ this.actions.add = { ...this.actions.add, ...validated };
25
32
  return this;
26
33
  }
27
34
  delete(attrs) {
@@ -72,17 +79,17 @@ class UpdateBuilder {
72
79
  setParts.push(`${nameKey} = ${valueKey}`);
73
80
  }
74
81
  if (setParts.length > 0) {
75
- clauses.push(`SET ${setParts.join(', ')}`);
82
+ clauses.push(`SET ${setParts.join(", ")}`);
76
83
  }
77
84
  }
78
85
  // Build REMOVE clause.
79
86
  if (this.actions.remove && this.actions.remove.length > 0) {
80
- const removeParts = this.actions.remove.map(attr => {
87
+ const removeParts = this.actions.remove.map((attr) => {
81
88
  const nameKey = `#remove_${String(attr)}`;
82
89
  ExpressionAttributeNames[nameKey] = String(attr);
83
90
  return nameKey;
84
91
  });
85
- clauses.push(`REMOVE ${removeParts.join(', ')}`);
92
+ clauses.push(`REMOVE ${removeParts.join(", ")}`);
86
93
  }
87
94
  // Build ADD clause.
88
95
  if (this.actions.add) {
@@ -95,7 +102,7 @@ class UpdateBuilder {
95
102
  addParts.push(`${nameKey} ${valueKey}`);
96
103
  }
97
104
  if (addParts.length > 0) {
98
- clauses.push(`ADD ${addParts.join(', ')}`);
105
+ clauses.push(`ADD ${addParts.join(", ")}`);
99
106
  }
100
107
  }
101
108
  // Build DELETE clause.
@@ -109,7 +116,7 @@ class UpdateBuilder {
109
116
  deleteParts.push(`${nameKey} ${valueKey}`);
110
117
  }
111
118
  if (deleteParts.length > 0) {
112
- clauses.push(`DELETE ${deleteParts.join(', ')}`);
119
+ clauses.push(`DELETE ${deleteParts.join(", ")}`);
113
120
  }
114
121
  }
115
122
  // Merge any provided condition attribute values.
@@ -117,9 +124,9 @@ class UpdateBuilder {
117
124
  Object.assign(ExpressionAttributeValues, this.condition.attributeValues);
118
125
  }
119
126
  return {
120
- updateExpression: clauses.join(' '),
127
+ updateExpression: clauses.join(" "),
121
128
  attributeNames: ExpressionAttributeNames,
122
- attributeValues: ExpressionAttributeValues
129
+ attributeValues: ExpressionAttributeValues,
123
130
  };
124
131
  }
125
132
  /**
@@ -132,7 +139,7 @@ class UpdateBuilder {
132
139
  Key: this.parent.buildKey(this.key),
133
140
  UpdateExpression: updateExpression,
134
141
  ExpressionAttributeNames: attributeNames,
135
- ExpressionAttributeValues: attributeValues
142
+ ExpressionAttributeValues: attributeValues,
136
143
  };
137
144
  if (this.condition && this.condition.expression) {
138
145
  updateItem.ConditionExpression = this.condition.expression;
@@ -149,12 +156,12 @@ class UpdateBuilder {
149
156
  // Combine with extra transaction items.
150
157
  const allItems = [...this.extraTransactItems, myTransactItem];
151
158
  await this.parent.getClient().send(new lib_dynamodb_1.TransactWriteCommand({
152
- TransactItems: allItems
159
+ TransactItems: allItems,
153
160
  }));
154
161
  // After transaction, retrieve the updated item.
155
162
  const result = await this.parent.get(this.key).execute();
156
163
  if (result === null) {
157
- throw new Error('Item not found after transaction update');
164
+ throw new Error("Item not found after transaction update");
158
165
  }
159
166
  return result;
160
167
  }
@@ -167,14 +174,16 @@ class UpdateBuilder {
167
174
  UpdateExpression: updateExpression,
168
175
  ExpressionAttributeNames: attributeNames,
169
176
  ExpressionAttributeValues: attributeValues,
170
- ReturnValues: 'ALL_NEW'
177
+ ReturnValues: "ALL_NEW",
171
178
  };
172
179
  if (this.condition && this.condition.expression) {
173
180
  params.ConditionExpression = this.condition.expression;
174
181
  }
175
- const result = await this.parent.getClient().send(new lib_dynamodb_1.UpdateCommand(params));
182
+ const result = await this.parent
183
+ .getClient()
184
+ .send(new lib_dynamodb_1.UpdateCommand(params));
176
185
  if (!result.Attributes) {
177
- throw new Error('No attributes returned after update');
186
+ throw new Error("No attributes returned after update");
178
187
  }
179
188
  return this.parent.getSchema().parse(result.Attributes);
180
189
  }
package/lib/operator.js CHANGED
@@ -3,29 +3,29 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getOperatorExpression = void 0;
4
4
  function getOperatorExpression(operator, nameKey, valueKey, secondValueKey) {
5
5
  switch (operator) {
6
- case '==':
6
+ case "==":
7
7
  return `${nameKey} = ${valueKey}`;
8
- case '!=':
8
+ case "!=":
9
9
  return `${nameKey} <> ${valueKey}`;
10
- case '<':
10
+ case "<":
11
11
  return `${nameKey} < ${valueKey}`;
12
- case '<=':
12
+ case "<=":
13
13
  return `${nameKey} <= ${valueKey}`;
14
- case '>':
14
+ case ">":
15
15
  return `${nameKey} > ${valueKey}`;
16
- case '>=':
16
+ case ">=":
17
17
  return `${nameKey} >= ${valueKey}`;
18
- case 'begins_with':
18
+ case "begins_with":
19
19
  return `begins_with(${nameKey}, ${valueKey})`;
20
- case 'between':
20
+ case "between":
21
21
  if (!secondValueKey) {
22
22
  throw new Error("The 'between' operator requires two value keys");
23
23
  }
24
24
  return `${nameKey} BETWEEN ${valueKey} AND ${secondValueKey}`;
25
- case 'contains':
25
+ case "contains":
26
26
  return `contains(${nameKey}, ${valueKey})`;
27
27
  default:
28
- throw new Error(`Unsupported operator: ${operator}`);
28
+ throw new Error("Unsupported operator");
29
29
  }
30
30
  }
31
31
  exports.getOperatorExpression = getOperatorExpression;
@@ -1,12 +1,12 @@
1
- import { z } from 'zod';
2
- import { QueryBuilder } from './builders/query-builder';
3
- import { ScanBuilder } from './builders/scan-builder';
4
- import { UpdateBuilder } from './builders/update-builder';
5
- import { CreateBuilder } from './builders/create-builder';
6
- import { GetBuilder } from './builders/get-builder';
7
- import { DeleteBuilder } from './builders/delete-builder';
8
- import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
9
- import { BatchGetBuilder } from './builders/batch-get-builder';
1
+ import { z } from "zod";
2
+ import { QueryBuilder } from "./builders/query-builder";
3
+ import { ScanBuilder } from "./builders/scan-builder";
4
+ import { UpdateBuilder } from "./builders/update-builder";
5
+ import { CreateBuilder } from "./builders/create-builder";
6
+ import { GetBuilder } from "./builders/get-builder";
7
+ import { DeleteBuilder } from "./builders/delete-builder";
8
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
9
+ import { BatchGetBuilder } from "./builders/batch-get-builder";
10
10
  export type PrimaryKeyValue = string | number;
11
11
  /**
12
12
  * A key definition can be either a simple key (a property name)
@@ -66,6 +66,7 @@ export interface BetterDDBOptions<T> {
66
66
  entityType?: string;
67
67
  keys: KeysConfig<T>;
68
68
  client: DynamoDBDocumentClient;
69
+ counter?: boolean;
69
70
  /**
70
71
  * If true, automatically inject timestamp fields:
71
72
  * - On create, sets both `createdAt` and `updatedAt`
@@ -85,7 +86,9 @@ export declare class BetterDDB<T> {
85
86
  protected client: DynamoDBDocumentClient;
86
87
  protected keys: KeysConfig<T>;
87
88
  protected timestamps: boolean;
89
+ protected counter: boolean;
88
90
  constructor(options: BetterDDBOptions<T>);
91
+ getCounter(): boolean;
89
92
  getKeys(): KeysConfig<T>;
90
93
  getTableName(): string;
91
94
  getClient(): DynamoDBDocumentClient;
@@ -133,3 +136,4 @@ export declare class BetterDDB<T> {
133
136
  */
134
137
  scan(): ScanBuilder<T>;
135
138
  }
139
+ //# sourceMappingURL=betterddb.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"betterddb.d.ts","sourceRoot":"","sources":["../../src/betterddb.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAC/D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,CAAC;AAE9C;;;;GAIG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IACvB,MAAM,CAAC,GACP;IACE,KAAK,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,KAAK,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AAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IAI7C;;OAEG;IACI,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,eAAe,CAAC,CAAC,CAAC;IAI1D;;OAEG;IACI,MAAM,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC;IAIhD;;OAEG;IACI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC;IAInD;;OAEG;IACI,KAAK,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC;IAI9C;;OAEG;IACI,IAAI,IAAI,WAAW,CAAC,CAAC,CAAC;CAG9B"}
@@ -1,4 +1,4 @@
1
- import { BetterDDB } from '../betterddb';
1
+ import { BetterDDB } from "../betterddb";
2
2
  export declare class BatchGetBuilder<T> {
3
3
  private parent;
4
4
  private keys;
@@ -13,3 +13,4 @@ export declare class BatchGetBuilder<T> {
13
13
  */
14
14
  execute(): Promise<T[]>;
15
15
  }
16
+ //# sourceMappingURL=batch-get-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"batch-get-builder.d.ts","sourceRoot":"","sources":["../../../src/builders/batch-get-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,qBAAa,eAAe,CAAC,CAAC;IAM1B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,IAAI;IANd;;;OAGG;gBAEO,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE;IAG5B;;;OAGG;IACU,OAAO,IAAI,OAAO,CAAC,CAAC,EAAE,CAAC;CAqCrC"}
@@ -1,5 +1,5 @@
1
- import { TransactWriteItem } from '@aws-sdk/client-dynamodb';
2
- import { BetterDDB } from '../betterddb';
1
+ import { TransactWriteItem } from "@aws-sdk/client-dynamodb";
2
+ import { BetterDDB } from "../betterddb";
3
3
  export declare class CreateBuilder<T> {
4
4
  private parent;
5
5
  private item;
@@ -9,3 +9,4 @@ export declare class CreateBuilder<T> {
9
9
  transactWrite(ops: TransactWriteItem[] | TransactWriteItem): this;
10
10
  toTransactPut(): TransactWriteItem;
11
11
  }
12
+ //# sourceMappingURL=create-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-builder.d.ts","sourceRoot":"","sources":["../../../src/builders/create-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,iBAAiB,EAClB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,qBAAa,aAAa,CAAC,CAAC;IAIxB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,IAAI;IAJd,OAAO,CAAC,kBAAkB,CAA2B;gBAG3C,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,IAAI,EAAE,CAAC;IAGJ,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC;IA+C3B,aAAa,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,iBAAiB,GAAG,IAAI;IASjE,aAAa,IAAI,iBAAiB;CAwB1C"}
@@ -1,5 +1,5 @@
1
- import { BetterDDB } from '../betterddb';
2
- import { TransactWriteItem } from '@aws-sdk/client-dynamodb';
1
+ import { BetterDDB } from "../betterddb";
2
+ import { TransactWriteItem } from "@aws-sdk/client-dynamodb";
3
3
  export declare class DeleteBuilder<T> {
4
4
  private parent;
5
5
  private key;
@@ -14,3 +14,4 @@ export declare class DeleteBuilder<T> {
14
14
  transactWrite(ops: TransactWriteItem[] | TransactWriteItem): this;
15
15
  toTransactDelete(): TransactWriteItem;
16
16
  }
17
+ //# sourceMappingURL=delete-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delete-builder.d.ts","sourceRoot":"","sources":["../../../src/builders/delete-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAmB,MAAM,0BAA0B,CAAC;AAE9E,qBAAa,aAAa,CAAC,CAAC;IAOxB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,GAAG;IAPb,OAAO,CAAC,SAAS,CAAC,CAGhB;IACF,OAAO,CAAC,kBAAkB,CAA2B;gBAE3C,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;IAGzB;;OAEG;IACI,aAAa,CAClB,UAAU,EAAE,MAAM,EAClB,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACnC,IAAI;IAUM,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA6B9B,aAAa,CAAC,GAAG,EAAE,iBAAiB,EAAE,GAAG,iBAAiB,GAAG,IAAI;IASjE,gBAAgB,IAAI,iBAAiB;CAW7C"}
@@ -1,5 +1,5 @@
1
- import { BetterDDB } from '../betterddb';
2
- import { TransactGetItem } from '@aws-sdk/client-dynamodb';
1
+ import { BetterDDB } from "../betterddb";
2
+ import { TransactGetItem } from "@aws-sdk/client-dynamodb";
3
3
  export declare class GetBuilder<T> {
4
4
  private parent;
5
5
  private key;
@@ -15,3 +15,4 @@ export declare class GetBuilder<T> {
15
15
  transactGet(ops: TransactGetItem[] | TransactGetItem): this;
16
16
  toTransactGet(): TransactGetItem;
17
17
  }
18
+ //# sourceMappingURL=get-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-builder.d.ts","sourceRoot":"","sources":["../../../src/builders/get-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAgB,eAAe,EAAE,MAAM,0BAA0B,CAAC;AACzE,qBAAa,UAAU,CAAC,CAAC;IAKrB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,GAAG;IALb,OAAO,CAAC,oBAAoB,CAAC,CAAS;IACtC,OAAO,CAAC,wBAAwB,CAA8B;IAC9D,OAAO,CAAC,kBAAkB,CAAyB;gBAEzC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;IAGzB;;OAEG;IACI,cAAc,CAAC,UAAU,EAAE,CAAC,MAAM,CAAC,CAAC,EAAE,GAAG,IAAI;IAUvC,OAAO,IAAI,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IA6BlC,WAAW,CAAC,GAAG,EAAE,eAAe,EAAE,GAAG,eAAe,GAAG,IAAI;IAS3D,aAAa,IAAI,eAAe;CAWxC"}
@@ -1,6 +1,6 @@
1
- import { BetterDDB } from '../betterddb';
2
- import { Operator } from '../operator';
3
- import { PaginatedResult } from '../types/paginated-result';
1
+ import { BetterDDB } from "../betterddb";
2
+ import { Operator } from "../operator";
3
+ import { PaginatedResult } from "../types/paginated-result";
4
4
  export declare class QueryBuilder<T> {
5
5
  private parent;
6
6
  private key;
@@ -25,3 +25,4 @@ export declare class QueryBuilder<T> {
25
25
  */
26
26
  execute(): Promise<PaginatedResult<T>>;
27
27
  }
28
+ //# sourceMappingURL=query-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-builder.d.ts","sourceRoot":"","sources":["../../../src/builders/query-builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAa,MAAM,cAAc,CAAC;AACpD,OAAO,EAAyB,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAE5D,qBAAa,YAAY,CAAC,CAAC;IAWvB,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,GAAG;IAXb,OAAO,CAAC,aAAa,CAAgB;IACrC,OAAO,CAAC,gBAAgB,CAAgB;IACxC,OAAO,CAAC,wBAAwB,CAA8B;IAC9D,OAAO,CAAC,yBAAyB,CAA2B;IAC5D,OAAO,CAAC,KAAK,CAAC,CAAe;IAC7B,OAAO,CAAC,KAAK,CAAC,CAAS;IACvB,OAAO,CAAC,OAAO,CAAC,CAAsB;IACtC,OAAO,CAAC,SAAS,CAAiB;gBAGxB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC;IAUlB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAkBnC,aAAa,IAAI,IAAI;IAKrB,cAAc,IAAI,IAAI;IAKtB,KAAK,CACV,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,GAC5C,IAAI;IAqDA,MAAM,CACX,SAAS,EAAE,MAAM,CAAC,EAClB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GACvB,IAAI;IAsCA,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKjC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;IAKpD;;OAEG;IACU,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;CA6BpD"}
@@ -1,6 +1,6 @@
1
- import { BetterDDB } from '../betterddb';
2
- import { Operator } from '../operator';
3
- import { PaginatedResult } from '../types/paginated-result';
1
+ import { BetterDDB } from "../betterddb";
2
+ import { Operator } from "../operator";
3
+ import { PaginatedResult } from "../types/paginated-result";
4
4
  export declare class ScanBuilder<T> {
5
5
  private parent;
6
6
  private filters;
@@ -17,3 +17,4 @@ export declare class ScanBuilder<T> {
17
17
  */
18
18
  execute(): Promise<PaginatedResult<T>>;
19
19
  }
20
+ //# sourceMappingURL=scan-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scan-builder.d.ts","sourceRoot":"","sources":["../../../src/builders/scan-builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAyB,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAE5D,qBAAa,WAAW,CAAC,CAAC;IAOZ,OAAO,CAAC,MAAM;IAN1B,OAAO,CAAC,OAAO,CAAgB;IAC/B,OAAO,CAAC,wBAAwB,CAA8B;IAC9D,OAAO,CAAC,yBAAyB,CAA2B;IAC5D,OAAO,CAAC,KAAK,CAAC,CAAS;IACvB,OAAO,CAAC,OAAO,CAAC,CAAsB;gBAElB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAEjC,KAAK,CACV,SAAS,EAAE,MAAM,CAAC,EAClB,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,GAAG,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,GACvB,IAAI;IA+BA,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAKjC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;IAKpD;;OAEG;IACU,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;CAwBpD"}
@@ -1,5 +1,5 @@
1
- import { BetterDDB } from '../betterddb';
2
- import { TransactWriteItem } from '@aws-sdk/client-dynamodb';
1
+ import { BetterDDB } from "../betterddb";
2
+ import { TransactWriteItem } from "@aws-sdk/client-dynamodb";
3
3
  export declare class UpdateBuilder<T> {
4
4
  private parent;
5
5
  private key;
@@ -32,3 +32,4 @@ export declare class UpdateBuilder<T> {
32
32
  */
33
33
  execute(): Promise<T>;
34
34
  }
35
+ //# sourceMappingURL=update-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"update-builder.d.ts","sourceRoot":"","sources":["../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version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
@@ -0,0 +1,3 @@
1
+ export type Operator = "==" | "!=" | "<" | "<=" | ">" | ">=" | "begins_with" | "between" | "contains";
2
+ export declare function getOperatorExpression(operator: Operator, nameKey: string, valueKey: string, secondValueKey?: string): string;
3
+ //# sourceMappingURL=operator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"operator.d.ts","sourceRoot":"","sources":["../../src/operator.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,GAChB,IAAI,GACJ,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,GAAG,GACH,IAAI,GACJ,aAAa,GACb,SAAS,GACT,UAAU,CAAC;AAEf,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,cAAc,CAAC,EAAE,MAAM,GACtB,MAAM,CA0BR"}
@@ -2,3 +2,4 @@ export type PaginatedResult<T> = {
2
2
  items: T[];
3
3
  lastKey: Record<string, any> | undefined;
4
4
  };
5
+ //# sourceMappingURL=paginated-result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paginated-result.d.ts","sourceRoot":"","sources":["../../../src/types/paginated-result.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI;IAC/B,KAAK,EAAE,CAAC,EAAE,CAAC;IACX,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC;CAC1C,CAAC"}
package/package.json CHANGED
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "name": "betterddb",
3
- "version": "0.6.0",
3
+ "version": "0.6.4",
4
4
  "description": "A definition-based DynamoDB wrapper library that provides a schema-driven and fully typesafe DAL.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "scripts": {
8
- "build": "tsc",
9
8
  "prepublishOnly": "npm run build",
10
9
  "docker:up": "docker-compose up -d",
11
10
  "docker:down": "docker-compose down",
12
11
  "test": "npm run docker:up && jest && npm run docker:down",
13
12
  "test:watch": "npm run docker:up && jest --watch",
14
- "test:coverage": "npm run docker:up && jest --coverage && npm run docker:down"
13
+ "test:coverage": "npm run docker:up && jest --coverage && npm run docker:down",
14
+ "format": "prettier --write 'src/**/*.ts'",
15
+ "lint": "eslint src/**/*.ts",
16
+ "clean": "del-cli ./lib",
17
+ "build": "npm run clean && tsc -p ./tsconfig.json",
18
+ "prepack": "npm run build"
15
19
  },
16
20
  "keywords": [
17
21
  "dynamodb",
@@ -28,14 +32,20 @@
28
32
  "author": "R.R. Wang",
29
33
  "license": "MIT",
30
34
  "dependencies": {
31
- "@aws-sdk/lib-dynamodb": "^3.744.0",
32
35
  "@aws-sdk/client-dynamodb": "^3.744.0",
36
+ "@aws-sdk/lib-dynamodb": "^3.744.0",
33
37
  "zod": "^3.24.1"
34
38
  },
35
39
  "devDependencies": {
36
40
  "@types/jest": "^29.5.14",
37
- "@types/node": "^18.15.0",
41
+ "@types/node": "^22.13.5",
42
+ "@typescript-eslint/eslint-plugin": "^8.24.1",
43
+ "del-cli": "^6.0.0",
44
+ "eslint": "^8.56.0",
45
+ "eslint-config-prettier": "^9.1.0",
46
+ "eslint-plugin-prettier": "^5.2.1",
38
47
  "jest": "^29.7.0",
48
+ "prettier": "^3.3.3",
39
49
  "ts-jest": "^29.2.5",
40
50
  "typescript": "^4.9.5"
41
51
  }
@@ -0,0 +1,6 @@
1
+ /** @type {import('prettier').Config} */
2
+ const config = {
3
+ tabWidth: 2,
4
+ };
5
+
6
+ export default config;
package/src/betterddb.ts CHANGED
@@ -1,13 +1,12 @@
1
- import { z } from 'zod';
2
- import { QueryBuilder } from './builders/query-builder';
3
- import { ScanBuilder } from './builders/scan-builder';
4
- import { UpdateBuilder } from './builders/update-builder';
5
- import { CreateBuilder } from './builders/create-builder';
6
- import { GetBuilder } from './builders/get-builder';
7
- import { DeleteBuilder } from './builders/delete-builder';
8
- import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
9
- import { BatchGetBuilder } from './builders/batch-get-builder';
10
-
1
+ import { z } from "zod";
2
+ import { QueryBuilder } from "./builders/query-builder";
3
+ import { ScanBuilder } from "./builders/scan-builder";
4
+ import { UpdateBuilder } from "./builders/update-builder";
5
+ import { CreateBuilder } from "./builders/create-builder";
6
+ import { GetBuilder } from "./builders/get-builder";
7
+ import { DeleteBuilder } from "./builders/delete-builder";
8
+ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
9
+ import { BatchGetBuilder } from "./builders/batch-get-builder";
11
10
  export type PrimaryKeyValue = string | number;
12
11
 
13
12
  /**
@@ -75,6 +74,7 @@ export interface BetterDDBOptions<T> {
75
74
  entityType?: string;
76
75
  keys: KeysConfig<T>;
77
76
  client: DynamoDBDocumentClient;
77
+ counter?: boolean;
78
78
  /**
79
79
  * If true, automatically inject timestamp fields:
80
80
  * - On create, sets both `createdAt` and `updatedAt`
@@ -95,6 +95,7 @@ export class BetterDDB<T> {
95
95
  protected client: DynamoDBDocumentClient;
96
96
  protected keys: KeysConfig<T>;
97
97
  protected timestamps: boolean;
98
+ protected counter: boolean;
98
99
 
99
100
  constructor(options: BetterDDBOptions<T>) {
100
101
  this.schema = options.schema;
@@ -103,21 +104,25 @@ export class BetterDDB<T> {
103
104
  this.keys = options.keys;
104
105
  this.client = options.client;
105
106
  this.timestamps = options.timestamps ?? false;
107
+ this.counter = options.counter ?? false;
108
+ }
109
+
110
+ public getCounter(): boolean {
111
+ return this.counter;
106
112
  }
107
113
 
108
114
  public getKeys(): KeysConfig<T> {
109
115
  return this.keys;
110
116
  }
111
-
117
+
112
118
  public getTableName(): string {
113
119
  return this.tableName;
114
120
  }
115
-
121
+
116
122
  public getClient(): DynamoDBDocumentClient {
117
123
  return this.client;
118
124
  }
119
-
120
-
125
+
121
126
  public getSchema(): z.ZodType<T, z.ZodTypeDef, any> {
122
127
  return this.schema;
123
128
  }
@@ -132,7 +137,11 @@ export class BetterDDB<T> {
132
137
 
133
138
  // Helper: Retrieve the key value from a KeyDefinition.
134
139
  protected getKeyValue(def: KeyDefinition<T>, rawKey: Partial<T>): string {
135
- if (typeof def === 'string' || typeof def === 'number' || typeof def === 'symbol') {
140
+ if (
141
+ typeof def === "string" ||
142
+ typeof def === "number" ||
143
+ typeof def === "symbol"
144
+ ) {
136
145
  return String(rawKey[def]);
137
146
  } else {
138
147
  return def.build(rawKey);
@@ -144,29 +153,29 @@ export class BetterDDB<T> {
144
153
  */
145
154
  public buildKey(rawKey: Partial<T>): Record<string, any> {
146
155
  const keyObj: Record<string, any> = {};
147
-
156
+
148
157
  // For primary (partition) key:
149
158
  const pkConfig = this.keys.primary;
150
159
  keyObj[pkConfig.name] =
151
- (typeof pkConfig.definition === 'string' ||
152
- typeof pkConfig.definition === 'number' ||
153
- typeof pkConfig.definition === 'symbol')
160
+ typeof pkConfig.definition === "string" ||
161
+ typeof pkConfig.definition === "number" ||
162
+ typeof pkConfig.definition === "symbol"
154
163
  ? String((rawKey as any)[pkConfig.definition])
155
164
  : pkConfig.definition.build(rawKey);
156
-
165
+
157
166
  // For sort key, if defined:
158
167
  if (this.keys.sort) {
159
168
  const skConfig = this.keys.sort;
160
169
  keyObj[skConfig.name] =
161
- (typeof skConfig.definition === 'string' ||
162
- typeof skConfig.definition === 'number' ||
163
- typeof skConfig.definition === 'symbol')
170
+ typeof skConfig.definition === "string" ||
171
+ typeof skConfig.definition === "number" ||
172
+ typeof skConfig.definition === "symbol"
164
173
  ? String((rawKey as any)[skConfig.definition])
165
174
  : skConfig.definition.build(rawKey);
166
175
  }
167
176
  return keyObj;
168
177
  }
169
-
178
+
170
179
  /**
171
180
  * Build index attributes for each defined GSI.
172
181
  */
@@ -175,23 +184,23 @@ export class BetterDDB<T> {
175
184
  if (this.keys.gsis) {
176
185
  for (const gsiName in this.keys.gsis) {
177
186
  const gsiConfig = this.keys.gsis[gsiName];
178
-
187
+
179
188
  // Compute primary index attribute.
180
- const primaryConfig = gsiConfig.primary;
189
+ const primaryConfig = gsiConfig!.primary;
181
190
  indexAttributes[primaryConfig.name] =
182
- (typeof primaryConfig.definition === 'string' ||
183
- typeof primaryConfig.definition === 'number' ||
184
- typeof primaryConfig.definition === 'symbol')
191
+ typeof primaryConfig.definition === "string" ||
192
+ typeof primaryConfig.definition === "number" ||
193
+ typeof primaryConfig.definition === "symbol"
185
194
  ? String((rawItem as any)[primaryConfig.definition])
186
195
  : primaryConfig.definition.build(rawItem);
187
-
196
+
188
197
  // Compute sort index attribute if provided.
189
- if (gsiConfig.sort) {
198
+ if (gsiConfig?.sort) {
190
199
  const sortConfig = gsiConfig.sort;
191
200
  indexAttributes[sortConfig.name] =
192
- (typeof sortConfig.definition === 'string' ||
193
- typeof sortConfig.definition === 'number' ||
194
- typeof sortConfig.definition === 'symbol')
201
+ typeof sortConfig.definition === "string" ||
202
+ typeof sortConfig.definition === "number" ||
203
+ typeof sortConfig.definition === "symbol"
195
204
  ? String((rawItem as any)[sortConfig.definition])
196
205
  : sortConfig.definition.build(rawItem);
197
206
  }
@@ -199,7 +208,7 @@ export class BetterDDB<T> {
199
208
  }
200
209
  return indexAttributes;
201
210
  }
202
-
211
+
203
212
  /**
204
213
  * Create an item:
205
214
  * - Computes primary key and index attributes,
@@ -209,7 +218,7 @@ export class BetterDDB<T> {
209
218
  public create(item: T): CreateBuilder<T> {
210
219
  return new CreateBuilder<T>(this, item);
211
220
  }
212
-
221
+
213
222
  /**
214
223
  * Get an item by its primary key.
215
224
  */
@@ -247,7 +256,7 @@ export class BetterDDB<T> {
247
256
 
248
257
  /**
249
258
  * Scan for items.
250
- */
259
+ */
251
260
  public scan(): ScanBuilder<T> {
252
261
  return new ScanBuilder<T>(this);
253
262
  }