MukundM
Convex Community12mo ago
2 replies
Mukund

Autoincrement and unique fields in convex.

This is my drizzle schema:

const admins = createTable("admins", {
  id: int("id").autoincrement().primaryKey(),
  name: varchar("name", { length: 255 }).notNull().unique(),
  username: varchar("username", { length: 12 }).notNull(),
  email: varchar("email", { length: 255 }).notNull(),
  phone: varchar("phone", { length: 15 }).notNull(),
  passwordHash: varchar("password_hash", { length: 255 }).notNull(),

  ...created_updated_at,
}, (t)=>[
    uniqueIndex("username_idx").on(t.username),
    index("email_idx").on(t.email),
    index("phone_idx").on(t.phone),
  ],
);


I created a convex schema like this:
const admin = defineTable({
  name: v.string(),
  username: v.number(),
  email: v.string(),
  phone: v.string(),
  passwordHash: v.string(),
});

I want username to be something like this ADMIN00001, ADMIN00002, ADMIN00003, and so on.
Currently I have SQL DB and I am using AUTO_INCREMENT field by creating an id field and based on value of id updating the username.
How can I do that in convex.
Also, I didn't find any way to create unique fields (in my sql schema username is unique) how can I do same here.
Was this page helpful?