Decide best practice for user ID in DB schema
#163Drizzle adder uses an integer. Lucia uses a string
It would be nice to be consistent both to promote best practices and to avoid users having to migrate their DB if they setup drizzle first and then come back later and setup lucia
Some people don't like ints because it reveals how many users they have
The Lucia integration is done app side. I'm not sure if that has downsides and would be better to be done in the database? It does use an extra dependency currently, but maybe there's a Node built-in we could leverage if we do want to do it app side?
Lucia v3 used generateIdFromEntropySize()
function to generate user IDs.
Looking at Lucia codebase, this is what the function looked like:
import { encodeBase32LowerCaseNoPadding } from '@oslojs/encoding';
export function generateIdFromEntropySize(size: number): string {
const buffer = crypto.getRandomValues(new Uint8Array(size));
return encodeBase32LowerCaseNoPadding(buffer);
}
https://github.com/lucia-auth/lucia/blob/v3/packages/lucia/src/crypto.ts
Perhaps do the following:
import { encodeBase32LowerCaseNoPadding } from '@oslojs/encoding';
function generateIdFromEntropySize(size: number): string {
const buffer = crypto.getRandomValues(new Uint8Array(size));
return encodeBase32LowerCaseNoPadding(buffer);
}
export const user = sqliteTable('user', {
id: text('id')
.primaryKey()
.$defaultFn(() => generateIdFromEntropySize(10)),
age: integer('age'),
username: text('username').notNull().unique(),
passwordHash: text('password_hash').notNull()
});
This approach also doesn't require new dependencies since Lucia V4 approach already depends on Oslo.
Ideally, generateIdFromEntropySize()
would be a helper function outside of schema file.
Oslo docs: https://oslo.js.org/reference/encoding/Base32Encoding/
this was updated a bit in #254
Why not just use UUID for the user ID?