Skip to content

Instantly share code, notes, and snippets.

@betocmn
Last active May 19, 2021 01:15
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save betocmn/68cf08701ae02140840fc67e80c4cec8 to your computer and use it in GitHub Desktop.
import {
db,
foreignKeyReplacement,
formatFilter,
formatOrderBy,
} from 'src/lib/db'
import { requireAuth } from 'src/lib/auth'
const filterFieldMap = {
KEYWORD: {
OR: [
{ name: { contains: '{str}', mode: 'insensitive' } },
{ state: { contains: '{str}', mode: 'insensitive' } },
{ country: { name: { contains: '{str}', mode: 'insensitive' } } },
],
},
}
const orderByFieldMap = {
NAME: 'name',
STATE: 'state',
}
export const cities = ({
page = 1,
limit = 100,
filter = {},
orderBy = [{ field: 'NAME', direction: 'ASC' }],
}) => {
return db.city.findMany({
take: limit,
skip: (page - 1) * limit,
where: formatFilter(filter, filterFieldMap),
orderBy: formatOrderBy(orderBy, orderByFieldMap),
})
}
export const allCities = async ({
page = 1,
limit = 100,
filter = {},
orderBy = [{ field: 'NAME', direction: 'ASC' }],
}) => {
return {
cities: cities({ page, limit, filter, orderBy }),
count: db.city.count({ where: formatFilter(filter, filterFieldMap) }),
}
}
export const city = ({ id }) => {
return db.city.findOne({
where: { id },
})
}
export const createCity = ({ input }) => {
requireAuth()
return db.city.create({
data: foreignKeyReplacement(input),
})
}
export const updateCity = ({ id, input }) => {
requireAuth()
return db.city.update({
data: foreignKeyReplacement(input),
where: { id },
})
}
export const deleteCity = ({ id }) => {
requireAuth()
return db.city.delete({
where: { id },
})
}
export const City = {
country: (_obj, { root }) =>
db.city.findOne({ where: { id: root.id } }).country(),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment