Skip to content

Commit

Permalink
adds more robust update return signature
Browse files Browse the repository at this point in the history
  • Loading branch information
tywalch committed Nov 4, 2022
1 parent 1b1a269 commit 1e81e27
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,8 @@ All notable changes to this project will be documented in this file. Breaking ch

## [2.2.1] - 2022-11-02
### Fixed
- Addressed github issue #144, root map attributes would set an empty object regardless if the user supplied it.
- Addressed github issue #144, root map attributes would set an empty object regardless if the user supplied it.

## [2.2.2] - 2022-11-04
### Added
- the return type from an update/patch call now returns an Entity item when `all_new` or `all_old` response options are passed
18 changes: 11 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ export interface BatchWriteOperationOptions<A extends string, F extends string,
}

export interface SetRecordActionOptions<A extends string, F extends string, C extends string, S extends Schema<A,F,C>, SetAttr,IndexCompositeAttributes,TableItem> {
go: UpdateRecordGo<Partial<TableItem>, UpdateQueryOptions>;
go: UpdateRecordGo<TableItem>;
params: ParamRecord<UpdateQueryParams>;
set: SetRecord<A,F,C,S, SetItem<A,F,C,S>,IndexCompositeAttributes,TableItem>;
remove: SetRecord<A,F,C,S, Array<keyof SetItem<A,F,C,S>>,IndexCompositeAttributes,TableItem>;
Expand Down Expand Up @@ -987,7 +987,16 @@ export type ServiceQueryRecordsGo<ResponseType, Options = QueryOptions> = <T = R

export type QueryRecordsGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T, cursor: string | null }>;

export type UpdateRecordGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T }>;
export type UpdateRecordGo<ResponseType> = <T = ResponseType, Options extends UpdateQueryOptions = UpdateQueryOptions>(options?: Options) =>
Options extends infer O
? 'response' extends keyof O
? O['response'] extends 'all_new'
? Promise<{data: T}>
: O['response'] extends 'all_old'
? Promise<{data: T}>
: Promise<{data: Partial<T>}>
: Promise<{data: Partial<T>}>
: never;

export type PutRecordGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T }>;

Expand All @@ -996,11 +1005,6 @@ export type DeleteRecordOperationGo<ResponseType, Options = QueryOptions> = <T =
export type BatchWriteGo<ResponseType> = <O extends BulkOptions>(options?: O) =>
Promise<{ unprocessed: ResponseType }>

// export type PageRecord<ResponseType, CompositeAttributes> = (page?: (CompositeAttributes & OptionalDefaultEntityIdentifiers) | null, options?: PaginationOptions) => Promise<[
// (CompositeAttributes & OptionalDefaultEntityIdentifiers) | null,
// ResponseType
// ]>;

export type ParamRecord<Options = ParamOptions> = <P>(options?: Options) => P;

export class ElectroError extends Error {
Expand Down
65 changes: 60 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ let getKeys = ((val) => {}) as GetKeys;

let updateGo = updateItem.go;
let updateGoWithoutSK = updateItemWithoutSK.go;

let updateParams = updateItem.params;
let updateParamsWithoutSK = updateItemWithoutSK.params;

Expand All @@ -969,7 +969,6 @@ let getKeys = ((val) => {}) as GetKeys;
type UpdateParamsParams = Parameter<typeof updateParams>;
type UpdateParamsParamsWithoutSK = Parameter<typeof updateParamsWithoutSK>;


expectAssignable<UpdateGoParams>({includeKeys: true, originalErr: true, params: {}, raw: true, table: "abc", response: "updated_new"});
expectAssignable<UpdateGoParamsWithoutSK>({includeKeys: true, originalErr: true, params: {}, raw: true, table: "abc", response: "updated_new"});

Expand Down Expand Up @@ -3220,7 +3219,7 @@ entityWithComplexShapes.update({
})
.where((attr, op) => op.eq(attr.prop3.val1, 'def'))
.go({
response: "all_new",

}).then(res => {
expectType<ComplexShapesUpdateItem>(res.data);
});
Expand All @@ -3232,7 +3231,6 @@ entityWithComplexShapes.update({
.where((attr, op) => op.eq(attr.prop3.val1, 'def'))
.go({
originalErr: true,
response: "all_new",
}).then(res => {
expectType<ComplexShapesUpdateItem>(res.data);
});
Expand All @@ -3246,7 +3244,7 @@ entityWithComplexShapes.update({
})
.where((attr, op) => op.eq(attr.prop2, 'def'))
.go({
response: "all_new",

})
.then(res => {
expectType<ComplexShapesUpdateItem>(res.data);
Expand Down Expand Up @@ -3989,3 +3987,60 @@ expectAssignable<AvailableParsingOptions>(undefined);
expectAssignable<AvailableParsingOptions>({});
expectAssignable<AvailableParsingOptions>({ignoreOwnership: true});
expectAssignable<AvailableParsingOptions>({ignoreOwnership: false});

normalEntity2
.update({
prop1: 'abc',
prop2: 'def',
prop5: 123
})
.set({attr6: 456})
.go()
.then(results => {
expectType<{
prop1?: string | undefined;
prop2?: string | undefined;
prop3?: string | undefined;
prop5?: number | undefined;
attr6?: number | undefined;
attr9?: number | undefined;
}>(magnify(results.data));
});

normalEntity2
.update({
prop1: 'abc',
prop2: 'def',
prop5: 123
})
.set({attr6: 456})
.go({response: 'updated_new'})
.then(results => {
expectType<{
prop1?: string | undefined;
prop2?: string | undefined;
prop3?: string | undefined;
prop5?: number | undefined;
attr6?: number | undefined;
attr9?: number | undefined;
}>(magnify(results.data));
});

normalEntity2
.update({
prop1: 'abc',
prop2: 'def',
prop5: 123
})
.set({attr6: 456})
.go({response: 'all_new'})
.then(results => {
expectType<{
prop1: string;
prop2: string;
prop3: string;
prop5: number;
attr6?: number | undefined;
attr9?: number | undefined;
}>(magnify(results.data));
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electrodb",
"version": "2.2.1",
"version": "2.2.2",
"description": "A library to more easily create and interact with multiple entities and heretical relationships in dynamodb",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 1e81e27

Please sign in to comment.