simplified new entity ID allocation

This commit is contained in:
azivner 2018-04-02 20:30:00 -04:00
parent e2921a648d
commit 277368ab43
9 changed files with 15 additions and 53 deletions

View file

@ -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;

View file

@ -13,9 +13,7 @@ class Branch extends Entity {
}
beforeSaving() {
if (!this.branchId) {
this.branchId = utils.newBranchId();
}
super.beforeSaving();
if (!this.isDeleted) {
this.isDeleted = false;

View file

@ -12,6 +12,12 @@ class Entity {
}
}
beforeSaving() {
if (!this[this.constructor.primaryKeyName]) {
this[this.constructor.primaryKeyName] = utils.newEntityId();
}
}
async save() {
await repository.updateEntity(this);
}

View file

@ -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;

View file

@ -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

View file

@ -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');

View file

@ -17,9 +17,7 @@ class NoteImage extends Entity {
}
beforeSaving() {
if (!this.noteImageId) {
this.noteImageId = utils.newNoteImageId();
}
super.beforeSaving();
if (!this.isDeleted) {
this.isDeleted = false;

View file

@ -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);

View file

@ -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,