forge-sql-orm 2.1.0 → 2.1.2

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.
@@ -78,6 +78,7 @@ export async function saveTableIfInsideCacheContext<T extends AnyMySqlTable>(
78
78
  *
79
79
  * @param query - The Drizzle query to cache
80
80
  * @param rows - The query result data to cache
81
+ * @param options - ForgeSqlOrm options
81
82
  * @returns Promise that resolves when the data is saved to local cache
82
83
  *
83
84
  * @example
@@ -89,7 +90,7 @@ export async function saveTableIfInsideCacheContext<T extends AnyMySqlTable>(
89
90
  */
90
91
  export async function saveQueryLocalCacheQuery<
91
92
  T extends MySqlSelectDynamic<AnyMySqlSelectQueryBuilder>,
92
- >(query: T, rows: unknown[]): Promise<void> {
93
+ >(query: T, rows: unknown[], options: ForgeSqlOrmOptions): Promise<void> {
93
94
  const context = localCacheApplicationContext.getStore();
94
95
  if (context) {
95
96
  if (!context.cache) {
@@ -101,6 +102,12 @@ export async function saveQueryLocalCacheQuery<
101
102
  sql: sql.toSQL().sql.toLowerCase(),
102
103
  data: rows,
103
104
  };
105
+ if (options.logRawSqlQuery) {
106
+ const q = sql.toSQL();
107
+ console.log(
108
+ `[forge-sql-orm][local-cache][SAVE] Stored result in cache. sql="${q.sql}", params=${JSON.stringify(q.params)}`,
109
+ );
110
+ }
104
111
  }
105
112
  }
106
113
 
@@ -109,6 +116,7 @@ export async function saveQueryLocalCacheQuery<
109
116
  * This function checks if a query result is already cached in memory.
110
117
  *
111
118
  * @param query - The Drizzle query to check for cached results
119
+ * @param options - Option Property
112
120
  * @returns Promise that resolves to cached data if found, undefined otherwise
113
121
  *
114
122
  * @example
@@ -123,7 +131,7 @@ export async function saveQueryLocalCacheQuery<
123
131
  */
124
132
  export async function getQueryLocalCacheQuery<
125
133
  T extends MySqlSelectDynamic<AnyMySqlSelectQueryBuilder>,
126
- >(query: T): Promise<unknown[] | undefined> {
134
+ >(query: T, options: ForgeSqlOrmOptions): Promise<unknown[] | undefined> {
127
135
  const context = localCacheApplicationContext.getStore();
128
136
  if (context) {
129
137
  if (!context.cache) {
@@ -132,6 +140,12 @@ export async function getQueryLocalCacheQuery<
132
140
  const sql = query as { toSQL: () => Query };
133
141
  const key = hashKey(sql.toSQL());
134
142
  if (context.cache[key] && context.cache[key].sql === sql.toSQL().sql.toLowerCase()) {
143
+ if (options.logRawSqlQuery) {
144
+ const q = sql.toSQL();
145
+ console.log(
146
+ `[forge-sql-orm][local-cache][HIT] Returned cached result. sql="${q.sql}", params=${JSON.stringify(q.params)}`,
147
+ );
148
+ }
135
149
  return context.cache[key].data;
136
150
  }
137
151
  }
@@ -75,19 +75,44 @@ export const parseDateTime = (value: string | Date, format: string): Date => {
75
75
  };
76
76
 
77
77
  /**
78
- * Helper function to validate and format Date objects using DateTime
79
- * @param value - Date object to validate and format
80
- * @param format - DateTime format string
81
- * @returns Formatted date string
82
- * @throws Error if date is invalid
78
+ * Helper function to validate and format a date-like value using Luxon DateTime.
79
+ * @param value - Date object, ISO/RFC2822/SQL/HTTP string, or timestamp (number|string).
80
+ * @param format - DateTime format string (Luxon format tokens).
81
+ * @returns Formatted date string.
82
+ * @throws Error if value cannot be parsed as a valid date.
83
83
  */
84
- export function formatDateTime(value: Date, format: string): string {
85
- const fromJSDate = DateTime.fromJSDate(value);
86
- if (fromJSDate.isValid) {
87
- return fromJSDate.toFormat(format);
84
+ export function formatDateTime(value: Date | string | number, format: string): string {
85
+ let dt: DateTime | null = null;
86
+
87
+ if (value instanceof Date) {
88
+ dt = DateTime.fromJSDate(value);
89
+ } else if (typeof value === "string") {
90
+ for (const parser of [
91
+ DateTime.fromISO,
92
+ DateTime.fromRFC2822,
93
+ DateTime.fromSQL,
94
+ DateTime.fromHTTP,
95
+ ]) {
96
+ dt = parser(value);
97
+ if (dt.isValid) break;
98
+ }
99
+ if (!dt?.isValid) {
100
+ const parsed = Number(value);
101
+ if (!isNaN(parsed)) {
102
+ dt = DateTime.fromMillis(parsed);
103
+ }
104
+ }
105
+ } else if (typeof value === "number") {
106
+ dt = DateTime.fromMillis(value);
88
107
  } else {
108
+ throw new Error("Unsupported type");
109
+ }
110
+
111
+ if (!dt?.isValid) {
89
112
  throw new Error("Invalid Date");
90
113
  }
114
+
115
+ return dt.toFormat(format);
91
116
  }
92
117
 
93
118
  /**