I need to explicitly set database table name on a Loopback 4 model. I do that using @model
decorator as shown below:
@model({settings: {postgresql: {schema: 'public', table: 'dbtablename'}}})
class MyModel extends Entity {
...
In simple case, it works fine. However I need to now extend the model using an AuditMixin
that adds timestamp property to the model. This takes in the model declared above and returns an extented model.
Here is the code for the mixin:
import {MixinTarget} from '@loopback/core';
import {property} from '@loopback/repository';
export function AuditMixin<T extends MixinTarget<object>>(baseClass: T) {
class Mixin extends baseClass {
timestamp: Date;
constructor(...args: any[]) {
super(args);
this.timestamp= new Date();
}
}
return Mixin;
}
Mixin usage:
class MyModelWithMixin extends AuditMixin(MyModel) {}
The issue:
Loopback doesn't seem to respect the table name I provide in the decorator. In my database logs I can see it tries to access table public.mymodelwithmixin
. Database log line is:
ERROR: relation "public.mymodelwithmixin" does not exist at character xyz
The solution turned out to be quite simple: I needed to decorate where I used the mixin.
@model({
settings: {postgresql: {schema: 'public', table: 'dbtablename'}},
})
class MyModelWithMixin extends AuditMixin(MyModel) {}