Skip to content

Commit

Permalink
Never return fields not saved in Dynamo on create
Browse files Browse the repository at this point in the history
If an object containing undefined was provided to create then this
undefined field would be returned but not on read. This was confusing.

This guarantees that we treat them the same.
  • Loading branch information
tgandrews committed Oct 30, 2020
1 parent 45135f7 commit 1887dcf
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ describe("omanyd", () => {

expect(savedThing).toStrictEqual({
id: savedThing.id,
value: undefined,
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export function define<T>(options: Options) {
return {
async create(obj: Omit<T, "id"> | T): Promise<T> {
const validated: T = await validator.validateAsync(obj);
await t.create(validated);
return validated;
const result = await t.create(validated);
return (result as unknown) as T;
},

async put(obj: T): Promise<T> {
Expand Down
12 changes: 7 additions & 5 deletions src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,26 @@ export default class Table {
});
}

async create(obj: PlainObject): Promise<AWS.DynamoDB.PutItemOutput> {
async create(obj: PlainObject): Promise<PlainObject> {
const serializedItem = this.serializer.toDynamoMap(obj);
return new Promise((res, rej) => {
this.dynamoDB.putItem(
{
TableName: this.options.name,
Item: this.serializer.toDynamoMap(obj),
Item: serializedItem,
},
(err, data) => {
if (err) {
return rej(err);
}
return res(data);
// Serializing can drop undefined fields so return the deserialized serialized item
return res(this.deserializer.fromDynamoMap(serializedItem));
}
);
});
}

async getByHashKey(hashKey: string): Promise<Object | null> {
async getByHashKey(hashKey: string): Promise<PlainObject | null> {
return new Promise((res, rej) => {
this.dynamoDB.getItem(
{
Expand Down Expand Up @@ -208,7 +210,7 @@ export default class Table {
});
}

async getByIndex(name: string, hashKey: string): Promise<Object | null> {
async getByIndex(name: string, hashKey: string): Promise<PlainObject | null> {
const indexDefintion = (this.options.indexes ?? []).find(
(index) => index.name === name
);
Expand Down

0 comments on commit 1887dcf

Please sign in to comment.