An open API service indexing awesome lists of open source software.

https://github.com/veeso/ic-dbms

IC DBMS Canister is an Internet Computer framework which provides an easy way to implement a database canister by just providing the database schema.
https://github.com/veeso/ic-dbms

canister dfinity icp internet-computer

Last synced: 11 days ago
JSON representation

IC DBMS Canister is an Internet Computer framework which provides an easy way to implement a database canister by just providing the database schema.

Awesome Lists containing this project

README

          

# IC DBMS Canister

![logo](./assets/images/cargo/logo-128.png)

[![license-mit](https://img.shields.io/crates/l/ic-dbms-canister.svg)](https://opensource.org/licenses/MIT)
[![repo-stars](https://img.shields.io/github/stars/veeso/ic-dbms?style=flat)](https://github.com/veeso/ic-dbms/stargazers)
[![downloads](https://img.shields.io/crates/d/ic-dbms-canister.svg)](https://crates.io/crates/ic-dbms-canister)
[![latest-version](https://img.shields.io/crates/v/ic-dbms-canister.svg)](https://crates.io/crates/ic-dbms-canister)
[![ko-fi](https://img.shields.io/badge/donate-ko--fi-red)](https://ko-fi.com/veeso)
[![conventional-commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white)](https://conventionalcommits.org)

[![ci](https://github.com/veeso/ic-dbms/actions/workflows/ci.yml/badge.svg)](https://github.com/veeso/ic-dbms/actions)
[![coveralls](https://coveralls.io/repos/github/veeso/ic-dbms/badge.svg)](https://coveralls.io/github/veeso/ic-dbms)
[![docs](https://docs.rs/ic-dbms-canister/badge.svg)](https://docs.rs/ic-dbms-canister)

This project is in a very early stage of development. The goal is to provide a framework for building database canisters on the Internet Computer.

## Overview

IC DBMS Canister is an Internet Computer framework which provides an easy way to implement a database canister by just providing the database schema.

The user can just define the data entity by defining the tables:

```rust
use candid::CandidType;
use ic_dbms_api::prelude::{Text, Uint32};
use ic_dbms_canister::prelude::{DbmsCanister, Table};
use serde::Deserialize;

#[derive(Debug, Table, CandidType, Deserialize, Clone, PartialEq, Eq)]
#[table = "users"]
pub struct User {
#[primary_key]
id: Uint64,
#[sanitizer(ic_dbms_api::prelude::TrimSanitizer)]
#[validate(ic_dbms_api::prelude::MaxStrlenValidator(20))]
name: Text,
#[validate(ic_dbms_api::prelude::EmailValidator)]
email: Text,
age: Nullable,
}
```

You can also define relationships between tables:

```rust
#[derive(Debug, Table, CandidType, Deserialize, Clone, PartialEq, Eq)]
#[table = "posts"]
pub struct Post {
#[primary_key]
id: Uint32,
title: Text,
content: Text,
#[foreign_key(entity = "User", table = "users", column = "id")]
author: Uint32,
}
```

> [!NOTE]
> Mind that deriving `CandidType`, `Deserialize` and `Clone` is required for the tables to be used in the canister.

And once you have defined all your tables, you can instantiate the database canister by deriving the `DbmsCanister` macro on any struct, providing the table names and their corresponding Rust struct entity:

```rust
#[derive(DbmsCanister)]
#[tables(User = "users", Post = "posts")]
pub struct IcDbmsCanisterGenerator;
```

And you will have a fully functional database canister with all the CRUD operations implemented for you.

The canister API will be automatically generated based on the defined tables, with the following methods:

```candid
service : (IcDbmsCanisterArgs) -> {
acl_add_principal : (principal) -> (Result);
acl_allowed_principals : () -> (vec principal) query;
acl_remove_principal : (principal) -> (Result);
begin_transaction : () -> (nat);
commit : (nat) -> (Result);
delete_posts : (DeleteBehavior, opt Filter_1, opt nat) -> (Result_1);
delete_users : (DeleteBehavior, opt Filter_1, opt nat) -> (Result_1);
insert_posts : (PostInsertRequest, opt nat) -> (Result);
insert_users : (UserInsertRequest, opt nat) -> (Result);
rollback : (nat) -> (Result);
select_posts : (Query, opt nat) -> (Result_2) query;
select_users : (Query_1, opt nat) -> (Result_3) query;
update_posts : (PostUpdateRequest, opt nat) -> (Result_1);
update_users : (UserUpdateRequest, opt nat) -> (Result_1);
}
```

## API Documentation

The API generated by the `ic-dbms-canister` is the following:

### ACL Management

- `acl_add_principal(principal)`: Adds a principal to the ACL.
- `acl_allowed_principals()`: Returns the list of principals in the ACL.
- `acl_remove_principal(principal)`: Removes a principal from the ACL.

### Transaction Management

- `begin_transaction()`: Starts a new transaction and returns its ID.
- `commit(transaction_id)`: Commits the transaction with the given ID. The user must own the transaction to commit it.
- `rollback(transaction_id)`: Rolls back the transaction with the given ID. The user must own the transaction to roll it back.

### Data Manipulation

For each table defined in the schema, the following methods are generated:

- `insert_(records, transaction_id)`: Inserts records into the specified table. Optionally within a transaction.
- `select_(query, transaction_id)`: Selects records from the specified table based on the query. Optionally within a transaction.
- `update_(updates, transaction_id)`: Updates records in the specified table. Optionally within a transaction.
- `delete_(delete_behavior, filter, transaction_id)`: Deletes records from the specified table based on the filter and delete behavior. Optionally within a transaction.

## Getting Started

See the [Getting Started Guide](https://veeso.github.io/ic-dbms/docs/get-started.html) for more information on how to setup and deploy the DBMS canister.

## Interacting with the Canister

See the [ic-dbms-client](./ic-dbms-client/README.md) for more information on how to interact with the canister.

## Features

- [x] Define tables with common attributes
- [x] CRUD operations
- [x] Complex queries with filtering and pagination
- [x] Relationships between tables with foreign keys
- [x] Transactions with commit and rollback
- [x] Access Control Lists (ACL) to restrict access to the database
- [x] Validation, Sanitizers and constraints on table columns
- [x] JOIN operations between tables
- [ ] Indexes for faster queries
- [ ] Custom data types
- [ ] Migrations to update the database schema on canister upgrades
- [ ] SQL query support

## Documentation

Read the documentation at

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.