Trying to generate a short id via a random string function

Hi everyone, In my schema I have:
id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
I now want to add a shortId field, which will use something like:
shortId: unique().notNull(),
shortId: unique().notNull(),
Which would use this:
export function generateRandomString(length: number) {
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';

for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}

return result;
}
export function generateRandomString(length: number) {
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';

for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
result += characters.charAt(randomIndex);
}

return result;
}
So that I can have a shortId string generated each time, much like the normal uuid for the id field.
13 Replies
Angelelz
Angelelz15mo ago
So what is your question?
cobite
cobiteOP15mo ago
Hey! I want the shortId to use the generateRandomString function to populate when a new item for that schema is generated on the database.
Angelelz
Angelelz15mo ago
Drizzle introduced the $default() method on the column definitions that accepts a javascript function that should return the value that you want to insert as default
Angelelz
Angelelz15mo ago
This is exactly what you're looking for
cobite
cobiteOP15mo ago
Thank you, I will try that now, I assume it's something along the lines of:
shortId: varchar('shortId').default(generateRandomString(7)).unique().notNull(),
shortId: varchar('shortId').default(generateRandomString(7)).unique().notNull(),
Angelelz
Angelelz15mo ago
Be careful, it's .$default not .default .default() only accepts SQL it's the default at database level
cobite
cobiteOP15mo ago
hmm when I try $default I get:
Property '$default' does not exist on type 'PgVarcharBuilderInitial<"shortId", [string, ...string[]]>'. Did you mean 'default'?
Property '$default' does not exist on type 'PgVarcharBuilderInitial<"shortId", [string, ...string[]]>'. Did you mean 'default'?
Angelelz
Angelelz15mo ago
You need the newer versions of drizzle
cobite
cobiteOP15mo ago
thanks! Going to try the following now:
shortId: varchar('shortId')
.$defaultFn(() => generateRandomString(7))
.unique()
.notNull(),
shortId: varchar('shortId')
.$defaultFn(() => generateRandomString(7))
.unique()
.notNull(),
Just a side question, since updating, the following is deprecated:
export type Post = InferModel<typeof posts>;
export type Post = InferModel<typeof posts>;
have tried export type Post = typeof posts;
have tried export type Post = typeof posts;
But it shows too much data (the whole PgTable):
Angelelz
Angelelz15mo ago
Take a look at the docs: https://orm.drizzle.team/docs/goodies
Goodies - DrizzleORM
Drizzle ORM | %s
cobite
cobiteOP15mo ago
I so have trouble with the search functionality on the documentation. I've searched for inferselectmodel and infermodel before I asked etc but nothing shows. Just wondering if the search isn't great, or if it's something on my side.
Angelelz
Angelelz15mo ago
You might be onto something. In this case it's kinda hard to get to that page. You have to know what you're looking for
Want results from more Discord servers?
Add your server