1.0.9 • Published 5 years ago

mongsql v1.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

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.........

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago