seed

I'm trying to create a seed script, but I haven't been able to do it and I get an err that I don't see any sense in, could you help me?
No description
7 Replies
edarcode
edarcodeOP5mo ago
schema
No description
edarcode
edarcodeOP5mo ago
I managed to solve it by sending the uuid, but I don't understand why it fails with the default
routinelover
routinelover5mo ago
Guessing here (I'm not going to set this up to run it) but it looks like you're assigning the same id to every user at runtime: the .default(crypto.randomUUID()) runs once, and sets a single UUID as the default That's why you're seeing the "unique constraint failed" error Hope that helps!
edarcode
edarcodeOP5mo ago
But isn't the schema supposed to indicate the template for each insert? Why is the default executed only once? For example, the default in the role or createdAt must work for each instance that I create of my table or does it not work that way?
export const usersTable = sqliteTable("users", {
id: text("id", { length: 36 }).primaryKey().default(crypto.randomUUID()),
role: text("role", { enum: [ROLE.chief, ROLE.admin, ROLE.client] }).default(
ROLE.client
),
username: text("username").unique().notNull(),
email: text("email").unique().notNull(),
password: text("password").notNull(),
img: text("img"),
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`),
updateAt: text("updated_at").$onUpdate(() => sql`(CURRENT_TIMESTAMP)`),
});
export const usersTable = sqliteTable("users", {
id: text("id", { length: 36 }).primaryKey().default(crypto.randomUUID()),
role: text("role", { enum: [ROLE.chief, ROLE.admin, ROLE.client] }).default(
ROLE.client
),
username: text("username").unique().notNull(),
email: text("email").unique().notNull(),
password: text("password").notNull(),
img: text("img"),
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`),
updateAt: text("updated_at").$onUpdate(() => sql`(CURRENT_TIMESTAMP)`),
});
import "../utils/dotenv";
import { BCRYPT } from "../constants/bcrypt";
import { db } from "./db";
import { ROLE, usersTable } from "./schema";
import bcrypt from "bcrypt";

const EDARCODE = {
id: crypto.randomUUID(),
username: process.env.CHIEF_USERNAME as string,
email: process.env.CHIEF_EMAIL as string,
password: process.env.CHIEF_PASSWORD as string,
role: ROLE.chief,
};

const LORE = {
id: crypto.randomUUID(),
username: "lore",
password: "123456",
role: ROLE.client,
};

const seedUsers = async () => {
console.log("seed");
EDARCODE.password = await bcrypt.hash(EDARCODE.password, BCRYPT.salt);
LORE.password = await bcrypt.hash(LORE.password, BCRYPT.salt);

await db.insert(usersTable).values([EDARCODE, LORE]);
};

seedUsers().catch(console.error);
import "../utils/dotenv";
import { BCRYPT } from "../constants/bcrypt";
import { db } from "./db";
import { ROLE, usersTable } from "./schema";
import bcrypt from "bcrypt";

const EDARCODE = {
id: crypto.randomUUID(),
username: process.env.CHIEF_USERNAME as string,
email: process.env.CHIEF_EMAIL as string,
password: process.env.CHIEF_PASSWORD as string,
role: ROLE.chief,
};

const LORE = {
id: crypto.randomUUID(),
username: "lore",
password: "123456",
role: ROLE.client,
};

const seedUsers = async () => {
console.log("seed");
EDARCODE.password = await bcrypt.hash(EDARCODE.password, BCRYPT.salt);
LORE.password = await bcrypt.hash(LORE.password, BCRYPT.salt);

await db.insert(usersTable).values([EDARCODE, LORE]);
};

seedUsers().catch(console.error);
routinelover
routinelover5mo ago
Yeah: the way you've written your code though doesn't work the way you think. id: text("id", { length: 36 }).primaryKey().default(crypto.randomUUID()) is equivalent to id: text("id", { length: 36 }).primaryKey().default("ba70af14-4cbc-4cbb-97b4-21d5c5c31b5c") . You're looking for something like id: text("id", { length: 36 }).primaryKey().$defaultFn(() => crypto.randomUUID())
edarcode
edarcodeOP5mo ago
mm i understand. so this is bad too
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`)
createdAt: text("created_at").default(sql`(CURRENT_TIMESTAMP)`)
but it seems very strange to me, so what is default for? thanks. It's working as expected now, but I'm a little confused.
rodrigocezar
rodrigocezar2mo ago
for actual default values like in sql, like a config that should default to false if not provided
Want results from more Discord servers?
Add your server