Skip to content

Commit

Permalink
Merge a1857fe into 6f35910
Browse files Browse the repository at this point in the history
  • Loading branch information
fishcharlie committed Mar 10, 2021
2 parents 6f35910 + a1857fe commit abac15f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ export class Document {
settings = {};
}

let savedItem;

const localSettings: DocumentSaveSettings = settings;
const paramsPromise = this.toDynamo({"defaults": true, "validate": true, "required": true, "enum": true, "forceDefault": true, "combine": true, "saveUnknown": true, "customTypesDynamo": true, "updateTimestamps": true, "modifiers": ["set"]}).then((item) => {
savedItem = item;
let putItemObj: DynamoDB.PutItemInput = {
"Item": item,
"TableName": this.model.name
Expand Down Expand Up @@ -182,13 +185,22 @@ export class Document {
if (callback) {
const localCallback: CallbackType<Document, AWSError> = callback as CallbackType<Document, AWSError>;
promise.then(() => {
this[internalProperties].storedInDynamo = true; localCallback(null, this);
this[internalProperties].storedInDynamo = true;

const returnDocument = new this.model.Document(savedItem as any);
returnDocument[internalProperties].storedInDynamo = true;

localCallback(null, returnDocument);
}).catch((error) => callback(error));
} else {
return (async (): Promise<Document> => {
await promise;
this[internalProperties].storedInDynamo = true;
return this;

const returnDocument = new this.model.Document(savedItem as any);
returnDocument[internalProperties].storedInDynamo = true;

return returnDocument;
})();
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ describe("Document", () => {
expect(result).to.eql(user);
});

it("Should return correct result after saving with defaults", async () => {
putItemFunction = () => Promise.resolve();

User = dynamoose.model("User", {"id": Number, "name": String, "defaultValue": {"type": String, "default": "Hello World"}});
user = new User({"id": 1, "name": "Charlie"});

const result = await callType.func(user).bind(user)();
expect(result.toJSON()).to.eql({"id": 1, "name": "Charlie", "defaultValue": "Hello World"});
});

it("Should return request if return request is set as setting", async () => {
const result = await callType.func(user).bind(user)({"return": "request"});
expect(putParams).to.eql([]);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,15 @@ describe("Model", () => {
];
functionCallTypes.forEach((callType) => {
describe(callType.name, () => {
it("Should return correct result after saving with defaults", async () => {
createItemFunction = () => Promise.resolve();

User = dynamoose.model("User", {"id": Number, "name": String, "defaultValue": {"type": String, "default": "Hello World"}});

const result = await callType.func(User).bind(User)({"id": 1, "name": "Charlie"});
expect(result.toJSON()).to.eql({"id": 1, "name": "Charlie", "defaultValue": "Hello World"});
});

it("Should send correct params to putItem", async () => {
createItemFunction = () => Promise.resolve();
await callType.func(User).bind(User)({"id": 1, "name": "Charlie"});
Expand Down

0 comments on commit abac15f

Please sign in to comment.