Skip to content

Commit

Permalink
fix: fix batching getting confused or something
Browse files Browse the repository at this point in the history
  • Loading branch information
datner committed Jan 7, 2024
1 parent 26753ce commit b8f0004
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 47 deletions.
2 changes: 1 addition & 1 deletion apps/web/pages/menu/[restaurant].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const Menu: BlitzPage<InferGetServerSidePropsType<typeof getServerSidePro
{orderedCategories.map((category) => (
<Category.Section key={category.id} category={category}>
<Category.Items>
{category.categoryItems.map((ci, i) => <Category.Item priority={i < 6} key={ci.item.id} item={ci} />)}
{category.categoryItems.map((ci, i) => <Category.Item priority={i < 6} key={ci.Item.id} item={ci} />)}
</Category.Items>
</Category.Section>
))}
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/menu/components/Category/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const Item = memo<ItemProps>(({ item, priority }) => {
const orderItems = Order.getOrderItems(state.order);

const orderItem = pipe(
HashMap.filter(orderItems, (it) => it.item.id === item.item.id),
HashMap.filter(orderItems, (it) => it.item.id === item.Item.id),
HashMap.map((oi, key) => [key, oi] as const),
HashMap.values,
A.fromIterable,
Expand All @@ -26,8 +26,8 @@ export const Item = memo<ItemProps>(({ item, priority }) => {
onEmpty: () => [
<ListItem
priority={priority}
key={`${item.item.identifier}-0`}
item={Order.NewActiveItem({ item: Data.struct(item.item) })}
key={`${item.Item.identifier}-0`}
item={Order.NewActiveItem({ item: Data.struct(item.Item) })}
/>,
],
onNonEmpty: A.map(([key, item], i) => (
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/Category/requests/getById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Data, Request } from "effect";

export class GetCategoryByIdError extends Data.TaggedClass("GetCategoryByIdError")<{}> {}

export interface GetCategoryById extends Request.Request<GetCategoryByIdError, Models.Category> {
export interface GetCategoryById extends Request.Request<GetCategoryByIdError, any> {
readonly _tag: "GetCategoryById";
readonly id: number;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/shared/Category/requests/getItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { Data, Request } from "effect";

export class GetCategoryItemsError extends Data.TaggedClass("GetItemsByCategoryIdError")<{}> {}

export interface GetCategoryItems extends Request.Request<GetCategoryItemsError, Models.CategoryItem[]> {
export interface GetCategoryItems extends
Request.Request<
GetCategoryItemsError,
any
>
{
readonly _tag: "GetCategoryItems";
readonly id: number;
}
Expand Down
29 changes: 24 additions & 5 deletions packages/shared/Category/requests/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Effect, pipe, ReadonlyArray, Request, RequestResolver } from "effect";
import { Console, Effect, Order, pipe, ReadonlyArray, Request, RequestResolver } from "effect";
import { inspect } from "util";
import { Database } from "../../Database";
import { GetCategoryById } from "./getById";
import { GetCategoryContent } from "./getContent";
Expand All @@ -11,9 +12,21 @@ export const CategoryResolver = pipe(
requests: CategoryRequest[],
) => {
const reqMap = ReadonlyArray.groupBy(requests, _ => _._tag);
const byId = reqMap.GetCategoryById as GetCategoryById[] ?? [];
const content = reqMap.GetCategoryContent as GetCategoryContent[] ?? [];
const items = reqMap.GetCategoryItems as GetCategoryItems[] ?? [];
const byId = ReadonlyArray.sort(
reqMap.GetCategoryById as GetCategoryById[] ?? [],
Order.mapInput(Order.number, (_: GetCategoryById) => _.id),
);

const content = ReadonlyArray.sort(
reqMap.GetCategoryContent as GetCategoryContent[] ?? [],
Order.mapInput(Order.number, (_: GetCategoryContent) => _.id),
);

const items = ReadonlyArray.sort(
reqMap.GetCategoryItems as GetCategoryItems[] ?? [],
Order.mapInput(Order.number, (_: GetCategoryItems) => _.id),
);

return Effect.andThen(Database, db =>
Effect.all({
GetCategoryById: Effect.promise(() =>
Expand Down Expand Up @@ -46,7 +59,13 @@ export const CategoryResolver = pipe(
items.map(_ =>
db.category.findUnique({ where: { id: _.id } }).categoryItems({
where: { Item: { deleted: null } },
include: { Item: true },
include: {
Item: {
include: {
content: true,
},
},
},
})
),
)
Expand Down
94 changes: 59 additions & 35 deletions packages/shared/Venue/Menu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParseResult, Schema } from "@effect/schema";
import { Effect, pipe, ReadonlyArray } from "effect";
import { Console, Effect, pipe, ReadonlyArray } from "effect";
import { inspect } from "util";
import * as C from "../Category/category";
import * as CI from "../Category/item";
import * as CR from "../Category/requests";
Expand All @@ -12,55 +13,77 @@ import { Common } from "../schema";
import * as VR from "./requests";
import * as V from "./venue";

class Item extends I.Item.transformFrom<Item>()(
class _Item extends I.Item.extend<_Item>()({
content: Schema.array(Common.Content),
}) {}

class Item extends _Item.transformFrom<Item>()(
{
modifiers: Schema.array(IM.fromPrisma),
content: Schema.array(Common.Content),
},
(i) =>
Effect.all({
content: IR.getContent(i.id),
modifiers: IR.getModifiers(i.id),
}, { batching: true }).pipe(
IR.getModifiers(i.id).pipe(
Effect.mapBoth({
onSuccess: (_) => ({ ...i, ..._ }),
onSuccess: (modifiers) => ({ ...i, modifiers }),
onFailure: _ => ParseResult.parseError([ParseResult.missing]),
}),
accessing(Database),
),
ParseResult.succeed,
) {}
// class Item extends I.Item.transformFrom<Item>()(
// {
// modifiers: Schema.array(IM.fromPrisma),
// content: Schema.array(Common.Content),
// },
// (i) =>
// Effect.all({
// content: IR.getContent(i.id),
// modifiers: IR.getModifiers(i.id),
// }, { batching: true }).pipe(
// Effect.mapBoth({
// onSuccess: (_) => ({ ...i, ..._ }),
// onFailure: _ => ParseResult.parseError([ParseResult.missing]),
// }),
// accessing(Database),
// ),
// ParseResult.succeed,
// ) {}

export class CategoryItem extends CI.Item.transform<CategoryItem>()({
item: Item,
}, ci =>
IR.getById(ci.itemId).pipe(
Effect.andThen(_ => Schema.decode(Item)(_)),
Effect.mapBoth({
onSuccess: (item) => ({ ...ci, item }),
onFailure: _ => _._tag === "GetItemByIdError" ? ParseResult.parseError([ParseResult.missing]) : _,
}),
accessing(Database),
), ParseResult.succeed)
{}
export class CategoryItem extends CI.Item.extend<CategoryItem>()({
Item: Item,
}) {}
// export class CategoryItem extends CI.Item.transform<CategoryItem>()({
// item: Item,
// }, ci =>
// IR.getById(ci.itemId).pipe(
// Effect.andThen(_ => Schema.decode(Item)(_)),
// Effect.mapBoth({
// onSuccess: (item) => ({ ...ci, item }),
// onFailure: _ => _._tag === "GetItemByIdError" ? ParseResult.parseError([ParseResult.missing]) : _,
// }),
// accessing(Database),
// ), ParseResult.succeed)
// {}

export class Category extends C.Category.transformFrom<Category>()({
export class _Category extends C.Category.extend<_Category>()({
content: Schema.array(Common.Content),
categoryItems: Schema.to(Schema.array(CategoryItem)),
}, (c) =>
Effect.zip(
CR.getContent(c.id),
}) {}

export class Category extends _Category.transformFrom<Category>()(
{
categoryItems: Schema.to(Schema.array(CategoryItem)),
},
(c) =>
CR.getItems(c.id).pipe(
Effect.andThen(Effect.forEach(_ => Schema.decode(CategoryItem)(_), { batching: true })),
Effect.tap(Console.log),
Effect.flatMap(Schema.decode(Schema.array(CategoryItem))),
Effect.map((categoryItems) => ({ ...c, categoryItems })),
Effect.mapError(_ => _._tag === "ParseError" ? _ : ParseResult.parseError([ParseResult.missing])),
accessing(Database),
),
{ batching: true },
).pipe(
Effect.map(([content, categoryItems]) => ({ ...c, content, categoryItems })),
Effect.mapError(_ => _._tag === "ParseError" ? _ : ParseResult.parseError([ParseResult.missing])),
accessing(Database),
_ => _,
), ParseResult.succeed)
{}
ParseResult.succeed,
) {}

export class FromVenue extends V.Venue.transformFrom<FromVenue>()({
content: Schema.array(pipe(Common.Content, Schema.omit("description"))),
Expand All @@ -70,6 +93,7 @@ export class FromVenue extends V.Venue.transformFrom<FromVenue>()({
VR.getContent(v.id),
VR.getCategories(v.id).pipe(
Effect.andThen(Effect.forEach(_ => Schema.decode(Category)(_), { batching: true })),
Effect.tap(_ => console.log(inspect(_, false, 4, true))),
Effect.map(ReadonlyArray.filter(_ => ReadonlyArray.isNonEmptyReadonlyArray(_.categoryItems))),
),
{ batching: true },
Expand All @@ -92,7 +116,7 @@ export class MenuItem extends I.Item.extend<MenuItem>()({
}) {}

export class MenuCategoryItem extends CI.Item.extend<MenuCategoryItem>()({
item: MenuItem.struct,
Item: MenuItem.struct,
}) {}

export class MenuCategory extends C.Category.extend<MenuCategory>()({
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/Venue/requests/getCategories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Data, Request } from "effect";

export class GetVenueCategoriesError extends Data.TaggedClass("GetVenueCategoriesError")<{}> {}

export interface GetVenueCategories extends Request.Request<GetVenueCategoriesError, ReadonlyArray<Models.Category>> {
export interface GetVenueCategories extends Request.Request<GetVenueCategoriesError, ReadonlyArray<any>> {
readonly _tag: "GetVenueCategories";
readonly id: number;
}
Expand Down
1 change: 1 addition & 0 deletions packages/shared/Venue/requests/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const VenueResolver = RequestResolver.makeBatched<DB, VenueRequest>((
categories.map(_ =>
db.venue.findUniqueOrThrow({ where: { id: _.id } }).categories({
where: { deleted: null },
include: { content: true },
})
),
)
Expand Down

0 comments on commit b8f0004

Please sign in to comment.