npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@adonify/lucid-camel-case-serializer

v1.0.2

Published

Serialize your lucid models as camel cased with the minimal effort possible.

Downloads

13

Readme

Lucid Camel Case Serializer

Serialize your lucid models as camel cased with the minimal effort possible.

npm-image license-image typescript-image

Introduction

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 🐪

Installation

npm i @adonify/lucid-camel-case-serializer

# Or using Yarn

yarn add @adonify/lucid-camel-case-serializer

Configuration

node ace configure @adonify/lucid-camel-case-serializer

Usage

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 🐕" } ]
}

Manually using the naming strategy

There are a few ways you can do this in your AdonisJS app.

In a provider (suggested method)


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.

Create a base model your models will extend and register the strategy

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.

Add the naming strategy to specific models

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
}

Adding the serializer to the Database paginator directly

import CamelCaseNamingStrategy from "@ioc:Adonify/LucidCamelCaseSerializer";
import Database from '@ioc:Adonis/Lucid/Database'

Database.SimplePaginator.namingStrategy = new CamelCaseNamingStrategy()

Adding the paginator on the fly

import Database from '@ioc:Adonis/Lucid/Database'

const paginator = await Database.from('todos').paginate()
paginator.namingStrategy = new CamelCaseNamingStrategy()

return paginator.toJSON()

Issues

If you have a question or found a bug, feel free to open an issue.