betterddb 0.5.4 → 0.6.0

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.
@@ -63,7 +63,7 @@ export interface KeysConfig<T> {
63
63
  export interface BetterDDBOptions<T> {
64
64
  schema: z.ZodType<T, z.ZodTypeDef, any>;
65
65
  tableName: string;
66
- entityType: string;
66
+ entityType?: string;
67
67
  keys: KeysConfig<T>;
68
68
  client: DynamoDBDocumentClient;
69
69
  /**
@@ -81,7 +81,7 @@ export interface BetterDDBOptions<T> {
81
81
  export declare class BetterDDB<T> {
82
82
  protected schema: z.ZodType<T, z.ZodTypeDef, any>;
83
83
  protected tableName: string;
84
- protected entityType: string;
84
+ protected entityType?: string;
85
85
  protected client: DynamoDBDocumentClient;
86
86
  protected keys: KeysConfig<T>;
87
87
  protected timestamps: boolean;
@@ -91,7 +91,7 @@ export declare class BetterDDB<T> {
91
91
  getClient(): DynamoDBDocumentClient;
92
92
  getSchema(): z.ZodType<T, z.ZodTypeDef, any>;
93
93
  getTimestamps(): boolean;
94
- getEntityType(): string;
94
+ getEntityType(): string | undefined;
95
95
  protected getKeyValue(def: KeyDefinition<T>, rawKey: Partial<T>): string;
96
96
  /**
97
97
  * Build the primary key from a raw key object.
package/lib/betterddb.js CHANGED
@@ -13,13 +13,13 @@ const batch_get_builder_1 = require("./builders/batch-get-builder");
13
13
  */
14
14
  class BetterDDB {
15
15
  constructor(options) {
16
- var _a;
16
+ var _a, _b;
17
17
  this.schema = options.schema;
18
18
  this.tableName = options.tableName;
19
- this.entityType = options.entityType.toUpperCase();
19
+ this.entityType = (_a = options.entityType) === null || _a === void 0 ? void 0 : _a.toUpperCase();
20
20
  this.keys = options.keys;
21
21
  this.client = options.client;
22
- this.timestamps = (_a = options.timestamps) !== null && _a !== void 0 ? _a : false;
22
+ this.timestamps = (_b = options.timestamps) !== null && _b !== void 0 ? _b : false;
23
23
  }
24
24
  getKeys() {
25
25
  return this.keys;
@@ -140,9 +140,11 @@ class QueryBuilder {
140
140
  ExclusiveStartKey: this.lastKey,
141
141
  IndexName: (_b = (_a = this.index) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : undefined,
142
142
  };
143
- this.filterConditions.push(`#entity = :entity_value`);
144
- this.expressionAttributeNames['#entity'] = 'entityType';
145
- this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
143
+ if (this.parent.getEntityType()) {
144
+ this.filterConditions.push(`#entity = :entity_value`);
145
+ this.expressionAttributeNames['#entity'] = 'entityType';
146
+ this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
147
+ }
146
148
  params.FilterExpression = this.filterConditions.join(' AND ');
147
149
  const result = await this.parent.getClient().send(new lib_dynamodb_1.QueryCommand(params));
148
150
  return { items: this.parent.getSchema().array().parse(result.Items), lastKey: (_c = result.LastEvaluatedKey) !== null && _c !== void 0 ? _c : undefined };
@@ -57,9 +57,12 @@ class ScanBuilder {
57
57
  Limit: this.limit,
58
58
  ExclusiveStartKey: this.lastKey
59
59
  };
60
- if (this.filters.length > 0) {
61
- params.FilterExpression = this.filters.join(' AND ');
60
+ if (this.parent.getEntityType()) {
61
+ this.filters.push(`#entity = :entity_value`);
62
+ this.expressionAttributeNames['#entity'] = 'entityType';
63
+ this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
62
64
  }
65
+ params.FilterExpression = this.filters.join(' AND ');
63
66
  const result = await this.parent.getClient().send(new lib_dynamodb_1.ScanCommand(params));
64
67
  return { items: this.parent.getSchema().array().parse(result.Items), lastKey: (_a = result.LastEvaluatedKey) !== null && _a !== void 0 ? _a : undefined };
65
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "betterddb",
3
- "version": "0.5.4",
3
+ "version": "0.6.0",
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",
package/src/betterddb.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { z, ZodSchema } from 'zod';
1
+ import { z } from 'zod';
2
2
  import { QueryBuilder } from './builders/query-builder';
3
3
  import { ScanBuilder } from './builders/scan-builder';
4
4
  import { UpdateBuilder } from './builders/update-builder';
@@ -72,7 +72,7 @@ export interface KeysConfig<T> {
72
72
  export interface BetterDDBOptions<T> {
73
73
  schema: z.ZodType<T, z.ZodTypeDef, any>;
74
74
  tableName: string;
75
- entityType: string;
75
+ entityType?: string;
76
76
  keys: KeysConfig<T>;
77
77
  client: DynamoDBDocumentClient;
78
78
  /**
@@ -91,7 +91,7 @@ export interface BetterDDBOptions<T> {
91
91
  export class BetterDDB<T> {
92
92
  protected schema: z.ZodType<T, z.ZodTypeDef, any>;
93
93
  protected tableName: string;
94
- protected entityType: string;
94
+ protected entityType?: string;
95
95
  protected client: DynamoDBDocumentClient;
96
96
  protected keys: KeysConfig<T>;
97
97
  protected timestamps: boolean;
@@ -99,7 +99,7 @@ export class BetterDDB<T> {
99
99
  constructor(options: BetterDDBOptions<T>) {
100
100
  this.schema = options.schema;
101
101
  this.tableName = options.tableName;
102
- this.entityType = options.entityType.toUpperCase();
102
+ this.entityType = options.entityType?.toUpperCase();
103
103
  this.keys = options.keys;
104
104
  this.client = options.client;
105
105
  this.timestamps = options.timestamps ?? false;
@@ -126,7 +126,7 @@ export class BetterDDB<T> {
126
126
  return this.timestamps;
127
127
  }
128
128
 
129
- public getEntityType(): string {
129
+ public getEntityType(): string | undefined {
130
130
  return this.entityType;
131
131
  }
132
132
 
@@ -163,9 +163,11 @@ export class QueryBuilder<T> {
163
163
  IndexName: this.index?.name ?? undefined,
164
164
  };
165
165
 
166
- this.filterConditions.push(`#entity = :entity_value`);
167
- this.expressionAttributeNames['#entity'] = 'entityType';
168
- this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
166
+ if (this.parent.getEntityType()) {
167
+ this.filterConditions.push(`#entity = :entity_value`);
168
+ this.expressionAttributeNames['#entity'] = 'entityType';
169
+ this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
170
+ }
169
171
  params.FilterExpression = this.filterConditions.join(' AND ');
170
172
 
171
173
  const result = await this.parent.getClient().send(new QueryCommand(params));
@@ -69,9 +69,12 @@ export class ScanBuilder<T> {
69
69
  ExclusiveStartKey: this.lastKey
70
70
  };
71
71
 
72
- if (this.filters.length > 0) {
73
- params.FilterExpression = this.filters.join(' AND ');
72
+ if (this.parent.getEntityType()) {
73
+ this.filters.push(`#entity = :entity_value`);
74
+ this.expressionAttributeNames['#entity'] = 'entityType';
75
+ this.expressionAttributeValues[':entity_value'] = this.parent.getEntityType();
74
76
  }
77
+ params.FilterExpression = this.filters.join(' AND ');
75
78
 
76
79
  const result = await this.parent.getClient().send(new ScanCommand(params));
77
80