Giter VIP home page Giter VIP logo

astrodb's Introduction

AstroDB

MongoDB driver api

AstroDB Logo

AstroDB is command library of mongodb. It reduces time required to build application with mongodb.

installation

Download the repository and add to your project. index.js file alone is enough to apply AstroDB.

$:npm install 

It uses config files to build mongo Schema.

db.config.js file

module.exports = function(Types){
    return [
        {
            'name':'Admin',
            'schema':{
                username:String,
                password:String,
                age:Number,
            },
            'statics':{
                'searchAndGetUsername':function(query) {
                    return this.where(query).limit(10).select('username');
                },
            },
            'virtuals':{
                'status':function(){
                    return `${this.username} is ${this.age} yrs old.`;
                }
            }
        },
    ]
}

Yes, we have statics functions and virtuals properties.

import these files.

index.js

let AstroDB =require('./AstroDB');
let config = require('./db.config');

In index.js file, write configuration codes.

AstroDB.connect('mongodb://localhost/AstroDB');
AstroDB.addAll(config);

Executing a query in AstroDB is easy!

let result = await AstroDB.exec('Admin','new',{username:'Min Si Thu',password:'Min Si Thu',age:19});

// other queries
await AstroDB.exec('Admin','find',{username:'Min Si Thu'});
await AstroDB.exec('Admin','delete',{username:'Min Si Thu',age:{$gt:15,$lt:20}});

Other queries

deleteMany
deleteOne
find
findById
findByIdAndDelete
findByIdAndRemove
findByIdAndUpdate
findOne
findOneAndDelete
findOneAndRemove
findOneAndUpdate
replaceOne
updateMany
updateOne

limit,select,sort //can be used

limit,sort,select

await AstroDB.exec('Admin','find',{age:{$gt:15}},{limit:10,sort:'-username',select:'username age'});

AstroDB also allows population.

Add this config object to your db.config.js

 {
            'name':'Post',
            'schema':{
                'author':{type:Types.ObjectId,ref:'Admin'},
                'content':String,
            },
 },

Add id of Admin when creating a new Post object.

await AstroDB.exec('Post','new',{content:'what is Node.js?',author:"5c7829144b4e2d0f9c4afbe5",})

This can be populated as

await DB.exec('Post','find',{content:'what is Node.js?'},{populate:['author']});

Resulting data is

[ { _id: '5c79664210095423542e27d3',
    content: 'what is java?',
    author:
     { _id: '5c7829144b4e2d0f9c4afbe5',
       username: 'test1',
       password: 'test1',
       age: 34,
       __v: 0 },
    __v: 0 } ]

AstroDB allows aggregation framework of MongDB.

still experimental

await AstroDB.exec('Post','aggregate',[
    {
        $match: {
            created: {$gt: new Date(time)}
            }
        },
        {
            $group: {
                _id: null,
                count: {$sum: 1}
            }
        }
    ]);

Contact Me @ Min Si Thu, [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.