#Seed

4 messages · Page 1 of 1 (latest)

karmic tundra
#

write a function that inserts test users. Better Auth facilitates this with the admin plugin, which you can use with local install.

lost lakeBOT
#

Hi @karmic tundra. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To seed test users in a Convex project using Better Auth with the admin plugin (and local install), you should:

  1. Use Local Install: This gives you full control over your Better Auth schema and allows you to use plugins like the admin plugin. Follow the Local Install guide to set up your project accordingly.

  2. Write a Seeding Function: You can create a Convex mutation or internal mutation that inserts test users directly into your Better Auth component tables. With local install, you have direct access to these tables and can use the admin plugin to facilitate user creation.

Here’s a basic example of how you might write such a function (assuming you have the admin plugin enabled and your schema set up):

// convex/betterAuth/seedUsers.ts
import { internalMutation } from "../_generated/server";
import { v } from "convex/values";
import { admin } from "./adapter"; // Assuming admin plugin is exported here

export const seedTestUsers = internalMutation({
  args: {},
  handler: async (ctx) => {
    // Example: Insert 5 test users
    for (let i = 1; i <= 5; i++) {
      await admin.createUser(ctx, {
        email: `testuser${i}@example.com`,
        password: "testpassword",
        // Add any additional fields required by your schema
      });
    }
  },
});
  • This function uses the admin plugin’s createUser method to insert users.
  • You can run this function from the Convex CLI with npx convex run betterAuth/seedUsers:seedTestUsers.

Note:

  • The exact method to call the admin plugin’s user creation depends on your schema and how you’ve set up the admin plugin in your local install.
  • Make sure your admin plugin is properly configured and exported in your convex/betterAuth/adapter.ts or similar file.
#

For more details on local install and customizing your schema, see the Local Install documentation. If you need to add custom indexes or modify the schema, follow the schema generation and customization steps described there.

If you need to generate more realistic test data, consider using Faker.js in your mutation to create varied user profiles.

If you need more specific code for your setup, please provide details about your schema and admin plugin configuration.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: