mongsql
v1.0.9
Published
```sh $ npm install mongsql --save ```
Downloads
5
Readme
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
- Insert into table
insert(tableName , params)
var params = {
username: "nkkumawat",
email: "[email protected]",
password : "password"
};
var tableName = "TableName";
connection.insert(tableName , params).then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});
- 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);
});
- 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);
});
- 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.........