So I'm storing data in SQLite now, which means I need to make sure I know what I send to the database.
Which is why I define objects that look like this:
const GamePasswordModel = {
tableName: "gamePasswords",
primaryKey: "id",
columns: {
id: Column.Integer,
password: Column.Text,
},
}
This allows me, for example, to compare two objects and decide if it needs to be updated in the DB (and figure out exactly what fields changed, and how to convert them to the right type for the DB).
It also allows me to compare the actual DB schema with the model, as a last-resort safety net:
But I also want to manipulate DB records as Javascript objects, so I also have these interfaces:
interface IGamePassword {
id: number;
password: string;
}
Which is very nice because when I have an instance of IGamePassword, I can use autocomplete (<Ctrl-Space>
in Visual Studio Code)
in my IDE to know what fields I can access:
The problem arises when those get out of sync. If GameModel
is missing a field, for example,
I'll think everything's fine, but it won't get saved to the DB (because every DB update is filtered
by model columns).
The ensureExtends
trick just makes sure that FooBarModel.columns
and IFooBar
have the exact
same fields, so I can't forget it on either side. It's not strictly required for the code to run,
but I love having the additional peace of mind :)
Hope that's helpful!