nosql-json-database
v1.0.0
Published
package for building simple nosql databases using json files
Downloads
201
Maintainers
Readme
NoSQL JSON Database
Installation
Node.js
nosql-json-database
is available on npm. To install it, type:
$ npm install --save-dev nosql-json-database
Why?
This module aims to help simplify the use of file modules using fs and path whenever developing an application that needs a database but you just don't want to use a standard database due to the long setup or for any other reason.
This module uses JSON files to store database documents and files. This was designed to be a simple node-js module to assist during the development of applications temporarily, or even during the designing or testing of an application. The application can be useful in multiple backend areas and offers some useful methods such as populate to allow for complex no-sql data structures to be implemented.
Usage
import Container
from the package folder and use to create the container for the database
import { Container } from "nosql-json-database";
const database = new Container();
const usersCollection = database.createCollection("users");
Specifying location for database container
You can specify the location where all the database files would be contained
import { Container } from "nosql-json-database";
import path from "path";
const location = path.join(__dirname, "/");
const database = new Container(location);
Specifying Type as Models
It is recommended to specify the type as a model basis for the data to be contained this would help in intellisence and also when performing operations
import { Container } from "nosql-json-database";
type Users = {
name: string;
age: number;
email: string;
}
const database = new Container();
const usersCollection = database.createCollection<Users>("users");
Adding Documents
import { Container } from "nosql-json-database";
type Users = {
name: string;
age: number;
email: string;
}
const database = new Container();
const usersCollection = database.createCollection("users");
usersCollection.addOne({
email: "[email protected]",
age: 23,
name: "joe",
});
Retrieving Documents
In addition to storing the data provided, an _id along with createdAt and updatedAt information is
added to the newly inserted document.
The _id is generated from using uuid
import { Container } from "nosql-json-database";
type Users = {
name: string;
password: string;
email: string;
}
const database = new Container();
const usersCollection = database.createCollection("users");
usersCollection.addOne({
email: "[email protected]",
password: "daniel1",
name: "daniel",
});
const response = usersCollection.find();
console.log(response);
// [
// {
// _id: 'json://users.be72b673-3f7c-4315-bbe0-a7311bf591d0',
// email: '[email protected]',
// name: 'daniel',
// password: 'daniel1',
// createdAt: '2024-01-17T21:22:06.532Z',
// updatedAt: '2024-01-17T21:22:06.534Z'
// }
// ]
Populating a document
A document can be populated by specifying the field to be populated. The JSON database would handle the matching of the collections
import { Container } from "nosql-json-database";
type Users = {
name: stringl;
password: string;
email: string;
}
type Posts = {
title: string;
user: string;
body: string;
}
type Comments = {
postId: string;
userId: string;
text: string;
}
const database = new Container();
const users = database.createCollection<Users>("users");
const post = database.createCollection<Posts>("posts");
const comment = database.createCollection<Comments>("comments");
const newUser = usersCollection.addOne({
email: "[email protected]",
name: "joe davis",
password: "joe1"
});
const newPost = postCollection.addOne({
user: newUser._id,
title: "test case post title",
body: "test case body 2",
});
const newComment = commentCollection.addOne({
postId: newPost._id,
userId: newUser._id,
text: "comment case",
});
console.log(comment.findAndPopulate(["postId", "userId"]));
// [
// {
// _id: 'json://comments.8a82370e-11e9-4514-a04f-bda2d30938fa',
// postId: {
// _id: 'json://posts.b1b3bf21-631d-4065-88b0-3b1b06983837',
// user: 'json://users.ba8febaf-0df2-4df4-afb7-4b3862293255',
// title: 'test case post title',
// body: 'test case body 2',
// createdAt: '2024-01-17T21:31:30.746Z',
// updatedAt: '2024-01-17T21:31:30.746Z'
// },
// userId: {
// _id: 'json://users.ba8febaf-0df2-4df4-afb7-4b3862293255',
// email: '[email protected]',
// password: 'joe1',
// name: 'joe davis',
// createdAt: '2024-01-17T21:31:30.744Z',
// updatedAt: '2024-01-17T21:31:30.745Z'
// },
// text: 'comment case',
// createdAt: '2024-01-17T21:31:30.747Z',
// updatedAt: '2024-01-17T21:31:30.747Z'
// }
// ]
License
MIT