Skip to content

Create table or collection command, standalone dev environment #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.8'
services:
mongodb:
image: mongo:6-jammy
ports:
- '27017:27017'
volumes:
- dbdata6:/data/db
volumes:
dbdata6:
33 changes: 33 additions & 0 deletions sample/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {SqlToNoSql} from "../dist/index.mjs"

const runner = new SqlToNoSql({
srcDBtype: "postgresql",
destDBtype: "mongodb",
connection: "mongodb://localhost:27017/admin",
});



const main = async () => {
// Create a users table
const createUsers = await runner.run(
"create users"
);


const resp = await runner.run(
"select * from users where email = [email protected]",
);

console.log(resp);
return resp;
}

main()
/** ☝️ [{
_id: new ObjectId("622f07d56852c662cb8b953b"),
role: 'admin',
name: 'Arif Hossain',
email: '[email protected]',
__v: 0
}]*/
98 changes: 60 additions & 38 deletions src/index.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MongoClient } from "mongodb";
import { MongoClient, MongoServerError } from "mongodb";

import { mappings } from "./config/mapping.mjs";
import { parseQuery } from "./utils/parser.mjs";
Expand All @@ -22,46 +22,68 @@ export class SqlToNoSql {

const q = parseQuery(query);

const filters: {
[key: string]: {
[operator: string]: string | number;
};
} = {};

// Convert parsed filters to MongoDB query
q.filters?.forEach((filter) => {
const { column, operator, value } = filter;

if (!filters[column]) {
filters[column] = {
[mappings["mongodb"]["operators"][operator]]: value,
if (q.command === "select"){
const filters: {
[key: string]: {
[operator: string]: string | number;
};
} = {};

// Convert parsed filters to MongoDB query
q.filters?.forEach((filter) => {
const { column, operator, value } = filter;

if (!filters[column]) {
filters[column] = {
[mappings["mongodb"]["operators"][operator]]: value,
};
}
});

const mongoQuery = {
collection: q.table,
[q.command]: mappings["mongodb"]["commands"][q.command],
query: filters,
};

try {
if (!this.client) {
this.client = await connect(this.config.connection);
await this.client.connect();
}

const db = this.client.db();
const collection = db.collection(mongoQuery.collection);

const data = await collection[mongoQuery[q.command]](
mongoQuery.query,
).toArray();

return data;
} catch (err) {
console.error(err);
throw Error("Something went wrong!");
}
});

const mongoQuery = {
collection: q.table,
[q.command]: mappings["mongodb"]["commands"][q.command],
query: filters,
};

try {
if (!this.client) {
this.client = await connect(this.config.connection);
await this.client.connect();
} else if (q.command === "create") {
try {
if (!this.client) {
this.client = await connect(this.config.connection);
await this.client.connect();
}

const db = this.client.db();
const result = await db.createCollection(q.table);
console.log("Mongo result", result);
return result;
} catch (err) {
if (err instanceof MongoServerError) {
if (err.codeName === 'NamespaceExists'){
console.error("Collection already exists: " + q.table)
}
} else {
throw Error("Something went wrong!");
}
}

const db = this.client.db();
const collection = db.collection(mongoQuery.collection);

const data = await collection[mongoQuery[q.command]](
mongoQuery.query,
).toArray();

return data;
} catch (err) {
console.error(err);
throw Error("Something went wrong!");
}
}
}
2 changes: 1 addition & 1 deletion src/types/sql.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface filterType {
}

export interface ParsedSqlType {
command: "select";
command: "select" | "create";
table: string;
columns: string[];
filters: filterType[] | null;
Expand Down
54 changes: 30 additions & 24 deletions src/utils/parser.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { ParsedSqlType } from "types/sql.mjs";

const supportedCommands = ["select", "create"];

export const parseQuery = (query: string): ParsedSqlType => {
const parsedQuery: ParsedSqlType = {
command: "select",
Expand All @@ -13,32 +15,36 @@ export const parseQuery = (query: string): ParsedSqlType => {
const [command, ...rest] = query.split(" ");

const lowerCaseCommand = command.toLowerCase();
if (lowerCaseCommand !== "select") {
throw new Error("Only select queries are supported");
if (supportedCommands.indexOf(lowerCaseCommand) < 0) {
throw new Error(`Only following commands are supported: ${supportedCommands} given: ${lowerCaseCommand}`);
}
// parsedQuery.command = command;

const fromIndex = rest.findIndex((word) => word.toLowerCase() === "from");
if (fromIndex === -1) {
throw new Error("Invalid query, missing FROM keyword");
if (lowerCaseCommand === "select"){
const fromIndex = rest.findIndex((word) => word.toLowerCase() === "from");
if (fromIndex === -1) {
throw new Error("Invalid query, missing FROM keyword");
}
parsedQuery.table = rest[fromIndex + 1];
parsedQuery.columns = rest.slice(0, fromIndex);

const whereIndex = rest.findIndex((word) => word.toLowerCase() === "where");
if (whereIndex !== -1) {
parsedQuery.filters = [
{
column: rest[whereIndex + 1],
operator: rest[whereIndex + 2] as "=",
// to handle string and number values
value:
Number(rest[whereIndex + 3]) ||
// remove quotes from string values
String(rest[whereIndex + 3]).replace(/^'(.*)'$/, "$1"),
},
];
}
} else if (lowerCaseCommand === "create") {
parsedQuery.command = "create";
const table_name = rest[0];
parsedQuery.table = table_name;
}
parsedQuery.table = rest[fromIndex + 1];
parsedQuery.columns = rest.slice(0, fromIndex);

const whereIndex = rest.findIndex((word) => word.toLowerCase() === "where");
if (whereIndex !== -1) {
parsedQuery.filters = [
{
column: rest[whereIndex + 1],
operator: rest[whereIndex + 2] as "=",
// to handle string and number values
value:
Number(rest[whereIndex + 3]) ||
// remove quotes from string values
String(rest[whereIndex + 3]).replace(/^'(.*)'$/, "$1"),
},
];
}

return parsedQuery;
};