Serialize your lucid models as camel cased with the minimal effort possible.
By default, AdonisJS serializes Lucid models as snake case π
This is fine for some API's, but changing that behaviour to camel case should be simple.
This package does just that without having to write a single line of code πͺ
npm i @adonify/lucid-camel-case-serializer
# Or using Yarn
yarn add @adonify/lucid-camel-case-serializer
node ace configure @adonify/lucid-camel-case-serializer
If you opted in to auto serialize models and database pagination during the configuration step, then you're already done!
This will attach the camel case naming strategy to the Database module and Lucid BaseModel class.
class Todo extends BaseModel {
@column({ isPrimary: true })
public id: number;
@column()
public thingToDo: string
}
const todo = await Todo.create({ thingToDo: 'Stop coding and walk the dog π' })
console.log(todo.serialize()) // π serialization results in camel case
Before adding this package:
{
id: 1,
thing_to_do: "Stop coding and walk the dog π"
}
After adding this package π
{
id: 1,
thingToDo: "Stop coding and walk the dog π"
}
Camel cased serialization will be applied to pagination on the models and the Database module as well.
const todos = await Todo.query().paginate(1, 50)
console.log(todos.serialize()) // π paginated serialization
Results in:
{
meta: {
total: 1,
perPage: 50,
currentPage: 1,
lastPage: 1,
firstPage: 1,
firstPageUrl: "/?page=1",
lastPageUrl: "/?page=1",
nextPageUrl: null,
previousPageUrl: null
},
data: [ { id: 1, thingToDo: "Stop coding and walk the dog π" } ]
}
There are a few ways you can do this in your AdonisJS app.
export default class AppProvider {
constructor(protected app: ApplicationContract) {}
public async boot() {
const { default: CamelCaseNamingStrategy } = await import('@ioc:Adonify/LucidCamelCaseSerializer')
// Registers the naming strategy on the base model
const { BaseModel } = this.app.container.use('Adonis/Lucid/Orm')
BaseModel.namingStrategy = new CamelCaseNamingStrategy()
// Registers the naming strategy on the database paginator
const Database = this.app.container.use('Adonis/Lucid/Database')
Database.SimplePaginator.namingStrategy = {
paginationMetaKeys() {
return new CamelCaseNamingStrategy().paginationMetaKeys()
},
}
}
}
Choosing to auto serialize in the configuration step will essentially do this for you in the package provider.
import CamelCaseNamingStrategy from "@ioc:Adonify/LucidCamelCaseSerializer";
export default class AppBaseModel extends BaseModel {
public static namingStrategy = new CamelCaseNamingStrategy() // π set as naming strategy
}
This will serialize your models but not paginated calls directly to the Database module.
import CamelCaseNamingStrategy from "@ioc:Adonify/LucidCamelCaseSerializer";
class Todo extends BaseModel {
public static namingStrategy = new CamelCaseNamingStrategy() // π set as naming strategy
@column({ isPrimary: true })
public id: number;
@column()
public thingToDo: string
}
import CamelCaseNamingStrategy from "@ioc:Adonify/LucidCamelCaseSerializer";
import Database from '@ioc:Adonis/Lucid/Database'
Database.SimplePaginator.namingStrategy = new CamelCaseNamingStrategy()
import Database from '@ioc:Adonis/Lucid/Database'
const paginator = await Database.from('todos').paginate()
paginator.namingStrategy = new CamelCaseNamingStrategy()
return paginator.toJSON()
If you have a question or found a bug, feel free to open an issue.