/** @returns {BAttachment} */
getAttachmentByTitle(title) {
- return sql.getRows(`
- SELECT attachments.*
- FROM attachments
- WHERE ownerId = ?
- AND title = ?
- AND isDeleted = 0
- ORDER BY position`, [this.noteId, title])
- .map(row => new BAttachment(row))[0];
+ // cannot use SQL to filter by title since it can be encrypted
+ return this.getAttachments().filter(attachment => attachment.title === title)[0];
}
/**
@@ -1663,24 +1657,32 @@
Source: becca/entities/bnote.js
}
/**
+ * @param {string} matchBy - choose by which property we detect if to update an existing attachment.
+ * Supported values are either 'attachmentId' (default) or 'title'
* @returns {BAttachment}
*/
- saveAttachment({attachmentId, role, mime, title, content, position}) {
+ saveAttachment({attachmentId, role, mime, title, content, position}, matchBy = 'attachmentId') {
+ if (!['attachmentId', 'title'].includes(matchBy)) {
+ throw new Error(`Unsupported value '${matchBy}' for matchBy param, has to be either 'attachmentId' or 'title'.`);
+ }
+
let attachment;
- if (attachmentId) {
+ if (matchBy === 'title') {
+ attachment = this.getAttachmentByTitle(title);
+ } else if (matchBy === 'attachmentId' && attachmentId) {
attachment = this.becca.getAttachmentOrThrow(attachmentId);
- } else {
- attachment = new BAttachment({
- ownerId: this.noteId,
- title,
- role,
- mime,
- isProtected: this.isProtected,
- position
- });
}
+ attachment = attachment || new BAttachment({
+ ownerId: this.noteId,
+ title,
+ role,
+ mime,
+ isProtected: this.isProtected,
+ position
+ });
+
content = content || "";
attachment.setContent(content, {forceSave: true});
diff --git a/docs/backend_api/becca_entities_brevision.js.html b/docs/backend_api/becca_entities_brevision.js.html
index 009e72a2e4..b493ce8a0a 100644
--- a/docs/backend_api/becca_entities_brevision.js.html
+++ b/docs/backend_api/becca_entities_brevision.js.html
@@ -185,14 +185,8 @@
Source: becca/entities/brevision.js
/** @returns {BAttachment} */
getAttachmentByTitle(title) {
- return sql.getRows(`
- SELECT attachments.*
- FROM attachments
- WHERE ownerId = ?
- AND title = ?
- AND isDeleted = 0
- ORDER BY position`, [this.revisionId, title])
- .map(row => new BAttachment(row))[0];
+ // cannot use SQL to filter by title since it can be encrypted
+ return this.getAttachments().filter(attachment => attachment.title === title)[0];
}
beforeSaving() {
diff --git a/docs/backend_api/services_backend_script_api.js.html b/docs/backend_api/services_backend_script_api.js.html
index 4bc787c464..0f6b3c3ec5 100644
--- a/docs/backend_api/services_backend_script_api.js.html
+++ b/docs/backend_api/services_backend_script_api.js.html
@@ -47,7 +47,8 @@
Source: services/backend_script_api.js
const specialNotesService = require("./special_notes");
const branchService = require("./branches");
const exportService = require("./export/zip");
-const syncMutex = require("./sync_mutex.js");
+const syncMutex = require("./sync_mutex");
+const backupService = require("./backup");
/**
* <p>This is the main backend API interface for scripts. All the properties and methods are published in the "api" object
@@ -614,6 +615,13 @@
Source: services/backend_script_api.js
*/
this.runOutsideOfSync = syncMutex.doExclusively;
+ /**
+ * @method
+ * @param {string} backupName - If the backupName is e.g. "now", then the backup will be written to "backup-now.db" file
+ * @returns {Promise} - resolves once the backup is finished
+ */
+ this.backupNow = backupService.backupNow;
+
/**
* This object contains "at your risk" and "no BC guarantees" objects for advanced use cases.
*
diff --git a/docs/frontend_api/FAttribute.html b/docs/frontend_api/FAttribute.html
index 8956485710..c4f3cc0b9a 100644
--- a/docs/frontend_api/FAttribute.html
+++ b/docs/frontend_api/FAttribute.html
@@ -912,7 +912,7 @@
This is the main backend API interface for scripts. All the properties and methods are published in the "api" object
@@ -586,6 +587,13 @@ function BackendScriptApi(currentNote, apiParams) {
*/
this.runOutsideOfSync = syncMutex.doExclusively;
+ /**
+ * @method
+ * @param {string} backupName - If the backupName is e.g. "now", then the backup will be written to "backup-now.db" file
+ * @returns {Promise} - resolves once the backup is finished
+ */
+ this.backupNow = backupService.backupNow;
+
/**
* This object contains "at your risk" and "no BC guarantees" objects for advanced use cases.
*