Trying to write next-auth adapter

blahblahblahrober AKA @Dan Kochetov helped me get most of the way to having a next-auth adapter done but, in the couple weeks I waited for a review, I came back and now the SQLite migration is betraying me. See video. Starting from a clean slate, it's saying nothing to migrate and, when I run tests against my db.sqlite, I get back errors for no tables, kiddo. Pretty confused; seems like I'm not pushing any schema somehow? Pull request if you want to look at code: https://github.com/nextauthjs/next-auth/pull/7165 I'm working in ./packages/adapter-drizzle-sqlite
GitHub
feat(adapters): add Drizzle adapter by anthonyshew Β· Pull Request #...
β˜•οΈ Reasoning Introducing a drizzle-orm adapter! With drizzle's increasing popularity, it's no surprise that there's a discussion for an adapter. This PR adds it. 🧒 Checklist Documenta...
41 Replies
Andrii Sherman
Andrii Shermanβ€’2y ago
Wrong script used Need to have generate:sqlite And you have generate:mysql But will improve warning for this case It seems to be unclear at all what’s happening
anthonyshew
anthonyshewOPβ€’2y ago
Oh, it still fails when it's right, generate:sqlte My b, I was doing science and forgot to switch that back in the video.
Andrii Sherman
Andrii Shermanβ€’2y ago
so you are using pnpm build? I'll clone your PR and will try to reproduce
anthonyshew
anthonyshewOPβ€’2y ago
Anything that invokes the generator will do it. pnpm generate-schema will have the same behavior.
Andrii Sherman
Andrii Shermanβ€’2y ago
Looking at your PR generate-schema has generate:mysql script So I guess the only right script is pnpm build
Andrii Sherman
Andrii Shermanβ€’2y ago
or dev
anthonyshew
anthonyshewOPβ€’2y ago
I haven't pushed the correction yet.
Andrii Sherman
Andrii Shermanβ€’2y ago
got it I'll ping you back after checking it locally
anthonyshew
anthonyshewOPβ€’2y ago
Gracias, papa. Just pushed a cleanup some of my science.
Andrii Sherman
Andrii Shermanβ€’2y ago
Andrii Sherman
Andrii Shermanβ€’2y ago
so I made a fork and run pnpm generate-schema if you are open for a call we can debug it on your end but it seems to work
Andrii Sherman
Andrii Shermanβ€’2y ago
anthonyshew
anthonyshewOPβ€’2y ago
Bless ittttttttt. That's just the worst outcome haha. Thanks for taking the time, legend. I'll go figure my life out.
Andrii Sherman
Andrii Shermanβ€’2y ago
I can send you my laptop
Dan
Danβ€’2y ago
And that's how Docker was born
anthonyshew
anthonyshewOPβ€’2y ago
wut kind is it
Liltripple_reid
Liltripple_reidβ€’2y ago
this needs more attention, I really wouldn't want to pay 100 bucks for 5000 users on Clerk 😭
anthonyshew
anthonyshewOPβ€’2y ago
How do I let Drizzle auto-assign a uuid? Or do I need to handle it in my code?
Dan
Danβ€’2y ago
if it cannot be done via SQL, you can't for now but we have a ticket for that ...ok, maybe we don't let me create it
anthonyshew
anthonyshewOPβ€’2y ago
Gotcha, thanks. One last one and then I think I'm going to be home free: I'm trying to give the maintainers the option to have a drizzle-adapter-all if they want. Essentially, an adapter that will accept any of the SQL flavors. But I'm getting this. Is this me being dumb or will is drizzle freaking out because I have multiple clients (client flavors?) in one workspace?
tacomanator
tacomanatorβ€’2y ago
Would love to have this! for now I am always passing an empty id when inserting into a table like this:
const cuid2 = customType<{ data: string; notNull: true }>({
dataType() {
return "text";
},
toDriver(value): string {
return value ? value : createId();
},
});

const User = sqliteTable(
"User",
{ id: cuid2("id").primaryKey() }
);

// adapter
async createUser(user) {
return db
.insert(User)
.values({ ...user, id: "" })
.returning()
.get();
},
const cuid2 = customType<{ data: string; notNull: true }>({
dataType() {
return "text";
},
toDriver(value): string {
return value ? value : createId();
},
});

const User = sqliteTable(
"User",
{ id: cuid2("id").primaryKey() }
);

// adapter
async createUser(user) {
return db
.insert(User)
.values({ ...user, id: "" })
.returning()
.get();
},
anthonyshew
anthonyshewOPβ€’2y ago
Wow, fancy. I just did this. Is this naive? Using uuidv4 from the uuid npm package.
tacomanator
tacomanatorβ€’2y ago
That works too. I needed something reusable and automatic for use with multiple models.
Andrii Sherman
Andrii Shermanβ€’2y ago
I can suggest a bit of improvement here. May work
// notNull in customType will be left false, because we always are
// generating it in toDriver
const cuid2 = customType<{ data: string; notNull: false }>({
dataType() {
return "text";
},
toDriver(value): string {
return typeof value === 'undefined' ? createId() : value;
},
});

const User = sqliteTable("User", {
id: cuid2("id"),
name: text("name").notNull(),
}, (t) => ({
// need to define primary key here, so migration will create PK constraint
// but type will be left nullable
f: primaryKey(t.id)
}));

async createUser(user) {
// id will be optional
return db.insert(User).values(...user).returning().get();
};
// notNull in customType will be left false, because we always are
// generating it in toDriver
const cuid2 = customType<{ data: string; notNull: false }>({
dataType() {
return "text";
},
toDriver(value): string {
return typeof value === 'undefined' ? createId() : value;
},
});

const User = sqliteTable("User", {
id: cuid2("id"),
name: text("name").notNull(),
}, (t) => ({
// need to define primary key here, so migration will create PK constraint
// but type will be left nullable
f: primaryKey(t.id)
}));

async createUser(user) {
// id will be optional
return db.insert(User).values(...user).returning().get();
};
so no need for id: "" on insert I didn't test a real query, but it should work
tacomanator
tacomanatorβ€’2y ago
wouldn't end up being a nullable column though?
Andrii Sherman
Andrii Shermanβ€’2y ago
only in types. As long as id will be always generated by customType it means for insert function it can be nullable but for migration kit will use primary key for this field which is notNull on database layer
tacomanator
tacomanatorβ€’2y ago
ah, I see, let me try it then!
Andrii Sherman
Andrii Shermanβ€’2y ago
and tbh, I would never think of using customType for this case πŸ˜… it was smart
tacomanator
tacomanatorβ€’2y ago
I don't know it was the first thing I grabbed for when I figured out that program generated default values were not yet supported ✌️ tried it, but one downside seems to be that all of the places in my code where I took for granted that id is not null now seem to think it could be null 🫒
Andrii Sherman
Andrii Shermanβ€’2y ago
Maybe try this
const cuid2 = customType<{ data: string; notNull: true, default: true }>({
const cuid2 = customType<{ data: string; notNull: true, default: true }>({
make it back notNull but default true and then you can put primary key back
const User = sqliteTable("User", {
id: cuid2("id").primaryKey(),
name: text("name").notNull(),
});
const User = sqliteTable("User", {
id: cuid2("id").primaryKey(),
name: text("name").notNull(),
});
it will still work and default: true sounds much better
tacomanator
tacomanatorβ€’2y ago
I think this is essentially what I tried in the past but couldn't get it to work, but let me try again! Yeah, for some reason when I do it this way, it never seems to get to the toDriver method of the custom type It seems like default: true causes it to bypass toDriver when the value is undefined, assuming that the database will take care of it?
Andrii Sherman
Andrii Shermanβ€’2y ago
I'll check a code now it shouldn't because those are just types yeah, you are right if value is undefined orm will bypass toDriver, because already know, that the value is null/db default but I don't think it should work this way toDriver should always intercept insert values
Andrii Sherman
Andrii Shermanβ€’2y ago
It would be nice to have it, so you don't need to create customType it would be nice to make it both work as expected πŸ˜… will do that thanks for you investigations in this part. Was super helpful!
tacomanator
tacomanatorβ€’2y ago
anytime thank you too!
Dan
Danβ€’2y ago
uhhh this is an error text I'm seeing for the first time in my life will need to investigate
Dan
Danβ€’2y ago
Did you try things from this thread? https://github.com/microsoft/TypeScript/issues/42873
GitHub
The inferred type of "X" cannot be named without a reference to "Y"...
Bug Report πŸ”Ž Search Terms inferred type cannot be named, symlink node_modules πŸ•— Version & Regression Information I'm verifying the problem on the [email protected]. I've not tried older ...
anthonyshew
anthonyshewOPβ€’2y ago
Yeah, I definitely made a spooky one, huh. πŸ™ƒ i tried a couple different things to no avail. I eventually decided to just drop the β€œall” adapter since it barely makes sense to have anyway. Just pick the right flavor, duh.
Andrii Sherman
Andrii Shermanβ€’2y ago
Today I've learned a new word: "avail" thanks @anthonyshew yesterday it was "plethora"
NeonCop
NeonCopβ€’2y ago
this is awesome work!!
Want results from more Discord servers?
Add your server