Skip to content

Commit

Permalink
type placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciabad committed Apr 7, 2024
1 parent 71f7348 commit 8dc78af
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 40 deletions.
7 changes: 4 additions & 3 deletions src/server/api/router/admin/images.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sql } from 'drizzle-orm'
import 'server-only'

import { sql } from 'drizzle-orm'
import { z } from 'zod'
import { numericIdSchema } from '~/schemas/shared'

import { db } from '~/server/db/db'
import { adminProcedure, router } from '~/server/trpc'

Expand All @@ -13,7 +13,8 @@ const getByIdSchema = z.object({
const getAllImages = db.query.images.findMany().prepare('images/getAll')
const getById = db.query.images
.findFirst({
where: (image, { eq }) => eq(image.id, sql.placeholder('id')),
where: (image, { eq }) =>
eq(image.id, sql`${sql.placeholder('id')}::integer`),
})
.prepare('images/getById')

Expand Down
19 changes: 13 additions & 6 deletions src/server/api/router/admin/places.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,19 @@ const getAllPlaceIds = db
.where(
and(
or(
isNull(sql.placeholder('categoryId')),
eq(places.mainCategoryId, sql.placeholder('categoryId')),
eq(placesToPlaceCategories.categoryId, sql.placeholder('categoryId'))
isNull(sql`${sql.placeholder('categoryId')}::integer`),
eq(
places.mainCategoryId,
sql`${sql.placeholder('categoryId')}::integer`
),
eq(
placesToPlaceCategories.categoryId,
sql`${sql.placeholder('categoryId')}::integer`
)
),
or(
isNull(sql.placeholder('query')),
like(places.name, sql.placeholder('query'))
isNull(sql`${sql.placeholder('query')}::text`),
like(places.name, sql`${sql.placeholder('query')}::text`)
)
)
)
Expand Down Expand Up @@ -141,7 +147,8 @@ const getPlace = flattenTranslationsOnExecute(
extras: {
location: selectPoint('location', places.location),
},
where: (place, { eq }) => eq(place.id, sql.placeholder('id')),
where: (place, { eq }) =>
eq(place.id, sql`${sql.placeholder('id')}::integer`),
with: {
mainImage: true,
externalLinks: withTranslations({}),
Expand Down
19 changes: 13 additions & 6 deletions src/server/api/router/admin/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ const getAllRouteIds = db
.where(
and(
or(
isNull(sql.placeholder('categoryId')),
eq(routes.mainCategoryId, sql.placeholder('categoryId')),
eq(routesToRouteCategories.categoryId, sql.placeholder('categoryId'))
isNull(sql`${sql.placeholder('categoryId')}::integer`),
eq(
routes.mainCategoryId,
sql`${sql.placeholder('categoryId')}::integer`
),
eq(
routesToRouteCategories.categoryId,
sql`${sql.placeholder('categoryId')}::integer`
)
),
or(
isNull(sql.placeholder('query')),
like(routes.name, sql.placeholder('query'))
isNull(sql`${sql.placeholder('query')}::text`),
like(routes.name, sql`${sql.placeholder('query')}::text`)
)
)
)
Expand Down Expand Up @@ -143,7 +149,8 @@ const getRoute = flattenTranslationsOnExecute(
extras: {
path: selectMultiLine('path', routes.path),
},
where: (route, { eq }) => eq(route.id, sql.placeholder('id')),
where: (route, { eq }) =>
eq(route.id, sql`${sql.placeholder('id')}::integer`),
with: {
mainImage: true,
externalLinks: withTranslations({}),
Expand Down
6 changes: 4 additions & 2 deletions src/server/api/router/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const getPlace = flattenTranslationsOnExecute(
name: true,
description: true,
},
where: (place, { eq }) => eq(place.id, sql.placeholder('id')),
where: (place, { eq }) =>
eq(place.id, sql`${sql.placeholder('id')}::integer`),
with: {
mainImage: {
columns: {
Expand Down Expand Up @@ -59,7 +60,8 @@ const getRoute = flattenTranslationsOnExecute(
name: true,
description: true,
},
where: (route, { eq }) => eq(route.id, sql.placeholder('id')),
where: (route, { eq }) =>
eq(route.id, sql`${sql.placeholder('id')}::integer`),
with: {
mainImage: {
columns: {
Expand Down
22 changes: 15 additions & 7 deletions src/server/api/router/missions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ const getVisitMissions = flattenTranslationsOnExecute(
and(
eq(placeCategories.hasVisitMission, true),
or(
isNull(sql.placeholder('placeId')),
isNull(sql`${sql.placeholder('placeId')}::integer`),
eq(
placeCategories.id,
db
.selectDistinct({ data: places.mainCategoryId })
.from(places)
.where(eq(places.id, sql.placeholder('placeId')))
.where(
eq(places.id, sql`${sql.placeholder('placeId')}::integer`)
)
),
inArray(
placeCategories.id,
Expand All @@ -50,7 +52,7 @@ const getVisitMissions = flattenTranslationsOnExecute(
.where(
eq(
placesToPlaceCategories.placeId,
sql.placeholder('placeId')
sql`${sql.placeholder('placeId')}::integer`
)
)
)
Expand Down Expand Up @@ -101,8 +103,11 @@ const getVisitMissions = flattenTranslationsOnExecute(
],
where: (verification, { or, isNull, eq }) =>
or(
isNull(sql.placeholder('userId')),
eq(verification.userId, sql.placeholder('userId'))
isNull(sql`${sql.placeholder('userId')}::text`),
eq(
verification.userId,
sql`${sql.placeholder('userId')}::text`
)
),
limit: 1,
},
Expand Down Expand Up @@ -152,8 +157,11 @@ const getVisitMissions = flattenTranslationsOnExecute(
],
where: (verification, { or, isNull, eq }) =>
or(
isNull(sql.placeholder('userId')),
eq(verification.userId, sql.placeholder('userId'))
isNull(sql`${sql.placeholder('userId')}::text`),
eq(
verification.userId,
sql`${sql.placeholder('userId')}::text`
)
),
limit: 1,
},
Expand Down
14 changes: 10 additions & 4 deletions src/server/api/router/placeLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { protectedProcedure, router } from '~/server/trpc'
const addToPlaceList = db
.insert(placeListToPlace)
.values({
placeListId: sql.placeholder('placeListId'),
placeId: sql.placeholder('placeId'),
placeListId: sql`${sql.placeholder('placeListId')}::integer`,
placeId: sql`${sql.placeholder('placeId')}::integer`,
})
.prepare('placeLists/addToPlaceList')

Expand All @@ -32,7 +32,10 @@ const getPlacesFromPlaceListQuery = flattenTranslationsOnExecute(
addedAt: true,
},
where: (placeList, { eq }) =>
eq(placeList.placeListId, sql.placeholder('placeListId')),
eq(
placeList.placeListId,
sql`${sql.placeholder('placeListId')}::integer`
),

with: {
place: withTranslations({
Expand Down Expand Up @@ -80,7 +83,10 @@ const getPlacesFromPlaceListCountQuery = db.query.placeListToPlace
placeId: true,
},
where: (placeList, { eq }) =>
eq(placeList.placeListId, sql.placeholder('placeListId')),
eq(
placeList.placeListId,
sql`${sql.placeholder('placeListId')}::integer`
),
})
.prepare('placeLists/getPlacesFromPlaceListCount')

Expand Down
7 changes: 4 additions & 3 deletions src/server/api/router/places.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ const getPlace = flattenTranslationsOnExecute(
extras: {
location: selectPoint('location', places.location),
},
where: (place, { eq }) => eq(place.id, sql.placeholder('id')),
where: (place, { eq }) =>
eq(place.id, sql`${sql.placeholder('id')}::integer`),
with: {
mainImage: true,
externalLinks: withTranslations({}),
Expand Down Expand Up @@ -63,8 +64,8 @@ const getPlace = flattenTranslationsOnExecute(
],
where: (verification, { or, isNull, eq }) =>
or(
isNull(sql.placeholder('userId')),
eq(verification.userId, sql.placeholder('userId'))
isNull(sql`${sql.placeholder('userId')}::text`),
eq(verification.userId, sql`${sql.placeholder('userId')}::text`)
),
limit: 1,
},
Expand Down
5 changes: 3 additions & 2 deletions src/server/api/router/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sql } from 'drizzle-orm'
import 'server-only'

import { sql } from 'drizzle-orm'
import { calculatePath } from '~/helpers/spatial-data/multi-line'
import { getRoutesSchema } from '~/schemas/routes'
import { db } from '~/server/db/db'
Expand Down Expand Up @@ -28,7 +28,8 @@ const getRoute = flattenTranslationsOnExecute(
path: selectMultiLine('path', routes.path),
},
orderBy: [ascNullsEnd(routes.importance)],
where: (route, { eq }) => eq(route.id, sql.placeholder('id')),
where: (route, { eq }) =>
eq(route.id, sql`${sql.placeholder('id')}::integer`),
with: {
mainImage: true,
externalLinks: withTranslations({}),
Expand Down
20 changes: 16 additions & 4 deletions src/server/api/router/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ const searchPlacesByMainCategory = flattenTranslationsOnExecute(
where: (place, { eq, and, isNotNull }) =>
and(
isNotNull(place.mainCategoryId),
eq(place.mainCategoryId, sql.placeholder('category'))
eq(
place.mainCategoryId,
sql`${sql.placeholder('category')}::integer`
)
),
with: {
mainImage: true,
Expand Down Expand Up @@ -65,7 +68,10 @@ const searchPlacesBySecondaryCategory = doSomethingAfterExecute(
where: (placeToCategory, { eq, and, isNotNull }) =>
and(
isNotNull(placeToCategory.categoryId),
eq(placeToCategory.categoryId, sql.placeholder('category'))
eq(
placeToCategory.categoryId,
sql`${sql.placeholder('category')}::integer`
)
),
with: {
place: withTranslations({
Expand Down Expand Up @@ -122,7 +128,10 @@ const searchRoutesByMainCategory = flattenTranslationsOnExecute(
where: (route, { eq, and, isNotNull }) =>
and(
isNotNull(route.mainCategoryId),
eq(route.mainCategoryId, sql.placeholder('category'))
eq(
route.mainCategoryId,
sql`${sql.placeholder('category')}::integer`
)
),
with: {
mainImage: true,
Expand Down Expand Up @@ -158,7 +167,10 @@ const searchRoutesBySecondaryCategory = doSomethingAfterExecute(
where: (routeToCategory, { eq, and, isNotNull }) =>
and(
isNotNull(routeToCategory.categoryId),
eq(routeToCategory.categoryId, sql.placeholder('category'))
eq(
routeToCategory.categoryId,
sql`${sql.placeholder('category')}::integer`
)
),
with: {
route: withTranslations({
Expand Down
8 changes: 6 additions & 2 deletions src/server/helpers/db-queries/placeLists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const getVisitedPlaceListId = db.query.users
columns: {
visitedPlaceListId: true,
},
where: (users, { eq }) => eq(users.id, sql.placeholder('userId')),
where: (users, { eq }) =>
eq(users.id, sql`${sql.placeholder('userId')}::text`),
})
.prepare('helpers/placeLists/getVisitedPlaceListId')

Expand All @@ -27,7 +28,10 @@ const getPlacesInList = db.query.placeListToPlace
placeId: true,
},
where: (placeListToPlace, { eq }) =>
eq(placeListToPlace.placeListId, sql.placeholder('placeListId')),
eq(
placeListToPlace.placeListId,
sql`${sql.placeholder('placeListId')}::integer`
),
})
.prepare('helpers/placeLists/getPlacesInList')

Expand Down
2 changes: 1 addition & 1 deletion src/server/helpers/translations/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function flattenTranslationsFromSelect<
* .leftJoin(translationsTable, eq(normalTable.id, translationsTable.normalTableItemId))
* .where(
* or(
* eq(translationsTable.locale, sql.placeholder('locale')),
* eq(translationsTable.locale, sql`${sql.placeholder('locale')}::text`),
* isNull(translationsTable.locale)
* )
* )
Expand Down

0 comments on commit 8dc78af

Please sign in to comment.