Skip to content

Instantly share code, notes, and snippets.

@kasperpeulen
Created November 24, 2023 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasperpeulen/281eb5afe53083b40c34ae3b2a80f8de to your computer and use it in GitHub Desktop.
Save kasperpeulen/281eb5afe53083b40c34ae3b2a80f8de to your computer and use it in GitHub Desktop.
import { factory, manyOf, nullable, oneOf, primaryKey } from "@mswjs/data";
import { faker } from "@faker-js/faker";
import { Address, Category, Tag } from "./petstore.openapi.ts";
import { ModelDefinition } from "@mswjs/data/lib/glossary";
const PetStatus = ["available", "pending", "sold"];
const OrderStatus = ["placed", "approved", "delivered"];
const Pet = {
id: primaryKey<number>(() => faker.number.int()),
name: () => faker.string.alphanumeric(),
category: oneOf("Category"),
photoUrls: () => faker.string.alphanumeric(),
tags: manyOf("Tag"),
status: () => faker.helpers.arrayElement(PetStatus),
} satisfies ModelDefinition;
const Order = {
id: primaryKey<number>(() => faker.number.int()),
petId: nullable<number>(() => faker.number.int()),
quantity: nullable<number>(() => faker.number.int()),
shipDate: nullable<string>(() => faker.string.alphanumeric()),
status: () => faker.helpers.arrayElement(OrderStatus),
complete: nullable<boolean>(() => faker.datatype.boolean()),
} satisfies ModelDefinition;
const Customer = {
id: primaryKey<number>(() => faker.number.int()),
username: nullable<string>(() => faker.string.alphanumeric()),
address: manyOf("Address"),
};
const Address = {
id: primaryKey<number>(() => faker.number.int()),
street: nullable<string>(() => faker.string.alphanumeric()),
city: nullable<string>(() => faker.string.alphanumeric()),
state: nullable<string>(() => faker.string.alphanumeric()),
zip: nullable<string>(() => faker.string.alphanumeric()),
};
const Category = {
id: primaryKey<number>(() => faker.number.int()),
name: nullable<string>(() => faker.string.alphanumeric()),
};
const User = {
id: primaryKey<number>(() => faker.number.int()),
username: nullable<string>(() => faker.string.alphanumeric()),
firstName: nullable<string>(() => faker.string.alphanumeric()),
lastName: nullable<string>(() => faker.string.alphanumeric()),
email: nullable<string>(() => faker.string.alphanumeric()),
password: nullable<string>(() => faker.string.alphanumeric()),
phone: nullable<string>(() => faker.string.alphanumeric()),
userStatus: nullable<number>(() => faker.number.int()),
};
const Tag = {
id: primaryKey<number>(() => faker.number.int()),
name: nullable<string>(() => faker.string.alphanumeric()),
};
const ApiResponse = {
id: primaryKey<number>(() => faker.number.int()),
code: nullable<number>(() => faker.number.int()),
type: nullable<string>(() => faker.string.alphanumeric()),
message: nullable<string>(() => faker.string.alphanumeric()),
};
export const db = factory({
Order,
Customer,
Address,
Category,
User,
Tag,
Pet,
ApiResponse,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment