Skip to content

Commit 3aad6d1

Browse files
authored
feat(#153): Added documentation & structure (#1)
* Removed content * Added User Guide content * Added Testing Guide content * Added SQLite DB Guide content * Added 404 page * Added Introduction page * Added summary & toml file * Added gitignore * Cleanup
1 parent e65f604 commit 3aad6d1

23 files changed

+451
-38
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
book

book.toml

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
[book]
2-
title = "Njord - A lightweight ORM library in Rust"
3-
authors = ["Marcus Cvjeticanin"]
2+
authors = ["Marcus Cvjeticanin", "Christopher Zeuch"]
3+
title = "Njord"
44
description = "A lightweight ORM library in Rust with strong-typed SQL DSL and sequence API."
55
language = "en"
6-
git-repository-url = "https://github.com/mjovanc/njord"
6+
multilingual = false
7+
git-repository-url = "https://github.com/njord-rs/docs"
78
git-repository-icon = "fa-github"
8-
copy-fonts = true
9-
edit-url-template = "https://github.com/mjovanc/njord/tree/docs/src/{path}"
10-
cname = "njord.rs"
11-
input-404 = "not-found.md"
9+
src = "src"
10+
input-404 = "404.md"
11+
12+
[output.html]
13+
copy-code = true
1214

1315
[build]
14-
target-dir = "book"
16+
target-dir = "book"

docs/CNAME

-1
This file was deleted.

docs/src/README.md

-14
This file was deleted.

docs/src/SUMMARY.md

-11
This file was deleted.

docs/src/not-found.md

-1
This file was deleted.

docs/src/sqlite/sqlite.md

Whitespace-only changes.

src/404.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# 😵 404
2+
3+
The page you are looking for does not exist.

src/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Introduction
2+
3+
A lightweight ORM library in Rust with strong-typed SQL DSL and sequence API.
4+
5+
We support the following databases:
6+
7+
| Database | Support |
8+
| ---------- | :-------: |
9+
| SQLite ||
10+
| PostgreSQL ||
11+
| MySQL ||
12+
| MariaDB ||
13+
| Oracle ||
14+
| MSSQL ||
15+
16+
## ✨ Contributors
17+
18+
We'd like to take a moment to recognize our awesome contributors:
19+
20+
[<img src="https://github.com/mjovanc.png?size=72" alt="mjovanc" width="72">](https://github.com/mjovanc)
21+
[<img src="https://github.com/appelskrutt34.png?size=72" alt="appelskrutt34" width="72">](https://github.com/appelskrutt34)
22+
[<img src="https://avatars.githubusercontent.com/u/23294573?v=4&size=72">](https://github.com/ahsentekd)
23+
[<img src="https://avatars.githubusercontent.com/u/167654108?v=4&size=72">](https://github.com/chinmer)
24+
[<img src="https://github.com/SvMak.png?size=72" alt="SvMak" width="72">](https://github.com/SvMak)
25+
[<img src="https://github.com/TomasWild.png?size=72" alt="TomasWild" width="72">](https://github.com/TomasWild)
26+
[<img src="https://github.com/chaseWillden.png?size=72" alt="chaseWillden" width="72">](https://github.com/chaseWillden)
27+
[<img src="https://github.com/Hiccup-za.png?size=72" alt="Hiccup-za" width="72">](https://github.com/Hiccup-za)
28+
29+
## 🤝 Contributing
30+
31+
Help us implement support for these databases and more!
32+
33+
Please first talk to us in [Discord](https://discord.gg/AbK57nAGv8) about what you are planning to do so we both save time and effort. If it's only bug fixes or documentation, feel free to submit a PR.
34+
35+
## 📄 License
36+
37+
The BSD 3-Clause License.

src/SUMMARY.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Summary
2+
3+
[Introduction](README.md)
4+
5+
# ⚡️ Getting Started
6+
- [Installation](user-guides/installation.md)
7+
- [Add a schema file](user-guides/add-schema.md)
8+
9+
# 📚 Database Guides
10+
- [SQLite](db-guides/sqlite/sqlite.md)
11+
- [Connection](db-guides/sqlite/connection.md)
12+
- [Initialize a database](db-guides/sqlite/initialize-database-with-tables.md)
13+
- [Define Tables](db-guides/sqlite/define-tables.md)
14+
- [Insert](db-guides/sqlite/insert.md)
15+
- [Select](db-guides/sqlite/select.md)
16+
- [Drop](db-guides/sqlite/drop.md)
17+
18+
# 🧪 Testing Guides
19+
- [Test Structure](testing-guides/test-structure.md)
20+
- [Running Tests](testing-guides/running-tests.md)
21+
- [Adding New Tests](testing-guides/adding-new-tests.md)

src/db-guides/README.MD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 📚 Database Guides
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/db-guides/sqlite/sqlite.md

+173
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# SQlite
2+
3+
## Establish a connection
4+
5+
To establish a connection we first need to call the `sqlite::open()` function and use it with a match statement.
6+
7+
```rust
8+
let db_name = "njord.db";
9+
let db_path = Path::new(&db_name);
10+
11+
match sqlite::open(db_path) {
12+
Ok(c) => {
13+
println!("Database opened successfully!");
14+
15+
// Additional logic when we are connected.
16+
// We need to open a connection and pass it
17+
// to the corresponding sqlite function.
18+
}
19+
Err(err) => eprintln!("Error opening the database: {}", err),
20+
}
21+
```
22+
23+
## Insert data
24+
25+
```rust
26+
let user = User {
27+
id: AutoIncrementPrimaryKey::default(),
28+
username: String::from("john_doe"),
29+
email: String::from("[email protected]"),
30+
address: String::from("123 Main St"),
31+
};
32+
33+
let result = sqlite::insert(c, vec![user]);
34+
assert!(result.is_ok());
35+
```
36+
37+
**Generated SQL**
38+
39+
```sql
40+
INSERT INTO users (
41+
username,
42+
email,
43+
address
44+
) VALUES (
45+
'john_doe',
46+
47+
'123 Main St'
48+
)
49+
```
50+
51+
## Update data
52+
53+
```rust
54+
let columns = vec!["username".to_string(), "address".to_string()];
55+
let where_condition = Condition::Eq("username".to_string(), "john_doe".to_string());
56+
57+
let user = User {
58+
id: AutoIncrementPrimaryKey::<usize>::new(Some(0)),
59+
username: String::from("john_doe_2"),
60+
email: String::from("[email protected]"),
61+
address: String::from("1234 Main St"),
62+
};
63+
64+
let mut order = HashMap::new();
65+
order.insert(vec!["id".to_string()], "DESC".to_string());
66+
67+
let result = sqlite::update(c, user)
68+
.set(columns)
69+
.where_clause(where_condition)
70+
.order_by(order)
71+
.limit(4)
72+
.offset(0)
73+
.build();
74+
75+
assert!(result.is_ok());
76+
```
77+
78+
**Generated SQL**
79+
80+
```sql
81+
UPDATE users
82+
SET
83+
username
84+
WHERE
85+
username = 'john_doe'
86+
ORDER BY
87+
id DESC
88+
LIMIT 4
89+
OFFSET 0
90+
91+
```
92+
93+
## Delete data
94+
95+
```rust
96+
let where_condition = Condition::Eq("username".to_string(), "john_doe".to_string());
97+
98+
let mut order = HashMap::new();
99+
order.insert(vec!["id".to_string()], "DESC".to_string());
100+
101+
let result = sqlite::delete(c)
102+
.from(User::default())
103+
.where_clause(where_condition)
104+
.order_by(order)
105+
.limit(20)
106+
.offset(0)
107+
.build();
108+
109+
assert!(result.is_ok());
110+
```
111+
112+
**Generated SQL**
113+
114+
```sql
115+
DELETE FROM users
116+
WHERE
117+
username = 'john_doe'
118+
ORDER BY
119+
id DESC
120+
LIMIT 20
121+
OFFSET 0
122+
```
123+
124+
## Select data
125+
126+
```rust
127+
let columns = vec!["id".to_string(), "username".to_string(), "email".to_string(), "address".to_string()];
128+
let where_condition = Condition::Eq("username".to_string(), "john_doe".to_string());
129+
let group_by = vec!["username".to_string(), "address".to_string()];
130+
131+
let mut order_by = HashMap::new();
132+
order_by.insert(vec!["id".to_string()], "ASC".to_string());
133+
134+
let having_condition = Condition::Gt("id".to_string(), "1".to_string());
135+
136+
// Build the query
137+
// We need to pass the struct User with the Default trait in .from()
138+
let result: Result<Vec<User>> = sqlite::select(c, columns)
139+
.from(User::default())
140+
.where_clause(where_condition)
141+
.order_by(order_by)
142+
.group_by(group_by)
143+
.having(having_condition)
144+
.build();
145+
146+
match result {
147+
Ok(result) => {
148+
assert_eq!(result.len(), 1);
149+
}
150+
Err(error) => panic!("Failed to SELECT: {:?}", error),
151+
};
152+
```
153+
154+
**Generated SQL**
155+
156+
```sql
157+
SELECT
158+
id,
159+
username,
160+
email,
161+
address
162+
FROM
163+
users
164+
WHERE
165+
username = 'mjovanc'
166+
GROUP BY
167+
username,
168+
email
169+
HAVING
170+
id > 1
171+
ORDER BY
172+
email DESC
173+
```
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Adding New Tests
2+
3+
To add new tests to Njord:
4+
5+
1. Create a new folder in the `tests` directory if it doesn't exist.
6+
2. Add a `mod.rs` file to declare submodules and shared utilities.
7+
3. Create test files (e.g., `test_1.rs`, `test_2.rs`) within the folder.
8+
9+
For example, to add tests for a new feature called "user_management":
10+
11+
```
12+
njord/
13+
└── tests/
14+
└── user_management/
15+
├── mod.rs
16+
├── create_user.rs
17+
└── delete_user.rs
18+
```
19+
20+
In the `mod.rs` file, declare the test files as modules:
21+
22+
```rust
23+
mod create_user;
24+
mod delete_user;
25+
```
26+
27+
Write your test functions in `create_user.rs` and `delete_user.rs` using the `#[test]` attribute.
28+
29+
This structure allows you to:
30+
- Group related tests together
31+
- Share common setup and utilities across tests in the same module
32+
- Easily run all tests for a specific feature
33+
34+
Remember to use descriptive names for your test functions and keep tests isolated and independent of each other.
35+
36+
---
37+
38+
## Best Practices
39+
40+
1. Group related tests in the same module (folder).
41+
2. Use descriptive names for test functions.
42+
3. Keep tests isolated and independent of each other.
43+
4. Use setup and teardown functions in `mod.rs` for common operations.
44+
5. Run tests frequently during development to catch issues early.
45+
46+
By following this structure and these practices, you can maintain a clean and efficient testing suite for Njord.

src/testing-guides/running-tests.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Running Tests
2+
3+
Now that we understand the structure, let's look at how to run tests.
4+
Rust's test runner is built into the `cargo` command and provides several options:
5+
6+
1. Run all tests in the project:
7+
```bash
8+
cargo test
9+
```
10+
11+
2. Run tests in a specific module (folder):
12+
```bash
13+
cargo test --test folder_1_tests
14+
```
15+
This command runs all tests in the `folder_1` module, as defined by its `mod.rs` file.
16+
17+
3. Run a specific test function:
18+
```bash
19+
cargo test --test folder_1_tests test_function_name
20+
```
21+
This runs a specific test function within the `folder_1` module.

0 commit comments

Comments
 (0)