dyno-table 2.0.2 → 2.1.1

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 (48) hide show
  1. package/README.md +126 -0
  2. package/dist/builders/condition-check-builder.cjs.map +1 -1
  3. package/dist/builders/condition-check-builder.js.map +1 -1
  4. package/dist/builders/delete-builder.cjs.map +1 -1
  5. package/dist/builders/delete-builder.js.map +1 -1
  6. package/dist/builders/paginator.cjs.map +1 -1
  7. package/dist/builders/paginator.js.map +1 -1
  8. package/dist/builders/put-builder.cjs.map +1 -1
  9. package/dist/builders/put-builder.js.map +1 -1
  10. package/dist/builders/query-builder.cjs +44 -21
  11. package/dist/builders/query-builder.cjs.map +1 -1
  12. package/dist/builders/query-builder.d.cts +1 -1
  13. package/dist/builders/query-builder.d.ts +1 -1
  14. package/dist/builders/query-builder.js +44 -21
  15. package/dist/builders/query-builder.js.map +1 -1
  16. package/dist/builders/transaction-builder.cjs.map +1 -1
  17. package/dist/builders/transaction-builder.js.map +1 -1
  18. package/dist/builders/update-builder.cjs.map +1 -1
  19. package/dist/builders/update-builder.js.map +1 -1
  20. package/dist/conditions.cjs.map +1 -1
  21. package/dist/conditions.js.map +1 -1
  22. package/dist/entity.cjs +196 -47
  23. package/dist/entity.cjs.map +1 -1
  24. package/dist/entity.d.cts +19 -7
  25. package/dist/entity.d.ts +19 -7
  26. package/dist/entity.js +196 -47
  27. package/dist/entity.js.map +1 -1
  28. package/dist/index.cjs +246 -61
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.d.cts +2 -2
  31. package/dist/index.d.ts +2 -2
  32. package/dist/index.js +246 -61
  33. package/dist/index.js.map +1 -1
  34. package/dist/{query-builder-BNWRCrJW.d.ts → query-builder-CUWdavZw.d.ts} +2 -0
  35. package/dist/{query-builder-DZ9JKgBN.d.cts → query-builder-DoZzZz_c.d.cts} +2 -0
  36. package/dist/{table-BhEeYauU.d.ts → table-4UxlW_wD.d.ts} +2 -1
  37. package/dist/{table-BpNOboD9.d.cts → table-D-xNCVFa.d.cts} +2 -1
  38. package/dist/table.cjs +58 -22
  39. package/dist/table.cjs.map +1 -1
  40. package/dist/table.d.cts +2 -2
  41. package/dist/table.d.ts +2 -2
  42. package/dist/table.js +58 -22
  43. package/dist/table.js.map +1 -1
  44. package/dist/types.d.cts +9 -2
  45. package/dist/types.d.ts +9 -2
  46. package/dist/utils.cjs.map +1 -1
  47. package/dist/utils.js.map +1 -1
  48. package/package.json +1 -1
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- export { T as Table } from './table-BpNOboD9.cjs';
1
+ export { T as Table } from './table-D-xNCVFa.cjs';
2
2
  export { EntityConfig, EntityRepository, IndexDefinition, QueryEntity, QueryRecord, createIndex, createQueries, defineEntity } from './entity.cjs';
3
3
  export { p as ComparisonOperator, C as Condition, q as ConditionOperator, E as ExpressionParams, K as KeyConditionOperator, L as LogicalOperator, P as PrimaryKey, r as PrimaryKeyWithoutExpression, k as and, h as attributeExists, j as attributeNotExists, d as beginsWith, c as between, f as contains, e as eq, g as gt, b as gte, i as inArray, l as lt, a as lte, n as ne, m as not, o as or } from './conditions-CC3NDfUU.cjs';
4
- export { Q as QueryBuilder, a as QueryOptions } from './query-builder-DZ9JKgBN.cjs';
4
+ export { Q as QueryBuilder, a as QueryOptions } from './query-builder-DoZzZz_c.cjs';
5
5
  export { PutBuilder, PutOptions } from './builders/put-builder.cjs';
6
6
  export { UpdateBuilder, UpdateOptions } from './builders/update-builder.cjs';
7
7
  export { DeleteBuilder, DeleteOptions } from './builders/delete-builder.cjs';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export { T as Table } from './table-BhEeYauU.js';
1
+ export { T as Table } from './table-4UxlW_wD.js';
2
2
  export { EntityConfig, EntityRepository, IndexDefinition, QueryEntity, QueryRecord, createIndex, createQueries, defineEntity } from './entity.js';
3
3
  export { p as ComparisonOperator, C as Condition, q as ConditionOperator, E as ExpressionParams, K as KeyConditionOperator, L as LogicalOperator, P as PrimaryKey, r as PrimaryKeyWithoutExpression, k as and, h as attributeExists, j as attributeNotExists, d as beginsWith, c as between, f as contains, e as eq, g as gt, b as gte, i as inArray, l as lt, a as lte, n as ne, m as not, o as or } from './conditions-DD0bvyHm.js';
4
- export { Q as QueryBuilder, a as QueryOptions } from './query-builder-BNWRCrJW.js';
4
+ export { Q as QueryBuilder, a as QueryOptions } from './query-builder-CUWdavZw.js';
5
5
  export { PutBuilder, PutOptions } from './builders/put-builder.js';
6
6
  export { UpdateBuilder, UpdateOptions } from './builders/update-builder.js';
7
7
  export { DeleteBuilder, DeleteOptions } from './builders/delete-builder.js';
package/dist/index.js CHANGED
@@ -519,30 +519,40 @@ var FilterBuilder = class {
519
519
  * @returns The builder instance for method chaining
520
520
  */
521
521
  filter(condition) {
522
- if (typeof condition === "function") {
523
- const conditionOperator = {
524
- eq,
525
- ne,
526
- lt,
527
- lte,
528
- gt,
529
- gte,
530
- between,
531
- inArray,
532
- beginsWith,
533
- contains,
534
- attributeExists,
535
- attributeNotExists,
536
- and,
537
- or,
538
- not
539
- };
540
- this.options.filter = condition(conditionOperator);
522
+ const newCondition = typeof condition === "function" ? condition(this.getConditionOperator()) : condition;
523
+ if (this.options.filter) {
524
+ if (this.options.filter.type === "and" && this.options.filter.conditions) {
525
+ this.options.filter = {
526
+ type: "and",
527
+ conditions: [...this.options.filter.conditions, newCondition]
528
+ };
529
+ } else {
530
+ this.options.filter = and(this.options.filter, newCondition);
531
+ }
541
532
  } else {
542
- this.options.filter = condition;
533
+ this.options.filter = newCondition;
543
534
  }
544
535
  return this;
545
536
  }
537
+ getConditionOperator() {
538
+ return {
539
+ eq,
540
+ ne,
541
+ lt,
542
+ lte,
543
+ gt,
544
+ gte,
545
+ between,
546
+ inArray,
547
+ beginsWith,
548
+ contains,
549
+ attributeExists,
550
+ attributeNotExists,
551
+ and,
552
+ or,
553
+ not
554
+ };
555
+ }
546
556
  /**
547
557
  * Specifies which attributes to return in the results.
548
558
  *
@@ -833,10 +843,23 @@ var QueryBuilder = class _QueryBuilder extends FilterBuilder {
833
843
  */
834
844
  clone() {
835
845
  const clone = new _QueryBuilder(this.executor, this.keyCondition);
836
- clone.options = { ...this.options };
846
+ clone.options = {
847
+ ...this.options,
848
+ filter: this.deepCloneFilter(this.options.filter)
849
+ };
837
850
  clone.selectedFields = new Set(this.selectedFields);
838
851
  return clone;
839
852
  }
853
+ deepCloneFilter(filter) {
854
+ if (!filter) return filter;
855
+ if (filter.type === "and" || filter.type === "or") {
856
+ return {
857
+ ...filter,
858
+ conditions: filter.conditions?.map((condition) => this.deepCloneFilter(condition)).filter((c) => c !== void 0)
859
+ };
860
+ }
861
+ return { ...filter };
862
+ }
840
863
  /**
841
864
  * Executes the query against DynamoDB and returns a generator that behaves like an array.
842
865
  *
@@ -3155,10 +3178,23 @@ var ScanBuilder = class _ScanBuilder extends FilterBuilder {
3155
3178
  */
3156
3179
  clone() {
3157
3180
  const clone = new _ScanBuilder(this.executor);
3158
- clone.options = { ...this.options };
3181
+ clone.options = {
3182
+ ...this.options,
3183
+ filter: this.deepCloneFilter(this.options.filter)
3184
+ };
3159
3185
  clone.selectedFields = new Set(this.selectedFields);
3160
3186
  return clone;
3161
3187
  }
3188
+ deepCloneFilter(filter) {
3189
+ if (!filter) return filter;
3190
+ if (filter.type === "and" || filter.type === "or") {
3191
+ return {
3192
+ ...filter,
3193
+ conditions: filter.conditions?.map((condition) => this.deepCloneFilter(condition)).filter((c) => c !== void 0)
3194
+ };
3195
+ }
3196
+ return { ...filter };
3197
+ }
3162
3198
  /**
3163
3199
  * Executes the scan against DynamoDB and returns a generator that behaves like an array.
3164
3200
  *
@@ -3772,27 +3808,132 @@ function createEntityAwareDeleteBuilder(builder, entityName) {
3772
3808
  return createEntityAwareBuilder(builder, entityName);
3773
3809
  }
3774
3810
 
3775
- // src/entity.ts
3776
- function defineEntity(config) {
3777
- const entityTypeAttributeName = config.settings?.entityTypeAttributeName ?? "entityType";
3778
- const buildIndexes = (dataForKeyGeneration, table) => {
3779
- return Object.entries(config.indexes ?? {}).reduce(
3780
- (acc, [indexName, index]) => {
3781
- const key = index.generateKey(dataForKeyGeneration);
3782
- const gsiConfig = table.gsis[indexName];
3783
- if (!gsiConfig) {
3784
- throw new Error(`GSI configuration not found for index: ${indexName}`);
3785
- }
3786
- if (key.pk) {
3787
- acc[gsiConfig.partitionKey] = key.pk;
3811
+ // src/entity/ddb-indexing.ts
3812
+ var IndexBuilder = class {
3813
+ /**
3814
+ * Creates a new IndexBuilder instance
3815
+ *
3816
+ * @param table - The DynamoDB table instance
3817
+ * @param indexes - The index definitions
3818
+ */
3819
+ constructor(table, indexes = {}) {
3820
+ this.table = table;
3821
+ this.indexes = indexes;
3822
+ }
3823
+ /**
3824
+ * Build index attributes for item creation
3825
+ *
3826
+ * @param item - The item to generate indexes for
3827
+ * @param options - Options for building indexes
3828
+ * @returns Record of GSI attribute names to their values
3829
+ */
3830
+ buildForCreate(item, options = {}) {
3831
+ const attributes = {};
3832
+ for (const [indexName, indexDef] of Object.entries(this.indexes)) {
3833
+ if (options.excludeReadOnly && indexDef.isReadOnly) {
3834
+ continue;
3835
+ }
3836
+ const key = indexDef.generateKey(item);
3837
+ const gsiConfig = this.table.gsis[indexName];
3838
+ if (!gsiConfig) {
3839
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3840
+ }
3841
+ if (key.pk) {
3842
+ attributes[gsiConfig.partitionKey] = key.pk;
3843
+ }
3844
+ if (key.sk && gsiConfig.sortKey) {
3845
+ attributes[gsiConfig.sortKey] = key.sk;
3846
+ }
3847
+ }
3848
+ return attributes;
3849
+ }
3850
+ /**
3851
+ * Build index attributes for item updates
3852
+ *
3853
+ * @param currentData - The current data before update
3854
+ * @param updates - The update data
3855
+ * @param options - Options for building indexes
3856
+ * @returns Record of GSI attribute names to their updated values
3857
+ */
3858
+ buildForUpdate(currentData, updates) {
3859
+ const attributes = {};
3860
+ const updatedItem = { ...currentData, ...updates };
3861
+ for (const [indexName, indexDef] of Object.entries(this.indexes)) {
3862
+ if (indexDef.isReadOnly) {
3863
+ continue;
3864
+ }
3865
+ let shouldUpdateIndex = false;
3866
+ try {
3867
+ const currentKey = indexDef.generateKey(currentData);
3868
+ const updatedKey = indexDef.generateKey(updatedItem);
3869
+ if (currentKey.pk !== updatedKey.pk || currentKey.sk !== updatedKey.sk) {
3870
+ shouldUpdateIndex = true;
3788
3871
  }
3789
- if (key.sk && gsiConfig.sortKey) {
3790
- acc[gsiConfig.sortKey] = key.sk;
3872
+ } catch {
3873
+ shouldUpdateIndex = true;
3874
+ }
3875
+ if (!shouldUpdateIndex) {
3876
+ continue;
3877
+ }
3878
+ let key;
3879
+ try {
3880
+ key = indexDef.generateKey(updatedItem);
3881
+ } catch (error) {
3882
+ if (error instanceof Error) {
3883
+ throw new Error(`Missing attributes: ${error.message}`);
3791
3884
  }
3792
- return acc;
3793
- },
3794
- {}
3795
- );
3885
+ throw error;
3886
+ }
3887
+ if (this.hasUndefinedValues(key)) {
3888
+ throw new Error(
3889
+ `Missing attributes: Cannot update entity: insufficient data to regenerate index "${indexName}". All attributes required by the index must be provided in the update operation, or the index must be marked as readOnly.`
3890
+ );
3891
+ }
3892
+ const gsiConfig = this.table.gsis[indexName];
3893
+ if (!gsiConfig) {
3894
+ throw new Error(`GSI configuration not found for index: ${indexName}`);
3895
+ }
3896
+ if (key.pk) {
3897
+ attributes[gsiConfig.partitionKey] = key.pk;
3898
+ }
3899
+ if (key.sk && gsiConfig.sortKey) {
3900
+ attributes[gsiConfig.sortKey] = key.sk;
3901
+ }
3902
+ }
3903
+ return attributes;
3904
+ }
3905
+ /**
3906
+ * Check if a key has undefined values
3907
+ *
3908
+ * @param key - The index key to check
3909
+ * @returns True if the key contains undefined values, false otherwise
3910
+ */
3911
+ hasUndefinedValues(key) {
3912
+ return (key.pk?.includes("undefined") ?? false) || (key.sk?.includes("undefined") ?? false);
3913
+ }
3914
+ };
3915
+
3916
+ // src/entity/index-utils.ts
3917
+ function buildIndexes(dataForKeyGeneration, table, indexes, excludeReadOnly = false) {
3918
+ if (!indexes) {
3919
+ return {};
3920
+ }
3921
+ const indexBuilder = new IndexBuilder(table, indexes);
3922
+ return indexBuilder.buildForCreate(dataForKeyGeneration, { excludeReadOnly });
3923
+ }
3924
+ function buildIndexUpdates(currentData, updates, table, indexes) {
3925
+ if (!indexes) {
3926
+ return {};
3927
+ }
3928
+ const indexBuilder = new IndexBuilder(table, indexes);
3929
+ return indexBuilder.buildForUpdate(currentData, updates);
3930
+ }
3931
+
3932
+ // src/entity/entity.ts
3933
+ function defineEntity(config) {
3934
+ const entityTypeAttributeName = config.settings?.entityTypeAttributeName ?? "entityType";
3935
+ const buildIndexes2 = (dataForKeyGeneration, table, excludeReadOnly = false) => {
3936
+ return buildIndexes(dataForKeyGeneration, table, config.indexes, excludeReadOnly);
3796
3937
  };
3797
3938
  const wrapMethodWithPreparation = (originalMethod, prepareFn, context) => {
3798
3939
  const wrappedMethod = (...args) => {
@@ -3844,7 +3985,7 @@ function defineEntity(config) {
3844
3985
  ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3845
3986
  };
3846
3987
  const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3847
- const indexes = buildIndexes(dataForKeyGeneration, table);
3988
+ const indexes = buildIndexes(dataForKeyGeneration, table, config.indexes, false);
3848
3989
  const validatedItem = {
3849
3990
  ...dataForKeyGeneration,
3850
3991
  [entityTypeAttributeName]: config.name,
@@ -3870,7 +4011,7 @@ function defineEntity(config) {
3870
4011
  ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3871
4012
  };
3872
4013
  const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3873
- const indexes = buildIndexes(dataForKeyGeneration, table);
4014
+ const indexes = buildIndexes(dataForKeyGeneration, table, config.indexes, false);
3874
4015
  const validatedItem = {
3875
4016
  ...dataForKeyGeneration,
3876
4017
  [entityTypeAttributeName]: config.name,
@@ -3912,7 +4053,7 @@ function defineEntity(config) {
3912
4053
  ...generateTimestamps(["createdAt", "updatedAt"], validatedData.value)
3913
4054
  };
3914
4055
  const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3915
- const indexes = buildIndexes(dataForKeyGeneration, table);
4056
+ const indexes = buildIndexes2(dataForKeyGeneration, table, false);
3916
4057
  const validatedItem = {
3917
4058
  [table.partitionKey]: primaryKey.pk,
3918
4059
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
@@ -3938,7 +4079,7 @@ function defineEntity(config) {
3938
4079
  ...generateTimestamps(["createdAt", "updatedAt"], validationResult.value)
3939
4080
  };
3940
4081
  const primaryKey = config.primaryKey.generateKey(dataForKeyGeneration);
3941
- const indexes = buildIndexes(dataForKeyGeneration, table);
4082
+ const indexes = buildIndexes(dataForKeyGeneration, table, config.indexes, false);
3942
4083
  const validatedItem = {
3943
4084
  [table.partitionKey]: primaryKey.pk,
3944
4085
  ...table.sortKey ? { [table.sortKey]: primaryKey.sk } : {},
@@ -3972,13 +4113,21 @@ function defineEntity(config) {
3972
4113
  }
3973
4114
  return createEntityAwarePutBuilder(builder, config.name);
3974
4115
  },
3975
- get: (key) => createEntityAwareGetBuilder(table.get(config.primaryKey.generateKey(key)), config.name),
4116
+ get: (key) => {
4117
+ return createEntityAwareGetBuilder(table.get(config.primaryKey.generateKey(key)), config.name);
4118
+ },
3976
4119
  update: (key, data) => {
3977
4120
  const primaryKeyObj = config.primaryKey.generateKey(key);
3978
4121
  const builder = table.update(primaryKeyObj);
3979
4122
  builder.condition(eq(entityTypeAttributeName, config.name));
3980
4123
  const timestamps = generateTimestamps(["updatedAt"], data);
3981
- builder.set({ ...data, ...timestamps });
4124
+ const indexUpdates = buildIndexUpdates(
4125
+ { ...key },
4126
+ { ...data, ...timestamps },
4127
+ table,
4128
+ config.indexes
4129
+ );
4130
+ builder.set({ ...data, ...timestamps, ...indexUpdates });
3982
4131
  return builder;
3983
4132
  },
3984
4133
  delete: (key) => {
@@ -4049,21 +4198,57 @@ function createQueries() {
4049
4198
  }
4050
4199
  function createIndex() {
4051
4200
  return {
4052
- input: (schema) => ({
4053
- partitionKey: (pkFn) => ({
4054
- sortKey: (skFn) => ({
4055
- name: "custom",
4056
- partitionKey: "pk",
4057
- sortKey: "sk",
4058
- generateKey: (item) => ({ pk: pkFn(item), sk: skFn(item) })
4201
+ input: (schema) => {
4202
+ const createIndexBuilder = (isReadOnly = false) => ({
4203
+ partitionKey: (pkFn) => ({
4204
+ sortKey: (skFn) => {
4205
+ const index = {
4206
+ name: "custom",
4207
+ partitionKey: "pk",
4208
+ sortKey: "sk",
4209
+ isReadOnly,
4210
+ generateKey: (item) => {
4211
+ const data = schema["~standard"].validate(item);
4212
+ if ("issues" in data && data.issues) {
4213
+ throw new Error(`Index validation failed: ${data.issues.map((i) => i.message).join(", ")}`);
4214
+ }
4215
+ const validData = "value" in data ? data.value : item;
4216
+ return { pk: pkFn(validData), sk: skFn(validData) };
4217
+ }
4218
+ };
4219
+ return Object.assign(index, {
4220
+ readOnly: (value = false) => ({
4221
+ ...index,
4222
+ isReadOnly: value
4223
+ })
4224
+ });
4225
+ },
4226
+ withoutSortKey: () => {
4227
+ const index = {
4228
+ name: "custom",
4229
+ partitionKey: "pk",
4230
+ isReadOnly,
4231
+ generateKey: (item) => {
4232
+ const data = schema["~standard"].validate(item);
4233
+ if ("issues" in data && data.issues) {
4234
+ throw new Error(`Index validation failed: ${data.issues.map((i) => i.message).join(", ")}`);
4235
+ }
4236
+ const validData = "value" in data ? data.value : item;
4237
+ return { pk: pkFn(validData) };
4238
+ }
4239
+ };
4240
+ return Object.assign(index, {
4241
+ readOnly: (value = true) => ({
4242
+ ...index,
4243
+ isReadOnly: value
4244
+ })
4245
+ });
4246
+ }
4059
4247
  }),
4060
- withoutSortKey: () => ({
4061
- name: "custom",
4062
- partitionKey: "pk",
4063
- generateKey: (item) => ({ pk: pkFn(item) })
4064
- })
4065
- })
4066
- })
4248
+ readOnly: (value = true) => createIndexBuilder(value)
4249
+ });
4250
+ return createIndexBuilder(false);
4251
+ }
4067
4252
  };
4068
4253
  }
4069
4254