ezdb 0.5.0
A declarative SQL library similary to Spring Data
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
EzDb
EzDb is a database system similar to Spring Data JPA. It currently supports both SQLite and an in-memory database.
Usage
Here is a simple example that explains the usage of EzDb:
import ezdb;
// Define the entity representing a single record in a table.
struct SomeEntity
{
@primaryKey
int id;
string someValue;
}
// A repository that functions as the interface to a repository.
interface SomeRepository : Repository!SomeEntity
{
}
// Create an instance for the repository.
auto db = makeRepository!SomeRepository;
scope(exit) db.close();
// Create an entity and save it.
SomeEntity newEntity;
newEntity.someValue = "hello, world!";
SomeEntity savedEntity = db.save(newEntity);
Foreign keys
Foreign keys are now also supported. Support for foreign keys is purposefully limited as to avoid issues such as eager/lazy loading, partial data, and cascading. Use them with the following syntax:
import ezdb;
struct Author
{
@primaryKey
int id;
string name;
}
struct Book
{
@primaryKey
int id;
string name;
// Refer to the ID of the author of this book.
@foreign!Author
int author;
}
// Create the repositories
interface AuthorRepository : Repository!Author {}
interface BookRepository : Repository!Book {}
// Open the repositories
auto authorDb = makeRepository!AuthorRepository;
auto bookDb = makeRepository!BookRepository;
// Add an author
Author stevenKing;
stevenKing.name = "Steven King";
stevenKing = authorDb.save(stevenKing);
// Add a book
Book theShining;
theShining.name = "The Shining";
theShining.author = stevenKing.id;
theShining = bookDb.save(theShining);
User-Defined Methods / Custom Queries
It is easy to add a custom query to a repository. This is done in nearly the same way as Hibernate does.
Simply add a method to a repository, like this:
interface AuthorRepository : Repository!Book
{
Book[] findByName(string name);
Book[] findByAuthor(int authorId);
}
The following query types are supported:
|=|=|
| find
/select
| Searches for data, retrieving all data that was found. |
Unit-testing
A system requiring a database can easily be tested using the mockRepository
function.
import ezdb;
unittest
{
auto db = mockRepository!SomeRepository;
SomeEntity mockEntity;
mockEntity.someValue = "hello, test!";
db.save(mockEntity);
// Do the rest of the test here
}
Other functionality
All supported operations on a database can currently be found in the package ezdb.repository
.
Check out the documentation!
- 0.5.0 released 4 years ago
- seeseemelk/ezdb
- MIT
- Copyright © 2020, Sebastiaan de Schaetzen
- Authors:
- Dependencies:
- d2sqlite3, optional
- Versions:
-
0.5.0 2020-Jun-18 0.4.1 2020-Apr-09 0.4.0 2020-Apr-09 0.3.0 2020-Apr-07 0.2.0 2020-Feb-09 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
122 downloads total
-
- Score:
- 0.6
- Short URL:
- ezdb.dub.pm