-
Notifications
You must be signed in to change notification settings - Fork 66
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
Improve DB columns to Ballerina record Mapping through Annotation #2652
Improve DB columns to Ballerina record Mapping through Annotation #2652
Comments
Proposal: Support Annotations for DB column to Ballerina Record MappingSummaryEnhance type Person record {
int id;
string first_name;
string last_name
};
Stream<Person, sql:Error?> persons = database->query(`SELECT is, first_name, last_name FROM ATS_PERSON`); Annotations will be bound to HistoryThe 1.2.x version of SQL packages included mapping support. However, the mapping was limited to only the order of returned columns to the record fields. The package will map The first column of the result will be mapped to the first field in the records. This feature held a few setbacks. Since the user now has to keep the order even if the mapping is not needed. Goals
MotivationThe ability to map table columns to Ballerina record fields in the remote function is efficient. In typical enterprise use cases, it is typical that the user would not have control over the database schema and its naming convention. With this support, the user does not need to process the return results to another record or change the query itself to change the metadata. DescriptionIntroduce type Person record {
int id;
@sql:Column { name: "first_name" }
string firstName;
@sql:Column { name: "last_name" }
string lastName
}; The above annotation will map the database column The annotation will be defined as,
The |
Opened a spec issue to request support for type Person record {
int id;
@sql:Column { "first_name" }
string firstName;
@sql:Column { "last_name" }
string lastName
}; |
IMO, having the annotation as
will be better. Because in future we can then add more fields to the annotation. Like column data type, required or not, unique or not, etc. With such information, we can improve this to generate database tables as in Hibernate. Therefore, it's better to keep room for such improvements with an annotation like the one above IMO. WDYT? |
Thanks, @IMS94 for the input. AFAIU The difference between JPA/HIbernate entities and Ballerina records is, in Hibernate, use for all SQL operations and it is a direct mapping for SQL Tables. But in Ballerina records use only for querying operations to map results to the record value. So most of the properties like data types, unique, nullable are not applicable for Ballerina. We will check further @niveathika are there any properties that can be included, in the annotation. |
@IMS94 Thank you for the suggestion. However, as @daneshk mentioned, these will not be applicable as we are not doing any operations which accepts Ballerina record for SQL operations. I agree likewise to Hiberanate, it would be cool, if we can support database operations, with only Ballerina code.(Currently we are exploring this option to come up with a plan 😄 ). Even if we support this, IMO that support should be on top |
@daneshk @niveathika Agreed that a library like Hibernate should come on top of At current version (say 1.0) this is how the annotation will look like:
In the next minor version (Say 1.1) we decide to add support for data type. Then the annotation will look like this:
Rather than introducing a new annotation to add additional details, we could modify the existing Not sure if my argument is correct when considering backward compatibility. Just wanted to convey the idea 🙂 |
@IMS94 My argument is, if the improvements are in different package should that be included in
Anyways Ballerina Can only keep forward compatibility not backward compatibility on coming releases. i.e Any new functionality will never be supported by older versions. |
@niveathika IMO, having to use 2 annotations for a column definition is not good for developer experience. I'm suggesting to make this annotation similar to JPA (java persistence API) If we can make an annotation similar to JPA's |
All we would be continuing with using |
Why can’t we have this support from the language side?. We even have a similar requirement in gRPC too. Let's say we add a similar kind of annotations for gRPC records too. Then, when someone develops a Ballerina app using gRPC and SQL has to maintain several annotations that would reduce the maintainability of the app. |
@BuddhiWathsala +1 for a language common annotation to indicate field mapping. Could you open a spec issue for this? We can depreciate this once language annotation is live. |
One problem I see is requirements will get changed from module to module. For example, the SQL module needs to map the column name to a different field name. But in the gRPC module, we need to attach field index to the field. If we define it at the language level, we need an annotation record that supports both. Some fields may apply only to specific modules. |
@sameerajayasoma Please review and give your input |
I also believe that we shouldn't mix ORM-ish concepts at this level. Let's stick to the By the way, when we have an ORM-ish solution for Ballerina, the direct |
@niveathika @daneshk Can you please update the proposal with the definition of |
@sameerajayasoma Updated the proposal |
The design looks good to me. Can you please take another shot at code documentation? |
@sameerajayasoma Tweaked the documentation as follows,
|
LGTM |
Description:
With the current design, the db columns must be matched with the Ballerina record for a select query.
id int auto_increment,
first_name varchar(100) not null,
last_name varchar(200) not null,
constraint ATS_PERSON_pk
primary key (id)
);
int id;
string first_name;
string last_name
};
In order to change the field names in the Ballerina record,
select id, first_name as firstName, last_name as lastName from ATS_PERSON
Annotation can be used to support the mapping of the DB columns to the Ballerina record. This will give value addition to the users.
The text was updated successfully, but these errors were encountered: