From f47f01f14745c4472211af1a58011ce01ae9fb7b Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Fri, 28 Aug 2020 14:16:06 +0200 Subject: [PATCH] Models: add autoFill method for asynchronous default value load --- src/db/Model.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/db/Model.ts b/src/db/Model.ts index e1dcf29..b635f2b 100644 --- a/src/db/Model.ts +++ b/src/db/Model.ts @@ -83,6 +83,13 @@ export default abstract class Model { } } + /** + * Override this to automatically fill obvious missing data i.e. from relation or default value that are fetched + * asynchronously. + */ + public async autoFill(): Promise { + } + protected async beforeSave(exists: boolean, connection: Connection): Promise { } @@ -90,6 +97,7 @@ export default abstract class Model { } public async save(connection?: Connection, postHook?: (callback: () => Promise) => void): Promise { + await this.autoFill(); await this.validate(false, connection); const exists = await this.exists();