npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cachejsorm

v1.1.4

Published

a javascript orm for mssql, mysql and postgress with caching support

Downloads

30

Readme

#CacheJSORM

A JavaScript ORM with caching and connection pooling support. It supports CRUD operations and aggeration(group, order, outer join, left join, right join, inner join, top, limit for mysql, max, min, sum, count) for mssql, mysql and postgres.

After installing this package;

  1. Create a model with table and field details.

    var table = require('cachejsorm').TableSchema
    var tableMapping = require('cachejsorm').TableMapper
    var mssqlTable ='dbo.[orders]';
    var schema = new table(
        {
            OrderNumber: {type: Number}, 
            orderDate: {type: String}, 
            customerNumber:{type:Number},
        }
    );
    var tabModel = new tableMapping(mssqlTable, schema);
    module.exports=tabModel;`
    
    
  2. Create a test.js

    1. Call model in test.js
    2. Setup connection details
    3. Use functions from package for CRUD, join and aggregate functions.

Refer to following examples;

var orderModel = require('./testmodel')
//setup configuration
var config={    
    driverType: "mssql",    //supports mysql/mssql/pg    
    server:'serverip',
    database:'databasename',
    port: 1433,
    username:'username',
    password: 'password',
    cacheDuration: 10,      //duration in seconds
    driverOptions:{
        connectionPoolLimit: 10,
        //trustedConnection: false  //true for integrated security
    }
}
orderModel.setConfig(config);

#####CRUD Operation

//find based on column values 
orderModel.find({OrderNumber: '123'},function(err,data){
    if(err){
        console.log(err);
    }else{
        console.log(data);
    }
});

//find all
orderModel.find({},function(err,data){
        if(err){
            res.send(err);
        }else{
            res.send(data);
        }
    });

//delete 
orderModel.delete({OrderNumber:1234},function(err,data){
        if(err){
            res.send(err);
        }else{
            res.send(data);
        }
    });

//Update (keyfields, field key value to update)
orderModel.update({OrderNumber:1234},{OrderQuantity: 2, customerNumber: 111},function(err,data){        
        if(err){
            res.send(err);
        }else{
            res.send(data);
        }
    });

//insert
orderModel.insert({OrderQuantity:1, orderDate:'2019-01-01', customerNumber:111},function(err,data){
        if(err){
            res.send(err);
        }else{
            res.send(data);
        }
    });

#####Join Conditions (join types inner, outer, left and right)

// use .join for joining multiple tables
orderModel.join(
    {
        _join: [{
            _localkey: 'customerid',
            _foreignkey: 'id',
            _foreignTable: 'dbo.[customer]',
            _type: 'inner',
            _name: '$join1'     //create multiple joins with same table and name them to reference later
        },
        {
            _localkey: 'id',
            _foreignkey: 'OrderId',
            _foreignTable: 'dbo.[OrderItem]',
            _type: 'left',
            _name: '$join2'     //create multiple joins with same table and name them to reference later
        }],
        _field: 
                [
                    {
                        _name: '_local.OrderNumber',
                        _alias: 'OrderNum',
                    },
                    {
                        _name: '_foreign.FirstName',    //fields to retrieve for first join 
                        _alias: 'CustomerName',
                        _join: '$join1'
                    },
                    {
                        _name: '_foreign.all',  //get all fields from second join 
                        _join: '$join2'
                    }
                ],
        _filter: [      //additional filters on joins 
            {
                _field:[{_name:'_foreign.id', _join: '$join1'}],
                _eq:'85'    //equal
            },
            {
                _field:[{_name:'_local.TotalAmount'}],
                _gteq:'400'     //greater than equal
            }
        ],
        _order: {
            _field: 
                [
                    {
                        _name: '_local.OrderNumber',
                    },
                    {
                        _name: '_foreign.FirstName',
                        _join: '$join1'
                    }
                ],
            _mode: 'desc'
        },
        _top:{_row:2}   //use _limit for driver type mysql
    },
    function(err,data){
        if(err){
            console.log(err);
        }else{
            console.log(data);
        }
    }
)

Above function produces following SQL Query internally to retrieve from DB;

SELECT  top 2 orders.OrderNumber as OrderNum ,dbo.[customer].FirstName as CustomerName ,dbo.[OrderItem].* 
FROM orders 
INNER JOIN dbo.[customer] ON orders.customerid=dbo.[customer].id 
LEFT JOIN dbo.[OrderItem] ON orders.id=dbo.[OrderItem].OrderId  
Where dbo.[customer].id = 85 
and orders.TotalAmount >= 400  
Order By orders.OrderNumber, dbo.[customer].FirstName desc

#####Aggregate Operations (_sum, _count, _max, _min)

//Use .aggregate for _sum , _count, _max and _min functions
orderModel.aggregate(
    {
        _sum:{
            _field: 'TotalAmount',
            _alias: 'MaxAmount'
        },
        _group: {
                _by: {
                    _field: [{_name:'customerId'}]
                },
                _having:{
                    _field:[{_name:'customerId'}],
                    _eq:'10'
                }
            }
        }
    ,function(err,data){
        if(err){
            console.log(err);
        }else{
            console.log(data);
        }
});

Above function produces following SQL Query internally;

SELECT sum(TotalAmount) as MaxAmount FROM orders group by customerId HAVING customerId = 10
orderModel.aggregate(
    {
        _sum:{
            _field: 'TotalAmount',
            _alias: 'MaxAmount'
        },
        _group: {
                _by: {
                    _field: [{_name:'customerId'}]
                },
                _having:{
                    _max: {
                        _field: 'TotalAmount'
                    },
                    _gt: '500'  //greater than
                }
            }
        }
    ,function(err,data){
        if(err){
            console.log(err);
        }else{
            console.log(data);
        }
});

Above function produces following SQL internally;

SELECT sum(TotalAmount) as MaxAmount FROM orders group by customerId HAVING  max(TotalAmount) > 500
  1. run node test.js

Note: Latest version has a fix for returning id post insert query for SQL Server.