Connecting Data Pools
Why Connect Data Pools?
There are situations where you might want to use data already in your database to add new relationships or enrich existing data. @snaplet/seed
offers a 'connect' option that makes it easy to integrate new data creation with existing database entries.
Practical Example with Prisma
Suppose you have a database where "files" and "users" are already seeded and you don't want to recreate these, but you do want to link them with new data generated by @snaplet/seed
.
import { PrismaClient } from "@prisma/client";import { createSeedClient } from "@snaplet/seed";const prisma = new PrismaClient();const users = await prisma.user.findMany({ select: { id: true } });const files = await prisma.file.findMany({ select: { id: true } });const seed = await createSeedClient();// Resetting specific tables while preserving users and filesawait seed.$resetDatabase(["!*_prisma_migrations", "!*user", "!*file"]);// Seeding new posts and linking them to existing users and filesawait seed.posts(x => x(10, { Attachments: [{}, {}, {}] }), { connect: { users, files } });
Leveraging Third-Party User Pools with Supabase and Snaplet
Using third-party services like Supabase can simplify user management by handling complex functionalities such as password hashing and email verification. This allows you to focus on your application’s core features.
Why Use Third-Party Services?
Third-party services provide robust, tested implementations of common functionalities, which can save time and reduce errors in your application development.
Implementation with Supabase
Here's how you can seed users into Supabase, enrich their data, and link it to additional content using Snaplet.
import { createSeedClient } from "@snaplet/seed";import { createClient } from '@supabase/supabase-js';const supabase = createClient( process.env.NEXT_PUBLIC_SUPABASE_URL, process.env.NEXT_PUBLIC_SUPABASE_ROLE_KEY);const PASSWORD = "testuser";const seed = await createSeedClient();// Reset our database to start from a clean stateawait seed.$resetDatabase();// Creating a pool of users with common attributesfor (let i = 0; i < 5; i++) {await supabase.auth.signUp({ email: `user${i}@example.com`, password: PASSWORD, options: { data: { user_name: `user${i}` } }});}// Retrieving profiles for linking with new dataconst { data: profiles } = await supabase.from("profiles").select("id");// Linking new posts to these profilesawait seed.posts(x => x(10), { connect: { profiles } });
Key Concepts
- Third-Party Client: Use third-party clients like Supabase for complex data operations, ensuring compatibility and security.
- Snaplet Connect: Utilize Snaplet's API to seamlessly link seeded data with external data pools, optimizing your data management and integration strategies.