Repository: dynamoosejs/dynamoose
Pull request #719 (2020-02-21 00:39:04)

document.delete


Patch:
diff --git a/docs/FAQ.md b/docs/FAQ.md
index 9a9bbf79..7faefdd7 100644
--- a/docs/FAQ.md
+++ b/docs/FAQ.md
@@ -14,6 +14,7 @@ The following is a chart of IAM permissions you need in order to run Dynamoose f
 | Model.update | `updateItem` |  |
 | Model.delete | `deleteItem` |  |
 | document.save | `putItem` |  |
+| document.delete | `deleteItem` |  |
 
 ## Why is it recommended to set `create` & `waitForActive` model options to false for production environments?
 
diff --git a/docs/api/Document.md b/docs/api/Document.md
index f71fec4a..71d4641c 100644
--- a/docs/api/Document.md
+++ b/docs/api/Document.md
@@ -53,6 +53,34 @@ myUser.save((error) => {
 });
 ```
 
+## document.delete([callback])
+
+This deletes the given document from DynamoDB. This method uses the `deleteItem` DynamoDB API call to delete your object in the given table associated with the model.
+
+This method returns a promise that will resolve when the operation is complete, this promise will reject upon failure. You can also pass in a function into the `callback` parameter to have it be used in a callback format as opposed to a promise format. Nothing will be passed into the result for the promise or callback.
+
+```js
+//...
+const myUser = User.get("1");
+
+try {
+	await myUser.delete();
+	console.log("Delete operation was successful.");
+} catch (error) {
+	console.error(error);
+}
+
+// OR
+
+myUser.delete((error) => {
+	if (error) {
+		console.error(error);
+	} else {
+		console.log("Delete operation was successful.");
+	}
+});
+```
+
 ## 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.
diff --git a/lib/Document.js b/lib/Document.js
index 34194a1e..10ea6c53 100644
--- a/lib/Document.js
+++ b/lib/Document.js
@@ -244,6 +244,11 @@ function DocumentCarrier(model) {
 			})();
 		}
 	};
+	Document.prototype.delete = function(callback) {
+		return model.delete({
+			[model.schema.getHashKey()]: this[model.schema.getHashKey()]
+		}, callback);
+	};
 	// This function will modify the document to conform to the Schema
 	Document.prototype.conformToSchema = async function(settings = {"type": "fromDynamo"}) {
 		Document.prepareForObjectFromSchema(this, settings);


Test patch:
diff --git a/test/Document.js b/test/Document.js
index f9106f68..5a991a59 100644
--- a/test/Document.js
+++ b/test/Document.js
@@ -104,6 +104,10 @@ describe("Document", () => {
 			putParams = [];
 		});
 
+		it("Should be a function", async () => {
+			expect(user.save).to.be.a("function");
+		});
+
 		const functionCallTypes = [
 			{"name": "Promise", "func": (document) => document.save},
 			{"name": "Callback", "func": (document) => util.promisify(document.save)}
@@ -962,6 +966,65 @@ describe("Document", () => {
 		});
 	});
 
+	describe("document.save", () => {
+		let User, user, deleteParams, deleteItemFunction;
+		beforeEach(() => {
+			Model.defaults = {
+				"create": false,
+				"waitForActive": false
+			};
+			aws.ddb.set({
+				"deleteItem": (params) => {
+					deleteParams = params;
+					return {"promise": deleteItemFunction};
+				}
+			});
+			User = new Model("User", {"id": Number, "name": String});
+			user = new User({"id": 1, "name": "Charlie"});
+		});
+		afterEach(() => {
+			Model.defaults = {};
+			aws.ddb.revert();
+			User = null;
+			user = null;
+			deleteParams = null;
+			deleteItemFunction = null;
+		});
+
+		it("Should be a function", async () => {
+			expect(user.delete).to.be.a("function");
+		});
+
+		const functionCallTypes = [
+			{"name": "Promise", "func": (document) => document.delete},
+			{"name": "Callback", "func": (document) => util.promisify(document.delete)}
+		];
+		functionCallTypes.forEach((callType) => {
+			describe(callType.name, () => {
+				it("Should deleteItem with correct parameters", async () => {
+					deleteItemFunction = () => Promise.resolve();
+					await callType.func(user).bind(user)();
+					expect(deleteParams).to.eql({
+						"Key": {"id": {"N": "1"}},
+						"TableName": "User"
+					});
+				});
+
+				it("Should throw error if DynamoDB API returns an error", async () => {
+					deleteItemFunction = () => Promise.reject({"error": "ERROR"});
+					let result, error;
+					try {
+						result = await callType.func(user).bind(user)();
+					} catch (e) {
+						error = e;
+					}
+					expect(result).to.not.exist;
+					expect(error).to.eql({"error": "ERROR"});
+				});
+			});
+		});
+	});
+
 	describe("conformToSchema", () => {
 		beforeEach(() => {
 			Model.defaults = {