Skip to content

Commit

Permalink
Merge branch 'main' into manual-expand
Browse files Browse the repository at this point in the history
  • Loading branch information
IldenH committed Jan 11, 2025
2 parents 377c8f4 + fafdeec commit d513462
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 171 deletions.
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 20.11.0
4 changes: 3 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-include .env

default: dev

dev: pb_types
Expand All @@ -11,4 +13,4 @@ db:

pb_types:
@echo Reading schema from $(PUBLIC_PB_HOST)
@npx --yes pocketbase-typegen@1.2.1 --url $(PUBLIC_PB_HOST) --email $(PB_ADMIN_EMAIL) --password $(PB_ADMIN_PASSWORD) --out ./src/lib/pocketbase/index.d.ts
@npx --yes pocketbase-typegen@1.3.0 --url $(PUBLIC_PB_HOST) --email $(PB_ADMIN_EMAIL) --password $(PB_ADMIN_PASSWORD) --out ./src/lib/pocketbase/index.d.ts
2 changes: 1 addition & 1 deletion pocketbase/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:latest

ARG PB_VERSION=0.22.21
ARG PB_VERSION=0.24.1

RUN apk add --no-cache \
unzip \
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/AuthButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
async function login() {
const authData = await pb.collection("users").authWithOAuth2({ provider: "google" });
const authData = await pb.collection("user").authWithOAuth2({ provider: "google" });
const meta = authData.meta;
Expand All @@ -32,7 +32,7 @@
formData.append("avatar", avatar);
}
await pb.collection("users").update(authData.record.id, formData);
await pb.collection("user").update(authData.record.id, formData);
}
document.cookie = pb.authStore.exportToCookie({ httpOnly: false });
Expand Down
243 changes: 154 additions & 89 deletions src/lib/pocketbase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import type PocketBase from 'pocketbase'
import type { RecordService } from 'pocketbase'

export enum Collections {
ActiveMessage = "activeMessage",
Categories = "categories",
DisplayMessages = "displayMessages",
Drinks = "drinks",
OrderDrink = "order_drink",
Orders = "orders",
Users = "users",
Authorigins = "_authOrigins",
Externalauths = "_externalAuths",
Mfas = "_mfas",
Otps = "_otps",
Superusers = "_superusers",
Category = "category",
Item = "item",
Message = "message",
Order = "order",
OrderItem = "order_item",
Status = "status",
User = "user",
}

// Alias types for improved usability
Expand All @@ -23,8 +28,6 @@ export type HTMLString = string
// System fields
export type BaseSystemFields<T = never> = {
id: RecordIdString
created: IsoDateString
updated: IsoDateString
collectionId: string
collectionName: Collections
expand?: T
Expand All @@ -39,124 +42,186 @@ export type AuthSystemFields<T = never> = {

// Record types for each collection

export type ActiveMessageRecord = {
isVisible?: boolean
message?: RecordIdString
export type AuthoriginsRecord = {
collectionRef: string
created?: IsoDateString
fingerprint: string
id: string
recordRef: string
updated?: IsoDateString
}

export type ExternalauthsRecord = {
collectionRef: string
created?: IsoDateString
id: string
provider: string
providerId: string
recordRef: string
updated?: IsoDateString
}

export type MfasRecord = {
collectionRef: string
created?: IsoDateString
id: string
method: string
recordRef: string
updated?: IsoDateString
}

export type OtpsRecord = {
collectionRef: string
created?: IsoDateString
id: string
password: string
recordRef: string
sentTo?: string
updated?: IsoDateString
}

export type SuperusersRecord = {
created?: IsoDateString
email: string
emailVisibility?: boolean
id: string
password: string
tokenKey: string
updated?: IsoDateString
verified?: boolean
}

export type CategoriesRecord = {
export type CategoryRecord = {
created?: IsoDateString
id: string
name: string
sort_order: number
updated?: IsoDateString
}

export type DisplayMessagesRecord = {
subtext?: string
title?: string
}

export type DrinksRecord = {
export type ItemRecord = {
category: RecordIdString
created?: IsoDateString
id: string
image: string
name: string
price: number
price_nok: number
updated?: IsoDateString
}

export enum OrderDrinkServingSizeOptions {
"small" = "small",
"big" = "big",
"custom" = "custom",
}

export enum OrderDrinkMilkOptions {
"oat" = "oat",
"soy" = "soy",
"whole" = "whole",
"low-fat" = "low-fat",
"lactose-free" = "lactose-free",
}

export enum OrderDrinkExtrasOptions {
"sirup" = "sirup",
"espresso" = "espresso",
"cream" = "cream",
}

export enum OrderDrinkFlavorOptions {
"vanilla" = "vanilla",
"salt-caramel" = "salt-caramel",
"pumpkin-spice" = "pumpkin-spice",
"irish" = "irish",
"spicy" = "spicy",
}
export type OrderDrinkRecord = {
drink: RecordIdString
extras?: OrderDrinkExtrasOptions[]
flavor?: OrderDrinkFlavorOptions[]
milk?: OrderDrinkMilkOptions
serving_size?: OrderDrinkServingSizeOptions
export type MessageRecord = {
created?: IsoDateString
id: string
subtitle?: string
title?: string
updated?: IsoDateString
}

export enum OrdersStateOptions {
export enum OrderStateOptions {
"received" = "received",
"production" = "production",
"completed" = "completed",
"dispatched" = "dispatched",
}
export type OrdersRecord = {
export type OrderRecord = {
created?: IsoDateString
customer?: RecordIdString
drinks: RecordIdString[]
payment_fulfilled?: boolean
state?: OrdersStateOptions
id: string
items: RecordIdString[]
state?: OrderStateOptions
updated?: IsoDateString
}

export type OrderItemRecord = {
created?: IsoDateString
id: string
item: RecordIdString
updated?: IsoDateString
}

export type UsersRecord = {
export type StatusRecord = {
created?: IsoDateString
id: string
message?: RecordIdString
online?: boolean
updated?: IsoDateString
}

export type UserRecord = {
avatar?: string
favorites?: RecordIdString[]
created?: IsoDateString
email?: string
emailVisibility?: boolean
id: string
is_admin?: boolean
name?: string
purchased_cup?: boolean
password: string
tokenKey: string
updated?: IsoDateString
username: string
verified?: boolean
}

// Response types include system fields and match responses from the PocketBase API
export type ActiveMessageResponse<Texpand = unknown> = Required<ActiveMessageRecord> & BaseSystemFields<Texpand>
export type CategoriesResponse<Texpand = unknown> = Required<CategoriesRecord> & BaseSystemFields<Texpand>
export type DisplayMessagesResponse<Texpand = unknown> = Required<DisplayMessagesRecord> & BaseSystemFields<Texpand>
export type DrinksResponse<Texpand = unknown> = Required<DrinksRecord> & BaseSystemFields<Texpand>
export type OrderDrinkResponse<Texpand = unknown> = Required<OrderDrinkRecord> & BaseSystemFields<Texpand>
export type OrdersResponse<Texpand = unknown> = Required<OrdersRecord> & BaseSystemFields<Texpand>
export type UsersResponse<Texpand = unknown> = Required<UsersRecord> & AuthSystemFields<Texpand>
export type AuthoriginsResponse<Texpand = unknown> = Required<AuthoriginsRecord> & BaseSystemFields<Texpand>
export type ExternalauthsResponse<Texpand = unknown> = Required<ExternalauthsRecord> & BaseSystemFields<Texpand>
export type MfasResponse<Texpand = unknown> = Required<MfasRecord> & BaseSystemFields<Texpand>
export type OtpsResponse<Texpand = unknown> = Required<OtpsRecord> & BaseSystemFields<Texpand>
export type SuperusersResponse<Texpand = unknown> = Required<SuperusersRecord> & AuthSystemFields<Texpand>
export type CategoryResponse<Texpand = unknown> = Required<CategoryRecord> & BaseSystemFields<Texpand>
export type ItemResponse<Texpand = unknown> = Required<ItemRecord> & BaseSystemFields<Texpand>
export type MessageResponse<Texpand = unknown> = Required<MessageRecord> & BaseSystemFields<Texpand>
export type OrderResponse<Texpand = unknown> = Required<OrderRecord> & BaseSystemFields<Texpand>
export type OrderItemResponse<Texpand = unknown> = Required<OrderItemRecord> & BaseSystemFields<Texpand>
export type StatusResponse<Texpand = unknown> = Required<StatusRecord> & BaseSystemFields<Texpand>
export type UserResponse<Texpand = unknown> = Required<UserRecord> & AuthSystemFields<Texpand>

// Types containing all Records and Responses, useful for creating typing helper functions

export type CollectionRecords = {
activeMessage: ActiveMessageRecord
categories: CategoriesRecord
displayMessages: DisplayMessagesRecord
drinks: DrinksRecord
order_drink: OrderDrinkRecord
orders: OrdersRecord
users: UsersRecord
_authOrigins: AuthoriginsRecord
_externalAuths: ExternalauthsRecord
_mfas: MfasRecord
_otps: OtpsRecord
_superusers: SuperusersRecord
category: CategoryRecord
item: ItemRecord
message: MessageRecord
order: OrderRecord
order_item: OrderItemRecord
status: StatusRecord
user: UserRecord
}

export type CollectionResponses = {
activeMessage: ActiveMessageResponse
categories: CategoriesResponse
displayMessages: DisplayMessagesResponse
drinks: DrinksResponse
order_drink: OrderDrinkResponse
orders: OrdersResponse
users: UsersResponse
_authOrigins: AuthoriginsResponse
_externalAuths: ExternalauthsResponse
_mfas: MfasResponse
_otps: OtpsResponse
_superusers: SuperusersResponse
category: CategoryResponse
item: ItemResponse
message: MessageResponse
order: OrderResponse
order_item: OrderItemResponse
status: StatusResponse
user: UserResponse
}

// Type for usage with type asserted PocketBase instance
// https://github.com/pocketbase/js-sdk#specify-typescript-definitions

export type TypedPocketBase = PocketBase & {
collection(idOrName: 'activeMessage'): RecordService<ActiveMessageResponse>
collection(idOrName: 'categories'): RecordService<CategoriesResponse>
collection(idOrName: 'displayMessages'): RecordService<DisplayMessagesResponse>
collection(idOrName: 'drinks'): RecordService<DrinksResponse>
collection(idOrName: 'order_drink'): RecordService<OrderDrinkResponse>
collection(idOrName: 'orders'): RecordService<OrdersResponse>
collection(idOrName: 'users'): RecordService<UsersResponse>
collection(idOrName: '_authOrigins'): RecordService<AuthoriginsResponse>
collection(idOrName: '_externalAuths'): RecordService<ExternalauthsResponse>
collection(idOrName: '_mfas'): RecordService<MfasResponse>
collection(idOrName: '_otps'): RecordService<OtpsResponse>
collection(idOrName: '_superusers'): RecordService<SuperusersResponse>
collection(idOrName: 'category'): RecordService<CategoryResponse>
collection(idOrName: 'item'): RecordService<ItemResponse>
collection(idOrName: 'message'): RecordService<MessageResponse>
collection(idOrName: 'order'): RecordService<OrderResponse>
collection(idOrName: 'order_item'): RecordService<OrderItemResponse>
collection(idOrName: 'status'): RecordService<StatusResponse>
collection(idOrName: 'user'): RecordService<UserResponse>
}
4 changes: 2 additions & 2 deletions src/lib/pocketbase/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import PocketBase from "pocketbase";
import { PUBLIC_PB_HOST } from "$env/static/public";
import { type TypedPocketBase, Collections, OrdersStateOptions } from "$lib/pocketbase/index.d";
import { type TypedPocketBase, Collections, OrderStateOptions } from "$lib/pocketbase/index.d";

const pb = new PocketBase(PUBLIC_PB_HOST) as TypedPocketBase;
pb.autoCancellation(false);

export default pb;
export { Collections, OrdersStateOptions };
export { Collections, OrderStateOptions };
export type * from "$lib/pocketbase/index.d";
6 changes: 3 additions & 3 deletions src/lib/stores/menuStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { createGenericPbStore } from "$stores/pbStore";
import { Collections } from "$lib/pocketbase";
import { Item, Category } from "$lib/types";

export const categories = createGenericPbStore(Collections.Categories, Category, {
export const categories = createGenericPbStore(Collections.Category, Category, {
sort: "sort_order",
expand: "drinks_via_category"
expand: "item_via_category"
});

export const items = createGenericPbStore(Collections.Drinks, Item);
export const items = createGenericPbStore(Collections.Item, Item);
Loading

0 comments on commit d513462

Please sign in to comment.