question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto mapping the field names to snake_case

See original GitHub issue

I’ve been trying to find a way to auto map the field names to snake case, this library has potential to do it cleanly. I think the easiest way right now is to have a helper function to inject a map attribute to the field options:

import { snakeCase } from 'snake-case';

function addStringFieldMappedSnakeCase(model: PrismaModel, name: string, options: StringFieldOption) {
    model.string(name, { map: snakeCase(name), ...options });

    return model;
}

But then I will have to add a similar method for each different field types in order to staisify the types. This also breaks the nice method chaining while defining the model.

Would you consider expand the API for this purpose? For example allowing users to retrieve all fields definition to edit them afterwards within the model declaration:

export default createModel((model) => {
    model
        .string('firstName')
        .string('lastName')
        .dateTime('createdAt');

    model.fields.forEach((field) => ({
        ...field,
        map: snakeCase(field.name),
    }));
});

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ridafkihcommented, Sep 24, 2022

@thwonghin Absolutely.

0reactions
thwonghincommented, Oct 16, 2022

I gave up on using Prisma so I will not continue to implement this. Good luck with the project!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to map camelCase properties to snake_case column ...
Learn how to map CameCase entity properties like phoneNumber to SnakeCase column names like phone_number using a Hibernate naming strategy.
Read more >
java - How to map Hibernate entity fields using camelCase to ...
You can achieve this using a custom Hibernate naming strategy. All you need to do is to use the hibernate-types open-source project.
Read more >
Automatically pascal case model names during introspection
change field names from snake case to pascal case and updates references to those fields in relations and indexes. Add @Map annotation to...
Read more >
Using snake case column names with Dapper and PostgreSQL
In this post I describe how to use snake case naming conventions when using Dapper, by using "Schema" utility classes, nameof(), ...
Read more >
Deserialize Snake Case to Camel Case With Jackson
4. Use @JsonProperty Annotation. We can use the @JsonProperty annotation on the fields of our class to map the fields to the exact...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found