-
-
Notifications
You must be signed in to change notification settings - Fork 49
Disallow any additional properties to be passed to db.insert #170
Description
I understand that it might be difficult because of how typescript interfaces work. Anyway:
Assume the following schema:
create table authors(name varchar(255) primary key, stage_name varchar(255));
I might have my own Author type:
interface Author {
name: string
stageName?: string
}
Then, I might forget about mapping my type to Insertable and try to insert my type directly. The type system will allow it:
const authors = [{ name: 'Name', stageName: 'Stage name' }]
db.insert('authors', authors).run(pool)
The generated query will break:
INSERT INTO "authors" ("name", "stageName") VALUES ($1, $2) RETURNING to_jsonb("authors".*) AS result // column "stageName" of relation "authors" does not exist
Related to #169 - if the nullable property had to be specified, I'd get Property 'stage_name' is missing in type 'Author' but required in type 'Insertable'.. That could prevent the error, unless I decided to add that property by using spread operator. Using an object-literal in that case would be safe.
However, it could be nice if there was a check for that - maybe even in runtime - to see if all the columns actually exist and filter out the imaginary columns. That should work because a TS interface checks in all the required fields are there, and the runtime filter removes any excess fields.
Is this doable? I don't know if the query generator has access to the schema.