• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ballerina Persist module

Ballerina Persist Library

Build codecov Trivy GraalVM Check GitHub Last Commit GitHub Issues

This library provides Ballerina persist features, which provides functionality to store and query data conveniently through a data model.

The persist tools provides following functionalities,

  1. Define and validate the entity data model definitions in the persist folder
  2. Initialize the Ballerina Persistence Layer for every model definitions in the persist folder
  3. Generate persistence derived entity types and clients
  4. Push persistence schema to the data store (only with supported data sources)
  5. Migration support for supported data stores (experimental feature)

Data Model Definitions

Within a Ballerina project, the data model should be defined in a separate bal file under the persist directory. This file is not considered part of the Ballerina project and is used only for data model definition.

The Ballerina persist library defines a mechanism to express the application's data model using Ballerina record types. All record types will be an entity in the model.

Entity Type Definition

An EntityType is defined using SimpleType and EntityType fields.

   // These are the type definitions for the data model when using MySQL
    type SimpleType ()|boolean|int|float|decimal|string|byte[]|time:Date|time:TimeOfDay|time:Utc|time:Civil;
    type EntityType record {|
       SimpleType|EntityType|EntityType[]...;
    |};

Note: The data types for SimpleType supported by persist will vary by data source. For example, the byte type is not supported by MySQL.

This design use fields of type EntityType or EntityType[] to define associations between two entities.

Here are some examples of entity type definitions:

// Valid with MySQL, in-memory and Google Sheets
type Employee record {|
   int id;
   string fname;
   string lname;
   Department department; // EntityType
|};

// Valid with MySQL and in-memory
type Department record {|
   int id;
   string name;
   byte[] logo; // Google Sheets does not support array types
   Employee[] employees; // EntityType
|};

// Valid with in-memory only
type Department record {|
   int id;
   string name;
   int[] integerArray; // MySQL only supports `byte[]` array type and Google Sheets does not support array types
   Employee[] employees; // EntityType
|};

// Invalid with all data sources
 Employee record {|
   int|string id;  // Persist does not support union data types
   string fname;
   string lname;
   Department department; // EntityType
|};

Entity Attributes Definition

Ballerina record fields are used to model the attributes of an entity. The type of the field should be a subtype of SimpleType.

Identity Field(s)

The entity must contain at least one identity field. The field's value is used to identify each record uniquely. The identity field(s) is indicated readonly flag.

Note: Only int, string, float, boolean, and decimal types are supported as identity fields.

type EntityType record {|
    readonly T <fieldName>;
|} 

The identity field can be a single field or a combination of multiple fields.

type EntityType record {|
    readonly T <fieldName1>;
    readonly T <fieldName2>;
|} 

Nullable Field(s)

Say type T is a subtype of SimpleType, and T does not contain (),

Field definition Semantics Examples
T field Mapped to a non-nullable type in the datastore (if supported) int id;
T? field Mapped to a nullable type in the datastore (if supported) string? description;
T field? Not allowed -
T? field? Not allowed -

Relationship Definition

Ballerina record fields are used to model a connection between two entities. The type of the field should be a subtype of EntityType|EntityType?|EntityType[].

This design supports the following cardinalities:

  1. One-to-one (1-1)
  2. One-to-many (1-n)

The relation field is mandatory in both entities.

One-to-one (1-1)

A 1-1 relationship is defined by a field of type EntityType in one entity and EntityType? in the other.

type Car record {|
   readonly int id;
   string name;
   User owner;
|};

type User record {|
   readonly int id;
   string name;
   Car? car;
|};

The above entities explains the following,

  • A Car must have a User as the owner.
  • A User may own a Car or do not own one.

The first record, Car, which holds the EntityType field owner is taken as the owner in the 1-1 relationship and will include the foreign key of the second record, User.

The default foreign key field name will be ownerId in the Car table, which refers to the identity field of the User table by default. (<lowercasedRelatedFieldName><First-LetterCapitalizedIdentityFieldName>)

One-to-Many (1-n)

A 1-n relationship is defined by a field of type EntityType in one entity and EntityType[] in the other.

type Car record {|
   readonly int id;
   string name;
   User owner;
|};

type User record {|
   int id;
   string name;
   Car[] cars;
|};

The above entities explains the following,

  • A Car must have a User as the owner.
  • A User may own multiple Cars or do not own one. (Represented with empty array [])

The entity that contains the field of type EntityType is taken as the owner in the 1-n relationship.

Issues and projects

Issues and Projects tabs are disabled for this repository as this is part of the Ballerina standard library. To report bugs, request new features, start new discussions, view project boards, etc. please visit Ballerina standard library parent repository.

This repository only contains the source code for the package.

Building from the source

Set up the prerequisites

  1. Download and install Java SE Development Kit (JDK) version 17 (from one of the following locations).

  2. Export your GitHub personal access token with the read package permissions as follows.

     export packageUser=<Username>
     export packagePAT=<Personal access token>
    

Building the source

Execute the commands below to build from source.

  1. To build the library:

     ./gradlew clean build
    
  2. Publish ZIP artifact to the local .m2 repository:

     ./gradlew clean build publishToMavenLocal
    
  3. Publish the generated artifacts to the local Ballerina central repository:

     ./gradlew clean build -PpublishToLocalCentral=true
    
  4. Publish the generated artifacts to the Ballerina central repository:

     ./gradlew clean build -PpublishToCentral=true
    

Contributing to Ballerina

As an open source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All contributors are encouraged to read the Ballerina code of conduct.

Useful links

More Repositories

1

ballerina-lang

The Ballerina Programming Language
Ballerina
3,286
star
2

lsp4intellij

This language client library provides language server protocol support for IntelliJ IDEA and other Jetbrains IDEs.
Java
413
star
3

ballerina-spec

Ballerina Language and Platform Specifications
HTML
171
star
4

ballerina-dev-website

Dev version of the ballerina.io website
HTML
163
star
5

module-ballerina-graphql

The Ballerina GraphQL module is part of the Ballerina Standard Library. It is a spec-compliant, production-ready GraphQL implementation for writing GraphQL APIs in Ballerina.
Ballerina
144
star
6

nballerina

Ballerina compiler that generates native executables.
Ballerina
142
star
7

ballerina-library

The Ballerina Library
Ballerina
137
star
8

module-ballerina-jwt

Ballerina JWT module.
Ballerina
130
star
9

openapi-tools

Ballerina OpenApi-Tool
Java
129
star
10

module-ballerina-grpc

Ballerina gRPC Module
Ballerina
128
star
11

ballerina-release

Ballerina release scripts
Python
126
star
12

openapi-connectors

Generate Ballerina connector with OpenAPI definition
Ballerina
126
star
13

module-ballerina-http

Ballerina HTTP Module
Java
124
star
14

module-ballerinax-nats

Ballerina NATS Module.
Ballerina
124
star
15

ballerina-platform.github.io

ballerina-platform.github.io - Github pages based ballerina.io website
HTML
124
star
16

ballerina-action

Dockerfile
124
star
17

module-ballerina-io

Ballerina io Module
Ballerina
123
star
18

module-ballerina-tcp

Ballerina socket module
Java
122
star
19

module-ballerina-oauth2

Ballerina OAuth2 Module
Ballerina
122
star
20

module-ballerina-websocket

Ballerina WebSocket Module
Java
121
star
21

module-ballerina-websub

Ballerina Websub module.
Ballerina
120
star
22

module-ballerina-mime

Ballerina MIME Module
Java
119
star
23

plugin-intellij

Ballerina extension for IntelliJ IDEA.
Java
119
star
24

module-ballerinax-mysql

Ballerina mysql Module
Ballerina
119
star
25

module-ballerina-auth

Ballerina Auth Module
Java
119
star
26

module-ballerina-sql

Ballerina SQL Module
Java
119
star
27

module-ballerina-email

Ballerina module to send and receive emails
Java
119
star
28

module-ballerinax-kafka

Ballerina Kafka Module.
Ballerina
119
star
29

module-ballerina-udp

Ballerina UDP module enables transport layer communication over UDP protocol.
Java
118
star
30

module-ballerinax-java.jdbc

Ballerina JDBC Module
Ballerina
118
star
31

module-ballerina-cache

Ballerina cache Module
Ballerina
118
star
32

module-ballerina-log

Ballerina log Module
Ballerina
118
star
33

module-ballerina-c2c

Ballerina Code2Cloud implementation
Java
118
star
34

module-ballerinax-slack

Ballerina slack module
Ballerina
118
star
35

module-ballerinax-azure-cosmosdb

Ballerina
118
star
36

plugin-vscode-compiler-toolkit

Compiler tools for Ballerina developers
TypeScript
118
star
37

ballerina-dev-tools

Ballerina Developer Tooling
Java
118
star
38

module-ballerinax-stan

Ballerina NATS Streaming Module.
Java
117
star
39

module-ballerina-crypto

Ballerina crypto Module
Ballerina
117
star
40

module-ballerina-websubhub

This modules includes a bunch of APIs to facilitate writing different WebSub Hub implementations
Ballerina
116
star
41

module-ballerinax-googleapis.calendar

Connector repository for Google Calendar API.
Ballerina
116
star
42

module-ballerina-xmldata

Ballerina xml utils Module
Ballerina
116
star
43

module-ballerinax-postgresql

Ballerina PostgreSQL DB module
Ballerina
116
star
44

module-ballerinax-java.jms

Ballerina
116
star
45

module-ballerina-file

Ballerina File Module
Ballerina
116
star
46

module-ballerinax-azure-service-bus

Ballerina
116
star
47

module-ballerinax-aws.dynamodb

This is to keep the Amazon DynamoDB connector for Ballerina.
Ballerina
116
star
48

module-ballerinax-aws.s3

Ballerina
116
star
49

module-ballerina-task

Ballerina task Module
Java
116
star
50

module-ballerina-time

Ballerina time Module
Ballerina
116
star
51

module-ballerinax-azure.functions

The implementation of Azure Functions compiler extension for Ballerina.
Java
116
star
52

module-ballerinax-datamapper

A compiler extension to extract abstract representation of Ballerina connector actions and their associated types
Java
116
star
53

module-ballerina-uuid

Ballerina UUID Module
Ballerina
116
star
54

module-ballerinax-netsuite

The Ballerina connector to perform operations on Netsuite integrate cloud system.
Ballerina
116
star
55

module-ballerinax-twitter

This repo is to keep Ballerina Twitter connector implementation for Ballerina
Ballerina
116
star
56

ballerina-update-tool

Ballerina Update Tool implementation to manage Ballerina versions
Java
116
star
57

module-ballerinax-ai.agent

Ballerina ReAct type Agent module using Large language models (LLMs)
Ballerina
115
star
58

module-ballerina-os

Ballerina system Module
Java
115
star
59

module-ballerinax-jaeger

Ballerina Jaeger Observability Extension Module
Java
115
star
60

module-ballerinax-aws.sqs

Ballerina
115
star
61

module-ballerinax-mssql

Ballerina MSSQL DB module
Ballerina
115
star
62

module-ballerinax-aws.lambda

Java
115
star
63

module-ballerina-serdes

This is the Ballerina SerDes package, which is a part of the Ballerina Language Standard Library
Java
115
star
64

module-ballerinax-oracledb

Oracle Database Connector for Ballerina
Ballerina
115
star
65

module-ballerina-xslt

Ballerina xslt module
Java
115
star
66

module-ballerina-url

Ballerina encoding module.
Ballerina
115
star
67

module-ballerinax-rabbitmq

Ballerina RabbitMQ Module.
Ballerina
115
star
68

module-ballerinax-prometheus

Ballerina Prometheus Observability Extension Module
Java
115
star
69

module-ballerinai-transaction

Ballerina internal module of transaction implementation
Ballerina
115
star
70

module-ballerinax-mysql.driver

Ballerina Azure MySQL Module
Ballerina
115
star
71

module-ballerinax-azure-storage-service

Ballerina
115
star
72

graphql-tools

Maintain the source code for GraphQL related tools.
Java
115
star
73

module-ballerina-jballerina.java.arrays

Ballerina Java Array Module
Ballerina
114
star
74

module-ballerina-constraint

Ballerina Constraint Module
Ballerina
114
star
75

module-ballerinax-choreo

Ballerina Choreo Observability Extension Module
Java
114
star
76

ballerina-performance-cloud

Ballerina Performance Tests in Cloud
Shell
114
star
77

module-ballerinax-azure.eventhub

Azure Eventhub connector
Ballerina
114
star
78

module-ballerina-regex

Ballerina Regex Module
Ballerina
114
star
79

plugin-gradle

Ballerina Gradle plugin
Groovy
114
star
80

module-ballerinax-mssql.driver

Ballerina MSSQL DB Driver
Ballerina
114
star
81

module-ballerina-random

Ballerina Random Library
Ballerina
114
star
82

module-ballerinax-health.fhir.templates

FHIR Ballerina templates
Ballerina
114
star
83

module-ballerinax-persist.sql

SQL database support of Ballerina Persist
Ballerina
114
star
84

module-ballerinax-microsoft.onedrive

The Ballerina connector to perform operations on the files, which is stored on OneDrive
Ballerina
114
star
85

ballerina-custom-jre

Generates platform-specific custom Java runtime images to be bundled with Ballerina platform distributions, which contains only the required modules for Ballerina runtime.
114
star
86

asyncapi-tools

This repository is the code base for the ballerina async-api tool
Java
114
star
87

persist-tools

Ballerina persist tools
Ballerina
113
star
88

module-ballerinax-cdata.connect

Manage Ballerina CData connector modules centrally.
Java
113
star
89

module-ballerinax-peoplehr

Ballerina connector for People HR
Ballerina
113
star
90

module-ballerinax-aws.ses

The Ballerina connector to perform operations on Amazon Simple Email Service(Amazon SES).
Ballerina
113
star
91

module-ballerinax-googleapis.people

Repository for Google People API Connector
Ballerina
113
star
92

module-ballerinax-microsoft.teams

The Ballerina Microsoft Teams Connector for teamwork and intelligent communications.
Ballerina
113
star
93

module-ballerinax-googleapis.drive

Repository for Google Drive module.
Ballerina
113
star
94

module-ballerinax-microsoft.excel

The Ballerina connector to perform operations on Excel workbooks stored in Microsoft OneDrive.
Ballerina
113
star
95

asyncapi-triggers

This repo will contain the trigger source code generated through ballerina async api tool
Ballerina
113
star
96

module-ballerinax-aws.simpledb

This is to keep the Amazon SimpleDB connector for Ballerina.
Ballerina
112
star
97

module-ballerinax-aws.sns

This repo is to keep the newly written Amazon SNS connector for Ballerina.
Ballerina
112
star
98

module-ballerina-toml

Ballerina TOML Parser
Ballerina
112
star
99

edi-tools

This library provides the functionality required to process EDI files and implement EDI integrations.
Ballerina
112
star
100

module-ballerinax-health.fhir.r4

FHIR R4 Ballerina modules
Ballerina
112
star