So, we indeed tried this. It’s in production now =)
Here is what our models look like:
@Table({ tableName: 'ChatMessages', schema: process.env.DATABASE_SCHEMA })
export default class ChatMessage extends Model {
@PrimaryKey
@Column(DataType.UUID)
Id: string
@CreatedAt
CreatedAt: Date
@UpdatedAt
UpdatedAt: Date
@Column
Body: string
@BelongsTo(() => User, 'UserId')
Author: User
@BelongsTo(() => Chatroom, 'ChatroomId')
Chatroom: Chatroom
}
This makes creating, updating and working with the models much, much easier now.
The main drawback is that many API endpoints for forest like record getters and smart field definitions still default to any
for types, without taking into account the actual model used.
For example:
import { collection } from 'forest-express-sequelize'
collection('User', { // magic string so we're not starting well there for any typing
actions: [],
fields: [
{
field: 'FullName',
type: 'String',
get: (user) => { // yep, user is any
return user.FirstName + ' ' + user.LastName // no type check
}
}
],
segments: []
})
Another from your TypeScript definitions:
export class RecordCreator<M extends Sequelize.Model> extends AbstractRecordTool<M> {
// ouch. Not using _creationAttributes means
// we don't get any help when creating a record
create(record: Record<string, unknown>): Promise<M>;
}
Of course we can still type by force:
collection('User', { // magic string
actions: [],
fields: [
{
field: 'FullName',
type: 'String',
get: (user: User) => { // user is a User
return user.FirstName + ' ' + user.LastName // type checked!
}
},
// ...
Which makes me think we could go even further, like so:
collection(User, { // BAM, pass the model constructor!
actions: [],
fields: [
{
field: 'FullName',
type: 'String',
get: (user) => { // user is instance of the model
return user.FirstName + ' ' + user.LastName // type checks for everyone
}
},
// ...
But it breaks native Forest project scaffolds by asking of developers that they export their models instead of blob-importing all of them.
We’ve actually been exporting all our models since switching the project to TypeScript and typescript-sequelize, so we’ve been tempted to write typed wrappers around Forest APIs…
Is there anything like this in your roadmap?