querymongo 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +61 -0
- package/.vscode/settings.json +3 -0
- package/ARCHITECTURE.md +286 -0
- package/dist/application/MongoConverter.js +20 -0
- package/dist/application/MongoConverter.js.map +1 -0
- package/dist/application/MongoConverter.spec.js +59 -0
- package/dist/application/MongoConverter.spec.js.map +1 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/domain/interfaces/Converter.js +2 -0
- package/dist/domain/interfaces/Converter.js.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/infrastructure/converters/ConverterFactory.js +14 -0
- package/dist/infrastructure/converters/ConverterFactory.js.map +1 -0
- package/dist/infrastructure/converters/CreateConverter.js +27 -0
- package/dist/infrastructure/converters/CreateConverter.js.map +1 -0
- package/dist/infrastructure/converters/DeleteConverter.js +21 -0
- package/dist/infrastructure/converters/DeleteConverter.js.map +1 -0
- package/dist/infrastructure/converters/SelectConverter.js +24 -0
- package/dist/infrastructure/converters/SelectConverter.js.map +1 -0
- package/dist/infrastructure/converters/UpdateConverter.js +23 -0
- package/dist/infrastructure/converters/UpdateConverter.js.map +1 -0
- package/dist/infrastructure/parser/sqlParser.js +13 -0
- package/dist/infrastructure/parser/sqlParser.js.map +1 -0
- package/dist/infrastructure/utils/queryUtils.js +71 -0
- package/dist/infrastructure/utils/queryUtils.js.map +1 -0
- package/dist/types/application/MongoConverter.d.ts +5 -0
- package/dist/types/application/MongoConverter.d.ts.map +1 -0
- package/dist/types/application/MongoConverter.spec.d.ts +2 -0
- package/dist/types/application/MongoConverter.spec.d.ts.map +1 -0
- package/dist/types/cli/main.d.ts +3 -0
- package/dist/types/cli/main.d.ts.map +1 -0
- package/{src/domain/interfaces/Converter.ts → dist/types/domain/interfaces/Converter.d.ts} +3 -2
- package/dist/types/domain/interfaces/Converter.d.ts.map +1 -0
- package/{src/index.ts → dist/types/index.d.ts} +2 -4
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/infrastructure/converters/ConverterFactory.d.ts +8 -0
- package/dist/types/infrastructure/converters/ConverterFactory.d.ts.map +1 -0
- package/dist/types/infrastructure/converters/CreateConverter.d.ts +7 -0
- package/dist/types/infrastructure/converters/CreateConverter.d.ts.map +1 -0
- package/dist/types/infrastructure/converters/DeleteConverter.d.ts +7 -0
- package/dist/types/infrastructure/converters/DeleteConverter.d.ts.map +1 -0
- package/dist/types/infrastructure/converters/SelectConverter.d.ts +7 -0
- package/dist/types/infrastructure/converters/SelectConverter.d.ts.map +1 -0
- package/dist/types/infrastructure/converters/UpdateConverter.d.ts +7 -0
- package/dist/types/infrastructure/converters/UpdateConverter.d.ts.map +1 -0
- package/dist/types/infrastructure/parser/sqlParser.d.ts +11 -0
- package/dist/types/infrastructure/parser/sqlParser.d.ts.map +1 -0
- package/dist/types/infrastructure/utils/queryUtils.d.ts +9 -0
- package/dist/types/infrastructure/utils/queryUtils.d.ts.map +1 -0
- package/examples.ts +79 -0
- package/package.json +4 -9
- package/src/application/MongoConverter.spec.ts +0 -73
- package/src/application/MongoConverter.ts +0 -29
- package/src/cli/main.ts +0 -64
- package/src/infrastructure/converters/ConverterFactory.ts +0 -21
- package/src/infrastructure/converters/CreateConverter.ts +0 -37
- package/src/infrastructure/converters/DeleteConverter.ts +0 -32
- package/src/infrastructure/converters/SelectConverter.ts +0 -37
- package/src/infrastructure/converters/UpdateConverter.ts +0 -36
- package/src/infrastructure/parser/sqlParser.ts +0 -17
- package/src/infrastructure/utils/queryUtils.ts +0 -93
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Conversor para queries UPDATE
|
|
3
|
-
* Convierte UPDATE SQL a updateOne/updateMany de MongoDB
|
|
4
|
-
*/
|
|
5
|
-
import type { IConverter } from "../../domain/interfaces/Converter.ts";
|
|
6
|
-
import {
|
|
7
|
-
buildFilter,
|
|
8
|
-
determineOperation,
|
|
9
|
-
removeUndefinedFields,
|
|
10
|
-
} from "../utils/queryUtils.ts";
|
|
11
|
-
|
|
12
|
-
export const UpdateConverter: IConverter = {
|
|
13
|
-
can: (statement: string): boolean => {
|
|
14
|
-
return statement.toLowerCase().startsWith("update");
|
|
15
|
-
},
|
|
16
|
-
|
|
17
|
-
convert: (query: any): Record<string, any> => {
|
|
18
|
-
const { ast } = query;
|
|
19
|
-
const { table, set, where } = ast;
|
|
20
|
-
|
|
21
|
-
const collectionName = table?.[0]?.table || "collection";
|
|
22
|
-
const updateData = Object.fromEntries(
|
|
23
|
-
set.map((item: any) => [item.column, item.value?.value || item.value]),
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
return removeUndefinedFields({
|
|
27
|
-
collection: collectionName,
|
|
28
|
-
operation: determineOperation(where, {
|
|
29
|
-
one: "updateOne",
|
|
30
|
-
many: "updateMany",
|
|
31
|
-
}),
|
|
32
|
-
filter: where ? buildFilter(where) : {},
|
|
33
|
-
update: { $set: updateData },
|
|
34
|
-
});
|
|
35
|
-
},
|
|
36
|
-
};
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parser SQL que encapsula node-sql-parser
|
|
3
|
-
* Abstrae la dependencia externa y proporciona una interfaz limpia
|
|
4
|
-
*/
|
|
5
|
-
import sqlParser from "node-sql-parser";
|
|
6
|
-
|
|
7
|
-
const parser = new sqlParser.Parser();
|
|
8
|
-
|
|
9
|
-
export type ParsedQuery = ReturnType<typeof parser.parse>;
|
|
10
|
-
|
|
11
|
-
export const parseSqlQuery = (sql: string): ParsedQuery | ParsedQuery[] => {
|
|
12
|
-
return parser.parse(sql);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export const normalizeQuery = (query: string): string => {
|
|
16
|
-
return query.trim().replace(/;$/, "").replace(/\s+/g, " ");
|
|
17
|
-
};
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utilidades para conversión de queries SQL a MongoDB
|
|
3
|
-
* Centraliza lógica reutilizable siguiendo principio DRY
|
|
4
|
-
*/
|
|
5
|
-
import _ from "lodash";
|
|
6
|
-
|
|
7
|
-
export const removeUndefinedFields = (
|
|
8
|
-
obj: Record<string, any>,
|
|
9
|
-
): Record<string, any> => {
|
|
10
|
-
return _.omitBy(obj, _.isUndefined);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export const normalizeFieldName = (field: string): string => {
|
|
14
|
-
return field.trim().replace(/`|"/g, "");
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
const normalizeFieldNames = (fields: string[]): string[] => {
|
|
18
|
-
return fields.map(normalizeFieldName);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const buildProjection = (
|
|
22
|
-
fields: string[] | "*",
|
|
23
|
-
): Record<string, 1 | 0> => {
|
|
24
|
-
if (fields === "*") return {};
|
|
25
|
-
return Object.fromEntries(normalizeFieldNames(fields).map((f) => [f, 1]));
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
export const buildFilter = (condition: any): Record<string, any> => {
|
|
29
|
-
if (!condition) return {};
|
|
30
|
-
|
|
31
|
-
const { operator, left, right } = condition;
|
|
32
|
-
|
|
33
|
-
if (!operator) return {};
|
|
34
|
-
|
|
35
|
-
if (operator.toUpperCase() === "AND") {
|
|
36
|
-
return {
|
|
37
|
-
$and: [buildFilter(left), buildFilter(right)],
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (operator.toUpperCase() === "OR") {
|
|
42
|
-
return {
|
|
43
|
-
$or: [buildFilter(left), buildFilter(right)],
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const field = normalizeFieldName(left?.column || left?.value || left || "");
|
|
48
|
-
const value = right?.value !== undefined ? right.value : right;
|
|
49
|
-
|
|
50
|
-
const operatorMap: Record<string, string> = {
|
|
51
|
-
"=": "$eq",
|
|
52
|
-
"!=": "$ne",
|
|
53
|
-
"<>": "$ne",
|
|
54
|
-
"<": "$lt",
|
|
55
|
-
">": "$gt",
|
|
56
|
-
"<=": "$lte",
|
|
57
|
-
">=": "$gte",
|
|
58
|
-
like: "$regex",
|
|
59
|
-
in: "$in",
|
|
60
|
-
"not in": "$nin",
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const mongoOp = operatorMap[operator.toLowerCase()];
|
|
64
|
-
|
|
65
|
-
return mongoOp ? { [field]: { [mongoOp]: value } } : {};
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
export function determineOperation(
|
|
69
|
-
where: any,
|
|
70
|
-
operations: { one: string; many: string },
|
|
71
|
-
): string {
|
|
72
|
-
const uniqueFields = ["id", "_id", "email", "username"];
|
|
73
|
-
const { operator, left } = where;
|
|
74
|
-
|
|
75
|
-
if (!where) return operations.many;
|
|
76
|
-
|
|
77
|
-
if (["and", "or"].includes(operator?.toLowerCase())) {
|
|
78
|
-
return operations.many;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const field = left?.column || left?.value || left || "";
|
|
82
|
-
const isUniqueField = uniqueFields.includes(field?.toLowerCase?.());
|
|
83
|
-
|
|
84
|
-
if (isUniqueField && operator === "=") {
|
|
85
|
-
return operations.one;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return [">", "<", ">=", "<=", "!=", "<>", "like", "in"].includes(
|
|
89
|
-
operator?.toLowerCase(),
|
|
90
|
-
)
|
|
91
|
-
? operations.many
|
|
92
|
-
: operations.one;
|
|
93
|
-
}
|