forge-sql-orm 1.0.1

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.
@@ -0,0 +1,72 @@
1
+ import { sql, UpdateQueryResponse } from "@forge/sql";
2
+ import type { EntitySchema } from "@mikro-orm/core/metadata/EntitySchema";
3
+ import { parseDateTime } from "../utils/sqlUtils";
4
+ import { SchemaSqlForgeSql } from "./ForgeSQLQueryBuilder";
5
+
6
+ export class ForgeSQLSelectOperations implements SchemaSqlForgeSql {
7
+ /**
8
+ * Executes a schema-based SQL query and maps the result to the entity schema.
9
+ * @param query - The SQL query to execute.
10
+ * @param schema - The entity schema defining the structure.
11
+ * @returns A list of mapped entity objects.
12
+ */
13
+ async executeSchemaSQL<T extends object>(query: string, schema: EntitySchema<T>): Promise<T[]> {
14
+ const datas = await this.executeRawSQL<unknown>(query);
15
+ if (!datas.length) return [];
16
+
17
+ return datas.map((r) => {
18
+ const rawModel = r as Record<string, unknown>;
19
+ const newModel: Record<string, unknown> = {};
20
+
21
+ schema.meta.props
22
+ .filter((p) => p.kind === "scalar")
23
+ .forEach((p) => {
24
+ const fieldName = p.name;
25
+ const fieldNames = p.fieldNames;
26
+ const rawFieldName = fieldNames && Array.isArray(fieldNames)? fieldNames[0]: p.name;
27
+
28
+ switch (p.type) {
29
+ case "datetime":
30
+ newModel[fieldName] = parseDateTime(
31
+ rawModel[rawFieldName] as string,
32
+ "YYYY-MM-DDTHH:mm:ss.SSS",
33
+ );
34
+ break;
35
+ case "date":
36
+ newModel[fieldName] = parseDateTime(rawModel[rawFieldName] as string, "YYYY-MM-DD");
37
+ break;
38
+ case "time":
39
+ newModel[fieldName] = parseDateTime(rawModel[rawFieldName] as string, "HH:mm:ss.SSS");
40
+ break;
41
+ default:
42
+ newModel[fieldName] = rawModel[rawFieldName];
43
+ }
44
+ });
45
+ return newModel as T;
46
+ });
47
+ }
48
+
49
+ /**
50
+ * Executes a raw SQL query and returns the results.
51
+ * @param query - The raw SQL query to execute.
52
+ * @returns A list of results as objects.
53
+ */
54
+ async executeRawSQL<T extends object | unknown>(query: string): Promise<T[]> {
55
+ console.debug("Executing raw SQL: " + query);
56
+ const sqlStatement = await sql.prepare<T>(query).execute();
57
+ console.debug("Query result: " + JSON.stringify(sqlStatement));
58
+ return sqlStatement.rows as T[];
59
+ }
60
+
61
+ /**
62
+ * Executes a raw SQL update query.
63
+ * @param query - The raw SQL update query.
64
+ * @returns The update response containing affected rows.
65
+ */
66
+ async executeRawUpdateSQL(query: string): Promise<UpdateQueryResponse> {
67
+ console.debug("Executing update SQL: " + query);
68
+ const sqlStatement = sql.prepare<UpdateQueryResponse>(query);
69
+ const updateQueryResponseResults = await sqlStatement.execute();
70
+ return updateQueryResponseResults.rows;
71
+ }
72
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ import ForgeSQLORM from "./core/ForgeSQLORM";
2
+
3
+ export * from "./core/ForgeSQLQueryBuilder";
4
+ export * from "./core/ForgeSQLCrudOperations";
5
+ export * from "./core/ForgeSQLSelectOperations";
6
+ export * from "@mikro-orm/mysql";
7
+ export * from "@mikro-orm/entity-generator";
8
+
9
+ export default ForgeSQLORM;
@@ -0,0 +1,4 @@
1
+ import knex from "@mikro-orm/knex";
2
+
3
+ export * from "@mikro-orm/knex";
4
+ export default knex;
@@ -0,0 +1,25 @@
1
+ import { types } from "@mikro-orm/core/types";
2
+ import moment from "moment";
3
+ import { AnyString } from "@mikro-orm/core/typings";
4
+
5
+ export const transformValue = (value: {
6
+ type: keyof typeof types | AnyString;
7
+ value: unknown;
8
+ }): unknown => {
9
+ switch (value.type) {
10
+ case "text":
11
+ case "string":
12
+ return `'${value.value}'`;
13
+ case "datetime":
14
+ return `'${moment(value.value as Date).format("YYYY-MM-DDTHH:mm:ss.SSS")}'`;
15
+ case "date":
16
+ return `'${moment(value.value as Date).format("YYYY-MM-DD")}'`;
17
+ case "time":
18
+ return `'${moment(value.value as Date).format("HH:mm:ss.SSS")}'`;
19
+ default:
20
+ return value.value;
21
+ }
22
+ };
23
+ export const parseDateTime = (value: string, format: string): Date => {
24
+ return moment(value, format).toDate();
25
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "ts-node": {
3
+ "compilerOptions": {
4
+ "module": "commonjs"
5
+ }
6
+ },
7
+ "compilerOptions": {
8
+ "outDir": "dist",
9
+ "declaration": true,
10
+ "declarationMap": true,
11
+ "emitDeclarationOnly": false,
12
+ "module": "commonjs",
13
+ "moduleResolution": "node",
14
+ "target": "ESNext",
15
+ "sourceMap": true,
16
+ "strict": true,
17
+ "allowJs": true,
18
+ "noImplicitAny": true,
19
+ "skipLibCheck": true,
20
+ "esModuleInterop": true,
21
+ "allowSyntheticDefaultImports": true
22
+ },
23
+ "include": ["src"]
24
+ }