Mongo Rest
orizon orizon Library update 1y ago

Mongo Rest

MongoRest transforms MongoDB into a generic RESTful API for simplified CRUD operations

thumb_up2upvotes
hard_drive 0 MB folder 25 files build Engine v17 history Last update 637d ago
open_in_new View on sbox.game

About This Package

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 a pre configured runner (You can update values in each runner):
Windows: ./run.bat
Linux: ./run.sh


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:

// [JsonPropertyName("_id")] is required on your Id property, an id property is also required.
[MongoCollection( "users" )]
public record User
{
  [JsonPropertyName("_id")] public string Id { get; set; }
  public string Name { get; set; }
  public int PermissionLevel { get; set; }
}

// Create a new database repository of type User
public sealed class UserRepository : MongoRepository<User>
{
}


Configure the API connection:

// You need to configure the MongoRest connection
// By default, the port is 443 (s&box only permit to connect to port: 80,8080,443,8443)
var system = Scene.GetSystem<MongoRestSystem>();

system.Configure( options =>
{
    // The url where the API is hosted
    options.Url = "https://localhost:443";
} );



Get a repository from the scene:

var users = Scene.GetRepository<UserRepository>();

CRUD Operations:


Insert:

// 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:

// Count how many john exists in the collection
var count = await users.CountAsync(x => x.Name = "John");


Update:

// 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:

// Delete all users named "John"
var deleted = await users.DeleteAsync(x => x.Name = "John");


Exists:

// Check if a record with the name "John" exists in the collection
var exists = await users.ExistsAsync(x => x.Name = "John");


Get:

// 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);

Media

No usage statistics available
This package may be new or not widely used yet. Usage data becomes available as more users install and play with the package.
Package ID: orizon.mongo_rest
Created: 2024-11-23 20:13 UTC
Updated: 2024-11-25 13:03 UTC
Type: library
Public: Yes
Version: 78705
files: 25
Size: 0.1 MB
Engine Version: 17
Asset Version ID: 78705
Version Created: 2024-11-25 13:03 UTC
Hash: -3190198998551797870
Manifest: Open manifest
Cs Proj Name:
Compiler: Mongo, SANDBOX;ADDON;DEBUG, 1701;1702;1591;, , , 1, 1, Debug, Array, Array, Array
Resources:
Code Package References:
new_releases Latest Update Nov 25, 2024
Changes on 2024-11-25
78705 Nov 25, 2024
Changes on 2024-11-25
78704 Nov 25, 2024
Changes on 2024-11-25
78653 Nov 24, 2024
Add support for flatten document filter
78614 Nov 24, 2024
Fix a lot of issues + writing some unit tests
78316 Nov 23, 2024
Changes on 2024-11-23
78306 Nov 23, 2024
Changes on 2024-11-23
78296 Nov 23, 2024
Changes on 2024-11-23