Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(dev): use es6-string-html for some of the SQL statements (closes #274)
Elian Doranba506c9c10d9
diff --git a/db/migrations/0216__move_content_into_blobs.ts b/db/migrations/0216__move_content_into_blobs.tsindex f37e570f22..c10fcc7801 100644--- a/db/migrations/0216__move_content_into_blobs.ts+++ b/db/migrations/0216__move_content_into_blobs.ts@@ -17,8 +17,8 @@ interface NoteRevisionContents {export default () => {const existingBlobIds = new Set();- for (const noteId of sql.getColumn<string>(`SELECT noteId FROM note_contents`)) {- const row = sql.getRow<NoteContentsRow>(`SELECT noteId, content, dateModified, utcDateModified FROM note_contents WHERE noteId = ?`, [noteId]);+ for (const noteId of sql.getColumn<string>(/*sql*/`SELECT noteId FROM note_contents`)) {+ const row = sql.getRow<NoteContentsRow>(/*sql*/`SELECT noteId, content, dateModified, utcDateModified FROM note_contents WHERE noteId = ?`, [noteId]);const blobId = utils.hashedBlobId(row.content);if (!existingBlobIds.has(blobId)) {@@ -40,8 +40,8 @@ export default () => {sql.execute("UPDATE notes SET blobId = ? WHERE noteId = ?", [blobId, row.noteId]);}- for (const noteRevisionId of sql.getColumn(`SELECT noteRevisionId FROM note_revision_contents`)) {- const row = sql.getRow<NoteRevisionContents>(`SELECT noteRevisionId, content, utcDateModified FROM note_revision_contents WHERE noteRevisionId = ?`, [noteRevisionId]);+ for (const noteRevisionId of sql.getColumn(/*sql*/`SELECT noteRevisionId FROM note_revision_contents`)) {+ const row = sql.getRow<NoteRevisionContents>(/*sql*/`SELECT noteRevisionId, content, utcDateModified FROM note_revision_contents WHERE noteRevisionId = ?`, [noteRevisionId]);const blobId = utils.hashedBlobId(row.content);if (!existingBlobIds.has(blobId)) {diff --git a/db/migrations/0220__migrate_images_to_attachments.ts b/db/migrations/0220__migrate_images_to_attachments.tsindex 53eac59583..f54f4a9563 100644--- a/db/migrations/0220__migrate_images_to_attachments.ts+++ b/db/migrations/0220__migrate_images_to_attachments.ts@@ -7,7 +7,7 @@ import sql from "../../src/services/sql";export default () => {cls.init(() => {// emergency disabling of image compression since it appears to make problems in migration to 0.61- sql.execute(`UPDATE options SET value = 'false' WHERE name = 'compressImages'`);+ sql.execute(/*sql*/`UPDATE options SET value = 'false' WHERE name = 'compressImages'`);becca_loader.load();diff --git a/src/becca/becca-interface.ts b/src/becca/becca-interface.tsindex cdee5d1cd2..3bc470b5c7 100644--- a/src/becca/becca-interface.ts+++ b/src/becca/becca-interface.ts@@ -171,11 +171,11 @@ export default class Becca {opts.includeContentLength = !!opts.includeContentLength;const query = opts.includeContentLength- ? `SELECT attachments.*, LENGTH(blobs.content) AS contentLength+ ? /*sql*/`SELECT attachments.*, LENGTH(blobs.content) AS contentLengthFROM attachmentsJOIN blobs USING (blobId)WHERE attachmentId = ? AND isDeleted = 0`- : `SELECT * FROM attachments WHERE attachmentId = ? AND isDeleted = 0`;+ : /*sql*/`SELECT * FROM attachments WHERE attachmentId = ? AND isDeleted = 0`;return sql.getRows<AttachmentRow>(query, [attachmentId]).map((row) => new BAttachment(row))[0];}diff --git a/src/becca/becca_loader.ts b/src/becca/becca_loader.tsindex 8397b2dec7..4e6154e4af 100644--- a/src/becca/becca_loader.ts+++ b/src/becca/becca_loader.ts@@ -40,11 +40,11 @@ function load() {// using a raw query and passing arrays to avoid allocating new objects,// this is worth it for the becca load since it happens every run and blocks the app until finished- for (const row of sql.getRawRows(`SELECT noteId, title, type, mime, isProtected, blobId, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes WHERE isDeleted = 0`)) {+ for (const row of sql.getRawRows(/*sql*/`SELECT noteId, title, type, mime, isProtected, blobId, dateCreated, dateModified, utcDateCreated, utcDateModified FROM notes WHERE isDeleted = 0`)) {new BNote().update(row).init();}- const branchRows = sql.getRawRows<BranchRow>(`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`);+ const branchRows = sql.getRawRows<BranchRow>(/*sql*/`SELECT branchId, noteId, parentNoteId, prefix, notePosition, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0`);// in-memory sort is faster than in the DBbranchRows.sort((a, b) => (a.notePosition || 0) - (b.notePosition || 0));@@ -52,15 +52,15 @@ function load() {new BBranch().update(row).init();}- for (const row of sql.getRawRows<AttributeRow>(`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`)) {+ for (const row of sql.getRawRows<AttributeRow>(/*sql*/`SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified FROM attributes WHERE isDeleted = 0`)) {new BAttribute().update(row).init();}- for (const row of sql.getRows<OptionRow>(`SELECT name, value, isSynced, utcDateModified FROM options`)) {+ for (const row of sql.getRows<OptionRow>(/*sql*/`SELECT name, value, isSynced, utcDateModified FROM options`)) {new BOption(row);}- for (const row of sql.getRows<EtapiTokenRow>(`SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified FROM etapi_tokens WHERE isDeleted = 0`)) {+ for (const row of sql.getRows<EtapiTokenRow>(/*sql*/`SELECT etapiTokenId, name, tokenHash, utcDateCreated, utcDateModified FROM etapi_tokens WHERE isDeleted = 0`)) {new BEtapiToken(row);}});diff --git a/src/becca/entities/abstract_becca_entity.ts b/src/becca/entities/abstract_becca_entity.tsindex a36132f3a0..1f3bd0d863 100644--- a/src/becca/entities/abstract_becca_entity.ts+++ b/src/becca/entities/abstract_becca_entity.ts@@ -259,7 +259,7 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {}protected _getContent(): string | Buffer {- const row = sql.getRow<{ content: string | Buffer }>(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);+ const row = sql.getRow<{ content: string | Buffer }>(/*sql*/`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);if (!row) {const constructorData = this.constructor as unknown as ConstructorData<T>;@@ -282,7 +282,7 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {this.utcDateModified = dateUtils.utcNowDateTime();sql.execute(- `UPDATE ${entityName} SET isDeleted = 1, deleteId = ?, utcDateModified = ?+ /*sql*/`UPDATE ${entityName} SET isDeleted = 1, deleteId = ?, utcDateModified = ?WHERE ${constructorData.primaryKeyName} = ?`,[deleteId, this.utcDateModified, entityId]);@@ -290,7 +290,7 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {if (this.dateModified) {this.dateModified = dateUtils.localNowDateTime();- sql.execute(`UPDATE ${entityName} SET dateModified = ? WHERE ${constructorData.primaryKeyName} = ?`, [this.dateModified, entityId]);+ sql.execute(/*sql*/`UPDATE ${entityName} SET dateModified = ? WHERE ${constructorData.primaryKeyName} = ?`, [this.dateModified, entityId]);}log.info(`Marking ${entityName} ${entityId} as deleted`);@@ -308,7 +308,7 @@ abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> {this.utcDateModified = dateUtils.utcNowDateTime();sql.execute(- `UPDATE ${entityName} SET isDeleted = 1, utcDateModified = ?+ /*sql*/`UPDATE ${entityName} SET isDeleted = 1, utcDateModified = ?WHERE ${constructorData.primaryKeyName} = ?`,[this.utcDateModified, entityId]);diff --git a/src/becca/entities/battachment.ts b/src/becca/entities/battachment.tsindex 9461647afd..b4d0c4d188 100644--- a/src/becca/entities/battachment.ts+++ b/src/becca/entities/battachment.ts@@ -210,7 +210,7 @@ class BAttachment extends AbstractBeccaEntity<BAttachment> {this.position =10 +sql.getValue<number>(- `SELECT COALESCE(MAX(position), 0)+ /*sql*/`SELECT COALESCE(MAX(position), 0)FROM attachmentsWHERE ownerId = ?`,[this.noteId]diff --git a/src/becca/entities/bnote.ts b/src/becca/entities/bnote.tsindex 720e954e5b..cda4d5f2f6 100644--- a/src/becca/entities/bnote.ts+++ b/src/becca/entities/bnote.ts@@ -1108,12 +1108,12 @@ class BNote extends AbstractBeccaEntity<BNote> {// given that we're always fetching attachments only for a specific note, we might just do it alwaysconst query = opts.includeContentLength- ? `SELECT attachments.*, LENGTH(blobs.content) AS contentLength+ ? /*sql*/`SELECT attachments.*, LENGTH(blobs.content) AS contentLengthFROM attachmentsJOIN blobs USING (blobId)WHERE ownerId = ? AND isDeleted = 0ORDER BY position`- : `SELECT * FROM attachments WHERE ownerId = ? AND isDeleted = 0 ORDER BY position`;+ : /*sql*/`SELECT * FROM attachments WHERE ownerId = ? AND isDeleted = 0 ORDER BY position`;return sql.getRows<AttachmentRow>(query, [this.noteId]).map((row) => new BAttachment(row));}@@ -1122,11 +1122,11 @@ class BNote extends AbstractBeccaEntity<BNote> {opts.includeContentLength = !!opts.includeContentLength;const query = opts.includeContentLength- ? `SELECT attachments.*, LENGTH(blobs.content) AS contentLength+ ? /*sql*/`SELECT attachments.*, LENGTH(blobs.content) AS contentLengthFROM attachmentsJOIN blobs USING (blobId)WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`- : `SELECT * FROM attachments WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`;+ : /*sql*/`SELECT * FROM attachments WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`;return sql.getRows<AttachmentRow>(query, [this.noteId, attachmentId]).map((row) => new BAttachment(row))[0];}diff --git a/src/becca/entities/brevision.ts b/src/becca/entities/brevision.tsindex bc9ce360c7..de5dcd19f0 100644--- a/src/becca/entities/brevision.ts+++ b/src/becca/entities/brevision.ts@@ -141,11 +141,11 @@ class BRevision extends AbstractBeccaEntity<BRevision> {opts.includeContentLength = !!opts.includeContentLength;const query = opts.includeContentLength- ? `SELECT attachments.*, LENGTH(blobs.content) AS contentLength+ ? /*sql*/`SELECT attachments.*, LENGTH(blobs.content) AS contentLengthFROM attachmentsJOIN blobs USING (blobId)WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`- : `SELECT * FROM attachments WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`;+ : /*sql*/`SELECT * FROM attachments WHERE ownerId = ? AND attachmentId = ? AND isDeleted = 0`;return sql.getRows<AttachmentRow>(query, [this.revisionId, attachmentId]).map((row) => new BAttachment(row))[0];}diff --git a/src/routes/api/attributes.ts b/src/routes/api/attributes.tsindex b28ca4b4f6..7d542ac16d 100644--- a/src/routes/api/attributes.ts+++ b/src/routes/api/attributes.ts@@ -70,7 +70,7 @@ function setNoteAttribute(req: Request) {const noteId = req.params.noteId;const body = req.body;- const attributeId = sql.getValue<string | null>(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = ? AND name = ?`, [noteId, body.type, body.name]);+ const attributeId = sql.getValue<string | null>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = ? AND name = ?`, [noteId, body.type, body.name]);if (attributeId) {const attr = becca.getAttribute(attributeId);@@ -196,7 +196,7 @@ function createRelation(req: Request) {const targetNoteId = req.params.targetNoteId;const name = req.params.name;- const attributeId = sql.getValue<string>(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [+ const attributeId = sql.getValue<string>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId,name,targetNoteId@@ -220,7 +220,7 @@ function deleteRelation(req: Request) {const targetNoteId = req.params.targetNoteId;const name = req.params.name;- const attributeId = sql.getValue<string | null>(`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [+ const attributeId = sql.getValue<string | null>(/*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 0 AND noteId = ? AND type = 'relation' AND name = ? AND value = ?`, [sourceNoteId,name,targetNoteIddiff --git a/src/routes/api/autocomplete.ts b/src/routes/api/autocomplete.tsindex 584c2c88ad..bbe990bcd6 100644--- a/src/routes/api/autocomplete.ts+++ b/src/routes/api/autocomplete.ts@@ -83,7 +83,7 @@ function getRecentNotes(activeNoteId: string) {// Get the total number of notesfunction getNotesCount(req: Request) {const notesCount = sql.getRow(- `SELECT COUNT(*) AS count FROM notes WHERE isDeleted = 0;`,+ /*sql*/`SELECT COUNT(*) AS count FROM notes WHERE isDeleted = 0;`,) as { count: number };return notesCount.count;}diff --git a/src/routes/api/branches.ts b/src/routes/api/branches.tsindex b81d0cfc0b..bed1c93b7a 100644--- a/src/routes/api/branches.ts+++ b/src/routes/api/branches.ts@@ -171,7 +171,7 @@ function setExpandedForSubtree(req: Request) {// root is always expandedbranchIds = branchIds.filter((branchId) => branchId !== "none_root");- sql.executeMany(`UPDATE branches SET isExpanded = ${expanded} WHERE branchId IN (???)`, branchIds);+ sql.executeMany(/*sql*/`UPDATE branches SET isExpanded = ${expanded} WHERE branchId IN (???)`, branchIds);for (const branchId of branchIds) {const branch = becca.branches[branchId];diff --git a/src/routes/api/recent_notes.ts b/src/routes/api/recent_notes.tsindex b09e166d79..df56e2e6e6 100644--- a/src/routes/api/recent_notes.ts+++ b/src/routes/api/recent_notes.ts@@ -15,7 +15,7 @@ function addRecentNote(req: Request) {// it's not necessary to run this every time ...const cutOffDate = dateUtils.utcDateTimeStr(new Date(Date.now() - 24 * 3600 * 1000));- sql.execute(`DELETE FROM recent_notes WHERE utcDateCreated < ?`, [cutOffDate]);+ sql.execute(/*sql*/`DELETE FROM recent_notes WHERE utcDateCreated < ?`, [cutOffDate]);}}diff --git a/src/routes/api/relation-map.ts b/src/routes/api/relation-map.tsindex 2cf591b50c..0f0df61495 100644--- a/src/routes/api/relation-map.ts+++ b/src/routes/api/relation-map.ts@@ -40,7 +40,7 @@ function getRelationMap(req: Request) {const hideRelationsVal = relationMapNote.getLabelValue("hideRelations");const hideRelations = !hideRelationsVal ? [] : hideRelationsVal.split(",").map((token) => token.trim());- const foundNoteIds = sql.getColumn<string>(`SELECT noteId FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds);+ const foundNoteIds = sql.getColumn<string>(/*sql*/`SELECT noteId FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds);const notes = becca.getNotes(foundNoteIds);for (const note of notes) {diff --git a/src/routes/api/sql.ts b/src/routes/api/sql.tsindex a78c3e2f44..ce2fc51274 100644--- a/src/routes/api/sql.ts+++ b/src/routes/api/sql.ts@@ -7,7 +7,7 @@ import ValidationError from "../../errors/validation_error.js";import { safeExtractMessageAndStackFromError } from "../../services/utils.js";function getSchema() {- const tableNames = sql.getColumn(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`);+ const tableNames = sql.getColumn(/*sql*/`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`);const tables = [];for (const tableName of tableNames) {diff --git a/src/services/anonymization.ts b/src/services/anonymization.tsindex 2d855130f9..e776cecf63 100644--- a/src/services/anonymization.ts+++ b/src/services/anonymization.ts@@ -35,7 +35,7 @@ VACUUM;}function getLightAnonymizationScript() {- return `UPDATE blobs SET content = 'text' WHERE content IS NOT NULL AND blobId NOT IN (+ return /*sql*/`UPDATE blobs SET content = 'text' WHERE content IS NOT NULL AND blobId NOT IN (SELECT blobId FROM notes WHERE mime IN ('application/javascript;env=backend', 'application/javascript;env=frontend')UNION ALLSELECT blobId FROM revisions WHERE mime IN ('application/javascript;env=backend', 'application/javascript;env=frontend')diff --git a/src/services/attributes.ts b/src/services/attributes.tsindex 43332280e7..c87defa435 100644--- a/src/services/attributes.ts+++ b/src/services/attributes.ts@@ -65,7 +65,7 @@ function getAttributeNames(type: string, nameLike: string) {nameLike = nameLike.toLowerCase();let names = sql.getColumn<string>(- `SELECT DISTINCT name+ /*sql*/`SELECT DISTINCT nameFROM attributesWHERE isDeleted = 0AND type = ?diff --git a/src/services/cloning.ts b/src/services/cloning.tsindex 4166d8a11f..3c197cdfa1 100644--- a/src/services/cloning.ts+++ b/src/services/cloning.ts@@ -103,7 +103,7 @@ function ensureNoteIsPresentInParent(noteId: string, parentNoteId: string, prefi}function ensureNoteIsAbsentFromParent(noteId: string, parentNoteId: string) {- const branchId = sql.getValue<string>(`SELECT branchId FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]);+ const branchId = sql.getValue<string>(/*sql*/`SELECT branchId FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]);const branch = becca.getBranch(branchId);if (branch) {diff --git a/src/services/consistency_checks.ts b/src/services/consistency_checks.tsindex 8e7da9c9ed..147d6ae747 100644--- a/src/services/consistency_checks.ts+++ b/src/services/consistency_checks.ts@@ -375,7 +375,7 @@ class ConsistencyChecks {({ noteId, parentNoteId }) => {if (this.autoFix) {const branchIds = sql.getColumn<string>(- `SELECT branchId+ /*sql*/`SELECT branchIdFROM branchesWHERE noteId = ?and parentNoteId = ?@@ -729,7 +729,7 @@ class ConsistencyChecks {LEFT JOIN entity_changes ec ON ec.entityName = '${entityName}' AND ec.entityId = ${entityName}.${key}WHERE ec.id IS NULL`,({ entityId }) => {- const entityRow = sql.getRow<EntityChange>(`SELECT * FROM ${entityName} WHERE ${key} = ?`, [entityId]);+ const entityRow = sql.getRow<EntityChange>(/*sql*/`SELECT * FROM ${entityName} WHERE ${key} = ?`, [entityId]);if (this.autoFix) {entityChangesService.putEntityChange({@@ -778,7 +778,7 @@ class ConsistencyChecks {AND entity_changes.entityName = '${entityName}'`,({ id, entityId }) => {if (this.autoFix) {- sql.execute(`DELETE FROM ${entityName} WHERE ${key} = ?`, [entityId]);+ sql.execute(/*sql*/`DELETE FROM ${entityName} WHERE ${key} = ?`, [entityId]);this.reloadNeeded = true;@@ -802,7 +802,7 @@ class ConsistencyChecks {}findWronglyNamedAttributes() {- const attrNames = sql.getColumn<string>(`SELECT DISTINCT name FROM attributes`);+ const attrNames = sql.getColumn<string>(/*sql*/`SELECT DISTINCT name FROM attributes`);for (const origName of attrNames) {const fixedName = sanitizeAttributeName(origName);@@ -883,7 +883,7 @@ class ConsistencyChecks {runDbDiagnostics() {function getTableRowCount(tableName: string) {- const count = sql.getValue<number>(`SELECT COUNT(1) FROM ${tableName}`);+ const count = sql.getValue<number>(/*sql*/`SELECT COUNT(1) FROM ${tableName}`);return `${tableName}: ${count}`;}diff --git a/src/services/entity_changes.ts b/src/services/entity_changes.tsindex f3abfba5db..66c2613ced 100644--- a/src/services/entity_changes.ts+++ b/src/services/entity_changes.ts@@ -60,7 +60,7 @@ function putNoteReorderingEntityChange(parentNoteId: string, componentId?: strineventService.emit(eventService.ENTITY_CHANGED, {entityName: "note_reordering",- entity: sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [parentNoteId])+ entity: sql.getMap(/*sql*/`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [parentNoteId])});}@@ -73,7 +73,7 @@ function putEntityChangeForOtherInstances(ec: EntityChange) {}function addEntityChangesForSector(entityName: string, sector: string) {- const entityChanges = sql.getRows<EntityChange>(`SELECT * FROM entity_changes WHERE entityName = ? AND SUBSTR(entityId, 1, 1) = ?`, [entityName, sector]);+ const entityChanges = sql.getRows<EntityChange>(/*sql*/`SELECT * FROM entity_changes WHERE entityName = ? AND SUBSTR(entityId, 1, 1) = ?`, [entityName, sector]);let entitiesInserted = entityChanges.length;@@ -125,7 +125,7 @@ function fillEntityChanges(entityName: string, entityPrimaryKey: string, conditicleanupEntityChangesForMissingEntities(entityName, entityPrimaryKey);sql.transactional(() => {- const entityIds = sql.getColumn<string>(`SELECT ${entityPrimaryKey} FROM ${entityName} ${condition}`);+ const entityIds = sql.getColumn<string>(/*sql*/`SELECT ${entityPrimaryKey} FROM ${entityName} ${condition}`);let createdCount = 0;diff --git a/src/services/erase.ts b/src/services/erase.tsindex e07cfaa4ce..28603e1365 100644--- a/src/services/erase.ts+++ b/src/services/erase.ts@@ -12,19 +12,19 @@ function eraseNotes(noteIdsToErase: string[]) {return;}- sql.executeMany(`DELETE FROM notes WHERE noteId IN (???)`, noteIdsToErase);- setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'notes' AND entityId IN (???)`, noteIdsToErase));+ sql.executeMany(/*sql*/`DELETE FROM notes WHERE noteId IN (???)`, noteIdsToErase);+ setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'notes' AND entityId IN (???)`, noteIdsToErase));// we also need to erase all "dependent" entities of the erased notes- const branchIdsToErase = sql.getManyRows<{ branchId: string }>(`SELECT branchId FROM branches WHERE noteId IN (???)`, noteIdsToErase).map((row) => row.branchId);+ const branchIdsToErase = sql.getManyRows<{ branchId: string }>(/*sql*/`SELECT branchId FROM branches WHERE noteId IN (???)`, noteIdsToErase).map((row) => row.branchId);eraseBranches(branchIdsToErase);- const attributeIdsToErase = sql.getManyRows<{ attributeId: string }>(`SELECT attributeId FROM attributes WHERE noteId IN (???)`, noteIdsToErase).map((row) => row.attributeId);+ const attributeIdsToErase = sql.getManyRows<{ attributeId: string }>(/*sql*/`SELECT attributeId FROM attributes WHERE noteId IN (???)`, noteIdsToErase).map((row) => row.attributeId);eraseAttributes(attributeIdsToErase);- const revisionIdsToErase = sql.getManyRows<{ revisionId: string }>(`SELECT revisionId FROM revisions WHERE noteId IN (???)`, noteIdsToErase).map((row) => row.revisionId);+ const revisionIdsToErase = sql.getManyRows<{ revisionId: string }>(/*sql*/`SELECT revisionId FROM revisions WHERE noteId IN (???)`, noteIdsToErase).map((row) => row.revisionId);eraseRevisions(revisionIdsToErase);@@ -47,9 +47,9 @@ function eraseBranches(branchIdsToErase: string[]) {return;}- sql.executeMany(`DELETE FROM branches WHERE branchId IN (???)`, branchIdsToErase);+ sql.executeMany(/*sql*/`DELETE FROM branches WHERE branchId IN (???)`, branchIdsToErase);- setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'branches' AND entityId IN (???)`, branchIdsToErase));+ setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'branches' AND entityId IN (???)`, branchIdsToErase));log.info(`Erased branches: ${JSON.stringify(branchIdsToErase)}`);}@@ -59,9 +59,9 @@ function eraseAttributes(attributeIdsToErase: string[]) {return;}- sql.executeMany(`DELETE FROM attributes WHERE attributeId IN (???)`, attributeIdsToErase);+ sql.executeMany(/*sql*/`DELETE FROM attributes WHERE attributeId IN (???)`, attributeIdsToErase);- setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'attributes' AND entityId IN (???)`, attributeIdsToErase));+ setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'attributes' AND entityId IN (???)`, attributeIdsToErase));log.info(`Erased attributes: ${JSON.stringify(attributeIdsToErase)}`);}@@ -71,9 +71,9 @@ function eraseAttachments(attachmentIdsToErase: string[]) {return;}- sql.executeMany(`DELETE FROM attachments WHERE attachmentId IN (???)`, attachmentIdsToErase);+ sql.executeMany(/*sql*/`DELETE FROM attachments WHERE attachmentId IN (???)`, attachmentIdsToErase);- setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'attachments' AND entityId IN (???)`, attachmentIdsToErase));+ setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'attachments' AND entityId IN (???)`, attachmentIdsToErase));log.info(`Erased attachments: ${JSON.stringify(attachmentIdsToErase)}`);}@@ -83,9 +83,9 @@ function eraseRevisions(revisionIdsToErase: string[]) {return;}- sql.executeMany(`DELETE FROM revisions WHERE revisionId IN (???)`, revisionIdsToErase);+ sql.executeMany(/*sql*/`DELETE FROM revisions WHERE revisionId IN (???)`, revisionIdsToErase);- setEntityChangesAsErased(sql.getManyRows(`SELECT * FROM entity_changes WHERE entityName = 'revisions' AND entityId IN (???)`, revisionIdsToErase));+ setEntityChangesAsErased(sql.getManyRows(/*sql*/`SELECT * FROM entity_changes WHERE entityName = 'revisions' AND entityId IN (???)`, revisionIdsToErase));log.info(`Removed revisions: ${JSON.stringify(revisionIdsToErase)}`);}@@ -105,10 +105,10 @@ function eraseUnusedBlobs() {return;}- sql.executeMany(`DELETE FROM blobs WHERE blobId IN (???)`, unusedBlobIds);+ sql.executeMany(/*sql*/`DELETE FROM blobs WHERE blobId IN (???)`, unusedBlobIds);// blobs are not marked as erased in entity_changes, they are just purged completely// this is because technically every keystroke can create a new blob and there would be just too many- sql.executeMany(`DELETE FROM entity_changes WHERE entityName = 'blobs' AND entityId IN (???)`, unusedBlobIds);+ sql.executeMany(/*sql*/`DELETE FROM entity_changes WHERE entityName = 'blobs' AND entityId IN (???)`, unusedBlobIds);log.info(`Erased unused blobs: ${JSON.stringify(unusedBlobIds)}`);}diff --git a/src/services/migration.ts b/src/services/migration.tsindex 17f1be40df..d73e34253e 100644--- a/src/services/migration.ts+++ b/src/services/migration.ts@@ -52,7 +52,7 @@ async function migrate() {executeMigration(mig);sql.execute(- `UPDATE options+ /*sql*/`UPDATE optionsSET value = ?WHERE name = ?`,[mig.dbVersion.toString(), "dbVersion"]diff --git a/src/services/sql_init.ts b/src/services/sql_init.tsindex b03c12f516..56422c7942 100644--- a/src/services/sql_init.ts+++ b/src/services/sql_init.ts@@ -21,7 +21,7 @@ import backup from "./backup.js";const dbReady = deferred<void>();function schemaExists() {- return !!sql.getValue(`SELECT name FROM sqlite_master+ return !!sql.getValue(/*sql*/`SELECT name FROM sqlite_masterWHERE type = 'table' AND name = 'options'`);}diff --git a/src/services/sync.ts b/src/services/sync.tsindex bfaf828f92..47985b4bd7 100644--- a/src/services/sync.ts+++ b/src/services/sync.ts@@ -347,7 +347,7 @@ function getEntityChangeRow(entityChange: EntityChange) {throw new Error(`Unknown entity for entity change ${JSON.stringify(entityChange)}`);}- const entityRow = sql.getRow<EntityRow>(`SELECT * FROM ${entityName} WHERE ${primaryKey} = ?`, [entityId]);+ const entityRow = sql.getRow<EntityRow>(/*sql*/`SELECT * FROM ${entityName} WHERE ${primaryKey} = ?`, [entityId]);if (!entityRow) {log.error(`Cannot find entity for entity change ${JSON.stringify(entityChange)}`);diff --git a/src/services/sync_update.ts b/src/services/sync_update.tsindex fae50dd70c..c6c0228a1e 100644--- a/src/services/sync_update.ts+++ b/src/services/sync_update.ts@@ -70,7 +70,7 @@ function updateEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow | undef}function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow | undefined, instanceId: string, updateContext: UpdateContext) {- const localEC = sql.getRow<EntityChange | undefined>(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]);+ const localEC = sql.getRow<EntityChange | undefined>(/*sql*/`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]);const localECIsOlderOrSameAsRemote = localEC && localEC.utcDateChanged && remoteEC.utcDateChanged && localEC.utcDateChanged <= remoteEC.utcDateChanged;if (!localEC || localECIsOlderOrSameAsRemote) {@@ -153,7 +153,7 @@ function eraseEntity(entityChange: EntityChange) {const primaryKeyName = entityConstructor.getEntityFromEntityName(entityName).primaryKeyName;- sql.execute(`DELETE FROM ${entityName} WHERE ${primaryKeyName} = ?`, [entityId]);+ sql.execute(/*sql*/`DELETE FROM ${entityName} WHERE ${primaryKeyName} = ?`, [entityId]);}function logUpdateContext(updateContext: UpdateContext) {diff --git a/src/services/ws.ts b/src/services/ws.tsindex f2674dd200..a405500e64 100644--- a/src/services/ws.ts+++ b/src/services/ws.ts@@ -139,19 +139,19 @@ function fillInAdditionalProperties(entityChange: EntityChange) {entityChange.entity = becca.getAttribute(entityChange.entityId);if (!entityChange.entity) {- entityChange.entity = sql.getRow(`SELECT * FROM attributes WHERE attributeId = ?`, [entityChange.entityId]);+ entityChange.entity = sql.getRow(/*sql*/`SELECT * FROM attributes WHERE attributeId = ?`, [entityChange.entityId]);}} else if (entityChange.entityName === "branches") {entityChange.entity = becca.getBranch(entityChange.entityId);if (!entityChange.entity) {- entityChange.entity = sql.getRow(`SELECT * FROM branches WHERE branchId = ?`, [entityChange.entityId]);+ entityChange.entity = sql.getRow(/*sql*/`SELECT * FROM branches WHERE branchId = ?`, [entityChange.entityId]);}} else if (entityChange.entityName === "notes") {entityChange.entity = becca.getNote(entityChange.entityId);if (!entityChange.entity) {- entityChange.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [entityChange.entityId]);+ entityChange.entity = sql.getRow(/*sql*/`SELECT * FROM notes WHERE noteId = ?`, [entityChange.entityId]);if (entityChange.entity?.isProtected) {entityChange.entity.title = protectedSessionService.decryptString(entityChange.entity.title || "");@@ -159,7 +159,7 @@ function fillInAdditionalProperties(entityChange: EntityChange) {}} else if (entityChange.entityName === "revisions") {entityChange.noteId = sql.getValue<string>(- `SELECT noteId+ /*sql*/`SELECT noteIdFROM revisionsWHERE revisionId = ?`,[entityChange.entityId]@@ -180,11 +180,11 @@ function fillInAdditionalProperties(entityChange: EntityChange) {entityChange.entity = becca.getOption(entityChange.entityId);if (!entityChange.entity) {- entityChange.entity = sql.getRow(`SELECT * FROM options WHERE name = ?`, [entityChange.entityId]);+ entityChange.entity = sql.getRow(/*sql*/`SELECT * FROM options WHERE name = ?`, [entityChange.entityId]);}} else if (entityChange.entityName === "attachments") {entityChange.entity = sql.getRow(- `SELECT attachments.*, LENGTH(blobs.content) AS contentLength+ /*sql*/`SELECT attachments.*, LENGTH(blobs.content) AS contentLengthFROM attachmentsJOIN blobs USING (blobId)WHERE attachmentId = ?`,@@ -217,7 +217,7 @@ function sendPing(client: WebSocket, entityChangeIds = []) {return;}- const entityChanges = sql.getManyRows<EntityChange>(`SELECT * FROM entity_changes WHERE id IN (???)`, entityChangeIds);+ const entityChanges = sql.getManyRows<EntityChange>(/*sql*/`SELECT * FROM entity_changes WHERE id IN (???)`, entityChangeIds);if (!entityChanges) {return;}diff --git a/src/share/shaca/entities/sattachment.ts b/src/share/shaca/entities/sattachment.tsindex e8d1e50d99..11d3af0969 100644--- a/src/share/shaca/entities/sattachment.ts+++ b/src/share/shaca/entities/sattachment.ts@@ -37,7 +37,7 @@ class SAttachment extends AbstractShacaEntity {}getContent(silentNotFoundError = false) {- const row = sql.getRow<Pick<Blob, "content">>(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);+ const row = sql.getRow<Pick<Blob, "content">>(/*sql*/`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);if (!row) {if (silentNotFoundError) {diff --git a/src/share/shaca/entities/snote.ts b/src/share/shaca/entities/snote.tsindex e4ba17d0c1..6b192b57e3 100644--- a/src/share/shaca/entities/snote.ts+++ b/src/share/shaca/entities/snote.ts@@ -95,7 +95,7 @@ class SNote extends AbstractShacaEntity {}getContent(silentNotFoundError = false) {- const row = sql.getRow<Pick<Blob, "content">>(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);+ const row = sql.getRow<Pick<Blob, "content">>(/*sql*/`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]);if (!row) {if (silentNotFoundError) {