tcurdt
tcurdt
Explore posts from servers
DTDrizzle Team
Created by tcurdt on 5/29/2024 in #help
schema validation enforcement
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name').notNull(),
email: text('email').notNull(),
role: text('role', { enum: ['admin', 'user'] }).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
});
The above schema is nice and well - but how can I enforce that email actually only allows to store strings that are emails? So I looked at drizzle-zod but it looks like I would have to define the schema twice?
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.id.positive(),
email: (schema) => schema.email.email(),
role: z.string(),
});
const insertUserSchema = createInsertSchema(users, {
id: (schema) => schema.id.positive(),
email: (schema) => schema.email.email(),
role: z.string(),
});
There is no way to attach this to the table schema itself? What about using postgres constraints? https://www.postgresql.org/docs/current/ddl-constraints.html
1 replies
DTDrizzle Team
Created by tcurdt on 7/19/2023 in #help
accessing results of a select
I am confused. Based on this docs https://orm.drizzle.team/docs/crud this here
const todos = await db
.select({
id: todosTable.id,
completed: todosTable.completed,
description: todosTable.description,
title: todosTable.title,
})
.from(todosTable);
console.log(todos);
const todos = await db
.select({
id: todosTable.id,
completed: todosTable.completed,
description: todosTable.description,
title: todosTable.title,
})
.from(todosTable);
console.log(todos);
todos should be an array of objects. But it seems to be
SQLiteSelect {
_: {
selectedFields: {
id: [SQLiteInteger],
completed: [SQLiteInteger],
description: [SQLiteText],
title: [SQLiteText]
}
},
config: {
...
SQLiteSelect {
_: {
selectedFields: {
id: [SQLiteInteger],
completed: [SQLiteInteger],
description: [SQLiteText],
title: [SQLiteText]
}
},
config: {
...
What am I missing?
9 replies