Setup the API:
Clone the following project on your machine (Linux or Windows).
When cloned, you have 2 options to run the API.
You have two possibilities to use this API (Linux or Windows).
- Using Docker
- Cloning the project on your machine
Using the donet run command:
dotnet run -c Release --APP_PORT=443 --MONGO_URL=mongodb://localhost/27017 --MONGO_DB_NAME=Orizon
Using Dockerfile:
docker run -e MONGO_URL=mongodb://localhost/27017 -e MONGO_DB_NAME=Orizon -e APP_PORT=443 -p 443:443 bubblum/mongorest:latest
You also need to install an instance of MongoDb on your machine. This documention does not explain how to setup a MongoDb database.
Default API port is: 443
Default MongoDb connection url: mongodb://localhost:27017
Setup a Repository:
Repositories a registered automaticaly when starting the scene/project.
Create a repository from a data model:
[MongoCollection( "users" )]
public record User(string Name, int PermissionLevel);
// Create a new database repository of type User
public sealed class UserRepository : MongoRepository<User>
{
}
Get a repository from the scene:
var users = Scene.GetRepository<UserRepository>();
CRUD Operations:
Insert Operations:
// Insert a new user called John with a permission level 4 in the users collection
var john = new User("John", 4);
var inserted = await users.InsertAsync(john);
// Add multiples users in a same batch
var marry = new User("Marry", 2);
var inserted = await users.InsertAsync(john, marry);
Count Operations:
// Count how many john exists in the collection
var count = await users.CountAsync(x => x.Name = "John");
Update Operations:
// Update all users named "John" and set it's permission level to 10
var updated = await users.UpdateAsync(x => x.Name = "John", x => x.PermissionLevel = 10);
Delete Operations:
// Delete all users named "John"
var deleted = await users.DeleteAsync(x => x.Name = "John");
Exists Operations:
// Check if a record with the name "John" exists in the collection
var exists = await users.ExistsAsync(x => x.Name = "John");
Get Operations:
// Get the first user called "John"
var john = (await users.GetAsync(x => x.Name = "John", 1)).FirstOrDefault();
// Get multiples users called "John" with a permission level set to 4
var johns = await users.GetAsync(x =>
{
x.Name = "John",
x.PermissionLevel = 4
});
// Get all users called "John"
var johns = await users.GetAsync(x =>
{
x.Name = "john"
});
// Get all users
var all = await users.GetAsync();
// Get all 50 first users
var all = await users.GetAsync(limit: 50);