supabase-community / seed

Automatically seed your database with production-like dummy data based on your schema for local development and testing.
MIT License
381 stars 15 forks source link

feat(core): add global connect option #153

Closed avallete closed 4 months ago

avallete commented 4 months ago

Fixes S-2071

linear[bot] commented 4 months ago
S-2071 Better default for connect

Helping an user onboard, he ended up with a script looking like this: ``` /** * ! Executing this script will delete all data in your database and seed it with 10 versions. * ! Make sure to adjust the script to your needs. * Use any TypeScript runner to run this script, for example: `npx tsx seed.mts` * Learn more about the Seed Client by following our guide: https://docs.snaplet.dev/seed/getting-started */ import { createSeedClient } from "snaplet/seed"; const seed = await createSeedClient(); console.log("Seeding started ") // Truncate all tables in the database seed.$resetDatabase(); console.log("Database truncated") // Seed DysfunctionStatus const { DysfunctionStatus } = await seed.DysfunctionStatus( [ { code: "OPEN" }, { code: "CLOSED" }, { code: "IN_PROGRESS" }, { code: "PENDING" }, { code: "CANCELLED" }, { code: "RESOLVED" }, { code: "REJECTED" }, { code: "UNKNOWN" } ] ); // Seed SubscriptionStatus const { SubscriptionStatus } = await seed.SubscriptionStatus( [ { code: "ACTIVE" }, { code: "INACTIVE" }, { code: "PENDING" }, { code: "CANCELLED" }, { code: "SUSPENDED" }, { code: "UNKNOWN" } ] ); // Seed EligibilityStatus const { EligibilityStatus } = await seed.EligibilityStatus( [ { code: "ELIGIBLE" }, { code: "INELIGIBLE" }, { code: "PENDING" }, { code: "UNKNOWN" } ] ); // Seed CopperClosureStatus const { CopperClosureStatus } = await seed.CopperClosureStatus( [ { code: "CLOSED" }, { code: "OPEN" }, { code: "IN_PROGRESS" }, { code: "PENDING" }, { code: "CANCELLED" }, { code: "UNKNOWN" } ] ); // Seed ISP const { ISP } = await seed.ISP((x) => x(2)); // Seed Location const { Location } = await seed.Location((x) => x(5)); // Seed User const { User } = await seed.User((x) => x(5, { // each user has between 1 and 2 subscriptions Subscription: (x) => x({ min: 1, max: 2 }), // each user has between 1 and 2 eligibilities Eligibility: (x) => x({ min: 1, max: 2 }), })); // Seed Technology const { Technology } = await seed.Technology((x) => x(2)); // Seed Connection const { Connection } = await seed.Connection((x) => x(5, { // each connection has between 1 and 2 dysfunctions Dysfunction: (x) => x({ min: 1, max: 2 }), })); // Seed Dysfunction const {Dysfunction} = await seed.Dysfunction((x) => x(10, ({index}) => ({ connectionId: Connection[index].id, statusId: DysfunctionStatus[Math.floor(Math.random() * DysfunctionStatus.length)].code }) )); // Seed Eligibility const { Eligibility } = await seed.Eligibility((x) => x(10, ({index}) => ({ userId: User[index].id, statusId: EligibilityStatus[Math.floor(Math.random() * EligibilityStatus.length)].code }) )); // Seed CopperClosure const { CopperClosure } = await seed.CopperClosure((x) => x(10, ({index}) => ({ connectionId: Connection[index].id, statusId: CopperClosureStatus[Math.floor(Math.random() * CopperClosureStatus.length)].code }) )); console.log("Seeding completed") process.exit(); ``` As he wasn't aware of the "connect" option. Then, when refactoring this, I've ended up putting `connect: true` in every calls. I think there is two issues here: 1. The connect which is an important part, doesn't stand up in our docs. It's not in the quick-start, it's at the end of the "Relationships" and it's only really findable in the "API Reference" (which is quite deep to reach for for first try), so I think we have some work to do on the docs side here julien jian 2. I think the user shouldn't have to pass the `connect: true` everytime everywhere, as in most cases, you probably want this to be the default, and only opt-out when necessary. So I'm thinking about an API like: `const seed = await createSeedClient({ connect: true | false })` We would put it to "true" in the first generated `seed.ts` example, and this would drive the default for all the subsequent calls. We could also add a comment on top of this default, linking to the docs so the discoverability for this concept is higher.