Synopsis
This is a Mysql and Mongodb interface for basic database operations (CRUD).
Code Example
Creat Mysql object
const mysql_mongo =const mysql = mysql_mongo
Create Mongo object
const mysql_mongo =const mongodb = mysql_mongo
Connect to database
let config = host: 'my-host' user: 'my-user' pass: 'my-pass' db: 'my-db'mysql
let config = host: 'my-host' user: 'my-user' port: 27017 pass: 'my-pass' db: 'my-db'mongodb
CRUD Mysql
let valuesToInsert = nombre: 'John Doe' age: 40 address: '42th. Street'mysqllet valuesToUpdate = name: 'Johana Daw' age: 30let updateParams = where: 'id = 1' and: 'name = John Doe'mysqllet deleteParams = where: 'id = 1'mysql
Params in mysql database operations If you want to add some params like where sentence (or like, limit, etc), you must use a object like:
let params = where: 'id > 1 ' and: 'age > 10'
Examples with Select
// This will get all the rows of my_tablemysql// This will get all the data with specific columnslet columns = 'nombre' 'edad' // Must be an arraymysql// This will get all the data with specific columns and the paramslet selectParams = where: 'id_pruebas > 1'mysql
CRUD Mongogb Before crud, you need create a schema and model:
// Basic schema, you can do more complex if you wantlet schema = name: String age: Number address: Stringlet personSchema = mongodblet PersonModel = mongodb
Using the previous example:
let doc = name: 'Jason' age: 25 address: '7th. Street'const docModel = docmongoDb// You can get the id with the find methodlet newValue = age: 30mongoDb// You can use other find method, explained belowmongoDbmongoDb// With paramsmongoDbmongoDbmongoDb
Close connection
mysql// Or with callbackmysql// Will print message in consolemongoDb
Motivation
I want to create a db library that you can use mysql or mongodb easily and in the same interface.
Installation
npm install mysql-mongo-db --save
License
MIT