mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	server-ts: Fix errors in abstract_becca_entity
This commit is contained in:
		
							parent
							
								
									f51f070b2f
								
							
						
					
					
						commit
						f9ba8ca87d
					
				| @ -13,16 +13,35 @@ import Becca = require('../becca-interface'); | |||||||
| 
 | 
 | ||||||
| let becca: Becca | null = null; | let becca: Becca | null = null; | ||||||
| 
 | 
 | ||||||
|  | interface ContentOpts { | ||||||
|  |     forceSave?: boolean; | ||||||
|  |     forceFrontendReload?: boolean; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | interface ConstructorData<T extends AbstractBeccaEntity<T>> { | ||||||
|  |     primaryKeyName: string; | ||||||
|  |     entityName: string; | ||||||
|  |     hashedProperties: (keyof T)[]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * Base class for all backend entities. |  * Base class for all backend entities. | ||||||
|  */ |  */ | ||||||
| abstract class AbstractBeccaEntity { | abstract class AbstractBeccaEntity<T extends AbstractBeccaEntity<T>> { | ||||||
| 
 | 
 | ||||||
|  |     protected utcDateCreated?: string; | ||||||
|     protected utcDateModified?: string; |     protected utcDateModified?: string; | ||||||
|  |     protected dateModified?: string; | ||||||
|  |     protected isProtected?: boolean; | ||||||
|  |     protected isDeleted?: boolean; | ||||||
|  |     protected isSynced?: boolean; | ||||||
|  | 
 | ||||||
|  |     protected blobId?: string; | ||||||
| 
 | 
 | ||||||
|     protected beforeSaving() { |     protected beforeSaving() { | ||||||
|         if (!this[this.constructor.primaryKeyName]) { |         const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|             this[this.constructor.primaryKeyName] = utils.newEntityId(); |         if (!(this as any)[constructorData.primaryKeyName]) { | ||||||
|  |             (this as any)[constructorData.primaryKeyName] = utils.newEntityId(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -39,21 +58,23 @@ abstract class AbstractBeccaEntity { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected putEntityChange(isDeleted: boolean) { |     protected putEntityChange(isDeleted: boolean) { | ||||||
|  |         const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|         entityChangesService.putEntityChange({ |         entityChangesService.putEntityChange({ | ||||||
|             entityName: this.constructor.entityName, |             entityName: constructorData.entityName, | ||||||
|             entityId: this[this.constructor.primaryKeyName], |             entityId: (this as any)[constructorData.primaryKeyName], | ||||||
|             hash: this.generateHash(isDeleted), |             hash: this.generateHash(isDeleted), | ||||||
|             isErased: false, |             isErased: false, | ||||||
|             utcDateChanged: this.getUtcDateChanged(), |             utcDateChanged: this.getUtcDateChanged(), | ||||||
|             isSynced: this.constructor.entityName !== 'options' || !!this.isSynced |             isSynced: constructorData.entityName !== 'options' || !!this.isSynced | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected generateHash(isDeleted: boolean): string { |     protected generateHash(isDeleted: boolean): string { | ||||||
|  |         const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|         let contentToHash = ""; |         let contentToHash = ""; | ||||||
| 
 | 
 | ||||||
|         for (const propertyName of this.constructor.hashedProperties) { |         for (const propertyName of constructorData.hashedProperties) { | ||||||
|             contentToHash += `|${this[propertyName]}`; |             contentToHash += `|${(this as any)[propertyName]}`; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (isDeleted) { |         if (isDeleted) { | ||||||
| @ -67,18 +88,21 @@ abstract class AbstractBeccaEntity { | |||||||
|         return this.getPojo(); |         return this.getPojo(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     abstract hasStringContent(): boolean; | ||||||
|  | 
 | ||||||
|     abstract getPojo(): {}; |     abstract getPojo(): {}; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Saves entity - executes SQL, but doesn't commit the transaction on its own |      * Saves entity - executes SQL, but doesn't commit the transaction on its own | ||||||
|      */ |      */ | ||||||
|     save(opts = {}): this { |     save(): this { | ||||||
|         const entityName = this.constructor.entityName; |         const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|         const primaryKeyName = this.constructor.primaryKeyName; |         const entityName = constructorData.entityName; | ||||||
|  |         const primaryKeyName = constructorData.primaryKeyName; | ||||||
| 
 | 
 | ||||||
|         const isNewEntity = !this[primaryKeyName]; |         const isNewEntity = !(this as any)[primaryKeyName]; | ||||||
| 
 | 
 | ||||||
|         this.beforeSaving(opts); |         this.beforeSaving(); | ||||||
| 
 | 
 | ||||||
|         const pojo = this.getPojoToSave(); |         const pojo = this.getPojoToSave(); | ||||||
| 
 | 
 | ||||||
| @ -108,13 +132,14 @@ abstract class AbstractBeccaEntity { | |||||||
|         return this; |         return this; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected _setContent(content, opts = {}) { |     protected _setContent(content: string | Buffer, opts: ContentOpts = {}) { | ||||||
|         // client code asks to save entity even if blobId didn't change (something else was changed)
 |         // client code asks to save entity even if blobId didn't change (something else was changed)
 | ||||||
|         opts.forceSave = !!opts.forceSave; |         opts.forceSave = !!opts.forceSave; | ||||||
|         opts.forceFrontendReload = !!opts.forceFrontendReload; |         opts.forceFrontendReload = !!opts.forceFrontendReload; | ||||||
| 
 | 
 | ||||||
|         if (content === null || content === undefined) { |         if (content === null || content === undefined) { | ||||||
|             throw new Error(`Cannot set null content to ${this.constructor.primaryKeyName} '${this[this.constructor.primaryKeyName]}'`); |             const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|  |             throw new Error(`Cannot set null content to ${constructorData.primaryKeyName} '${(this as any)[constructorData.primaryKeyName]}'`); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         if (this.hasStringContent()) { |         if (this.hasStringContent()) { | ||||||
| @ -123,32 +148,36 @@ abstract class AbstractBeccaEntity { | |||||||
|             content = Buffer.isBuffer(content) ? content : Buffer.from(content); |             content = Buffer.isBuffer(content) ? content : Buffer.from(content); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const unencryptedContentForHashCalculation = this.#getUnencryptedContentForHashCalculation(content); |         const unencryptedContentForHashCalculation = this.getUnencryptedContentForHashCalculation(content); | ||||||
| 
 | 
 | ||||||
|         if (this.isProtected) { |         if (this.isProtected) { | ||||||
|             if (protectedSessionService.isProtectedSessionAvailable()) { |             if (protectedSessionService.isProtectedSessionAvailable()) { | ||||||
|                 content = protectedSessionService.encrypt(content); |                 const encryptedContent = protectedSessionService.encrypt(content); | ||||||
|  |                 if (!encryptedContent) { | ||||||
|  |                     throw new Error(`Unable to encrypt the content of the entity.`);     | ||||||
|  |                 } | ||||||
|  |                 content = encryptedContent; | ||||||
|             } else { |             } else { | ||||||
|                 throw new Error(`Cannot update content of blob since protected session is not available.`); |                 throw new Error(`Cannot update content of blob since protected session is not available.`); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         sql.transactional(() => { |         sql.transactional(() => { | ||||||
|             const newBlobId = this.#saveBlob(content, unencryptedContentForHashCalculation, opts); |             const newBlobId = this.saveBlob(content, unencryptedContentForHashCalculation, opts); | ||||||
|             const oldBlobId = this.blobId; |             const oldBlobId = this.blobId; | ||||||
| 
 | 
 | ||||||
|             if (newBlobId !== oldBlobId || opts.forceSave) { |             if (newBlobId !== oldBlobId || opts.forceSave) { | ||||||
|                 this.blobId = newBlobId; |                 this.blobId = newBlobId; | ||||||
|                 this.save(); |                 this.save(); | ||||||
| 
 | 
 | ||||||
|                 if (newBlobId !== oldBlobId) { |                 if (oldBlobId && newBlobId !== oldBlobId) { | ||||||
|                     this.#deleteBlobIfNotUsed(oldBlobId); |                     this.deleteBlobIfNotUsed(oldBlobId); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     #deleteBlobIfNotUsed(oldBlobId) { |     private deleteBlobIfNotUsed(oldBlobId: string) { | ||||||
|         if (sql.getValue("SELECT 1 FROM notes WHERE blobId = ? LIMIT 1", [oldBlobId])) { |         if (sql.getValue("SELECT 1 FROM notes WHERE blobId = ? LIMIT 1", [oldBlobId])) { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| @ -167,7 +196,7 @@ abstract class AbstractBeccaEntity { | |||||||
|         sql.execute("DELETE FROM entity_changes WHERE entityName = 'blobs' AND entityId = ?", [oldBlobId]); |         sql.execute("DELETE FROM entity_changes WHERE entityName = 'blobs' AND entityId = ?", [oldBlobId]); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     #getUnencryptedContentForHashCalculation(unencryptedContent) { |     private getUnencryptedContentForHashCalculation(unencryptedContent: Buffer | string) { | ||||||
|         if (this.isProtected) { |         if (this.isProtected) { | ||||||
|             // a "random" prefix makes sure that the calculated hash/blobId is different for a decrypted/encrypted content
 |             // a "random" prefix makes sure that the calculated hash/blobId is different for a decrypted/encrypted content
 | ||||||
|             const encryptedPrefixSuffix = "t$[nvQg7q)&_ENCRYPTED_?M:Bf&j3jr_"; |             const encryptedPrefixSuffix = "t$[nvQg7q)&_ENCRYPTED_?M:Bf&j3jr_"; | ||||||
| @ -179,7 +208,7 @@ abstract class AbstractBeccaEntity { | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     #saveBlob(content, unencryptedContentForHashCalculation, opts = {}) { |     private saveBlob(content: string | Buffer, unencryptedContentForHashCalculation: string | Buffer, opts: ContentOpts = {}) { | ||||||
|         /* |         /* | ||||||
|          * We're using the unencrypted blob for the hash calculation, because otherwise the random IV would |          * We're using the unencrypted blob for the hash calculation, because otherwise the random IV would | ||||||
|          * cause every content blob to be unique which would balloon the database size (esp. with revisioning). |          * cause every content blob to be unique which would balloon the database size (esp. with revisioning). | ||||||
| @ -227,13 +256,14 @@ abstract class AbstractBeccaEntity { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     protected _getContent(): string | Buffer {         |     protected _getContent(): string | Buffer {         | ||||||
|         const row = sql.getRow(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]); |         const row = sql.getRow<{ content: string | Buffer }>(`SELECT content FROM blobs WHERE blobId = ?`, [this.blobId]); | ||||||
| 
 | 
 | ||||||
|         if (!row) { |         if (!row) { | ||||||
|             throw new Error(`Cannot find content for ${this.constructor.primaryKeyName} '${this[this.constructor.primaryKeyName]}', blobId '${this.blobId}'`); |             const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|  |             throw new Error(`Cannot find content for ${constructorData.primaryKeyName} '${(this as any)[constructorData.primaryKeyName]}', blobId '${this.blobId}'`); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return blobService.processContent(row.content, this.isProtected, this.hasStringContent()); |         return blobService.processContent(row.content, this.isProtected || false, this.hasStringContent()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -242,19 +272,20 @@ abstract class AbstractBeccaEntity { | |||||||
|      * This is a low-level method, for notes and branches use `note.deleteNote()` and 'branch.deleteBranch()` instead.
 |      * This is a low-level method, for notes and branches use `note.deleteNote()` and 'branch.deleteBranch()` instead.
 | ||||||
|      */ |      */ | ||||||
|     markAsDeleted(deleteId = null) { |     markAsDeleted(deleteId = null) { | ||||||
|         const entityId = this[this.constructor.primaryKeyName]; |         const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|         const entityName = this.constructor.entityName; |         const entityId = (this as any)[constructorData.primaryKeyName]; | ||||||
|  |         const entityName = constructorData.entityName; | ||||||
| 
 | 
 | ||||||
|         this.utcDateModified = dateUtils.utcNowDateTime(); |         this.utcDateModified = dateUtils.utcNowDateTime(); | ||||||
| 
 | 
 | ||||||
|         sql.execute(`UPDATE ${entityName} SET isDeleted = 1, deleteId = ?, utcDateModified = ?
 |         sql.execute(`UPDATE ${entityName} SET isDeleted = 1, deleteId = ?, utcDateModified = ?
 | ||||||
|                            WHERE ${this.constructor.primaryKeyName} = ?`,
 |                            WHERE ${constructorData.primaryKeyName} = ?`,
 | ||||||
|             [deleteId, this.utcDateModified, entityId]); |             [deleteId, this.utcDateModified, entityId]); | ||||||
| 
 | 
 | ||||||
|         if (this.dateModified) { |         if (this.dateModified) { | ||||||
|             this.dateModified = dateUtils.localNowDateTime(); |             this.dateModified = dateUtils.localNowDateTime(); | ||||||
| 
 | 
 | ||||||
|             sql.execute(`UPDATE ${entityName} SET dateModified = ? WHERE ${this.constructor.primaryKeyName} = ?`, |             sql.execute(`UPDATE ${entityName} SET dateModified = ? WHERE ${constructorData.primaryKeyName} = ?`, | ||||||
|                 [this.dateModified, entityId]); |                 [this.dateModified, entityId]); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -266,13 +297,14 @@ abstract class AbstractBeccaEntity { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     markAsDeletedSimple() { |     markAsDeletedSimple() { | ||||||
|         const entityId = this[this.constructor.primaryKeyName]; |         const constructorData = (this.constructor as unknown as ConstructorData<T>); | ||||||
|         const entityName = this.constructor.entityName; |         const entityId = (this as any)[constructorData.primaryKeyName]; | ||||||
|  |         const entityName = constructorData.entityName; | ||||||
| 
 | 
 | ||||||
|         this.utcDateModified = dateUtils.utcNowDateTime(); |         this.utcDateModified = dateUtils.utcNowDateTime(); | ||||||
| 
 | 
 | ||||||
|         sql.execute(`UPDATE ${entityName} SET isDeleted = 1, utcDateModified = ?
 |         sql.execute(`UPDATE ${entityName} SET isDeleted = 1, utcDateModified = ?
 | ||||||
|                            WHERE ${this.constructor.primaryKeyName} = ?`,
 |                            WHERE ${constructorData.primaryKeyName} = ?`,
 | ||||||
|             [this.utcDateModified, entityId]); |             [this.utcDateModified, entityId]); | ||||||
| 
 | 
 | ||||||
|         log.info(`Marking ${entityName} ${entityId} as deleted`); |         log.info(`Marking ${entityName} ${entityId} as deleted`); | ||||||
|  | |||||||
| @ -25,7 +25,6 @@ class BAttribute extends AbstractBeccaEntity { | |||||||
|     position!: number; |     position!: number; | ||||||
|     value!: string; |     value!: string; | ||||||
|     isInheritable!: boolean; |     isInheritable!: boolean; | ||||||
|     utcDateModified!: string; |  | ||||||
| 
 | 
 | ||||||
|     constructor(row: AttributeRow) { |     constructor(row: AttributeRow) { | ||||||
|         super(); |         super(); | ||||||
|  | |||||||
| @ -24,8 +24,6 @@ class BEtapiToken extends AbstractBeccaEntity { | |||||||
|     etapiTokenId!: string; |     etapiTokenId!: string; | ||||||
|     name!: string; |     name!: string; | ||||||
|     tokenHash!: string; |     tokenHash!: string; | ||||||
|     utcDateCreated!: string; |  | ||||||
|     utcDateModified!: string; |  | ||||||
|     isDeleted!: boolean; |     isDeleted!: boolean; | ||||||
| 
 | 
 | ||||||
|     constructor(row: EtapiTokenRow) { |     constructor(row: EtapiTokenRow) { | ||||||
|  | |||||||
| @ -15,7 +15,6 @@ class BOption extends AbstractBeccaEntity { | |||||||
|     name!: string; |     name!: string; | ||||||
|     value!: string; |     value!: string; | ||||||
|     isSynced!: boolean; |     isSynced!: boolean; | ||||||
|     utcDateModified!: string; |  | ||||||
| 
 | 
 | ||||||
|     constructor(row: OptionRow) { |     constructor(row: OptionRow) { | ||||||
|         super(); |         super(); | ||||||
|  | |||||||
| @ -21,8 +21,6 @@ interface GetByIdOpts { | |||||||
| /** | /** | ||||||
|  * Revision represents a snapshot of note's title and content at some point in the past. |  * Revision represents a snapshot of note's title and content at some point in the past. | ||||||
|  * It's used for seamless note versioning. |  * It's used for seamless note versioning. | ||||||
|  * |  | ||||||
|  * @extends AbstractBeccaEntity |  | ||||||
|  */ |  */ | ||||||
| class BRevision extends AbstractBeccaEntity { | class BRevision extends AbstractBeccaEntity { | ||||||
|     static get entityName() { return "revisions"; } |     static get entityName() { return "revisions"; } | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| export interface Blob { | export interface Blob { | ||||||
|     blobId: string; |     blobId: string; | ||||||
|     content: Buffer; |     content: string | Buffer; | ||||||
|     utcDateModified: string; |     utcDateModified: string; | ||||||
| } | } | ||||||
| @ -6,7 +6,7 @@ export interface EntityChange { | |||||||
| 	entity?: any; | 	entity?: any; | ||||||
| 	positions?: Record<string, string>; | 	positions?: Record<string, string>; | ||||||
| 	hash: string; | 	hash: string; | ||||||
| 	utcDateChanged: string; | 	utcDateChanged?: string; | ||||||
| 	isSynced: boolean | 1 | 0; | 	isSynced: boolean | 1 | 0; | ||||||
| 	isErased: boolean | 1 | 0; | 	isErased: boolean | 1 | 0; | ||||||
| 	componentId?: string | null; | 	componentId?: string | null; | ||||||
|  | |||||||
| @ -24,7 +24,7 @@ function md5(content: crypto.BinaryLike) { | |||||||
|     return crypto.createHash('md5').update(content).digest('hex'); |     return crypto.createHash('md5').update(content).digest('hex'); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function hashedBlobId(content: string) { | function hashedBlobId(content: string | Buffer) { | ||||||
|     if (content === null || content === undefined) { |     if (content === null || content === undefined) { | ||||||
|         content = ""; |         content = ""; | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran