Njord
A lightweight and extensible ORM library for Rust.
Table of Contents
- Supported Databases
- Getting Started
- Usage
- Getting Help
- Reporting Issues
- Contributing
- Code of Conduct
- Contributors
- License
Supported Databases
| Database | Support | Status |
|---|---|---|
| SQLite | ✅ | Currently supported. |
| PostgreSQL | ❌ | Not supported, help us implement it? |
| MySQL | ❌ | Not supported, help us implement it? |
| MariaDB | ❌ | Not supported, help us implement it? |
| Oracle | ❌ | Not supported, help us implement it? |
| MSSQL | ❌ | Not supported, help us implement it? |
Getting Started
Initializing a new project
The first thing we need to do is generate our project.
Now, let’s add Njord to our dependencies. We’re also going to use a tool called .env to manage our environment variables for us. We’ll add it to our dependencies as well.
[]
# The core APIs, including the Table trait.
# using #[derive(Table)] to make njord work with structs
# and enums defined in your crate.
= { = "<version>", = ["sqlite"] }
= { = "<version>" }
Add a schema file
Now we are going to define our schema file that we will create under src/schema.rs. We will store basically our structs that will map against the database.
Now that we have that in place, we need to create the SQL for setting this up in the database and execute it.
-- users table
(
id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
email TEXT NOT NULL,
address TEXT NOT NULL
);
-- products table
(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
description TEXT NOT NULL,
price REAL NOT NULL,
stock_quantity INTEGER NOT NULL,
category INTEGER REFERENCES categories(id),
discount: REAL NULL
);
-- orders table
(
id INTEGER PRIMARY KEY,
user_id INTEGER REFERENCES users(id),
total_cost REAL NOT NULL
);
-- order_products table
(
order_id INTEGER REFERENCES orders(id),
product_id INTEGER REFERENCES products(id),
PRIMARY KEY (order_id, product_id)
);
Usage
So how can we establish a connection and actually select or insert data to our database? Let's go through it. Note that these examples might be outdated, so dont treat it as a source of truth.
SQlite
Establish a connection
To establish a connection we first need to call the sqlite::open() function and use it with a match statement.
let db_name = "njord.db";
let db_path = new;
match open
Insert data
let user = User ;
let result = insert;
assert!;
Generated SQL
INSERT INTO users (
username,
email,
address
) VALUES (
'john_doe',
'john@example.com',
'123 Main St'
)
Update data
let columns = vec!;
let where_condition = Eq;
let user = User ;
let mut order = new;
order.insert;
let result = update
.set
.where_clause
.order_by
.limit
.offset
.build;
assert!;
Generated SQL
UPDATE users
SET
username
WHERE
username = 'john_doe'
ORDER BY
id DESC
LIMIT 4
OFFSET 0
Delete data
let where_condition = Eq;
let mut order = new;
order.insert;
let result = delete
.from
.where_clause
.order_by
.limit
.offset
.build;
assert!;
Generated SQL
DELETE FROM users
WHERE
username = 'john_doe'
ORDER BY
id DESC
LIMIT 20
OFFSET 0
Select data
let columns = vec!;
let where_condition = Eq;
let group_by = vec!;
let mut order_by = new;
order_by.insert;
let having_condition = Gt;
// Build the query
// We need to pass the struct User with the Default trait in .from()
let result: = select
.from
.where_clause
.order_by
.group_by
.having
.build;
match result ;
Generated SQL
SELECT
id,
username,
email,
address
FROM
users
WHERE
username = 'mjovanc'
GROUP BY
username,
email
HAVING
id > 1
ORDER BY
email DESC
Getting Help
Are you having trouble with Njord? We want to help!
-
Read through the documentation on our docs.
-
If you are upgrading, read the release notes for upgrade instructions and "new and noteworthy" features.
-
Ask a question we monitor stackoverflow.com for questions tagged with Njord.
-
Report bugs with Njord at https://github.com/mjovanc/njord/issues.
Reporting Issues
Njord uses GitHub’s integrated issue tracking system to record bugs and feature requests. If you want to raise an issue, please follow the recommendations below:
-
Before you log a bug, please search the issue tracker to see if someone has already reported the problem.
-
If the issue doesn’t already exist, create a new issue.
-
Please provide as much information as possible with the issue report. We like to know the Njord version, operating system, and Rust version version you’re using.
-
If you need to paste code or include a stack trace, use Markdown. ``` escapes before and after your text.
-
If possible, try to create a test case or project that replicates the problem and attach it to the issue.
Contributing
Before contributing, please read the contribution guide for useful information how to get started with Njord as well as what should be included when submitting a contribution to the project.
Code of Conduct
Anyone who interacts with Njord in any space, including but not limited to this GitHub repository, must follow our code of conduct.
Contributors
The following contributors have either helped to start this project, have contributed code, are actively maintaining it (including documentation), or in other ways being awesome contributors to this project. We'd like to take a moment to recognize them.
License
The BSD 3-Clause License.