-
Notifications
You must be signed in to change notification settings - Fork 25
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
A concise type for a table? #165
Comments
Do you mean a type for a row from that table, or the user table in the db instance? Regarding the type of the table itself, I generally pass the whole Db instance around and use it in some sort of repository where I then access those tables. |
Whoops, I meant a row from the table. |
Check. There is not a clean way to get the resulting row from a specific table. There is some internal tooling to get the type from a query though. Conceptually I think this is closer to what is actually happening: queries are returning rows and they may or may not match all the columns of a table, right? What I sometimes do if I want the row from a specific query in a type, and I keep this type in the same module, I put the query in a function and call As a helper, Mammoth could return a type e.g. |
I want a Currently I'm defining The
|
That's great. As long as you have it map from the a Mammoth return value somewhere it's at least going to break if it's diverging. Because it's a little annoying to manually type the table in your own type again, I added For example, the get the type of a row from table foo you can do this: export type Foo = TableRow<typeof foo>;
export const foo = defineTable({
id: uuid().primaryKey().default(`gen_random_uuid()`),
createDate: timestampWithTimeZone().notNull().default(`now()`),
name: text().notNull(),
value: integer(),
}); |
Also, in the next version you'll be able to use the db.select(db.user.star()).from(db.user); which will produce the below SQL SELECT user.* from user and automatically give you the whole type as a result. |
Tangent about |
You are right. This creates a possible collision. Perhaps |
Since
That's not so bad. Also, there's the full |
A change to this just hit master. There may be some edge cases left to support but a straightforward |
Let's say I have a DB with a
users
table and the appropriate MammothdefineTable
anddefineDb
calls.How can I get a concise type for that table, e.g. for use in a function signature:
The text was updated successfully, but these errors were encountered: