Skip to content

Commit

Permalink
[DEV-39] feat added seeding process for app (#14)
Browse files Browse the repository at this point in the history
feat added seeding process for app
  • Loading branch information
darklight9811 authored Jul 18, 2024
1 parent b5ef0d4 commit b47e908
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 13 deletions.
13 changes: 12 additions & 1 deletion apps/app/src/app/[locale]/(app)/entities/_components/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ export function Filter(props: Props) {
<Button type="submit" className="w-full">
Aplicar
</Button>
<Button type="button" variant="destructive">
<Button
type="button"
variant="destructive"
onClick={() => {
const url = new URL(window.location.href);

url.searchParams.delete("q");
url.searchParams.delete("date_disppeared");

router.push(`${url.pathname}?${url.searchParams.toString()}`);
}}
>
<Trash />
</Button>
</div>
Expand Down
6 changes: 5 additions & 1 deletion apps/app/src/app/[locale]/_components/navbar-background.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { useEffect, useRef, useState } from "react";

export default function NavbarBackground() {
const ref = useRef<HTMLDivElement>(null);
const [shadow, setShadow] = useState(false);
const [shadow, setShadow] = useState(() =>
typeof window !== "undefined"
? document.scrollingElement?.scrollTop || 0 > 25
: false,
);

useEffect(() => {
function onScroll(e: Event) {
Expand Down
8 changes: 6 additions & 2 deletions packages/ds/src/components/ui/pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ function Pagination(props: { page: number; pages: number }) {
)}

{Array.from(Array(props.pages)).map((_, i) => (
<PaginationLink href={`?page=${i + 1}`} key={`${i}${props.pages}`}>
<PaginationLink
isActive={props.page === i + 1}
href={`?page=${i + 1}`}
key={`${i}${props.pages}`}
>
{i + 1}
</PaginationLink>
))}
Expand Down Expand Up @@ -80,7 +84,7 @@ function PaginationLink({
aria-current={isActive ? "page" : undefined}
className={cn(
buttonVariants({
variant: isActive ? "outline" : "ghost",
variant: isActive ? "primary" : "ghost",
size,
}),
className,
Expand Down
4 changes: 3 additions & 1 deletion packages/env/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const server = z.object({
VERCEL_URL: z.string().optional(),
PORT: z.coerce.number().default(3000),
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]),
NODE_ENV: z
.enum(["development", "test", "production"])
.default("development"),

UPLOADTHING_SECRET: z.string().min(1),
UPLOADTHING_APP_ID: z.string().min(1),
Expand Down
6 changes: 3 additions & 3 deletions packages/schemas/src/schemas/pagination.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { z } from "zod";

const pagination = z.object({
page: z.coerce.number().default(1).optional(),
limit: z.coerce.number().default(25).optional(),
page: z.coerce.number().optional().default(1),
limit: z.coerce.number().optional().default(16),
order: z.coerce.string().optional(),
sort: z.enum(["asc", "desc"]).default("asc").optional(),
sort: z.enum(["asc", "desc"]).optional().default("asc"),

q: z.coerce.string().optional(),
});
Expand Down
2 changes: 1 addition & 1 deletion packages/services/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"migrate:deploy": "prisma migrate deploy",
"migrate:dev": "prisma migrate dev",
"push": "prisma db push",
"seed": "tsx src/seed.ts",
"seed": "bun ./src/seed.ts",
"generate": "prisma generate",
"format": "biome format --error-on-warnings ./src",
"verify": "biome check --error-on-warnings ./src",
Expand Down
2 changes: 0 additions & 2 deletions packages/services/src/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ function getPrisma() {
(this as any)
.aggregate({
_count: { id: true },
take: limit,
select: { _count: true },
where,
skip: (page - 1) * limit,
})
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
.then((response: any) => ({
Expand Down
96 changes: 96 additions & 0 deletions packages/services/src/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { db } from "./lib/db";

const quantity = 40;

type ApiResponse = {
gender: "male" | "female";
email: string;
cell: string;
name: {
first: string;
last: string;
};
dob: {
age: number;
};
picture: {
large: string;
medium: string;
thumbnail: string;
};
location: {
street: {
number: number;
name: string;
};
city: string;
state: string;
country: string;
postcode: number;
};
};

async function main() {
const profiles = await fetch(
`https://randomuser.me/api/?results=${quantity}&nat=br`,
)
.then((t) => t.json())
.then((t) => t.results);

await Promise.all(
profiles.map((profile: ApiResponse) =>
db.entity.create({
data: {
name: `${profile.name.first} ${profile.name.last}`,
type: "person",
description: "",
id_user_created: "clydp3jxl00002tduqdonwcw9",
data: {
create: {
age: profile.dob.age,
race: "Branca",
gender: profile.gender,
},
},
pictures: {
createMany: {
data: [
{
key: `${Math.ceil(Math.random() * 1000)}`,
url: profile.picture.large,
},
],
},
},
addresses: {
createMany: {
data: [
{
state: profile.location.state,
city: profile.location.city,
country: "Brasil",
district: "",
},
],
},
},
contact: {
create: {
description: "",
options: [
{ type: "email", value: profile.email },
{ type: "phone", value: profile.cell },
],
},
},
},
}),
),
);

await db.$disconnect();
console.log("Database seeded!");
process.exit(0);
}

main();
2 changes: 0 additions & 2 deletions packages/services/src/services/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const entityService = service({
const { page, limit, q, order, sort, date_disappeared } =
entityPagination.parse(input);

console.log(date_disappeared);

return db.entity.paginate({
page,
limit,
Expand Down

0 comments on commit b47e908

Please sign in to comment.