Skip to content

Khelechy/CSJsonDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSJsonDB

Introduction

This is a simple C# package that performs basic CRUD ( Create, Read, Update, Delete ) operations on a Json file, used for sample minimalistic DBs.

Installation

Install via .NET CLI

dotnet add package CSJsonDB --version 1.1.0

Install via Package Manager

Install-Package CSJsonDB --version 1.1.0

Add the namespace directive using CSJsonDB; IMPORTANT

Sample DB users.db

{
    "users": [
      {
        "id": 1,
        "firstname": "kelechi",
        "lastname": "onyekwere",
        "age": 19,
        "verified": true
      },
      {
        "id": 2,
        "firstname": "john",
        "lastname": "doe",
        "age": 33,
        "verified": true
      },
      {
        "id": 3,
        "firstname": "mark",
        "lastname": "parker",
        "age": 20,
        "verified": false
      }
    ]
  }

Load the Sample DB IMPORTANT

var db = JsonDB.Load("filepathtosampledb/users.db");

Available Methods 🧨

NOTE
Responses are returned as objects. You can use .ToJsonString() method to return json string from a json object

Load

var db = JsonDB.Load(string filePath);

ToJsonString

db.Select("users").Where("id", 2).ToJsonString();

result: string Returns the json string of the object.

Select

db.Select(string table);

Sample

db.Select("users");

result: object

[
      {
        "id": 1,
        "firstname": "kelechi",
        "lastname": "onyekwere",
        "age": 19,
        "verified": true
      },
      {
        "id": 2,
        "firstname": "john",
        "lastname": "doe",
        "age": 33,
        "verified": true
      },
      {
        "id": 3,
        "firstname": "mark",
        "lastname": "parker",
        "age": 20,
        "verified": false
      }
]

Where

db.Select(string table).Where(string key, dynamic value);

Sample

db.Select("users").Where("id", 2);

result: List<dynamic>

[
      {
        "id": 2,
        "firstname": "john",
        "lastname": "doe",
        "age": 33,
        "verified": true
      },
]

Add

db.Add(string table, object newData);

Sample

var newUser = new {
    id = 3,
    firstname = matt,
    lastname = doe,
    age = 23,
    verified = false
};

db.Add("users", newUser);

result: void

Delete

db.Delete(string table, string key, dynamic value);

Sample

db.Delete("users", "id", 1);

result: void

Update

db.Update(string table, string key, dynamic value, object newData);

Sample

var updateUser = new {
    verified = true
};

db.Update("users", "id", 3, updateUser);

result: object

[
      {
        "id": 3,
        "firstname": "mark",
        "lastname": "parker",
        "age": 20,
        "verified": true
      },
]

Contribution

If you find an issue with this package or you have any suggestion please help out. I am not perfect.

About

A C# package that performs basic CRUD ( Create, Read, Update, Delete ) operations on a Json file, used for sample minimalistic DBs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages