From 33c3209e875d92ff40301d4d52ce2b4acab3767f Mon Sep 17 00:00:00 2001 From: adis azhar Date: Wed, 15 May 2019 02:24:09 +0700 Subject: [PATCH 1/3] Added adterDestroy lifecycle example --- concepts/ORM/Lifecyclecallbacks.md | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/concepts/ORM/Lifecyclecallbacks.md b/concepts/ORM/Lifecyclecallbacks.md index 9dc018bd5..8fb6433c6 100644 --- a/concepts/ORM/Lifecyclecallbacks.md +++ b/concepts/ORM/Lifecyclecallbacks.md @@ -64,6 +64,61 @@ module.exports = { } }; +``` + +If you want to delete child objects after deleting its parent, you can use the `afterDestroy` lifecycle callback. Be careful when using this, as you may lose sensitive data! In this example we will have 2 Models. `Invoice` and `Item`. `Invoice` to `Item` corresponds to `One-to-many` association relation. See [One-to-many associations](https://sailsjs.com/documentation/concepts/models-and-orm/associations/one-to-many). + +```javascript +// Invoice.js + +module.exports = { + attributes: { + client_name: { type: 'string', required: true }, + client_email: { type: 'string', required: false }, + project_name: { type: 'string', required: true }, + project_description: { type: 'string', required: false }, + invoice_status: { type: 'string', defaultsTo: 'unpaid' }, + due_date: { type: 'string', required: false }, + total_price: { type: 'number', required: true}, + items: { + collection: 'item', + via: 'invoice' + }, + user: { + model: 'user' + } + }, + + async beforeDestroy(criteria, proceed) { + try { + // destroy items related to parent + // we can access the deleted parent object using + // criteria object + await Item.destroy({invoice: criteria.where.id}); + } catch (error) { + return proceed(error); + } + return proceed(); + } + +}; + + +// Item.js + +module.exports = { + + attributes: { + name: { type: 'string', required: true }, + price: { type: 'number', columnType: 'float', required: false }, + invoice: { + model: 'invoice' + } + }, +}; + + + ``` From 626fc3b03c287a203b12dd348f4d840f7da582aa Mon Sep 17 00:00:00 2001 From: adis azhar Date: Wed, 15 May 2019 02:32:37 +0700 Subject: [PATCH 2/3] Added afterDestroy lifecycle example --- concepts/ORM/Lifecyclecallbacks.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/concepts/ORM/Lifecyclecallbacks.md b/concepts/ORM/Lifecyclecallbacks.md index 8fb6433c6..bb76a24b5 100644 --- a/concepts/ORM/Lifecyclecallbacks.md +++ b/concepts/ORM/Lifecyclecallbacks.md @@ -83,9 +83,6 @@ module.exports = { items: { collection: 'item', via: 'invoice' - }, - user: { - model: 'user' } }, @@ -118,6 +115,21 @@ module.exports = { }; +// Delete Invoice Action + + deleteInvoice: async(req, res) => { + let invoice; + try { + //invoice_id is parameter send by client + invoice = await Invoice.destroy({id: req.param('invoice_id')}); + } catch (error) { + console.log(error); + return false; + } + return res.status(202).json({message: 'invoice deleted'}); + }, + + ``` From 6da52c0b75e7d43dfeafb59d5dbcac4e4eb3aba6 Mon Sep 17 00:00:00 2001 From: adis azhar Date: Wed, 15 May 2019 02:36:02 +0700 Subject: [PATCH 3/3] Added beforeDestroy lifecycle example [revision of previous commit] --- concepts/ORM/Lifecyclecallbacks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/ORM/Lifecyclecallbacks.md b/concepts/ORM/Lifecyclecallbacks.md index bb76a24b5..7050028ec 100644 --- a/concepts/ORM/Lifecyclecallbacks.md +++ b/concepts/ORM/Lifecyclecallbacks.md @@ -66,7 +66,7 @@ module.exports = { }; ``` -If you want to delete child objects after deleting its parent, you can use the `afterDestroy` lifecycle callback. Be careful when using this, as you may lose sensitive data! In this example we will have 2 Models. `Invoice` and `Item`. `Invoice` to `Item` corresponds to `One-to-many` association relation. See [One-to-many associations](https://sailsjs.com/documentation/concepts/models-and-orm/associations/one-to-many). +If you want to delete child objects when deleting its parent, you can use the `beforeDestroy` lifecycle callback. By using `beforeDestory` lifecycle, it will delete the child objects before the parent is deleted. You can stimulate a `cascade on delete` with this. Be careful when using this, as you may lose sensitive data! In this example we will have 2 Models. `Invoice` and `Item`. `Invoice` to `Item` corresponds to `One-to-many` association relation. See [One-to-many associations](https://sailsjs.com/documentation/concepts/models-and-orm/associations/one-to-many). ```javascript // Invoice.js