A way to make a custom type generatedAlwaysAsIdentity

I've tried the following ways to achive this, but none of them work.
id: customId("id").primaryKey().generatedAlwaysAs(sql`IDENTITY`)
id: customId("id").primaryKey().generatedAlwaysAs(sql`IDENTITY`)
dataType() {
return "INTEGER GENERATED ALWAYS AS IDENTITY";
},
dataType() {
return "INTEGER GENERATED ALWAYS AS IDENTITY";
},
10 Replies
icarus
icarusOP2mo ago
all issues related to this on github were closed with the release of generatedAlwaysAs and generatedAlwaysAsIdentity, but none of those two actually fix this issue as you can't do something like:
id: customId("id").primaryKey().generatedAlwaysAsIdentity()
id: customId("id").primaryKey().generatedAlwaysAsIdentity()
Angelelz
Angelelz2mo ago
This is quite literally what the docs say you can do
Angelelz
Angelelz2mo ago
Drizzle ORM - PostgreSQL column types
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Angelelz
Angelelz2mo ago
No need for a custom type
icarus
icarusOP2mo ago
i do need to use a custom type. to do some custom encoding/decoding to the integer coming from/going to the database think something like the following, for the sake of simplicity
export const customId = customType<{
data: string;
driverData: number;
notNull: true;
default: true;
}>({
dataType() {
return "INTEGER GENERATED ALWAYS AS IDENTITY";
},
fromDriver(value: number) {
return value.toString();
},
toDriver(value: string) {
return Number(value);
},
});
export const customId = customType<{
data: string;
driverData: number;
notNull: true;
default: true;
}>({
dataType() {
return "INTEGER GENERATED ALWAYS AS IDENTITY";
},
fromDriver(value: number) {
return value.toString();
},
toDriver(value: string) {
return Number(value);
},
});
Angelelz
Angelelz2mo ago
There’s a trick that’s nowhere in the docs Can you put this in drizzle-run so I can show you?
Angelelz
Angelelz2mo ago
The only problem with this approach is that it's not reusable, you need to assign those methods to each column where you want to use it Wait, that might not work for you anyway. When I did something like this a long time ago, I wasn't changing the type, just performing some logic. This will show as number at compile time but it will be a string a run time So, don't use it
icarus
icarusOP2mo ago
yeah I wish there was a simple .transform function available on all types similar to zod's but thank you for the attempt anyway
Want results from more Discord servers?
Add your server