mirror of
https://github.com/wasp-lang/open-saas.git
synced 2025-12-14 08:42:01 +01:00
clean up db seeding file (#197)
* improve naming and flow * make seed code cleaner * Update dbSeeds.ts * Update template/app/src/server/scripts/dbSeeds.ts Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com> * cleaner seed script * Update dbSeeds.ts --------- Co-authored-by: Martin Šošić <Martinsos@users.noreply.github.com>
This commit is contained in:
@@ -64,7 +64,7 @@ app OpenSaaS {
|
||||
// Run `wasp db seed` to seed the database with the seed functions below:
|
||||
seeds: [
|
||||
// Populates the database with a bunch of fake users to work with during development.
|
||||
import { devSeedUsers } from "@src/server/scripts/usersSeed.js",
|
||||
import { seedMockUsers } from "@src/server/scripts/dbSeeds",
|
||||
]
|
||||
},
|
||||
|
||||
@@ -100,8 +100,8 @@ entity User {=psl
|
||||
|
||||
stripeId String?
|
||||
checkoutSessionId String?
|
||||
subscriptionTier String?
|
||||
subscriptionStatus String?
|
||||
subscriptionStatus String? // 'active', 'canceled', 'past_due', 'deleted', null
|
||||
subscriptionTier String? // 'hobby-tier', 'pro-tier', null
|
||||
sendEmail Boolean @default(false)
|
||||
datePaid DateTime?
|
||||
credits Int @default(3)
|
||||
|
||||
47
template/app/src/server/scripts/dbSeeds.ts
Normal file
47
template/app/src/server/scripts/dbSeeds.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { type User } from 'wasp/entities';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import type { PrismaClient } from '@prisma/client';
|
||||
import { TierIds } from '../../shared/constants.js';
|
||||
import { type SubscriptionStatusOptions } from '../../shared/types.js';
|
||||
|
||||
type MockUserData = Omit<User, 'id'>;
|
||||
|
||||
/**
|
||||
* This function, which we've imported in `app.db.seeds` in the `main.wasp` file,
|
||||
* seeds the database with mock users via the `wasp db seed` command.
|
||||
* For more info see: https://wasp-lang.dev/docs/data-model/backends#seeding-the-database
|
||||
*/
|
||||
export async function seedMockUsers(prismaClient: PrismaClient) {
|
||||
await Promise.all(generateMockUsersData(50).map((data) =>
|
||||
prismaClient.user.create({ data }))
|
||||
);
|
||||
}
|
||||
|
||||
function generateMockUsersData(numOfUsers: number): MockUserData[] {
|
||||
return faker.helpers.multiple(generateMockUserData, { count: numOfUsers });
|
||||
}
|
||||
|
||||
function generateMockUserData(): MockUserData {
|
||||
const firstName = faker.person.firstName();
|
||||
const lastName = faker.person.lastName();
|
||||
const subscriptionStatus = faker.helpers.arrayElement<SubscriptionStatusOptions>(['active', 'canceled', 'past_due', 'deleted', null]);
|
||||
const now = new Date();
|
||||
const createdAt = faker.date.past({ refDate: now });
|
||||
const lastActiveTimestamp = faker.date.between({ from: createdAt, to: now });
|
||||
const credits = subscriptionStatus ? 0 : faker.number.int({ min: 0, max: 10 });
|
||||
const hasUserPaidOnStripe = !!subscriptionStatus || credits > 3
|
||||
return {
|
||||
email: faker.internet.email({ firstName, lastName }),
|
||||
username: faker.internet.userName({ firstName, lastName }),
|
||||
createdAt,
|
||||
lastActiveTimestamp,
|
||||
isAdmin: false,
|
||||
sendEmail: false,
|
||||
credits,
|
||||
subscriptionStatus,
|
||||
stripeId: hasUserPaidOnStripe ? `cus_test_${faker.string.uuid()}` : null,
|
||||
datePaid: hasUserPaidOnStripe ? faker.date.between({ from: createdAt, to: lastActiveTimestamp }) : null,
|
||||
checkoutSessionId: hasUserPaidOnStripe ? `cs_test_${faker.string.uuid()}` : null,
|
||||
subscriptionTier: subscriptionStatus ? faker.helpers.arrayElement([TierIds.HOBBY, TierIds.PRO]) : null,
|
||||
};
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import { type User } from 'wasp/entities';
|
||||
import { faker } from '@faker-js/faker';
|
||||
import type { PrismaClient } from '@prisma/client';
|
||||
import { TierIds } from '../../shared/constants.js';
|
||||
|
||||
// in a terminal window run `wasp db seed` to seed your dev database with mock user data
|
||||
export function createRandomUser() {
|
||||
const firstName = faker.person.firstName();
|
||||
const lastName = faker.person.lastName();
|
||||
const user: Omit<User, 'id'> = {
|
||||
email: faker.internet.email({
|
||||
firstName,
|
||||
lastName,
|
||||
}),
|
||||
username: faker.internet.userName({
|
||||
firstName,
|
||||
lastName,
|
||||
}),
|
||||
createdAt: faker.date.between({ from: new Date('2023-01-01'), to: new Date() }),
|
||||
lastActiveTimestamp: faker.date.recent(),
|
||||
isAdmin: false,
|
||||
stripeId: `cus_${faker.string.uuid()}`,
|
||||
sendEmail: false,
|
||||
subscriptionStatus: faker.helpers.arrayElement(['active', 'canceled', 'past_due', 'deleted', null]),
|
||||
datePaid: faker.date.recent(),
|
||||
credits: faker.number.int({ min: 0, max: 3 }),
|
||||
checkoutSessionId: null,
|
||||
subscriptionTier: faker.helpers.arrayElement([TierIds.HOBBY, TierIds.PRO]),
|
||||
};
|
||||
return user;
|
||||
}
|
||||
|
||||
const USERS: Omit<User, 'id'>[] = faker.helpers.multiple(createRandomUser, {
|
||||
count: 50,
|
||||
});
|
||||
|
||||
export async function devSeedUsers(prismaClient: PrismaClient) {
|
||||
try {
|
||||
await Promise.all(
|
||||
USERS.map(async (user) => {
|
||||
await prismaClient.user.create({
|
||||
data: user,
|
||||
});
|
||||
})
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user