Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/vit-orm/vitorm.mongodb


https://github.com/vit-orm/vitorm.mongodb

database mongo mongodb orm vitorm

Last synced: about 1 month ago
JSON representation

Awesome Lists containing this project

README

        


# Vitorm.MongoDB
Vitorm.MongoDB: an simple orm for MongoDB
>source address: [https://github.com/Vit-Orm/Vitorm.MongoDB](https://github.com/Vit-Orm/Vitorm.MongoDB "https://github.com/Vit-Orm/Vitorm.MongoDB")

![](https://img.shields.io/github/license/Vit-Orm/Vitorm.MongoDB.svg)
![](https://img.shields.io/github/repo-size/Vit-Orm/Vitorm.MongoDB.svg) ![](https://img.shields.io/github/last-commit/Vit-Orm/Vitorm.MongoDB.svg)

| Build | NuGet |
| -------- | -------- |
|![](https://github.com/Vit-Orm/Vitorm.MongoDB/workflows/ki_devops3_build/badge.svg) | [![](https://img.shields.io/nuget/v/Vitorm.MongoDB.svg)](https://www.nuget.org/packages/Vitorm.MongoDB) ![](https://img.shields.io/nuget/dt/Vitorm.MongoDB.svg) |

# Vitorm.MongoDB Documentation
This guide will walk you through the steps to set up and use Vitorm.MongoDB.

supported features:

| feature | method | remarks | |
| --- | --- | --- | --- |
| create table | TryCreateTable | | |
| drop table | TryDropTable | | |
| --- | --- | --- | --- |
| create records | Add AddRange | | |
| retrieve records | Query Get | | |
| update records | Update UpdateRange ExecuteUpdate | | |
| delete records | Delete DeleteRange DeleteByKey DeleteByKeys ExecuteDelete | | |
| --- | --- | --- | --- |
| change table | ChangeTable | change mapping table from database | |
| change database | ChangeDatabase | change database to be connected | |
| --- | --- | --- | --- |
| collection total count | TotalCount | Collection Total Count without Take and Skip | |
| collection total count and list | ToListAndTotalCount | query List and TotalCount in one request | |
| | | | |

## Installation
Before using Vitorm, install the necessary package:
``` bash
dotnet add package Vitorm.MongoDB
```

## Minimum viable demo
> code address: [Program_Min.cs](https://github.com/Vit-Orm/Vitorm.MongoDB/tree/master/test/Vitorm.MongoDB.Console/Program_Min.cs)
``` csharp
using System;
using System.Linq;
using System.Threading;
namespace App
{
public class Program_Min
{
static void Main2(string[] args)
{
// #1 Init
using var dbContext = new Vitorm.MongoDB.DbContext("mongodb://mongoadmin:mongoadminsecret@localhost:27017");
dbContext.TryDropTable();
dbContext.TryCreateTable();
dbContext.Add(new User { id = 1, name = "lith" });
Thread.Sleep(2000);

// #2 Query
var user = dbContext.Get(1);
var users = dbContext.Query().Where(u => u.name.Contains("li")).ToList();
}

// Entity Definition
[System.ComponentModel.DataAnnotations.Schema.Table("User")]
public class User
{
[System.ComponentModel.DataAnnotations.Key]
public int id { get; set; }
public string name { get; set; }
public DateTime? birth { get; set; }
public int? fatherId { get; set; }
}
}
}
```

## Full Example
> This example provides a comprehensive guide to utilizing Vitorm for basic and advanced database operations while maintaining lightweight performance.
> code address: [Program.cs](https://github.com/Vit-Orm/Vitorm.MongoDB/tree/master/test/Vitorm.MongoDB.Console/Program.cs)
``` csharp
using System;
using System.Linq;
using Vitorm;
namespace App
{
public class Program
{
static void Main(string[] args)
{
// #1 Configures Vitorm
using var dbContext = new Vitorm.MongoDB.DbContext("mongodb://mongoadmin:mongoadminsecret@localhost:27017");

// #2 Create Table
dbContext.TryDropTable();
dbContext.TryCreateTable();

// #3 Insert Records
dbContext.Add(new User { id = 1, name = "lith" });
dbContext.AddRange(new[] {
new User { id = 2, name = "lith", fatherId = 1 },
new User { id = 3, name = "lith", fatherId = 1 }
});

// #4 Query Records
{
var user = dbContext.Get(1);
var users = dbContext.Query().Where(u => u.name.Contains("li")).ToList();
var sql = dbContext.Query().Where(u => u.name.Contains("li")).ToExecuteString();
}

// #5 Update Records
dbContext.Update(new User { id = 1, name = "lith1" });
dbContext.UpdateRange(new[] {
new User { id = 2, name = "lith2", fatherId = 1 },
new User { id = 3, name = "lith3", fatherId = 2 }
});
//dbContext.Query().Where(u => u.name.Contains("li"))
// .ExecuteUpdate(u => new User { name = "Lith" + u.id });

// #6 Delete Records
dbContext.Delete(new User { id = 1 });
dbContext.DeleteRange(new[] {
new User { id = 2 },
new User { id = 3 }
});
dbContext.DeleteByKey(1);
dbContext.DeleteByKeys(new[] { 1, 2 });
//dbContext.Query().Where(u => u.name.Contains("li"))
// .ExecuteDelete();

// #7 Join Queries

// #8 Transactions

// #9 Database Functions
}

// Entity Definition
[System.ComponentModel.DataAnnotations.Schema.Table("User")]
public class User
{
[System.ComponentModel.DataAnnotations.Key]
public int id { get; set; }
public string name { get; set; }
public DateTime? birth { get; set; }
public int? fatherId { get; set; }
}
}
}
```

## Explanation
1. **Setup**: Initializes the database and configures Vitorm.
2. **Create Table**: Drops and recreates the `User` table.
3. **Insert Records**: Adds single and multiple user records.
4. **Query Records**: Retrieves user records using various querying methods.
5. **Update Records**: Updates single and multiple user records.
6. **Delete Records**: Deletes single and multiple user records.
7. **Join Queries**: Performs a join operation between user and father records.
8. **Transactions**: Demonstrates nested transactions and rollback/commit operations.
9. **Database Functions**: Uses custom database functions in queries.

# Vitorm.Data Documentation
Vitorm.Data is a static class that allows you to use Vitorm without explicitly creating or disposing of a DbContext.

## Installation
Before using Vitorm.Data, install the necessary package:
``` bash
dotnet add package Vitorm.Data
dotnet add package Vitorm.MongoDB
```

## Config settings
``` json
// appsettings.json
{
"Vitorm": {
"Data": [
{
"provider": "MongoDB",
"namespace": "App",
"connectionString": "mongodb://mongoadmin:mongoadminsecret@localhost:27017"
}
]
}
}
```

## Minimum viable demo
> After configuring the `appsettings.json` file, you can directly perform queries without any additional configuration or initialization, `Vitorm.Data` is that easy to use.
> code address: [Program_Min.cs](https://github.com/Vit-Orm/Vitorm/tree/master/test/Vitorm.Data.Console/Program_Min.cs)
``` csharp
using Vitorm;
namespace App
{
public class Program_Min
{
static void Main2(string[] args)
{
// Query Records
var user = Data.Get(1);
var users = Data.Query().Where(u => u.name.Contains("li")).ToList();
}

// Entity Definition
[System.ComponentModel.DataAnnotations.Schema.Table("User")]
public class User
{
[System.ComponentModel.DataAnnotations.Key]
public int id { get; set; }
public string name { get; set; }
public DateTime? birth { get; set; }
public int? fatherId { get; set; }
}
}
}

```

## Full Example
> code address: [Program.cs](https://github.com/Vit-Orm/Vitorm/tree/master/test/Vitorm.Data.Console/Program.cs)
``` csharp
using Vitorm;

namespace App
{
public class Program
{
static void Main(string[] args)
{
// #1 No need to init Vitorm.Data

// #2 Create Table
Data.TryDropTable();
Data.TryCreateTable();

// #3 Insert Records
Data.Add(new User { id = 1, name = "lith" });
Data.AddRange(new[] {
new User { id = 2, name = "lith", fatherId = 1 },
new User { id = 3, name = "lith", fatherId = 1 }
});

// #4 Query Records
{
var user = Data.Get(1);
var users = Data.Query().Where(u => u.name.Contains("li")).ToList();
var sql = Data.Query().Where(u => u.name.Contains("li")).ToExecuteString();
}

// #5 Update Records
Data.Update(new User { id = 1, name = "lith1" });
Data.UpdateRange(new[] {
new User { id = 2, name = "lith2", fatherId = 1 },
new User { id = 3, name = "lith3", fatherId = 2 }
});
Data.Query().Where(u => u.name.Contains("li"))
.ExecuteUpdate(u => new User { name = "Lith" + u.id });

// #6 Delete Records
Data.Delete(new User { id = 1, name = "lith1" });
Data.DeleteRange(new[] {
new User { id = 2, name = "lith2", fatherId = 1 },
new User { id = 3, name = "lith3", fatherId = 2 }
});
Data.DeleteByKey(1);
Data.DeleteByKeys(new[] { 1, 2 });
Data.Query().Where(u => u.name.Contains("li"))
.ExecuteDelete();

// #7 Join Queries

// #8 Transactions

}

// Entity Definition
[System.ComponentModel.DataAnnotations.Schema.Table("User")]
public class User
{
[System.ComponentModel.DataAnnotations.Key]
public int id { get; set; }
public string name { get; set; }
public DateTime? birth { get; set; }
public int? fatherId { get; set; }
}
}
}
```

# Examples:
[Test Example](https://github.com/Vit-Orm/Vitorm.MongoDB/tree/master/test/Vitorm.MongoDB.MsTest)