Last active
May 19, 2021 01:15
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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