madx
10/28/2021, 10:51 PMsupabase-js and I found out that it would be great to have a way to convert the casing in responses (Postgres convention is snake_case while JS's one is camelCase for example) to match the language's conventions.
This is something that some ORMs provide, such as Objection https://vincit.github.io/objection.js/recipes/snake-case-to-camel-case-conversion.html
In the meantime, I hacked a custom replacer using proxies:
js
function withCamelCase(builder) {
return new Proxy(builder, {
get: (target, prop, receiver) => {
if (prop === "then") {
return (...args) =>
target
.then((response) => ({
...response,
data: camelCase(response.data, { deep: true }),
}))
.then(...args)
}
if (typeof target[prop] === "function") {
return (...args) => withCamelCase(target[prop](...args))
}
return Reflect.get(target, prop, receiver)
},
})
}
export default () => {
const client = createClient(SUPABASE_URL, SUPABASE_KEY)
return withCamelCase(client)
}