Skip to content

Commit

Permalink
add type annotations for db entities
Browse files Browse the repository at this point in the history
  • Loading branch information
dlustre committed Aug 5, 2024
1 parent 333f382 commit 371bd67
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 29 deletions.
4 changes: 2 additions & 2 deletions packages/api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export async function upsert<T extends TableConfig>(

if (!isNotQueryResultNever(result))
throw new Error(
`upsert: no result for table '${table._.name}' with config ${JSON.stringify(config)}`,
`[upsert > ${table._.name}]: unexpected result with config ${JSON.stringify(config)}`,
);

if (!result[0]) throw new Error("upsert: no result");
if (!result[0]) throw new Error(`[upsert > ${table._.name}]: no result`);

return result[0];
}
1 change: 1 addition & 0 deletions packages/db/src/schema/dietRestrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export const dietRestrictions = pgTable("diet_restrictions", {
...metadataColumns,
});

/** The diet restrictions of a dish. */
export type DietRestriction = typeof dietRestrictions.$inferInsert;
3 changes: 2 additions & 1 deletion packages/db/src/schema/dishes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const dishes = pgTable("dishes", {
}),
name: text("name").notNull(),
description: text("description").notNull(),
/** Defaults to "Other" if not specified */
/** Defaults to "Other" if not specified. */
category: text("category").notNull().default("Other"),
numRatings: integer("num_ratings").default(0).notNull(),
totalRating: integer("total_rating").default(0).notNull(),
Expand All @@ -40,6 +40,7 @@ export const dishRelations = relations(dishes, ({ one, many }) => ({
}),
}));

/** A dish at a restaurant. */
export type Dish = typeof dishes.$inferInsert;
export interface DishWithRelations extends Dish {
dietRestriction: DietRestriction;
Expand Down
1 change: 1 addition & 0 deletions packages/db/src/schema/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ export const eventsRelations = relations(events, ({ one }) => ({
}),
}));

/** A special event at a restaurant. */
export type Event = typeof events.$inferInsert;
export const EventSchema = createInsertSchema(events);
2 changes: 2 additions & 0 deletions packages/db/src/schema/menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,7 @@ export const dishesToMenusRelations = relations(dishesToMenus, ({ one }) => ({
}),
}));

/** A restaurant menu for a given date and period. */
export type Menu = typeof menus.$inferInsert;
/** A join between a dish and a menu. */
export type DishToMenu = typeof dishesToMenus.$inferInsert;
1 change: 1 addition & 0 deletions packages/db/src/schema/nutritionInfos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ export const nutritionInfos = pgTable("nutrition_infos", {
...metadataColumns,
});

/** The nutrition information of a dish. */
export type NutritionInfo = typeof nutritionInfos.$inferInsert;
1 change: 1 addition & 0 deletions packages/db/src/schema/periods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export const periods = pgTable("periods", {
...metadataColumns,
});

/** A meal period, e.g. breakfast. */
export type Period = typeof periods.$inferInsert;
14 changes: 8 additions & 6 deletions packages/db/src/schema/pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ export const pins = pgTable(
},
);

/**
* Pin has one:
*
* {@linkcode dishes}
* {@linkcode users}
*/
export const pinsRelations = relations(pins, ({ one }) => ({
dish: one(dishes, {
fields: [pins.dishId],
Expand All @@ -48,4 +42,12 @@ export const pinsRelations = relations(pins, ({ one }) => ({
}),
}));

/**
* A pin a user has put on a dish.
*
* A pin has one:
*
* {@linkcode dishes}
* {@linkcode users}
*/
export type Pin = typeof pins.$inferInsert;
14 changes: 8 additions & 6 deletions packages/db/src/schema/ratings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ export const ratings = pgTable(
},
);

/**
* Rating has one:
*
* {@linkcode users}
* {@linkcode dishes}
*/
export const ratingsRelations = relations(ratings, ({ one }) => ({
dish: one(dishes, {
fields: [ratings.dishId],
Expand All @@ -50,6 +44,14 @@ export const ratingsRelations = relations(ratings, ({ one }) => ({
}),
}));

/**
* A rating a user has given a dish.
*
* A rating has one:
*
* {@linkcode users}
* {@linkcode dishes}
*/
export type Rating = typeof ratings.$inferInsert;

export const RatingSchema = createInsertSchema(ratings);
14 changes: 7 additions & 7 deletions packages/db/src/schema/restaurants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ export const restaurants = pgTable("restaurants", {
...metadataColumns,
});

/**
* Restaurant has many:
*
* {@linkcode stations}
* {@linkcode menus}
* {@linkcode events}
*/
export const restaurantsRelations = relations(restaurants, ({ many }) => ({
stations: many(stations),
menus: many(menus),
events: many(events),
}));

/**
* A restaurant has many:
*
* {@linkcode stations}
* {@linkcode menus}
* {@linkcode events}
*/
export type Restaurant = typeof restaurants.$inferInsert;
7 changes: 7 additions & 0 deletions packages/db/src/schema/stations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ export const stationsRelations = relations(stations, ({ one, many }) => ({
}),
}));

/**
* A station of a restaurant.
*
* A station has many:
*
* {@linkcode dishes}
*/
export type Station = typeof stations.$inferInsert;
16 changes: 9 additions & 7 deletions packages/db/src/schema/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import { pins } from "./pins";
import { ratings } from "./ratings";
import { metadataColumns } from "./utils";

/**
* A user has many:
*
* {@linkcode pins}
* {@linkcode ratings}
*
*/
export const users = pgTable("users", {
id: text("id").primaryKey(),
name: text("name").notNull(),
Expand All @@ -24,5 +17,14 @@ export const usersRelations = relations(users, ({ many }) => ({
ratings: many(ratings),
}));

/**
* A user of the app.
*
* A user has many:
*
* {@linkcode pins}
* {@linkcode ratings}
*
*/
export type User = typeof users.$inferInsert;
export const UserSchema = createInsertSchema(users);

0 comments on commit 371bd67

Please sign in to comment.