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 mock client generation for persist clients #374

Merged
merged 19 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [Add introspection support for PostgreSQL databases](https://github.com/ballerina-platform/ballerina-library/issues/6333)
- [Add introspection support for MSSQL databases](https://github.com/ballerina-platform/ballerina-library/issues/6460)
- [Add h2 database support as a datastore](https://github.com/ballerina-platform/ballerina-library/issues/5715)
- [Add support for generating client APIs for the test datastore](https://github.com/ballerina-platform/ballerina-library/issues/5840)

## [1.3.0] - 2024-05-03

Expand Down
31 changes: 22 additions & 9 deletions docs/spec/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ This specification elaborates on the `Persist CLI Tool` commands.
## 2. Initializing the Bal Project with Persistence Layer

```bash
bal persist add --datastore="datastore" --module="module_name"
bal persist add --datastore mysql --module db --test-datastore h2
```

| Command Parameter | Description | Mandatory | Default Value |
|:-----------------:|:----------------------------------------------------------------------------------------:|:---------:|:--------------:|
| --datastore | used to indicate the preferred database client. Currently, 'mysql', 'mssql', 'google sheets' and 'postgresql' are supported. | No | inmemory |
| --module | used to indicate the persist enabled module in which the files are generated. | No | <package_name> |
| --id | Used as an identifier | No | generate-db-client |
| Command Parameter | Description | Mandatory | Default Value |
|:-----------------:|:----------------------------------------------------------------------------------------------------------------------------:|:---------:|:------------------:|
| --datastore | used to indicate the preferred database client. Currently, 'mysql', 'mssql', 'google sheets' and 'postgresql' are supported. | No | inmemory |
| --module | used to indicate the persist enabled module in which the files are generated. | No | <package_name> |
| --id | Used as an identifier | No | generate-db-client |
| --test-datastore | used to indicate the preferred database client for testing. Currently, 'h2', 'inmemory' are supported. | No | No |


The command initializes the bal project with the persistence layer. This command includes the following steps,
Expand All @@ -54,6 +55,7 @@ The command initializes the bal project with the persistence layer. This command
id = "generate-db-client"
targetModule = "<package_name>.<module_name>"
options.datastore = "<datastore>"
options.testDatastore = "<test_datastore>"
filePath = "persist/model.bal"
```

Expand All @@ -69,6 +71,7 @@ medical-center
Behaviour of the `add` command,
- Users should invoke the command within a Ballerina project.
- Users can use optional arguments to indicate the preferred module name and data store; otherwise, default values will be used.
- User can use test-datastore to indicate the preferred database client for testing. Currently, 'h2', 'inmemory' are supported. If specified it will update the Ballerina.toml file with the testDatastore option to generate the test client along with the main client at the build time.
- Users cannot execute the command multiple times within the same project. They need to remove the persist configurations from the Ballerina.toml if they want to reinitialize the project.

Apart from the `bal persist add` command, if you want to use the `bal persist generate` command you can initialize the project with the following `init` command,
Expand Down Expand Up @@ -109,6 +112,8 @@ It will add generated files under the conventions,
├── generated
├── persist_client.bal
├── persist_db_config.bal
├── persist_test_client.bal (This file will be generated if the test-datastore is provided)
├── persist_test_init.bal (This file will be generated if h2 is provided as the test-datastore)
├── persist_types.bal
└── script.sql
├── persist
Expand All @@ -124,6 +129,8 @@ It will add generated files under the conventions,
└── medical-item
├── persist_client.bal
├── persist_db_config.bal
├── persist_test_client.bal (This file will be generated if the test-datastore is provided)
├── persist_test_init.bal (This file will be generated if h2 is provided as the test-datastore)
├── persist_types.bal
└── script.sql
├── persist
Expand All @@ -132,7 +139,8 @@ It will add generated files under the conventions,
├── Config.toml
└── main.bal
```


`persist_client.bal` file will contain the client API for the entities defined in the model file.
`persist_db_config.bal` file will contain the configurable variables required for the database access.
```ballerina
import ballerinax/mysql.driver as _;
Expand All @@ -143,6 +151,10 @@ It will add generated files under the conventions,
configurable string database = ?;
configurable string password = ?;
```
`persist_types.bal` file will contain the derived entity types based on the entities defined in the model file.
`persist_test_client.bal` file will contain the client API for the entities defined in the model file for the test-datastore.
`persist_test_init.bal` file will contain the initialization of the test database schema based on the entities defined in the model file for the test-datastore.
`script.sql` file will contain the SQL script to create the database schema based on the entities defined in the model file.

The database schema will contain the code to create,
1. Tables for each entity with defined primary keys
Expand All @@ -151,13 +163,14 @@ The database schema will contain the code to create,
Apart from the `bal build` users can also use the following command to generate the same as above mentioned,

```bash
bal persist generate --datastore mysql --module db
bal persist generate --datastore mysql --module db --test-datastore h2
```

| Command Parameter | Description | Mandatory | Default Value |
|:-----------------:|:----------------------------------------------------------------------------------------:|:---------:|:--------------:|
| --datastore | used to indicate the preferred database client. Currently, 'mysql', 'mssql', 'google sheets' and 'postgresql' are supported. | Yes | |
| --module | used to indicate the persist enabled module in which the files are generated. | No | <package_name> |
| --test-datastore | used to indicate the preferred database client for testing. Currently, 'h2', 'inmemory' are supported. | No | No |

Behaviour of the `generate` command,
- User should invoke the command within a Ballerina project
Expand All @@ -167,7 +180,7 @@ Behaviour of the `generate` command,

## 4. Push Persistence Schema to the Data Provider

>**Info:** This command is only supported yet. The SQL script generated by the `generate` command can be used to create the database schema.
>**Info:** This command is not supported yet. The SQL script generated by the `generate` command can be used to create the database schema.
>
```bash
bal persist push
Expand Down
Loading
Loading