From 700ee9c55ca2764274e343348dea4c7d63de1004 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Sun, 6 Oct 2019 03:16:14 +0530 Subject: [PATCH] feat: Rename methods --- backends/database.js | 4 ++++ backends/sqlite.js | 5 +++++ model/document.js | 7 +++++++ 3 files changed, 16 insertions(+) diff --git a/backends/database.js b/backends/database.js index 71093068..d3d431fd 100644 --- a/backends/database.js +++ b/backends/database.js @@ -299,6 +299,10 @@ module.exports = class Database extends Observable { // await frappe.db.run('delete from SingleValue where parent=?', name) } + async rename(doctype, oldName, newName) { + // await frappe.db.run('update doctype set name = ? where name = ?', name) + } + prepareChild(parenttype, parent, child, field, idx) { if (!child.name) { child.name = frappe.getRandomString(); diff --git a/backends/sqlite.js b/backends/sqlite.js index 357c01b5..b2e0e9ef 100644 --- a/backends/sqlite.js +++ b/backends/sqlite.js @@ -159,6 +159,11 @@ module.exports = class sqliteDatabase extends Database { await frappe.db.run('delete from SingleValue where parent=?', name) } + async rename(doctype, oldName, newName) { + await frappe.db.run(`update ${baseDoctype} set name = ? where name = ?`, [newName, oldName]); + await frappe.db.commit(); + } + async setValues(doctype, name, fieldValuePair) { const meta = frappe.getMeta(doctype); const validFields = this.getKeys(doctype); diff --git a/model/document.js b/model/document.js index 42eed550..258378a8 100644 --- a/model/document.js +++ b/model/document.js @@ -346,6 +346,13 @@ module.exports = class BaseDocument extends Observable { this.update(); } + async rename(newName) { + await this.trigger('beforeRename'); + await frappe.db.rename(this.doctype, this.name, newName); + this.name = newName; + await this.trigger('afterRename'); + } + // trigger methods on the class if they match // with the trigger name async trigger(event, params) {