Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mapping table and column name to different entity and field name #5943

Open
daneshk opened this issue Jan 14, 2024 · 3 comments
Assignees
Labels
module/persist Status/Draft In circulation by the author for initial review and consensus-building Type/Proposal

Comments

@daneshk
Copy link
Member

daneshk commented Jan 14, 2024

Summary

The current bal persist design doesn't support mapping entities to the DB table that have different names. Always entity name and table name should be the same. The same thing applies to the column name and entity field name. When we do DB introspection, we need to support this.

@daneshk
Copy link
Member Author

daneshk commented Jan 15, 2024

Proposed Design

# Defines the database column name which matches the record field. The default value is the record field name. 
public type MapConfig record {|
    # The DB table/column name
    string name;
|};

# The Annotation used to map database table and columns to record and record fields
public annotation MapConfig Map on record field, record;

So developer can give the mapping DB table name and column names for the record and record field using Mapping annotation in the data model definition as follows,

import ballerina/persist as _;
import ballerinax/persist.sql;

@sql:Map("DOCTORS")
type Doctor record {|
    @sql:Map("doc_id")
    readonly int id;
    string name;
    string specialty;
    string phoneNumber;
    Appointment[] appointment;
|};

@daneshk daneshk added the Status/Draft In circulation by the author for initial review and consensus-building label Jan 15, 2024
@daneshk daneshk self-assigned this Jan 17, 2024
@daneshk
Copy link
Member Author

daneshk commented Feb 1, 2024

Based on the offline discussion, annotation names should be a noun instead of a verb.
@sameerajayasoma How about going with the @sql:Mapping annotation(Similar to @sql:Generated annotation) like the below?

import ballerina/persist as _;
import ballerinax/persist.sql;

@sql:Mapping("DOCTORS")
type Doctor record {|
    @sql:Mapping("doc_id")
    readonly int id;
    string name;
    string specialty;
    string phoneNumber;
    Appointment[] appointment;
|};

@daneshk
Copy link
Member Author

daneshk commented Mar 13, 2024

Another option would be to have a Name annotation instead of a Mapping annotation similar to the xmldata module. like below,

# Defines the database column name which matches the record field. The default value is the record field name. 
public type NameConfig record {|
    # The DB table/column value
    string value;
|};

# The Annotation used to map database table and columns to record and record fields
public annotation NameConfig Name on record field, record;

The usages,

import ballerina/persist as _;
import ballerinax/persist.sql;

@sql:Name {value: "DOCTORS"}
type Doctor record {|
    @sql:Name {value: "doc_id"}
    readonly int id;
    string name;
    string specialty;
    string phoneNumber;
    Appointment[] appointment;
|};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module/persist Status/Draft In circulation by the author for initial review and consensus-building Type/Proposal
Projects
Status: BackLog
Development

No branches or pull requests

1 participant