Move to zenstack for table inheritance
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { Link } from '@remix-run/react';
|
||||
import { Beer } from '@zenstackhq/runtime/models';
|
||||
import { Card } from 'react-bootstrap';
|
||||
|
||||
interface BeerArgments {
|
||||
beer: Beer
|
||||
}
|
||||
|
||||
function BeerCard(options:BeerArgments) {
|
||||
return (
|
||||
<Card style={{ width: '18rem' }}>
|
||||
<Card.Body>
|
||||
<Card.Title>{options.beer.name}</Card.Title>
|
||||
<Card.Text>
|
||||
{options.beer.description}
|
||||
</Card.Text>
|
||||
<Link to={options.beer.id.toString()}>Permalink</Link>
|
||||
</Card.Body>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
export default BeerCard;
|
||||
@@ -8,13 +8,17 @@ import {
|
||||
useParams,
|
||||
isRouteErrorResponse,
|
||||
useRouteError } from "@remix-run/react";
|
||||
|
||||
import BeerCard from "~/components/cards/beer";
|
||||
|
||||
import { db } from "~/utils/db.server";
|
||||
import { enhance } from "@zenstackhq/runtime";
|
||||
|
||||
export const action = async ({
|
||||
params,
|
||||
request,
|
||||
}: ActionFunctionArgs) => {
|
||||
const dbe = enhance(db);
|
||||
const form = await request.formData();
|
||||
if (form.get("intent") !== "delete") {
|
||||
throw new Response(
|
||||
@@ -22,23 +26,26 @@ export const action = async ({
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const beer = await db.beer.findUnique({
|
||||
where: { id: params.beerid },
|
||||
const beerid = parseInt(params.beerid!);
|
||||
const beer = await dbe.beer.findUnique({
|
||||
where: { id: beerid },
|
||||
});
|
||||
if (!beer) {
|
||||
throw new Response("Can't delete what does not exist", {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
await db.beer.delete({ where: { id: params.beerid } });
|
||||
await dbe.beer.delete({ where: { id: beerid } });
|
||||
return redirect("/beers");
|
||||
};
|
||||
|
||||
export const loader = async ({
|
||||
params,
|
||||
}: LoaderFunctionArgs) => {
|
||||
const beer = await db.beer.findUnique({
|
||||
where: { id: params.beerid },
|
||||
const dbe = enhance(db);
|
||||
const beerid = parseInt(params.beerid!);
|
||||
const beer = await dbe.beer.findUnique({
|
||||
where: { id: beerid },
|
||||
});
|
||||
if (!beer) {
|
||||
throw new Response("Beer not found.", {
|
||||
@@ -54,8 +61,7 @@ export default function BeerRoute() {
|
||||
return (
|
||||
<div>
|
||||
<p>You selected this beer:</p>
|
||||
<p>{data.beer.name}</p>
|
||||
<p>ABV {data.beer.abv}%</p>
|
||||
<BeerCard beer={data.beer}></BeerCard>
|
||||
<form method="post">
|
||||
<button
|
||||
className="button"
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
import { json } from "@remix-run/node";
|
||||
import { Link, useLoaderData } from "@remix-run/react";
|
||||
import { enhance } from "@zenstackhq/runtime";
|
||||
|
||||
import BeerCard from "~/components/cards/beer";
|
||||
|
||||
import { db } from "~/utils/db.server";
|
||||
|
||||
export const loader = async () => {
|
||||
const count = await db.beer.count();
|
||||
const dbe = enhance(db);
|
||||
const count = await dbe.beer.count();
|
||||
const randomRowNumber = Math.floor(Math.random() * count);
|
||||
const [randomBeer] = await db.beer.findMany({
|
||||
const [randomBeer] = await dbe.beer.findMany({
|
||||
skip: randomRowNumber,
|
||||
take: 1,
|
||||
});
|
||||
@@ -19,10 +23,7 @@ export default function BeersIndexRoute() {
|
||||
return (
|
||||
<div>
|
||||
<h4 className="text-lg font-bold">Here's a random Beer:</h4>
|
||||
<p>{data.randomBeer.name}</p>
|
||||
<Link to={data.randomBeer.id}>
|
||||
"{data.randomBeer.name}" Permalink
|
||||
</Link>
|
||||
<BeerCard beer={data.randomBeer}></BeerCard>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { ActionFunctionArgs } from "@remix-run/node";
|
||||
import { redirect } from "@remix-run/node";
|
||||
import { useActionData } from "@remix-run/react";
|
||||
import { enhance } from "@zenstackhq/runtime";
|
||||
|
||||
import { db } from "~/utils/db.server";
|
||||
import { badRequest } from "~/utils/request.server";
|
||||
@@ -24,6 +25,8 @@ function validateBeerAbv(abv: number) {
|
||||
export const action = async ({
|
||||
request,
|
||||
}: ActionFunctionArgs) => {
|
||||
const dbe = enhance(db);
|
||||
|
||||
const form = await request.formData();
|
||||
const name = form.get("name");
|
||||
const abv = Number(form.get("abv"));
|
||||
@@ -44,7 +47,7 @@ export const action = async ({
|
||||
abv: validateBeerAbv(abv),
|
||||
name: validateBeerName(name),
|
||||
};
|
||||
const fields = { name, abv };
|
||||
const fields = { name, abv, slug: "test", description: "lalala", style: {connect: {id: 1} }, manufacturer: {connect: {id: 1} } };
|
||||
if (Object.values(fieldErrors).some(Boolean)) {
|
||||
return badRequest({
|
||||
fieldErrors,
|
||||
@@ -53,7 +56,7 @@ export const action = async ({
|
||||
});
|
||||
}
|
||||
|
||||
const beer = await db.beer.create({ data: fields });
|
||||
const beer = await dbe.beer.create({ data: fields });
|
||||
return redirect(`/beers/${beer.id}`);
|
||||
};
|
||||
|
||||
|
||||
@@ -4,14 +4,16 @@ import {
|
||||
Outlet,
|
||||
useLoaderData,
|
||||
} from "@remix-run/react";
|
||||
import { enhance } from "@zenstackhq/runtime";
|
||||
import BeerCard from "~/components/cards/beer";
|
||||
|
||||
import { db } from "~/utils/db.server";
|
||||
|
||||
export const loader = async () => {
|
||||
const dbe = enhance(db);
|
||||
return json({
|
||||
beerListItems: await db.beer.findMany({
|
||||
beerListItems: await dbe.beer.findMany({
|
||||
orderBy: { id: "asc" },
|
||||
select: { id: true, name: true },
|
||||
take: 5,
|
||||
}),
|
||||
});
|
||||
@@ -41,13 +43,9 @@ export default function BeersRoute() {
|
||||
<div className="basis-1/2">
|
||||
<Link to="." className="btn-primary">Get a random beer</Link>
|
||||
<p>Here are a few more beers to check out:</p>
|
||||
<ul className="list-disc">
|
||||
{data.beerListItems.map(({ id, name }) => (
|
||||
<li key={id} className="list-item">
|
||||
<Link to={id}>{name}</Link>
|
||||
</li>
|
||||
{data.beerListItems.map((beer) => (
|
||||
<BeerCard beer={beer}></BeerCard>
|
||||
))}
|
||||
</ul>
|
||||
<div className="mt-5">
|
||||
<Link to="new" className="btn-primary">
|
||||
Add your own
|
||||
|
||||
Generated
+1247
-8
File diff suppressed because it is too large
Load Diff
+3
-1
@@ -15,6 +15,7 @@
|
||||
"@remix-run/node": "^2.9.1",
|
||||
"@remix-run/react": "^2.9.1",
|
||||
"@remix-run/serve": "^2.9.1",
|
||||
"@zenstackhq/runtime": "2.0.1",
|
||||
"bootstrap": "^5.3.3",
|
||||
"isbot": "^4.1.0",
|
||||
"react": "^18.2.0",
|
||||
@@ -40,7 +41,8 @@
|
||||
"prisma": "^5.13.0",
|
||||
"typescript": "^5.4.5",
|
||||
"vite": "^5.1.0",
|
||||
"vite-tsconfig-paths": "^4.2.1"
|
||||
"vite-tsconfig-paths": "^4.2.1",
|
||||
"zenstack": "2.0.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0"
|
||||
|
||||
+112
-9
@@ -1,17 +1,120 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// DO NOT MODIFY THIS FILE //
|
||||
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
|
||||
//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
url = env("DATABASE_URL")
|
||||
enum DrinkType {
|
||||
Drink
|
||||
Beer
|
||||
Wine
|
||||
Soda
|
||||
}
|
||||
|
||||
enum ContainerType {
|
||||
BeerBottle
|
||||
WineBottle
|
||||
PlasticBottle
|
||||
Can
|
||||
Carton
|
||||
}
|
||||
|
||||
/// @@delegate(type)
|
||||
/// @@allow('all', true)
|
||||
model Drink {
|
||||
id Int @id() @default(autoincrement())
|
||||
slug String @unique()
|
||||
manufacturer_id Int
|
||||
manufacturer Manufacturer @relation(fields: [manufacturer_id], references: [id])
|
||||
type DrinkType
|
||||
name String @unique()
|
||||
description String
|
||||
abv Float
|
||||
image String?
|
||||
containers Container[]
|
||||
delegate_aux_beer Beer?
|
||||
delegate_aux_wine Wine?
|
||||
delegate_aux_soda Soda?
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
/// @@allow('all', true)
|
||||
model Beer {
|
||||
id String @id @default(uuid())
|
||||
name String @unique
|
||||
abv Float
|
||||
}
|
||||
id Int @id()
|
||||
style_id Int
|
||||
style BeerStyle @relation(fields: [style_id], references: [id])
|
||||
ibu Float?
|
||||
delegate_aux_drink Drink @relation(fields: [id], references: [id], onDelete: Cascade, onUpdate: Cascade)
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
model BeerStyle {
|
||||
id Int @id() @default(autoincrement())
|
||||
name String @unique()
|
||||
beers Beer[]
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
/// @@allow('all', true)
|
||||
model Wine {
|
||||
id Int @id()
|
||||
style_id Int
|
||||
style WineStyle @relation(fields: [style_id], references: [id])
|
||||
heavy_score Int?
|
||||
tannine_score Int?
|
||||
dry_score Int?
|
||||
fresh_score Int?
|
||||
notes String?
|
||||
delegate_aux_drink Drink @relation(fields: [id], references: [id], onDelete: Cascade, onUpdate: Cascade)
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
model WineStyle {
|
||||
id Int @id() @default(autoincrement())
|
||||
name String @unique()
|
||||
wines Wine[]
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
/// @@allow('all', true)
|
||||
model Soda {
|
||||
id Int @id()
|
||||
carbonated Boolean
|
||||
delegate_aux_drink Drink @relation(fields: [id], references: [id], onDelete: Cascade, onUpdate: Cascade)
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
model Container {
|
||||
barcode String @id()
|
||||
drink_id Int
|
||||
drink Drink @relation(fields: [drink_id], references: [id])
|
||||
type ContainerType
|
||||
volume Int
|
||||
portions Int?
|
||||
inventory Int @default(0)
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
model Manufacturer {
|
||||
id Int @id() @default(autoincrement())
|
||||
country_id String
|
||||
country Country @relation(fields: [country_id], references: [code])
|
||||
name String @unique()
|
||||
image String?
|
||||
drinks Drink[]
|
||||
}
|
||||
|
||||
/// @@allow('all', true)
|
||||
model Country {
|
||||
code String @id()
|
||||
name String
|
||||
manufacturers Manufacturer[]
|
||||
}
|
||||
|
||||
+143
-12
@@ -1,35 +1,166 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
const db = new PrismaClient();
|
||||
import { ContainerType, PrismaClient } from "@prisma/client";
|
||||
import { enhance } from "@zenstackhq/runtime";
|
||||
import { BeerStyle, Country, Manufacturer } from "@zenstackhq/runtime/models";
|
||||
const pr = new PrismaClient();
|
||||
const db = enhance(pr);
|
||||
|
||||
async function seed() {
|
||||
|
||||
await db.container.deleteMany({});
|
||||
await db.beer.deleteMany({});
|
||||
await db.beerStyle.deleteMany({});
|
||||
await db.manufacturer.deleteMany({});
|
||||
await db.country.deleteMany({});
|
||||
|
||||
await Promise.all(
|
||||
getCountries().map((country) => {
|
||||
return db.country.create({ data: country });
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
getManufacturers().map((man) => {
|
||||
return db.manufacturer.create({ data: man });
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
getBeerStyles().map((style) => {
|
||||
return db.beerStyle.create({ data: style });
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
getBeers().map((beer) => {
|
||||
return db.beer.create({ data: beer });
|
||||
})
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
getContainers().map((container) => {
|
||||
return db.container.create({ data: container });
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
seed();
|
||||
|
||||
function getBeers() {
|
||||
// shout-out to https://icanhazdadjoke.com/
|
||||
interface BeerQuery {
|
||||
slug: string;
|
||||
manufacturer: any;
|
||||
name: string;
|
||||
description: string;
|
||||
abv: number;
|
||||
style: any;
|
||||
ibu: number | null;
|
||||
}
|
||||
|
||||
interface ContainerQuery {
|
||||
barcode: string;
|
||||
drink: any;
|
||||
type: ContainerType;
|
||||
volume: number;
|
||||
portions: number | null;
|
||||
}
|
||||
|
||||
function getCountries() : Country[]{
|
||||
return [
|
||||
{
|
||||
name: "Ginger Paradise",
|
||||
abv: 6.2
|
||||
code: "nl",
|
||||
name: "Netherlands"
|
||||
},
|
||||
{
|
||||
name: "Hertog Jan Pilsener",
|
||||
abv: 5.1
|
||||
code: "de",
|
||||
name: "Germany"
|
||||
},
|
||||
{
|
||||
name: "Hertog Jan Weizener",
|
||||
abv: 5.7
|
||||
code: "be",
|
||||
name: "Belgium"
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function getManufacturers() : Manufacturer[] {
|
||||
return [
|
||||
{
|
||||
id: 1,
|
||||
country_id: "nl",
|
||||
name: "Hertog Jan",
|
||||
image: null
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
country_id: "nl",
|
||||
name: "Heineken",
|
||||
image: null
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
country_id: "nl",
|
||||
name: "Jupiler",
|
||||
image: null
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
country_id: "nl",
|
||||
name: "Brouwerij Het IJ",
|
||||
image: null
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function getBeerStyles() : BeerStyle[] {
|
||||
return [
|
||||
{
|
||||
id: 1,
|
||||
name: "Pilsener",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Dubbel"
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Tripel"
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function getBeers() : BeerQuery[] {
|
||||
return [
|
||||
{
|
||||
slug: "ginger_paradise",
|
||||
name: "Ginger Paradise",
|
||||
abv: 6.2,
|
||||
ibu: 5,
|
||||
|
||||
manufacturer: {connect: { id: 4}},
|
||||
style: {connect: { id: 3}},
|
||||
|
||||
description: "Some desciption on why this beer is so great and it has this and that flavour",
|
||||
},
|
||||
{
|
||||
slug: "ginger_paradise",
|
||||
name: "Ginger Paradise",
|
||||
abv: 6.2,
|
||||
ibu: 5,
|
||||
|
||||
manufacturer: {connect: { id: 4}},
|
||||
style: {connect: { id: 3}},
|
||||
|
||||
description: "Some desciption on why this beer is so great and it has this and that flavour",
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
function getContainers() : ContainerQuery[] {
|
||||
return [
|
||||
{
|
||||
name: "Leffe Blond 0,0%",
|
||||
abv: 0.0
|
||||
barcode: "temporary1",
|
||||
drink: {connect: { slug: "ginger_paradise"}},
|
||||
type: ContainerType.BeerBottle,
|
||||
volume: 330,
|
||||
portions: 1
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
// This is your Prisma schema file,
|
||||
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
enum DrinkType {
|
||||
Drink
|
||||
Beer
|
||||
Wine
|
||||
Soda
|
||||
}
|
||||
|
||||
enum ContainerType {
|
||||
BeerBottle
|
||||
WineBottle
|
||||
PlasticBottle
|
||||
Can
|
||||
Carton
|
||||
}
|
||||
|
||||
model Drink {
|
||||
id Int @id @default(autoincrement())
|
||||
slug String @unique
|
||||
|
||||
manufacturer_id Int
|
||||
manufacturer Manufacturer @relation(fields: [manufacturer_id], references: [id])
|
||||
|
||||
type DrinkType
|
||||
|
||||
name String @unique
|
||||
description String
|
||||
abv Float
|
||||
image String?
|
||||
|
||||
containers Container[]
|
||||
|
||||
@@delegate(type)
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model Beer extends Drink {
|
||||
style_id Int
|
||||
style BeerStyle @relation(fields: [style_id], references: [id])
|
||||
|
||||
ibu Float?
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model BeerStyle {
|
||||
id Int @id @default(autoincrement())
|
||||
|
||||
name String @unique
|
||||
|
||||
beers Beer[]
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model Wine extends Drink {
|
||||
style_id Int
|
||||
style WineStyle @relation(fields: [style_id], references: [id])
|
||||
|
||||
heavy_score Int?
|
||||
tannine_score Int?
|
||||
dry_score Int?
|
||||
fresh_score Int?
|
||||
notes String?
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model WineStyle {
|
||||
id Int @id @default(autoincrement())
|
||||
|
||||
name String @unique
|
||||
|
||||
wines Wine[]
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model Soda extends Drink {
|
||||
carbonated Boolean
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model Container {
|
||||
barcode String @id
|
||||
|
||||
drink_id Int
|
||||
drink Drink @relation(fields: [drink_id], references: [id])
|
||||
|
||||
type ContainerType
|
||||
volume Int
|
||||
portions Int?
|
||||
|
||||
inventory Int @default(0)
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model Manufacturer {
|
||||
id Int @id @default(autoincrement())
|
||||
|
||||
country_id String
|
||||
country Country @relation(fields: [country_id], references: [code])
|
||||
|
||||
name String @unique
|
||||
image String?
|
||||
|
||||
drinks Drink[]
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
|
||||
model Country {
|
||||
code String @id
|
||||
name String
|
||||
|
||||
manufacturers Manufacturer[]
|
||||
|
||||
@@allow('all', true)
|
||||
}
|
||||
Reference in New Issue
Block a user