cobite
DTDrizzle Team
•Created by cobite on 9/25/2023 in #help
Omit not working for me
I am trying to use Omit to return the object with the id field removed:
However when returned it still shows the id on the objects. I could add all the fields I want directly in the select() here but that seems like a lot of work.
4 replies
DTDrizzle Team
•Created by cobite on 9/24/2023 in #help
Making a column only allow letters and characters, including capitals
I am using Yup for my validation on the client side. For example the username field does this:
My user schema on the drizzle side is:
Just wondering is there a way to add the regex to it? Thanks
4 replies
DTDrizzle Team
•Created by cobite on 9/24/2023 in #help
Trying to generate a short id via a random string function
Hi everyone,
In my schema I have:
I now want to add a shortId field, which will use something like:
Which would use this:
So that I can have a shortId string generated each time, much like the normal uuid for the id field.
18 replies
DTDrizzle Team
•Created by cobite on 9/1/2023 in #help
Transforming SELECT from case to camelCase via sql with execute
I am getting some values for example
I wish to transform this with the AS keyword:
The issue is that it just returns as:
It does not follow the camelCase that was defined, it just goes lower case for some reason.
6 replies
DTDrizzle Team
•Created by cobite on 8/28/2023 in #help
How to get the count of records? Here is my normal sql, trying to translate into drizzle
100 replies
DTDrizzle Team
•Created by cobite on 8/28/2023 in #help
How to add to the database where the table has a many relationship to another table
I have a users table, which has a many relationship to to a posts table.
I want to add a new user to the table, which I can do, but how do I go about adding new posts to that user? Does it add a full post object to the user? Or just an id to the new post which will be in the post table?
export const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
username: varchar('username', { length: 512 }).unique().notNull(),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
5 replies
DTDrizzle Team
•Created by cobite on 8/26/2023 in #help
How to do this query in one step instead of two?
Is there a better way to do this than having two different queries?
// gets all posts for a specific user with only a users name passed in.
export default async function getAllPosts(
userName: string
): Promise<Post[]> {
const userResult = await queryDB
.select({ id: users.id })
.from(users)
.where(eq(users.name, userName))
.limit(1);
const results = await queryDB
.select()
.from(posts)
.where(eq(posts.userId, userResult[0].id))
return results;
}
3 replies
DTDrizzle Team
•Created by cobite on 8/25/2023 in #help
How to query from a many to many relationship?
Hi all,
I am trying to get all the posts by a user.
My schema has a many-to-many relationship set up for posts and users.
export const posts = pgTable('posts', {
id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
});
export const postsRelations = relations(posts, ({ many }) => ({
users: many(users),
}));
export type Post = InferModel<typeof posts>;
And then:
export const users = pgTable('users', {
id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
export type User = InferModel<typeof users>;
Now in my file I want to
export default async function getAllUsersPosts(userId: string): Promise<Post[]> {
const results = await queryDB.select().from(posts).where(eq(posts.users.id, userId));
return results;
}
Error is:
Property 'users' does not exist on type 'PgTableWithColumns<{ name: "posts"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "posts"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }
2 replies
DTDrizzle Team
•Created by cobite on 8/25/2023 in #help
optional parent child relationship on same table
Hi all
I have a table called 'comments'
Some comments will have a parent comment, while others will not.
The error I am getting is:
'comments' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.
The code:
export const comments = pgTable('comments', {
id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
parentId: uuid('parent_id').references(() => comments.id),
export const commentsRelations = relations(comments, ({ one }) => ({
parent: one(comments, {
fields: [comments.parentId],
references: [comments.id],
}),
}));
export type Comment = InferModel<typeof comments>;
I am using: pg-core1 replies