https://github.com/neuodev/online-db
Fast, unopinionated, minimalist, open source, No-SQL database uses document data model for store and retrieve data.
https://github.com/neuodev/online-db
nosql-database
Last synced: 11 months ago
JSON representation
Fast, unopinionated, minimalist, open source, No-SQL database uses document data model for store and retrieve data.
- Host: GitHub
- URL: https://github.com/neuodev/online-db
- Owner: neuodev
- Created: 2021-05-17T09:32:54.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-08T06:05:48.000Z (almost 5 years ago)
- Last Synced: 2025-08-09T12:45:25.758Z (12 months ago)
- Topics: nosql-database
- Language: JavaScript
- Homepage:
- Size: 157 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# OnlineDB
Fast, unopinionated, minimalist, open source, No-SQL database uses document data model for store and retrieve data.
## Simple example
```js
const { OnlineDB } = require('onlinedb');
const db = new OnlineDB('db');
const user = db.createCollection('users');
user.insertOne({
name: 'jone',
email: 'jone@test.com',
isDeveloper: true,
age: 32,
hobbies: ['sport', 'cooking', 'coding'],
});
const users = user.find();
```
## Advanced schema usage
```js
const userSchema = new Schema({
{
//
title: String,
author: String,
body: String,
//
//
comments: [{ body: String, date: Date }],
//
hidden: Boolean,
//
meta: {
votes: Number,
favs: Number,
},
//
age: {
type: Number,
required: true,
minValue: 25,
maxValue: 100,
},
//
text: {
type: String,
required: true,
default: 'Hello world text edition',
maxLength: 255,
minLength: 10,
},
//
email: {
type: String,
regExp:
/^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i,
},
//
role: {
type: String,
enum: ['ADMIN', 'STAFF', 'USER'],
},
});}
```
Note: if there is any some thing that is not clear feel free to open an issue on github :)
#
# Relation Between Data
Now You can apply relation between data like `one to one`, `many to one`, `one to many`, `many to one`
It maybe seems as a complex topic but believe me OnlineDB makes it ease now
First make an empty folder
mkdir myshop
Start new project using npm
npm init -y
now install the package
npm install onlinedb
create and index.js file any type this
```js
// myshop/index.js
const { OnlineDB, Schema } = require('./index');
const db = new OnlineDB('database');
const userSchema = new Schema({
id: String,
name: String,
products: [
{
type: 'ObjectId',
ref: 'product',
},
],
});
const productSchema = new Schema({
id: String,
name: String,
user: {
type: 'ObjectId',
ref: 'user',
},
});
```
* So what happened that we imported the main OnlineDB class and the schema. if you new to the no-sql world the schema is how our data should look like
* so we created two collections
* In user collection → every user can have multiple products so this is one to many relation
* this how we define it
* Use `Array` to represent many relationship and every item should be object with type ObjectId and ref field
* `ObjectId`
the ObjectId type for OnlineDB so it will now that you want a relation
* `Ref` this field is required you have to provide it. it just a pointer to another collection
* In the product Schema → every product should have one user who own it so this one to many relation
* to apply this relation we have user field that have type of ObjectId → to tell OnlineDB that we want a relation here and a ref field that point to another collection
* And here we are. you have it now a relation between your data
## Next you need to create a collection in our example we have two collections
const User = db.createCollection('user', userSchema);
const Product = db.createCollection('product', productSchema);
### Time to have a data in your database → notice that we stored the ids of the products in an array in the user collection → this how OnlineDB will parse this items one at a time
### Also notice that in the product collection you have in each product a user field and its value is the id of this user in this way OnlineDB will understand that when you query for products it will replace the user field id with actual information
```js
if (process.argv[2] === '-i') {
User.insertMany([
{ id: '1', name: 'jone', products: ['1', '2'] },
{ id: '2', name: 'Jane', products: ['2'] },
{ id: '3', name: 'Ahmed', products: ['3'] },
{ id: '4', name: 'more one ', products: ['4'] },
{ id: '5', name: 'someone ', products: ['5'] },
]);
Product.insertMany([
{ id: '1', name: 'Apply MacBook Pro', user: '1' },
{ id: '2', name: 'Apply MacBook Air', user: '2' },
{ id: '3', name: 'Iphone Pro', user: '3' },
{ id: '4', name: 'iMac M1 ', user: '4' },
{ id: '5', name: 'iPad pro ', user: '5' },
]);
}
```
## Now Time to query your data
### Go a long with me here now we are querying for all users and we need them to have the information for each product
* So we add the populate method that have field property and this field represent witch field we want to populate witch in our case is products and you have it now. if you print the users in the console you will have each user an his products
* Wait for a second what about this select property this for select witch fields to return in the products collection it only accept a string with space separated values as each value represent a field in our example it will only return the name and id fields for the products collection
```js
try {
const users = User.find({
populate:{
field: 'products',
select: 'name id'
},
});
} catch (error) {
console.log(error);
}
```
This package is created by Ahmed Ibrahim. If you want to show any support just leave a comment or contact me ahmedibarhim556@gmail.com