DBMS
Overview
During the build of your application you will notice there is a very narrow usage of the most common database operations:
- saving the element
- reading the element with a given ID
- reading all the elements from the given domain
- updating the existing element
- deleting the element with a given ID
- obtaining the next available ID within the given domain
- number of all elements in the given domain
The e_db module provides a convenient API to all those operations.
Supported DBMS
Currently only Mnesia and CouchDB DBMS are supported. Moreover, CouchDB support is in incubation phase, so it is not recommended you use it in production systems. To set the desired DBMS you specify its name in the project.conf file:
{dbms, DBMS}.where DBMS is either mnesia for Mnesia support or couchdb for CouchDB.
After setting this option, the access to the selected DBMS is completely transparent when you are using e_db module.
e_db module
The e_db module is a transparent API to the DBMS laying a foundation for the system. It allows you to run the following operations on the database:
install() - installs the selected database. If you are using mnesia, it creates the table for used ID’s. Otherwise, if you are using CouchDB, it creates two new databases: one for your project (with your project name as specified in project.conf file) and one for your project’s ID’s.
write(Domain, Element) - saves the Element to the database in the specified Domain.
read(Domain) - reads and returns a list of all the entities from the given Domain.
read(Domain, Id) - reads the element from the Domain with the given Id.
update(Domain, Element) - updates the Element in the given Domain.
size(Domain) - returns the number of elements stored in the given Domain.
delete(Domain, Element) - deletes Element from the Domain.
get next id(Domain) - returns the next, unique ID within the Domain.
Example
The following examples demonstrate the basic use cases of the e_db module:
%% creating new item ID = e_db:get_next_id(blog_post), Post = blog_post:create_post(ID), e_db:write(blog_post, Post), ... %% reading all the items Posts = e_db:read(blog_post), ... %% reading the particular item Post = e_db:read(blog_post, 10), ... %% updating the existing item Post = e_db:read(blog_post, 3), NewPost = blog_post:update_post(Post), e_db:update(blog_post, NewPost), ... %% removing the item Post = e_db:read(blog_post, 5), e_db:delete(blog_post, Post), ... %% getting the domain size Size = e_db:size(blog_post), ...
