You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I create a Model base class with some shared functionality, then sub-class all my table models from that, I get SchemaError(Table ${this.name} has several attributes named ${attribute.name})
For example, if I have these three files:
import { Dyngoose } from 'dyngoose'
export abstract class Model extends Dyngoose.Table {
@Dyngoose.Attribute.Date({ nowOnCreate: true })
createDate: Date
@Dyngoose.Attribute.Date({ nowOnUpdate: true })
updateDate: Date
}
import { Dyngoose } from 'dyngoose'
import { Model } from 'Model'
import { Organization } from 'Organization'
import { v4 } from 'uuid'
@Dyngoose.$Table({ name: 'user', billingMode: 'PAY_PER_REQUEST' })
export class User extends Model {
@Dyngoose.$PrimaryKey('id')
static readonly idIndex: Dyngoose.Query.PrimaryKey<User, string, null>
@Dyngoose.Attribute.String({ default: () => v4() })
id: string
@Dyngoose.Attribute.String()
email: string
@Dyngoose.Attribute.String()
orgId: string
@Dyngoose.Attribute.String()
name: string
organization: Organization
async getOrganization(): Promise<Organization> {
return Organization.idIndex.get({ id: this.orgId })
}
}
import { Dyngoose } from 'dyngoose'
import { Model } from 'Model'
import { v4 } from 'uuid'
@Dyngoose.$Table({ name: 'user', billingMode: 'PAY_PER_REQUEST' })
export class Organization extends Model {
@Dyngoose.$PrimaryKey('id')
static readonly idIndex: Dyngoose.Query.PrimaryKey<Organization, string, null>
@Dyngoose.Attribute.String({ default: () => v4() })
id: string
@Dyngoose.Attribute.String()
name: string
}
When I run the app, I will get the following error:
/Users/ryan/Code/dyngoose-subclass-problem/node_modules/dyngoose/dist/tables/schema.js:173
throw new errors_1.SchemaError(`Table ${this.name} has several attributes named ${attribute.name}`);
^
SchemaError: Table user has several attributes named id
at Schema.addAttribute (/Users/ryan/Code/dyngoose-subclass-problem/node_modules/dyngoose/dist/tables/schema.js:173:19)
at StringAttributeType.decorate (/Users/ryan/Code/dyngoose-subclass-problem/node_modules/dyngoose/dist/tables/attribute-type.js:24:21)
at define (/Users/ryan/Code/dyngoose-subclass-problem/node_modules/dyngoose/dist/decorator/attribute-types/index.js:29:19)
at __decorate (/Users/ryan/Code/dyngoose-subclass-problem/dist/User.js:5:110)
at Object.<anonymous> (/Users/ryan/Code/dyngoose-subclass-problem/dist/User.js:22:1)
It seems to be that the @Dyngoose.$Table() decorator can't handle subclassing.
Because the way decorators work I don’t know of any easy way to fix this. TypeScript 5 supports decorators that provides context, so it might work, but I would need to wait until more libraries migrate to the new decorators.
I’ll look into this more when building out single table design support further.
If I create a
Model
base class with some shared functionality, then sub-class all my table models from that, I getSchemaError(
Table ${this.name} has several attributes named ${attribute.name})
For example, if I have these three files:
When I run the app, I will get the following error:
It seems to be that the @Dyngoose.$Table() decorator can't handle subclassing.
I can use mixins as a workaround.
The text was updated successfully, but these errors were encountered: