textYash
textYash
Explore posts from servers
DTDrizzle Team
Created by textYash on 4/9/2024 in #help
How to change schema without losing data?
I want to change my schema & add a new field hidden: boolean("hidden").notNull().default(false), My current schema looks like:
id: serial('id').primaryKey(),
slug: varchar('slug', { length: 64 }).unique().notNull(),
title: varchar('title', { length: 64 }).unique().notNull(),
content: text('content').notNull(),
createdAt: timestamp('created_at').defaultNow(),
lastEdit: timestamp('last_edit').defaultNow(),
readTime: smallint('read_time').notNull()
id: serial('id').primaryKey(),
slug: varchar('slug', { length: 64 }).unique().notNull(),
title: varchar('title', { length: 64 }).unique().notNull(),
content: text('content').notNull(),
createdAt: timestamp('created_at').defaultNow(),
lastEdit: timestamp('last_edit').defaultNow(),
readTime: smallint('read_time').notNull()
Now whenever I edit my schema the data from my supabase(postgres) database gets deleted. How can I change the schema while also preserving all the rows in my database??
4 replies
DTDrizzle Team
Created by textYash on 4/1/2024 in #help
How to search the docs for the correct datatype returned by each function?
Since I'm using typescript, quite often I am welcomed by eslint errors. For eample:
try {
await db.insert(posts).values({ slug, title, content, readTime });
} catch (error) {
return fail(401, { title, content, error: error.message });
}
try {
await db.insert(posts).values({ slug, title, content, readTime });
} catch (error) {
return fail(401, { title, content, error: error.message });
}
Here, how can I specify the datatype for error? The documentation does not specify any types. The documentation only shows examples.
4 replies
DTDrizzle Team
Created by textYash on 3/25/2024 in #help
How to format date?
I tried:
const allPosts = db
.select({
...getTableColumns(posts),
formatted: sql<string>`to_char(created_at, 'Mon DD, YYYY')`,
})
.from(posts);
const allPosts = db
.select({
...getTableColumns(posts),
formatted: sql<string>`to_char(created_at, 'Mon DD, YYYY')`,
})
.from(posts);
but it does not return an array of objects. How do I get an array of objects from the above query? Similar to what this statement returns:
const allPosts = await db.select().from(posts); // returns {title:string...}[]
const allPosts = await db.select().from(posts); // returns {title:string...}[]
12 replies
DTDrizzle Team
Created by textYash on 3/24/2024 in #help
How to check if an entry already exist?
I found this query to check if an entry in the table already exists:
await db.execute(
sql`select exists(select 1 from ${posts} where ${posts.slug} = ${slug})`,
)
await db.execute(
sql`select exists(select 1 from ${posts} where ${posts.slug} = ${slug})`,
)
Which returns the response: Result(1) [ { exists: false } ] but how do I use it in a typescript if statement to run conditional code?
10 replies
DTDrizzle Team
Created by textYash on 3/21/2024 in #help
How to write a good username schema?
I am creating a users table with fields, id, username, email & password. Why do I need to use the id field? Why can't I just use username as the primary key? Would there be any drawbacks to that? If I use primaryKey() then do I need unique().notNull()?
6 replies
DTDrizzle Team
Created by textYash on 3/19/2024 in #help
How to check schema fields against a regex?
I want to restrict the usernames in my user table with a regex. Basically instagram like criteria for usernames. This is my current schema:
username: varchar('username', { length: 128 }).unique().notNull(),
username: varchar('username', { length: 128 }).unique().notNull(),
Is there any method that I can call on this to restrict the username? Something like this:
username: varchar('username', { length: 64 }).unique().notNull()./^(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,64}$/igm.test(email),
username: varchar('username', { length: 64 }).unique().notNull()./^(?!.*\.\.)(?!.*\.$)[^\W][\w.]{0,64}$/igm.test(email),
I know that's nonsense code above, but I'm just trying to convey myself. please don't mind 😅
3 replies