There are two object in our datamodel that refer to each other, on database level each "client" owns some "scopes" (HasManyRelation). And each "scope" has many "clients" that are allowed access (ManyToManyRelation). The objection models would look a bit like:
import {Model, ModelObject} from "objection";
import {OauthClientUriModel} from "./OauthClientUriModel";
import {OauthScopeModel} from "./OauthScopeModel";
export class OauthClientModel extends Model {
id!: number;
static get useLimitInFirst(): boolean {
return true;
}
static get tableName(): string {
return "oauth_client"
}
static get idColumn(): string {
return "id";
}
name!: string;
static get NameColumn(): string {
return "name";
}
_scopes!: ModelObject<OauthScopeModel>[]|null;
static get relationMappings() {
return {
_scopes: {
relation: Model.HasManyRelation,
modelClass: OauthScopeModel,
join: {
from: 'oauth_client.id',
to: 'oauth_scope.owner',
}
}
}
}
}
The line _scopes!: ModelObject<OauthScopeModel>[]|null; is there to give typescript information in the returned queries.
Now in the scopes object we need to be able to access all clients that are allowed to access it.
export class OauthScopeModel extends Model {
id!: number;
static get useLimitInFirst(): boolean {
return true;
}
static get tableName(): string {
return "oauth_scope"
}
static get idColumn(): string {
return "id";
}
name!: string;
static get nameColumn(): string {
return "name";
}
owner!: number;
static get ownerColumn(): string {
return "owner";
}
_allowed_clients!: ModelObject<OauthClientModel>|null;
static get relationMappings() {
return {
_allowed_clients: {
relation: Model.ManyToManyRelation,
modelClass: OauthClientModel,
join: {
from: 'oauth_scope.id',
through: {
from: 'oauth_scope_client.scope',
to: 'oauth_scope_client.client'
},
to: 'oauth_client.id',
}
},
}
}
}
This, however leads to an error with circular imports:
_allowed_clients is referenced directly or indirectly in its own type annotation at the statement
_allowed_clients!: ModelObject<OauthClientModel>[]|null;
How would I prevent this circular import while still maintaining strong type safety? (So not making one of the arrays have any type).