From 277368ab43d33a3691ca3ebe80997d966a1e2559 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 2 Apr 2018 20:30:00 -0400 Subject: [PATCH] simplified new entity ID allocation --- src/entities/api_token.js | 4 +--- src/entities/branch.js | 4 +--- src/entities/entity.js | 6 ++++++ src/entities/image.js | 4 +--- src/entities/label.js | 4 +--- src/entities/note.js | 4 +--- src/entities/note_image.js | 4 +--- src/entities/note_revision.js | 4 +--- src/services/utils.js | 34 ++-------------------------------- 9 files changed, 15 insertions(+), 53 deletions(-) diff --git a/src/entities/api_token.js b/src/entities/api_token.js index 14e80e17b..9d50a4479 100644 --- a/src/entities/api_token.js +++ b/src/entities/api_token.js @@ -8,9 +8,7 @@ class ApiToken extends Entity { static get primaryKeyName() { return "apiTokenId"; } beforeSaving() { - if (!this.apiTokenId) { - this.apiTokenId = utils.newApiTokenId(); - } + super.beforeSaving(); if (!this.isDeleted) { this.isDeleted = false; diff --git a/src/entities/branch.js b/src/entities/branch.js index 14a48ce95..11fa2c1ec 100644 --- a/src/entities/branch.js +++ b/src/entities/branch.js @@ -13,9 +13,7 @@ class Branch extends Entity { } beforeSaving() { - if (!this.branchId) { - this.branchId = utils.newBranchId(); - } + super.beforeSaving(); if (!this.isDeleted) { this.isDeleted = false; diff --git a/src/entities/entity.js b/src/entities/entity.js index 41fbfd93c..40c1a9969 100644 --- a/src/entities/entity.js +++ b/src/entities/entity.js @@ -12,6 +12,12 @@ class Entity { } } + beforeSaving() { + if (!this[this.constructor.primaryKeyName]) { + this[this.constructor.primaryKeyName] = utils.newEntityId(); + } + } + async save() { await repository.updateEntity(this); } diff --git a/src/entities/image.js b/src/entities/image.js index 5b1e899b1..8bd995f94 100644 --- a/src/entities/image.js +++ b/src/entities/image.js @@ -8,9 +8,7 @@ class Image extends Entity { static get primaryKeyName() { return "imageId"; } beforeSaving() { - if (!this.imageId) { - this.imageId = utils.newImageId(); - } + super.beforeSaving(); if (!this.isDeleted) { this.isDeleted = false; diff --git a/src/entities/label.js b/src/entities/label.js index 1f1595b53..3112e3709 100644 --- a/src/entities/label.js +++ b/src/entities/label.js @@ -14,9 +14,7 @@ class Label extends Entity { } async beforeSaving() { - if (!this.labelId) { - this.labelId = utils.newLabelId(); - } + super.beforeSaving(); if (!this.value) { // null value isn't allowed diff --git a/src/entities/note.js b/src/entities/note.js index 2e6a4f283..3b4810d6a 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -131,9 +131,7 @@ class Note extends Entity { } beforeSaving() { - if (!this.noteId) { - this.noteId = utils.newNoteId(); - } + super.beforeSaving(); if (this.isJson()) { this.content = JSON.stringify(this.jsonContent, null, '\t'); diff --git a/src/entities/note_image.js b/src/entities/note_image.js index 944bf6e17..8f1340773 100644 --- a/src/entities/note_image.js +++ b/src/entities/note_image.js @@ -17,9 +17,7 @@ class NoteImage extends Entity { } beforeSaving() { - if (!this.noteImageId) { - this.noteImageId = utils.newNoteImageId(); - } + super.beforeSaving(); if (!this.isDeleted) { this.isDeleted = false; diff --git a/src/entities/note_revision.js b/src/entities/note_revision.js index 41b2f995c..a96c0f1b6 100644 --- a/src/entities/note_revision.js +++ b/src/entities/note_revision.js @@ -22,9 +22,7 @@ class NoteRevision extends Entity { } beforeSaving() { - if (!this.noteRevisionId) { - this.noteRevisionId = utils.newNoteRevisionId(); - } + super.beforeSaving(); if (this.isProtected) { protected_session.encryptNoteRevision(this); diff --git a/src/services/utils.js b/src/services/utils.js index cdb4b75c9..d9389624b 100644 --- a/src/services/utils.js +++ b/src/services/utils.js @@ -4,31 +4,7 @@ const crypto = require('crypto'); const randtoken = require('rand-token').generator({source: 'crypto'}); const unescape = require('unescape'); -function newNoteId() { - return randomString(12); -} - -function newBranchId() { - return randomString(12); -} - -function newNoteRevisionId() { - return randomString(12); -} - -function newImageId() { - return randomString(12); -} - -function newNoteImageId() { - return randomString(12); -} - -function newLabelId() { - return randomString(12); -} - -function newApiTokenId() { +function newEntityId() { return randomString(12); } @@ -154,13 +130,7 @@ module.exports = { dateStr, parseDate, parseDateTime, - newNoteId, - newBranchId, - newNoteRevisionId, - newImageId, - newNoteImageId, - newLabelId, - newApiTokenId, + newEntityId, toBase64, fromBase64, hmac,