mongsql

1.0.9 • Public • Published

Install

$ npm install mongsql --save

Create Connection

const mongsql = require('mongsql');
 var connection = new mongsql({
	host: "localhost",
	username: "username",
	password: "password",
	database: "databaseName"
});

connection.connect().then(result => {
	console.log(result);
}).catch(err => {
	console.log(err);
});

Define Model

var model = {
	tableName: "TableName",
	defination : [{
		id : "INT",
		autoIncrement: true,
		primaryKey: true
	},{
		username: "VARCHAR(100)",
		unique: true
	},{
		email : "VARCHAR(100)",
		allowNull : true
	},{
		password: "VARCHAR(100)",
		allowNull: false
	}]
};

Sync Tables with Database

connection.sync(model).then(result =>{
	console.log(result);
}).catch(err => {
	console.log(err);
});
   
Foreign Key ( Only One)
{
	tableName: "books",
	defination : [{
		id : "INT",
		autoIncrement: true,
		primaryKey: true
	},{
		bookname: "VARCHAR(100)",
		unique: true
	},{
		person_id : "INT",
		allowNull : false
	},{
		description: "VARCHAR(100)",
		allowNull: false
	}],
	belongsToOne : {
		thisKey : "person_id",
		targetKey : "id", 
		targetTable : "person"
	}
}

Quering

  1. Insert into table
 insert(tableName , params)
var params = {
	username: "nkkumawat",
	email: "nk0kumawat@gmail.com",
	password : "password"
};
var tableName = "TableName";

connection.insert(tableName , params).then(result => {
	console.log(result);
}).catch(err => {
	console.log(err);
});
  1. find one result
 findOne(tableName , params)
var tableName = "TableName";
var params = {
	where :{
		id: "1"
	},
	include : ['id' , 'password']
};

connection.findOne(tableName,params).then(result => {
	console.log(result);
}).catch(err => {
	console.log(err);
});
  1. find All result
 findAll(tableName , params)
var tableName = "TableName";
var params = {
	where :{
		id: "1"
	},
	include : ['id' , 'password']
};

connection.findAll(tableName,params).then(result => {
	console.log(result);
}).catch(err => {
	console.log(err);
});
  1. Join (Full Join)
 add fullJoins in the query parameters.
 
 fullJoins : {
		tableNames : ['books'],
		include: ['books.id' , 'books.bookname']
 } 
  
var tableName = "TableName";
var params = {
	where :{
		id: 'books.id'
	},
	include : ['id' , 'password'],
	fullJoins : {
		tableNames : ['books'],
		include: ['books.id' , 'books.bookname']
	}
};
connection.findAll(tableName,params).then(result => {
	console.log(result);
}).catch(err => {
	console.log(err);
});

Contribute

still under contruction.........

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i mongsql

    Weekly Downloads

    9

    Version

    1.0.9

    License

    ISC

    Unpacked Size

    11.9 kB

    Total Files

    4

    Last publish

    Collaborators

    • nkkumawat