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

mongo2sql

v1.0.4

Published

Use MongoDB Functions for MySQL ( to avoid code rewrite )

Downloads

20

Readme

Welcome to Mongo2Sql

Hello, this is Mongo2Sql Package for Translate MongoDB Like Work to MySQL. example: "MongoDB" db.collection("accounts").find({id:user.id}).toArray(function(err,result) { ... }); "MySQL": db.connection.query("SELECT * FROM accounts WHERE id = ?", [user.id], function(err,result) { ... }); To avoid that the script works like: "MySQL" db.collection('accounts').find({id:user.id}).toArray(function(err,result) { ... }); and all that translated to SELECT * FROM accounts WHERE id = ${user.id}

that is how the script works

Install

npm install --save mongo2sql

Create Connection

var db = require('mongo2sql')({ host:'localhost', user:'root', pass:'', db:'mongo2sql' });

Functions

collection: Define the Table you want to select.
Find: Create Find Query but do not execute it. toArray: execute Find query and return array value. sort: JSON sort (ASC,DESC). deleteOne/remove: Create Delete query and execute it. insert: Insert ONE document to Database. insertMany: insert Multiple Documents to database by array. update: Create update Query and execute it.

Collection

Select Table

No individual Work

e.g. :

db.collection('account') ...

Find

Create Find query.

No individual Work

Note: You can't use it after toArray or after sort when run find query this functions shall be the first function

toArray

Execute Find Query.

No individual Work

Note: you can't run it before find function

e.g. :

db.collection('test').find({id:3}).toArray(function(err,result) { if(err) return handle(err); console.log(result) //=> [{id:3,name:"Jamal Abo Mokh"}] });

Sort

Create Sort Query.

The Query Executed in toArray function

No individual Work

Note: You can't use it after toArray function or before find function

e.g. :

db.collection('test').find({id:3}).sort({id:-1}).toArray(function(err,result) { if(err) return handle(err); console.log(result) //=> [{id:6,name:"Jamal Abo Mokh2"},{id:3,name:"Jamal Abo Mokh"}] });

deleteOne / remove

Delete All Rows that match the condition from Table.

Note: deleteOne deletes one document in MongoDB but in this functions delete every document match the condition.

e.g. :

db.collection('accounts').deleteOne({id:5}, function(err,result) { console.log(result); //=> {ok:1,nDeleted: 0 || "Deleted Rows" } })

insert

Insert One Document to table (database)

Note: JSON tree limits to 5. See JSON translate

e.g. :

db.collection('accounts').insert({id:5,name:"Jamal Abo Mokh"}, function(err,result) { console.log(result); //=> {ok:1,nInserted: 0 || "Inserted Rows" } })

insertMany

Insert Multiple Documents to table (database) by Array

Note: JSON tree limits to 5. See JSON translate

e.g. :

db.collection('accounts').insert([{id:5,name:"Jamal Abo Mokh"},{id:3,name:"Jamal Abo Mokh2"}], function(err,result) { console.log(result); //=> {ok:1,ColumnInserted: 0 || "Inserted Columns (See Auto-Column)" } })

Update

Update All Rows that match the condition from Table.

Note: JSON tree limits to 5. See JSON translate ({'name.givenName':"Jamal"})

You have to add $set to query. Update Will Not Work without $set

e.g. :

db.collection('accounts').update({id:3},{$set:{address: "Main Street"}}, function(err,result) { console.log(result); // => {ok:1, nModified: 1||0, affectedRows: (number of rows got update)} })

JSON Translate

In MySQL you can't Save JSON like MongoDB:

{ name:{ givenName:"Jamal", familyName: "Abo Mokh" } }

JSON Translate is function that convert JSON to "key.key2"

e.g. :

JSON : { name:{ givenName:"Jamal", familyName: "Abo Mokh" } }

that contain 1 trees

Translated:

name.givenName = Jamal name.familyName = Abo Mokh they will be stored in individual on the database but when run find query it will reconvert it to JSON.

Note: this function translate up to 5 tree in JSON"

Auto-Column

In MySQL Database you have to create Column before insert data on it but in mongodb there is not limitation for this. so there is function that every time you run insert and insertMany will create column with JSON Translated Name even if the column exist.

e.g.

Translated JSON: name.givenName

Runs ALTER TABLE ${table_name} ADD "name.givenName" ${typeof || TEXT || DOUBLE || INT}

License

GPL-3.0