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

@cabesoft/mongosql

v1.0.4

Published

A library to convert SQL queries to MongoDB queries

Downloads

95

Readme

MongoSQL

MongoSQL is a Node.js library that converts SQL queries into MongoDB queries. It supports basic SQL operations such as SELECT, INSERT, UPDATE, DELETE, and JOIN operations, making it easier to transition SQL-based queries to MongoDB.

Installation

npm install @cabesoft/mongosql

Usage

1. Initializing the MongoSqlConverter

Before you can execute any queries, you'll need to initialize the MongoSqlConverter by providing the MongoDB connection string and the database name.

const MongoSqlConverter = require("@cabesoft/mongosql");

const converter = new MongoSqlConverter(
  "mongodb://localhost:27017",
  "testDatabase"
);

2. SELECT Queries

Basic SELECT Query

Convert a basic SQL SELECT query to a MongoDB find query.

const sqlQuery = "SELECT name, age FROM Users WHERE age > 30 ORDER BY name ASC";
const results = await converter.executeSqlQuery(sqlQuery);
console.log(results);

SELECT with INNER JOIN

Convert a SELECT query with an INNER JOIN.

const sqlQuery = `
  SELECT Users.name, Orders.total 
  FROM Users 
  INNER JOIN Orders ON Users.userId = Orders.userId 
  WHERE Orders.total > 100 
  ORDER BY Users.name ASC
`;
const results = await converter.executeSqlQuery(sqlQuery);
console.log(results);

SELECT with LEFT JOIN

Convert a SELECT query with a LEFT JOIN.

const sqlQuery = `
  SELECT Users.name, Orders.total 
  FROM Users 
  LEFT JOIN Orders ON Users.userId = Orders.userId 
  WHERE Orders.total > 100 
  ORDER BY Users.name ASC
`;
const results = await converter.executeSqlQuery(sqlQuery);
console.log(results);

3. INSERT Queries

Convert an SQL INSERT query to a MongoDB insertOne operation.

const sqlQuery = "INSERT INTO Users (name, age) VALUES ('John Doe', 29)";
await converter.executeSqlQuery(sqlQuery);

4. UPDATE Queries

Convert an SQL UPDATE query to a MongoDB updateOne operation.

const sqlQuery = "UPDATE Users SET age = 30 WHERE name = 'John Doe'";
await converter.executeSqlQuery(sqlQuery);

5. DELETE Queries

Convert an SQL DELETE query to a MongoDB deleteOne operation.

const sqlQuery = "DELETE FROM Users WHERE name = 'John Doe'";
await converter.executeSqlQuery(sqlQuery);

6. Complex Queries with Multiple Conditions

MongoSQL also supports complex WHERE clauses with multiple conditions.

AND Condition

const sqlQuery =
  "SELECT name, age FROM Users WHERE age > 30 AND name = 'John Doe'";
const results = await converter.executeSqlQuery(sqlQuery);
console.log(results);

OR Condition

const sqlQuery =
  "SELECT name, age FROM Users WHERE age > 30 OR name = 'Jane Doe'";
const results = await converter.executeSqlQuery(sqlQuery);
console.log(results);

License

ISC