Skip to content

Commit

Permalink
Merge pull request #716 from dynamoosejs/documentOriginalItem
Browse files Browse the repository at this point in the history
document.original
  • Loading branch information
fishcharlie committed Feb 20, 2020
2 parents 28005ec + 4f61c74 commit 678959c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions BREAKING_CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
- `dynamoose.local` has been renamed to `dynamoose.aws.ddb.local`
- `Model.getTableReq` has been renamed to `Model.table.create.request`
- `Model.table.create.request` (formerly `Model.getTableReq`) is now an async function
- `model.originalItem` has been renamed to `model.original` (or `Document.original`)
- `Document.original` formerly (`model.originalItem`) no longer returns the last item saved, but the item first retrieved from DynamoDB
13 changes: 13 additions & 0 deletions docs/api/Document.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,16 @@ myUser.save((error) => {
}
});
```

## document.original()

This function returns the original item that was received from DynamoDB. This function will return a JSON object that represents the original item. In the event no item has been retrieved from DynamoDB `null` will be returned.

```js
const user = await User.get(1);
console.log(user); // {"id": 1, "name": "Bob"}
user.name = "Tim";

console.log(user); // {"id": 1, "name": "Tim"}
console.log(user.original()); // {"id": 1, "name": "Bob"}
```
6 changes: 6 additions & 0 deletions lib/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function DocumentCarrier(model) {
const documentObject = Document.isDynamoObject(object) ? aws.converter().unmarshall({...object}) : object;
Object.keys(documentObject).forEach((key) => this[key] = documentObject[key]);
this[internalProperties] = {};
this[internalProperties].originalObject = {...object};
this[internalProperties].originalSettings = {...settings};

if (settings.fromDynamo) {
this[internalProperties].storedInDynamo = true;
Expand Down Expand Up @@ -258,6 +260,10 @@ function DocumentCarrier(model) {
return this;
};

Document.prototype.original = function() {
return this[internalProperties].originalSettings.type === "fromDynamo" ? this[internalProperties].originalObject : null;
};

Document.Model = model;

// TODO: figure out if there is a better way to do this below.
Expand Down
31 changes: 30 additions & 1 deletion test/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("Document", () => {
});
});

describe("save", () => {
describe("document.save", () => {
let User, user, putParams = [], putItemFunction;
beforeEach(() => {
Model.defaults = {
Expand Down Expand Up @@ -933,6 +933,35 @@ describe("Document", () => {
});
});

describe("document.original", () => {
let model;
beforeEach(() => {
model = new Model("User", {"id": Number}, {"create": false, "waitForActive": false});
});
afterEach(() => {
model = null;
});

it("Should be a function", () => {
expect(new model({}).original).to.be.a("function");
});

it("Should return null if not retrieving from database", () => {
expect(new model({}).original()).to.eql(null);
});

it("Should return original object if retrieving from database", () => {
expect(new model({"id": 1}, {"type": "fromDynamo"}).original()).to.eql({"id": 1});
});

it("Should return original object if retrieving from database even after modifying document", () => {
const document = new model({"id": 1}, {"type": "fromDynamo"});
document.id = 2;
expect(document.original()).to.eql({"id": 1});
expect({...document}).to.eql({"id": 2});
});
});

describe("conformToSchema", () => {
beforeEach(() => {
Model.defaults = {
Expand Down

0 comments on commit 678959c

Please sign in to comment.