electrodb 2.5.1 → 2.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.
package/index.d.ts CHANGED
@@ -874,6 +874,65 @@ interface QueryOperations<A extends string, F extends string, C extends string,
874
874
  where: WhereClause<A,F,C,S,Item<A,F,C,S,S["attributes"]>,QueryBranches<A,F,C,S,ResponseItem,IndexCompositeAttributes>>
875
875
  }
876
876
 
877
+ type IndexKeyComposite<A extends string, F extends string, C extends string, S extends Schema<A,F,C>, I extends keyof S["indexes"]> =
878
+ & IndexCompositeAttributes<A,F,C,S,I>
879
+ & TableIndexCompositeAttributes<A,F,C,S>;
880
+
881
+ type IndexKeyCompositeWithMaybeTableIndex<A extends string, F extends string, C extends string, S extends Schema<A,F,C>, I extends keyof S["indexes"]> =
882
+ & IndexCompositeAttributes<A,F,C,S,I>
883
+ & Partial<TableIndexCompositeAttributes<A,F,C,S>>;
884
+
885
+ type IndexKeyCompositeFromItem<A extends string, F extends string, C extends string, S extends Schema<A,F,C>, I extends keyof S["indexes"]> =
886
+ & Partial<IndexCompositeAttributes<A,F,C,S,I>>
887
+ & Partial<TableIndexCompositeAttributes<A,F,C,S>>;
888
+
889
+ type ConversionOptions = {
890
+ strict?: 'all' | 'pk' | 'none';
891
+ }
892
+
893
+ export type Conversions<A extends string, F extends string, C extends string, S extends Schema<A,F,C>> = {
894
+ fromComposite: {
895
+ toCursor: (composite: {
896
+ [I in keyof S["indexes"]]: IndexKeyCompositeWithMaybeTableIndex<A,F,C,S,I>
897
+ }[keyof S['indexes']]) => string;
898
+ toKeys: <T = Record<string, string | number>>(composite: {
899
+ [I in keyof S["indexes"]]: IndexKeyCompositeWithMaybeTableIndex<A,F,C,S,I>
900
+ }[keyof S['indexes']], options?: ConversionOptions) => T;
901
+ },
902
+ fromKeys: {
903
+ toComposite: <T = {
904
+ [I in keyof S["indexes"]]: IndexKeyCompositeFromItem<A,F,C,S,I>
905
+ }[keyof S['indexes']]>(keys: Record<string, string | number>) => T;
906
+ toCursor: (keys: Record<string, string | number>) => string;
907
+ },
908
+ fromCursor: {
909
+ toKeys: <T = Record<string, string | number>> (cursor: string) => T;
910
+ toComposite: <T = Partial<{
911
+ [I in keyof S["indexes"]]: IndexKeyCompositeFromItem<A,F,C,S,I>
912
+ }>[keyof S['indexes']]>(cursor: string) => T;
913
+ },
914
+ byAccessPattern: {
915
+ [I in keyof S["indexes"]]: {
916
+ fromKeys: {
917
+ toCursor: (keys: Record<string, string | number>) => string;
918
+ // keys supplied may include the table index, maybe not so composite attributes for the table index are `Partial`
919
+ toComposite: <T = IndexKeyCompositeWithMaybeTableIndex<A,F,C,S,I>>(keys: Record<string, string | number>, options?: ConversionOptions) => T;
920
+ },
921
+ fromCursor: {
922
+ toKeys: <T = Record<string, string | number>>(cursor: string, options?: ConversionOptions) => T;
923
+ // a cursor must have the table index defined along with the keys for the index (if applicable)
924
+ toComposite: <T = IndexKeyComposite<A,F,C,S,I>>(cursor: string, options?: ConversionOptions) => T;
925
+ },
926
+ fromComposite: {
927
+ // a cursor must have the table index defined along with the keys for the index (if applicable)
928
+ toCursor: (composite: IndexKeyComposite<A,F,C,S,I>, options?: ConversionOptions) => string;
929
+ // maybe the only keys you need are for this index and not the
930
+ toKeys: <T = Record<string, string | number>>(composite: IndexKeyCompositeWithMaybeTableIndex<A,F,C,S,I>, options?: ConversionOptions) => T;
931
+ }
932
+ }
933
+ }
934
+ }
935
+
877
936
  export type Queries<A extends string, F extends string, C extends string, S extends Schema<A,F,C>> = {
878
937
  [I in keyof S["indexes"]]: <CompositeAttributes extends IndexCompositeAttributes<A,F,C,S,I>>(composite: CompositeAttributes) =>
879
938
  IndexSKAttributes<A,F,C,S,I> extends infer SK
@@ -990,6 +1049,25 @@ interface GoBatchGetTerminalOptions<Attributes> {
990
1049
  logger?: ElectroEventListener;
991
1050
  }
992
1051
 
1052
+ interface ServiceQueryGoTerminalOptions {
1053
+ cursor?: string | null,
1054
+ data?: 'raw' | 'includeKeys' | 'attributes';
1055
+ /** @depricated use 'data=raw' instead */
1056
+ raw?: boolean;
1057
+ /** @depricated use 'data=raw' instead */
1058
+ includeKeys?: boolean;
1059
+ table?: string;
1060
+ limit?: number;
1061
+ params?: object;
1062
+ originalErr?: boolean;
1063
+ ignoreOwnership?: boolean;
1064
+ pages?: number | 'all';
1065
+ listeners?: Array<ElectroEventListener>;
1066
+ logger?: ElectroEventListener;
1067
+ order?: 'asc' | 'desc';
1068
+ hydrate?: boolean;
1069
+ }
1070
+
993
1071
  interface GoQueryTerminalOptions<Attributes> {
994
1072
  cursor?: string | null,
995
1073
  data?: 'raw' | 'includeKeys' | 'attributes';
@@ -1007,6 +1085,7 @@ interface GoQueryTerminalOptions<Attributes> {
1007
1085
  listeners?: Array<ElectroEventListener>;
1008
1086
  logger?: ElectroEventListener;
1009
1087
  order?: 'asc' | 'desc';
1088
+ hydrate?: boolean;
1010
1089
  }
1011
1090
 
1012
1091
  interface TransactWriteQueryOptions {
@@ -1123,11 +1202,20 @@ export type EntityParseMultipleItems<A extends string, F extends string, C exten
1123
1202
 
1124
1203
  export type ParamTerminal<A extends string, F extends string, C extends string, S extends Schema<A,F,C>, ResponseItem> = <P extends any = any, Options extends ParamTerminalOptions<keyof ResponseItem> = ParamTerminalOptions<keyof ResponseItem>>(options?: Options) => P;
1125
1204
 
1126
- export type ServiceQueryRecordsGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T, cursor: string | null }>;
1205
+ export type ServiceQueryRecordsGo<ResponseType, Options = ServiceQueryGoTerminalOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T, cursor: string | null }>;
1127
1206
 
1128
1207
  export type QueryRecordsGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T, cursor: string | null }>;
1129
1208
 
1130
- export type UpdateRecordGo<ResponseType> = <T = ResponseType, Options extends UpdateQueryOptions = UpdateQueryOptions>(options?: Options) => Promise<{data: Partial<T>}>
1209
+ export type UpdateRecordGo<ResponseType> = <T = ResponseType, Options extends UpdateQueryOptions = UpdateQueryOptions>(options?: Options) =>
1210
+ Options extends infer O
1211
+ ? 'response' extends keyof O
1212
+ ? O['response'] extends 'all_new'
1213
+ ? Promise<{data: T}>
1214
+ : O['response'] extends 'all_old'
1215
+ ? Promise<{data: T}>
1216
+ : Promise<{data: Partial<T>}>
1217
+ : Promise<{data: Partial<T>}>
1218
+ : never;
1131
1219
 
1132
1220
  export type PutRecordGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T }>;
1133
1221
 
@@ -1136,7 +1224,7 @@ export type DeleteRecordOperationGo<ResponseType, Options = QueryOptions> = <T =
1136
1224
  export type BatchWriteGo<ResponseType> = <O extends BulkOptions>(options?: O) =>
1137
1225
  Promise<{ unprocessed: ResponseType }>
1138
1226
 
1139
- export type ParamRecord<Options = ParamOptions> = <P>(options?: Options) => P;
1227
+ export type ParamRecord<Options = ParamOptions> = <P = Record<string, any>>(options?: Options) => P;
1140
1228
 
1141
1229
  export class ElectroError extends Error {
1142
1230
  readonly name: 'ElectroError';
@@ -1660,6 +1748,7 @@ export interface Schema<A extends string, F extends string, C extends string> {
1660
1748
  };
1661
1749
  readonly indexes: {
1662
1750
  [accessPattern: string]: {
1751
+ readonly project?: 'keys_only';
1663
1752
  readonly index?: string;
1664
1753
  readonly type?: 'clustered' | 'isolated';
1665
1754
  readonly collection?: AccessPatternCollection<C>;
@@ -2398,13 +2487,13 @@ export class Entity<A extends string, F extends string, C extends string, S exte
2398
2487
  create(record: PutItem<A,F,C,S>): PutRecordOperationOptions<A,F,C,S, ResponseItem<A,F,C,S>>
2399
2488
 
2400
2489
  update(key: AllTableIndexCompositeAttributes<A,F,C,S>): {
2401
- set: SetRecord<A,F,C,S, SetItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
2402
- remove: RemoveRecord<A,F,C,S, RemoveItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
2403
- add: SetRecord<A,F,C,S, AddItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
2404
- subtract: SetRecord<A,F,C,S, SubtractItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
2405
- append: SetRecord<A,F,C,S, AppendItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
2406
- delete: SetRecord<A,F,C,S, DeleteItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
2407
- data: DataUpdateMethodRecord<A,F,C,S, Item<A,F,C,S,S["attributes"]>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
2490
+ set: SetRecord<A,F,C,S, SetItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, Partial<ResponseItem<A,F,C,S>>>;
2491
+ remove: RemoveRecord<A,F,C,S, RemoveItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, Partial<ResponseItem<A,F,C,S>>>;
2492
+ add: SetRecord<A,F,C,S, AddItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, Partial<ResponseItem<A,F,C,S>>>;
2493
+ subtract: SetRecord<A,F,C,S, SubtractItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, Partial<ResponseItem<A,F,C,S>>>;
2494
+ append: SetRecord<A,F,C,S, AppendItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, Partial<ResponseItem<A,F,C,S>>>;
2495
+ delete: SetRecord<A,F,C,S, DeleteItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, Partial<ResponseItem<A,F,C,S>>>;
2496
+ data: DataUpdateMethodRecord<A,F,C,S, Item<A,F,C,S,S["attributes"]>, TableIndexCompositeAttributes<A,F,C,S>, Partial<ResponseItem<A,F,C,S>>>;
2408
2497
  };
2409
2498
  patch(key: AllTableIndexCompositeAttributes<A,F,C,S>): {
2410
2499
  set: SetRecord<A,F,C,S, SetItem<A,F,C,S>, TableIndexCompositeAttributes<A,F,C,S>, ResponseItem<A,F,C,S>>;
@@ -2422,6 +2511,7 @@ export class Entity<A extends string, F extends string, C extends string, S exte
2422
2511
 
2423
2512
  scan: RecordsActionOptions<A,F,C,S, ResponseItem<A,F,C,S>[], TableIndexCompositeAttributes<A,F,C,S>>;
2424
2513
  query: Queries<A,F,C,S>;
2514
+ conversions: Conversions<A,F,C,S>;
2425
2515
 
2426
2516
  parse<Options extends ParseOptions<keyof ResponseItem<A,F,C,S>>>(item: ParseSingleInput, options?: Options):
2427
2517
  Options extends ParseOptions<infer Attr>
@@ -2506,7 +2596,7 @@ type TransactWriteExtractedType<T extends readonly any[], A extends readonly any
2506
2596
  type TransactGetExtractedType<T extends readonly any[], A extends readonly any[] = []> =
2507
2597
  T extends [infer F, ...infer R] ?
2508
2598
  F extends CommittedTransactionResult<infer V, TransactGetItem>
2509
- ? TransactWriteExtractedType<R, [...A, TransactionItem<V>]>
2599
+ ? TransactGetExtractedType<R, [...A, TransactionItem<V>]>
2510
2600
  : never
2511
2601
  : A
2512
2602
 
package/output ADDED
@@ -0,0 +1,168 @@
1
+ "target": "top",
2
+ "target": "byOrganization",
3
+ "target": "byCategory",
4
+ "target": "top",
5
+ "target": "byOrganization",
6
+ "target": "byCategory",
7
+ "target": "top",
8
+ "target": "byOrganization",
9
+ "target": "byCategory",
10
+ "target": "top",
11
+ "target": "byOrganization",
12
+ "target": "byCategory",
13
+ "target": "top",
14
+ "target": "byOrganization",
15
+ "target": "byCategory",
16
+ "target": "top",
17
+ "target": "byOrganization",
18
+ "target": "byCategory",
19
+ "target": "top",
20
+ "target": "byOrganization",
21
+ "target": "byCategory",
22
+ "target": "top",
23
+ "target": "byOrganization",
24
+ "target": "byCategory",
25
+ "target": "top",
26
+ "target": "byOrganization",
27
+ "target": "byCategory",
28
+ "target": "top",
29
+ "target": "byOrganization",
30
+ "target": "byCategory",
31
+ "target": "top",
32
+ "target": "byOrganization",
33
+ "target": "byCategory",
34
+ "target": "top",
35
+ "target": "byOrganization",
36
+ "target": "byCategory",
37
+ "target": "top",
38
+ "target": "byOrganization",
39
+ "target": "byCategory",
40
+ "target": "top",
41
+ "target": "byOrganization",
42
+ "target": "byCategory",
43
+ "target": "top",
44
+ "target": "byOrganization",
45
+ "target": "byCategory",
46
+ "target": "top",
47
+ "target": "byOrganization",
48
+ "target": "byCategory",
49
+ "target": "top",
50
+ "target": "byOrganization",
51
+ "target": "byCategory",
52
+ "target": "top",
53
+ "target": "byOrganization",
54
+ "target": "byCategory",
55
+ "target": "top",
56
+ "target": "byOrganization",
57
+ "target": "byCategory",
58
+ "target": "top",
59
+ "target": "byOrganization",
60
+ "target": "byCategory",
61
+ "target": "top",
62
+ "target": "byOrganization",
63
+ "target": "byCategory",
64
+ "target": "top",
65
+ "target": "byOrganization",
66
+ "target": "byCategory",
67
+ "target": "top",
68
+ "target": "byOrganization",
69
+ "target": "byCategory",
70
+ "target": "top",
71
+ "target": "byOrganization",
72
+ "target": "byCategory",
73
+ "target": "top",
74
+ "target": "byOrganization",
75
+ "target": "byCategory",
76
+ "target": "top",
77
+ "target": "byOrganization",
78
+ "target": "byCategory",
79
+ "target": "top",
80
+ "target": "byOrganization",
81
+ "target": "byCategory",
82
+ "target": "top",
83
+ "target": "byOrganization",
84
+ "target": "byCategory",
85
+ "target": "top",
86
+ "target": "byOrganization",
87
+ "target": "byCategory",
88
+ "target": "top",
89
+ "target": "byOrganization",
90
+ "target": "byCategory",
91
+ "target": "top",
92
+ "target": "byOrganization",
93
+ "target": "byCategory",
94
+ "target": "top",
95
+ "target": "byOrganization",
96
+ "target": "byCategory",
97
+ "target": "top",
98
+ "target": "byOrganization",
99
+ "target": "byCategory",
100
+ "target": "top",
101
+ "target": "byOrganization",
102
+ "target": "byCategory",
103
+ "target": "top",
104
+ "target": "byOrganization",
105
+ "target": "byCategory",
106
+ "target": "top",
107
+ "target": "byOrganization",
108
+ "target": "byCategory",
109
+ "target": "top",
110
+ "target": "byOrganization",
111
+ "target": "byCategory",
112
+ "target": "top",
113
+ "target": "byOrganization",
114
+ "target": "byCategory",
115
+ "target": "top",
116
+ "target": "byOrganization",
117
+ "target": "byCategory",
118
+ "target": "top",
119
+ "target": "byOrganization",
120
+ "target": "byCategory",
121
+ "target": "top",
122
+ "target": "byOrganization",
123
+ "target": "byCategory",
124
+ "target": "top",
125
+ "target": "byOrganization",
126
+ "target": "byCategory",
127
+ "target": "top",
128
+ "target": "byOrganization",
129
+ "target": "byCategory",
130
+ "target": "top",
131
+ "target": "byOrganization",
132
+ "target": "byCategory",
133
+ "target": "top",
134
+ "target": "byOrganization",
135
+ "target": "byCategory",
136
+ "target": "top",
137
+ "target": "byOrganization",
138
+ "target": "byCategory",
139
+ "target": "top",
140
+ "target": "byOrganization",
141
+ "target": "byCategory",
142
+ "target": "top",
143
+ "target": "byOrganization",
144
+ "target": "byCategory",
145
+ "target": "top",
146
+ "target": "byOrganization",
147
+ "target": "byCategory",
148
+ "target": "top",
149
+ "target": "byOrganization",
150
+ "target": "byCategory",
151
+ "target": "top",
152
+ "target": "byOrganization",
153
+ "target": "byCategory",
154
+ "target": "top",
155
+ "target": "byOrganization",
156
+ "target": "byCategory",
157
+ "target": "top",
158
+ "target": "byOrganization",
159
+ "target": "byCategory",
160
+ "target": "top",
161
+ "target": "byOrganization",
162
+ "target": "byCategory",
163
+ "target": "top",
164
+ "target": "byOrganization",
165
+ "target": "byCategory",
166
+ "target": "top",
167
+ "target": "byOrganization",
168
+ "target": "byCategory",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electrodb",
3
- "version": "2.5.1",
3
+ "version": "2.6.0",
4
4
  "description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -43,9 +43,9 @@
43
43
  "moment": "2.24.0",
44
44
  "nyc": "^15.1.0",
45
45
  "source-map-support": "^0.5.19",
46
- "ts-node": "^10.8.2",
46
+ "ts-node": "^10.9.1",
47
47
  "tsd": "^0.21.0",
48
- "typescript": "^4.7.4",
48
+ "typescript": "^4.9.5",
49
49
  "uuid": "7.0.1"
50
50
  },
51
51
  "keywords": [
package/src/clauses.js CHANGED
@@ -52,7 +52,7 @@ let clauses = {
52
52
  }
53
53
  })
54
54
  .whenOptions(({ options, state }) => {
55
- if (!options.ignoreOwnership) {
55
+ if (!options.ignoreOwnership && !state.getParams()) {
56
56
  state.query.options.expressions.names = {
57
57
  ...state.query.options.expressions.names,
58
58
  ...state.query.options.identifiers.names,
@@ -90,7 +90,7 @@ let clauses = {
90
90
  .setCollection(collection)
91
91
  .setPK(entity._expectFacets(facets, pk))
92
92
  .whenOptions(({ options, state }) => {
93
- if (!options.ignoreOwnership) {
93
+ if (!options.ignoreOwnership && !state.getParams()) {
94
94
  state.query.options.expressions.names = {
95
95
  ...state.query.options.expressions.names,
96
96
  ...state.query.options.identifiers.names,
@@ -121,7 +121,7 @@ let clauses = {
121
121
  try {
122
122
  return state.setMethod(MethodTypes.scan)
123
123
  .whenOptions(({ state, options }) => {
124
- if (!options.ignoreOwnership) {
124
+ if (!options.ignoreOwnership && !state.getParams()) {
125
125
  state.unsafeApplyFilter(FilterOperationNames.eq, entity.identifiers.entity, entity.getName());
126
126
  state.unsafeApplyFilter(FilterOperationNames.eq, entity.identifiers.version, entity.getVersion());
127
127
  }
@@ -247,6 +247,12 @@ let clauses = {
247
247
  .ifSK(() => {
248
248
  entity._expectFacets(record, attributes.sk);
249
249
  state.setSK(entity._buildQueryFacets(record, attributes.sk));
250
+ })
251
+ .whenOptions(({ state, options }) => {
252
+ if (!state.getParams()) {
253
+ state.query.update.set(entity.identifiers.entity, entity.getName());
254
+ state.query.update.set(entity.identifiers.version, entity.getVersion());
255
+ }
250
256
  });
251
257
  } catch(err) {
252
258
  state.setError(err);
@@ -526,7 +532,7 @@ let clauses = {
526
532
  }
527
533
 
528
534
  state.whenOptions(({ options, state }) => {
529
- if (state.query.options.indexType === IndexTypes.clustered && Object.keys(composites).length < sk.length && !options.ignoreOwnership) {
535
+ if (state.query.options.indexType === IndexTypes.clustered && Object.keys(composites).length < sk.length && !options.ignoreOwnership && !state.getParams()) {
530
536
  state.unsafeApplyFilter(FilterOperationNames.eq, entity.identifiers.entity, entity.getName())
531
537
  .unsafeApplyFilter(FilterOperationNames.eq, entity.identifiers.version, entity.getVersion());
532
538
  }
@@ -714,7 +720,9 @@ let clauses = {
714
720
  provided: [ state.getOptions(), state.query.options, options ],
715
721
  context: { operation: options._isTransaction ? MethodTypes.transactWrite : undefined }
716
722
  });
723
+
717
724
  state.applyWithOptions(normalizedOptions);
725
+
718
726
  let results;
719
727
  switch (method) {
720
728
  case MethodTypes.query: {
@@ -741,6 +749,8 @@ let clauses = {
741
749
  delete results.ExpressionAttributeValues;
742
750
  }
743
751
 
752
+ state.setParams(results);
753
+
744
754
  if (options._returnOptions) {
745
755
  return {
746
756
  params: results,
@@ -816,6 +826,17 @@ class ChainState {
816
826
  this.hasSortKey = hasSortKey;
817
827
  this.prev = null;
818
828
  this.self = null;
829
+ this.params = null;
830
+ }
831
+
832
+ getParams() {
833
+ return this.params;
834
+ }
835
+
836
+ setParams(params) {
837
+ if (params) {
838
+ this.params = params;
839
+ }
819
840
  }
820
841
 
821
842
  init(entity, allClauses, currentClause) {