sensedeep / dynamodb-onetable

DynamoDB access and management for one table designs with NodeJS
https://doc.onetable.io/
MIT License
682 stars 109 forks source link

Incomplete typescript instructions and inaccurate type spec #13

Closed juranki closed 3 years ago

juranki commented 3 years ago

First, I really like the concept of onetable! It makes working with single table designs a lot easier.

I wasn't able to make Model work with instructions in README, but had to add options parameter to the Model constructor

const EmailValidationModel = new Model<EmailValidation>(table, 'EmailValidation', {
    indexes: schema.indexes,
    timestamps: true,
    //@ts-ignore
    fields: schema.models.EmailValidation,
})

Also, typing suggest that options could include models, but constructor expects fields

mobsense commented 3 years ago

Thanks for your email and for the bug reports. I've pushed a fix to this repo.

You are right, the ModelConstructorOptions.models should be .fields.

You need to provide the options to specify the schema model fields if you are using the Model constructor. However, a more common pattern is to define all models in the table schema and then use getModel.

let EmailValidationModel: Model< EmailValidationModel > = table.getModel('EmailValidationModel')

This has the benefit of a centralized schema. We're also working on some promising design tools where a centralized schema is pretty key. So I'd recommend this design path.

If you define the fields via the Model constructor. Note: If you don't provide timestamps it defaults to your table.timestamps which defaults to true. If you don't provide indexes, then it will use the table indexes defined via the schema or the default indexes if not defined. The default indexes are:

    primary: {
        hash: 'pk',
        sort: 'sk',
    },