Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't use a model base class #601

Open
rbrcurtis opened this issue Aug 17, 2022 · 3 comments
Open

can't use a model base class #601

rbrcurtis opened this issue Aug 17, 2022 · 3 comments

Comments

@rbrcurtis
Copy link

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.

I can use mixins as a workaround.

@rbrcurtis
Copy link
Author

Here's an example project to reproduce easily: https://github.com/rbrcurtis/dyngoose-subclass-problem

@benhutchins
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@benhutchins @rbrcurtis and others