betterddb 0.5.0 → 0.5.2

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.
@@ -54,9 +54,21 @@ class CreateBuilder {
54
54
  return this;
55
55
  }
56
56
  toTransactPut() {
57
+ const validated = this.parent.getSchema().parse(this.item);
58
+ let finalItem = { ...this.item, entityType: this.parent.getEntityType() };
59
+ if (this.parent.getTimestamps()) {
60
+ const now = new Date().toISOString();
61
+ finalItem = { ...finalItem, createdAt: now, updatedAt: now };
62
+ }
63
+ // Compute and merge primary key.
64
+ const computedKeys = this.parent.buildKey(validated);
65
+ finalItem = { ...finalItem, ...computedKeys };
66
+ // Compute and merge index attributes.
67
+ const indexAttributes = this.parent.buildIndexes(validated);
68
+ finalItem = { ...finalItem, ...indexAttributes };
57
69
  const putItem = {
58
70
  TableName: this.parent.getTableName(),
59
- Item: this.item,
71
+ Item: finalItem,
60
72
  };
61
73
  return { Put: putItem };
62
74
  }
@@ -12,6 +12,11 @@ class QueryBuilder {
12
12
  this.expressionAttributeNames = {};
13
13
  this.expressionAttributeValues = {};
14
14
  this.ascending = true;
15
+ const keys = this.parent.getKeys();
16
+ let pkName = keys.primary.name;
17
+ let builtKey = this.parent.buildKey(this.key);
18
+ this.expressionAttributeNames['#pk'] = pkName;
19
+ this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
15
20
  }
16
21
  usingIndex(indexName) {
17
22
  if (!this.parent.getKeys().gsis) {
@@ -21,6 +26,10 @@ class QueryBuilder {
21
26
  throw new Error('index does not exist');
22
27
  }
23
28
  this.index = this.parent.getKeys().gsis[indexName];
29
+ const pkName = this.index.primary.name;
30
+ const builtKey = this.parent.buildIndexes(this.key);
31
+ this.expressionAttributeNames['#pk'] = pkName;
32
+ this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
24
33
  return this;
25
34
  }
26
35
  sortAscending() {
@@ -119,18 +128,7 @@ class QueryBuilder {
119
128
  */
120
129
  async execute() {
121
130
  var _a, _b, _c;
122
- const keys = this.parent.getKeys();
123
- let pkName = keys.primary.name;
124
- let builtKey = this.parent.buildKey(this.key);
125
- if (this.index) {
126
- pkName = this.index.primary.name;
127
- builtKey = this.parent.buildIndexes(this.key);
128
- }
129
- if (!this.expressionAttributeNames['#pk']) {
130
- this.expressionAttributeNames['#pk'] = pkName;
131
- this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
132
- this.keyConditions.unshift(`#pk = :pk_value`);
133
- }
131
+ this.keyConditions.unshift(`#pk = :pk_value`);
134
132
  const keyConditionExpression = this.keyConditions.join(' AND ');
135
133
  const params = {
136
134
  TableName: this.parent.getTableName(),
@@ -146,6 +144,7 @@ class QueryBuilder {
146
144
  this.expressionAttributeNames['#entity'] = 'entityType';
147
145
  this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
148
146
  params.FilterExpression = this.filterConditions.join(' AND ');
147
+ console.log(params);
149
148
  const result = await this.parent.getClient().send(new lib_dynamodb_1.QueryCommand(params));
150
149
  return { items: this.parent.getSchema().array().parse(result.Items), lastKey: (_c = result.LastEvaluatedKey) !== null && _c !== void 0 ? _c : undefined };
151
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "betterddb",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
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",
@@ -59,9 +59,23 @@ export class CreateBuilder<T> {
59
59
  }
60
60
 
61
61
  public toTransactPut(): TransactWriteItem{
62
+ const validated = this.parent.getSchema().parse(this.item);
63
+ let finalItem: T = { ...this.item , entityType: this.parent.getEntityType() };
64
+ if (this.parent.getTimestamps()) {
65
+ const now = new Date().toISOString();
66
+ finalItem = { ...finalItem, createdAt: now, updatedAt: now } as T;
67
+ }
68
+
69
+ // Compute and merge primary key.
70
+ const computedKeys = this.parent.buildKey(validated as Partial<T>);
71
+ finalItem = { ...finalItem, ...computedKeys };
72
+
73
+ // Compute and merge index attributes.
74
+ const indexAttributes = this.parent.buildIndexes(validated as Partial<T>);
75
+ finalItem = { ...finalItem, ...indexAttributes };
62
76
  const putItem: Put = {
63
77
  TableName: this.parent.getTableName(),
64
- Item: this.item as Record<string, AttributeValue>,
78
+ Item: finalItem as Record<string, AttributeValue>,
65
79
  };
66
80
  return { Put: putItem };
67
81
  }
@@ -13,7 +13,14 @@ export class QueryBuilder<T> {
13
13
  private lastKey?: Record<string, any>;
14
14
  private ascending: boolean = true;
15
15
 
16
- constructor(private parent: BetterDDB<T>, private key: Partial<T>, ) {}
16
+ constructor(private parent: BetterDDB<T>, private key: Partial<T>, ) {
17
+ const keys = this.parent.getKeys();
18
+ let pkName = keys.primary.name;
19
+ let builtKey = this.parent.buildKey(this.key) as Record<string, any>;
20
+
21
+ this.expressionAttributeNames['#pk'] = pkName;
22
+ this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
23
+ }
17
24
 
18
25
  public usingIndex(indexName: string): this {
19
26
  if (!this.parent.getKeys().gsis) {
@@ -24,6 +31,12 @@ export class QueryBuilder<T> {
24
31
  }
25
32
 
26
33
  this.index = this.parent.getKeys().gsis![indexName];
34
+
35
+ const pkName = this.index.primary.name;
36
+ const builtKey = this.parent.buildIndexes(this.key);
37
+ this.expressionAttributeNames['#pk'] = pkName;
38
+ this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
39
+
27
40
  return this;
28
41
  }
29
42
 
@@ -136,19 +149,7 @@ export class QueryBuilder<T> {
136
149
  * Executes the query and returns a Promise that resolves with an array of items.
137
150
  */
138
151
  public async execute(): Promise<PaginatedResult<T>> {
139
- const keys = this.parent.getKeys();
140
- let pkName = keys.primary.name;
141
- let builtKey = this.parent.buildKey(this.key) as Record<string, any>;
142
- if (this.index) {
143
- pkName = this.index.primary.name;
144
- builtKey = this.parent.buildIndexes(this.key);
145
- }
146
- if (!this.expressionAttributeNames['#pk']) {
147
- this.expressionAttributeNames['#pk'] = pkName;
148
- this.expressionAttributeValues[':pk_value'] = builtKey[pkName];
149
- this.keyConditions.unshift(`#pk = :pk_value`);
150
- }
151
-
152
+ this.keyConditions.unshift(`#pk = :pk_value`);
152
153
  const keyConditionExpression = this.keyConditions.join(' AND ');
153
154
 
154
155
  const params: QueryCommandInput = {
@@ -167,6 +168,7 @@ export class QueryBuilder<T> {
167
168
  this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
168
169
  params.FilterExpression = this.filterConditions.join(' AND ');
169
170
 
171
+ console.log(params);
170
172
  const result = await this.parent.getClient().send(new QueryCommand(params));
171
173
  return {items: this.parent.getSchema().array().parse(result.Items) as T[], lastKey: result.LastEvaluatedKey ?? undefined};
172
174
  }