@stacksjs/database 0.75.581 → 0.75.584

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.
@@ -3,6 +3,14 @@ export declare function setQueryTracker(fn: QueryTracker): void;
3
3
  * Process an executed query and store it in the database
4
4
  */
5
5
  export declare function logQuery(event: LogEvent): Promise<void>;
6
+ /**
7
+ * True for a statement against one of the framework's own polling tables.
8
+ *
9
+ * Matched on the table a statement reads or writes, not on its text, so a
10
+ * query that merely mentions `jobs` in a value or names `user_jobs` is not
11
+ * taken for one.
12
+ */
13
+ export declare function isHousekeepingQuery(query: string): boolean;
6
14
  export declare function isExcludedQuery(query: string, excludedQueries: readonly string[]): boolean;
7
15
  /** The `executed_at` before which rows have outlived `retentionDays`, or null to keep everything. */
8
16
  export declare function queryLogRetentionCutoff(retentionDays: unknown, now?: number): string | null;
@@ -1,2 +1,2 @@
1
- import{sqlDateTime}from"./sql-helpers";import{memoryUsage}from"node:process";import{AsyncLocalStorage}from"node:async_hooks";import{config}from"@stacksjs/config";import{env as envVars}from"@stacksjs/env";import{log}from"@stacksjs/logging";import{parseCaptureBindings,parseSensitiveColumn,persistQueryLogValues,queryLogError}from"./query-log-bindings";import{normalizeQuery,parseQuery}from"./query-parser";import{db,getDatabaseDialect}from"./utils";import{isLibsqlDriver}from"./dialect";const QUERY_TRACKER_KEY=Symbol.for("stacks.database.queryTracker");let configuredQueryTracker=()=>{};export function setQueryTracker(fn){configuredQueryTracker=fn}function trackQuery(query,durationMs,connection){const shared=globalThis[QUERY_TRACKER_KEY];const tracker=typeof shared==="function"?shared:configuredQueryTracker;tracker(query,durationMs,connection)}const queryLogContext=new AsyncLocalStorage;const QUERY_LOG_BATCH_SIZE=100;const QUERY_LOG_BATCH_DELAY_MS=5;const pendingQueryLogs=[];let queryLogFlushScheduled=false;let queryLogFlushInFlight=false;let queryLogTableMissing=false;function enqueueQueryLog(record){const settled=new Promise((resolve)=>{pendingQueryLogs.push({record,resolve})});if(pendingQueryLogs.length>=QUERY_LOG_BATCH_SIZE){flushQueuedQueryLogs()}else if(!queryLogFlushScheduled){queryLogFlushScheduled=true;setTimeout(()=>{queryLogFlushScheduled=false;flushQueuedQueryLogs()},QUERY_LOG_BATCH_DELAY_MS)}return settled}async function flushQueuedQueryLogs(){if(queryLogFlushInFlight)return;queryLogFlushInFlight=true;try{while(pendingQueryLogs.length>0){const batch=pendingQueryLogs.splice(0,QUERY_LOG_BATCH_SIZE);const recordsByShape=Map.groupBy(batch.map((item)=>item.record),(record)=>Object.keys(record).join("\x00"));if(queryLogTableMissing)queryLogTableMissing=!await queryLogContext.run(true,queryLogTableExists);for(const records of recordsByShape.values()){if(queryLogTableMissing)break;const stored=await queryLogContext.run(true,()=>storeQueryLogs(records,records.length===1));if(stored==="failed"){for(const record of records){if(await queryLogContext.run(true,()=>storeQueryLogs([record]))==="missing")break}}}for(const item of batch)item.resolve();if(!queryLogTableMissing)maybePruneQueryLogs()}}finally{queryLogFlushInFlight=false;if(pendingQueryLogs.length>0&&!queryLogFlushScheduled){queryLogFlushScheduled=true;setTimeout(()=>{queryLogFlushScheduled=false;flushQueuedQueryLogs()},QUERY_LOG_BATCH_DELAY_MS)}}}export async function logQuery(event){if(queryLogContext.getStore())return;try{const{query,durationMs,error,parameters}=extractQueryInfo(event);if(query){try{trackQuery(query,durationMs,config.database?.default||"unknown")}catch{}}if(!config.database?.queryLogging?.enabled)return;const excludedQueries=config.database?.queryLogging?.excludedQueries;if(!query.trim()||isExcludedQuery(query,Array.isArray(excludedQueries)?excludedQueries:[]))return;const status=determineQueryStatus(durationMs,error);const logRecord=await createQueryLogRecord(query,durationMs,status,error,parameters);if(config.database?.queryLogging?.analysis?.enabled&&(status==="slow"||config.database.queryLogging.analysis.analyzeAll)){await enhanceWithQueryAnalysis(logRecord)}await enqueueQueryLog(logRecord);if(status!=="completed"){const logMethod=status==="failed"?"error":"warn";log[logMethod](`Query ${status}:`,{query:logRecord.query,duration:logRecord.duration,connection:logRecord.connection,...error&&{error}})}}catch(err){log.error("Failed to log query:",err)}}export function isExcludedQuery(query,excludedQueries){const normalizedQuery=query.toLowerCase();return excludedQueries.some((pattern)=>{const normalizedPattern=pattern.trim().toLowerCase();return normalizedPattern.length>0&&normalizedQuery.includes(normalizedPattern)})}function queryText(sql){if(typeof sql==="string")return usableQueryText(sql);if(sql&&typeof sql==="object"&&typeof sql.then==="function")return"";if(sql&&typeof sql.toString==="function")return usableQueryText(String(sql));return""}function usableQueryText(text){return text.startsWith("[object ")?"":text}function extractQueryInfo(event){const query=queryText(event.query?.sql);const durationMs=event.queryDurationMillis||0;const error=event.error;const parameters=event.query?.parameters;return{query,durationMs,error,parameters}}function determineQueryStatus(durationMs,error){const slowThreshold=config.database?.queryLogging?.slowThreshold||100;if(error)return"failed";if(durationMs>slowThreshold)return"slow";return"completed"}const warnedCaptureBindings=new Set;function capturesBindingValues(){const setting=config.database?.queryLogging?.captureBindings??envVars.DB_QUERY_LOGGING_CAPTURE_BINDINGS;const capture=parseCaptureBindings(setting);if(capture===null){const shown=String(setting);if(!warnedCaptureBindings.has(shown)){warnedCaptureBindings.add(shown);console.warn(`[database] queryLogging.captureBindings (DB_QUERY_LOGGING_CAPTURE_BINDINGS) is "${shown}", which is not true, false, 1, 0, yes, no, on or off; query logs keep only the type of each binding.`)}return false}if(capture!==undefined)return capture;const appEnv=config.app?.env;return appEnv!=="production"&&appEnv!=="prod"}const warnedSensitiveColumns=new Set;function sensitiveColumns(){const setting=config.database?.queryLogging?.sensitiveColumns;if(setting===undefined||setting===null)return[];const entries=Array.isArray(setting)?setting:[setting];for(const entry of entries){const shown=String(entry);if(parseSensitiveColumn(entry)===undefined&&!warnedSensitiveColumns.has(shown)){warnedSensitiveColumns.add(shown);console.warn(`[database] queryLogging.sensitiveColumns has "${shown}", which is not a column or table.column; it marks nothing as sensitive.`)}}return entries}async function createQueryLogRecord(query,durationMs,status,error,parameters){const connection=config.database.default||"unknown";const normalizedQuery=normalizeQuery(query)||query;const traceInfo=status==="completed"&&!config.database?.queryLogging?.captureAllTraces?undefined:extractTraceInfo();const errorText=error?String(error):undefined;const persisted=parameters?persistQueryLogValues(query,parameters,errorText,{captureValues:capturesBindingValues(),sensitiveColumns:sensitiveColumns()}):{bindings:undefined,error:errorText===undefined?undefined:queryLogError(errorText,[],[])};return{query,normalized_query:normalizedQuery,duration:durationMs,connection,status,error:persisted.error,executed_at:sqlDateTime(),bindings:persisted.bindings,trace:traceInfo?.trace,...traceInfo?.caller??{},memory_usage:memoryUsage().heapUsed/1024/1024}}const SECRET_PATTERNS=[/\b(?:sk|pk|rk|api|secret|token|bearer|password|pwd|key)[_-]?[A-Za-z0-9]{12,}/gi,/\bsk_(?:live|test)_[A-Za-z0-9]{20,}/g,/\bAKIA[0-9A-Z]{16}/g,/\b[A-Za-z0-9_-]{40,}\b(?=\s|$|['"])/g];function sanitizeStackTrace(stack){let out=stack;for(const pattern of SECRET_PATTERNS)out=out.replace(pattern,"<redacted>");return out}let lastTraceInfo;function extractTraceInfo(){try{const stack=new Error("Stack trace capture").stack||"";if(lastTraceInfo?.trace===stack)return lastTraceInfo;const stackLines=stack.split(`
1
+ import{sqlDateTime}from"./sql-helpers";import{memoryUsage}from"node:process";import{AsyncLocalStorage}from"node:async_hooks";import{config}from"@stacksjs/config";import{env as envVars}from"@stacksjs/env";import{log}from"@stacksjs/logging";import{parseCaptureBindings,parseSensitiveColumn,persistQueryLogValues,queryLogError}from"./query-log-bindings";import{normalizeQuery,parseQuery}from"./query-parser";import{db,getDatabaseDialect}from"./utils";import{isLibsqlDriver}from"./dialect";const QUERY_TRACKER_KEY=Symbol.for("stacks.database.queryTracker");let configuredQueryTracker=()=>{};export function setQueryTracker(fn){configuredQueryTracker=fn}function trackQuery(query,durationMs,connection){const shared=globalThis[QUERY_TRACKER_KEY];const tracker=typeof shared==="function"?shared:configuredQueryTracker;tracker(query,durationMs,connection)}const queryLogContext=new AsyncLocalStorage;const QUERY_LOG_BATCH_SIZE=100;const QUERY_LOG_BATCH_DELAY_MS=5;const pendingQueryLogs=[];let queryLogFlushScheduled=false;let queryLogFlushInFlight=false;let queryLogTableMissing=false;function enqueueQueryLog(record){const settled=new Promise((resolve)=>{pendingQueryLogs.push({record,resolve})});if(pendingQueryLogs.length>=QUERY_LOG_BATCH_SIZE){flushQueuedQueryLogs()}else if(!queryLogFlushScheduled){queryLogFlushScheduled=true;setTimeout(()=>{queryLogFlushScheduled=false;flushQueuedQueryLogs()},QUERY_LOG_BATCH_DELAY_MS)}return settled}async function flushQueuedQueryLogs(){if(queryLogFlushInFlight)return;queryLogFlushInFlight=true;try{while(pendingQueryLogs.length>0){const batch=pendingQueryLogs.splice(0,QUERY_LOG_BATCH_SIZE);const recordsByShape=Map.groupBy(batch.map((item)=>item.record),(record)=>Object.keys(record).join("\x00"));if(queryLogTableMissing)queryLogTableMissing=!await queryLogContext.run(true,queryLogTableExists);for(const records of recordsByShape.values()){if(queryLogTableMissing)break;const stored=await queryLogContext.run(true,()=>storeQueryLogs(records,records.length===1));if(stored==="failed"){for(const record of records){if(await queryLogContext.run(true,()=>storeQueryLogs([record]))==="missing")break}}}for(const item of batch)item.resolve();if(!queryLogTableMissing)maybePruneQueryLogs()}}finally{queryLogFlushInFlight=false;if(pendingQueryLogs.length>0&&!queryLogFlushScheduled){queryLogFlushScheduled=true;setTimeout(()=>{queryLogFlushScheduled=false;flushQueuedQueryLogs()},QUERY_LOG_BATCH_DELAY_MS)}}}export async function logQuery(event){if(queryLogContext.getStore())return;try{const{query,durationMs,error,parameters}=extractQueryInfo(event);if(query){try{trackQuery(query,durationMs,config.database?.default||"unknown")}catch{}}if(!config.database?.queryLogging?.enabled)return;const excludedQueries=config.database?.queryLogging?.excludedQueries;if(!query.trim()||isExcludedQuery(query,Array.isArray(excludedQueries)?excludedQueries:[]))return;const status=determineQueryStatus(durationMs,error);if(status==="completed"&&!config.database?.queryLogging?.logHousekeeping&&isHousekeepingQuery(query))return;const logRecord=await createQueryLogRecord(query,durationMs,status,error,parameters);if(config.database?.queryLogging?.analysis?.enabled&&(status==="slow"||config.database.queryLogging.analysis.analyzeAll)){await enhanceWithQueryAnalysis(logRecord)}await enqueueQueryLog(logRecord);if(status!=="completed"){const logMethod=status==="failed"?"error":"warn";log[logMethod](`Query ${status}:`,{query:logRecord.query,duration:logRecord.duration,connection:logRecord.connection,...error&&{error}})}}catch(err){log.error("Failed to log query:",err)}}const HOUSEKEEPING_TABLES=["jobs","queue_workers","queue_circuit_state"];const HOUSEKEEPING_QUERY=new RegExp(`\\b(?:from|update|into|join)\\s+["\`]?(?:${HOUSEKEEPING_TABLES.join("|")})["\`]?(?![\\w"\`])`,"i");export function isHousekeepingQuery(query){return HOUSEKEEPING_QUERY.test(query)}export function isExcludedQuery(query,excludedQueries){const normalizedQuery=query.toLowerCase();return excludedQueries.some((pattern)=>{const normalizedPattern=pattern.trim().toLowerCase();return normalizedPattern.length>0&&normalizedQuery.includes(normalizedPattern)})}function queryText(sql){if(typeof sql==="string")return usableQueryText(sql);if(sql&&typeof sql==="object"&&typeof sql.then==="function")return"";if(sql&&typeof sql.toString==="function")return usableQueryText(String(sql));return""}function usableQueryText(text){return text.startsWith("[object ")?"":text}function extractQueryInfo(event){const query=queryText(event.query?.sql);const durationMs=event.queryDurationMillis||0;const error=event.error;const parameters=event.query?.parameters;return{query,durationMs,error,parameters}}function determineQueryStatus(durationMs,error){const slowThreshold=config.database?.queryLogging?.slowThreshold||100;if(error)return"failed";if(durationMs>slowThreshold)return"slow";return"completed"}const warnedCaptureBindings=new Set;function capturesBindingValues(){const setting=config.database?.queryLogging?.captureBindings??envVars.DB_QUERY_LOGGING_CAPTURE_BINDINGS;const capture=parseCaptureBindings(setting);if(capture===null){const shown=String(setting);if(!warnedCaptureBindings.has(shown)){warnedCaptureBindings.add(shown);console.warn(`[database] queryLogging.captureBindings (DB_QUERY_LOGGING_CAPTURE_BINDINGS) is "${shown}", which is not true, false, 1, 0, yes, no, on or off; query logs keep only the type of each binding.`)}return false}if(capture!==undefined)return capture;const appEnv=config.app?.env;return appEnv!=="production"&&appEnv!=="prod"}const warnedSensitiveColumns=new Set;function sensitiveColumns(){const setting=config.database?.queryLogging?.sensitiveColumns;if(setting===undefined||setting===null)return[];const entries=Array.isArray(setting)?setting:[setting];for(const entry of entries){const shown=String(entry);if(parseSensitiveColumn(entry)===undefined&&!warnedSensitiveColumns.has(shown)){warnedSensitiveColumns.add(shown);console.warn(`[database] queryLogging.sensitiveColumns has "${shown}", which is not a column or table.column; it marks nothing as sensitive.`)}}return entries}async function createQueryLogRecord(query,durationMs,status,error,parameters){const connection=config.database.default||"unknown";const normalizedQuery=normalizeQuery(query)||query;const traceInfo=status==="completed"&&!config.database?.queryLogging?.captureAllTraces?undefined:extractTraceInfo();const errorText=error?String(error):undefined;const persisted=parameters?persistQueryLogValues(query,parameters,errorText,{captureValues:capturesBindingValues(),sensitiveColumns:sensitiveColumns()}):{bindings:undefined,error:errorText===undefined?undefined:queryLogError(errorText,[],[])};return{query,normalized_query:normalizedQuery,duration:durationMs,connection,status,error:persisted.error,executed_at:sqlDateTime(),bindings:persisted.bindings,trace:traceInfo?.trace,...traceInfo?.caller??{},memory_usage:memoryUsage().heapUsed/1024/1024}}const SECRET_PATTERNS=[/\b(?:sk|pk|rk|api|secret|token|bearer|password|pwd|key)[_-]?[A-Za-z0-9]{12,}/gi,/\bsk_(?:live|test)_[A-Za-z0-9]{20,}/g,/\bAKIA[0-9A-Z]{16}/g,/\b[A-Za-z0-9_-]{40,}\b(?=\s|$|['"])/g];function sanitizeStackTrace(stack){let out=stack;for(const pattern of SECRET_PATTERNS)out=out.replace(pattern,"<redacted>");return out}let lastTraceInfo;function extractTraceInfo(){try{const stack=new Error("Stack trace capture").stack||"";if(lastTraceInfo?.trace===stack)return lastTraceInfo;const stackLines=stack.split(`
2
2
  `).slice(1);const callerLine=stackLines.find((line)=>!line.includes("query-logger.ts"));let caller={};if(callerLine){const methodMatch=callerLine.match(/at (.+?) \(/);const fileMatch=callerLine.match(/\((.+?):(\d+):(\d+)\)/);if(methodMatch&&methodMatch[1]){const methodParts=methodMatch[1].split(".");caller={model:methodParts.length>1?methodParts[0]:undefined,method:methodParts.length>1?methodParts[1]:methodParts[0]}}if(fileMatch&&fileMatch[1]&&fileMatch[2]){caller={...caller,file:fileMatch[1],line:Number.parseInt(fileMatch[2],10)}}}const result={trace:sanitizeStackTrace(stack),caller};if(result.trace===stack&&stack.length<=8192)lastTraceInfo=result;return result}catch{return{trace:"",caller:{}}}}async function enhanceWithQueryAnalysis(logRecord){try{const{tables,type}=parseQuery(logRecord.query);logRecord.affected_tables=JSON.stringify(tables||[]);if(type==="SELECT"&&config.database?.queryLogging?.analysis?.explainPlan){const explainResult=await getExplainPlan(logRecord.query);if(explainResult){logRecord.explain_plan=explainResult.plan;logRecord.indexes_used=JSON.stringify(explainResult.indexesUsed||[]);logRecord.missing_indexes=JSON.stringify(explainResult.missingIndexes||[]);if(config.database?.queryLogging?.analysis?.suggestions){logRecord.optimization_suggestions=JSON.stringify(generateOptimizationSuggestions(explainResult,logRecord))}}}if(type!=="SELECT"){}const tags=[type];if(tables&&tables.length>0){tags.push(...tables.map((table)=>`table:${table}`))}logRecord.tags=JSON.stringify(tags)}catch(error){log.debug("Error during query analysis:",error)}}const EXPLAIN_DIALECTS=new Set(["sqlite","turso","mysql","postgres"]);async function getExplainPlan(query){try{const rawDriver=(await import("@stacksjs/config")).config?.database?.default;const driver=typeof rawDriver==="string"?isLibsqlDriver(rawDriver)?"sqlite":rawDriver:"sqlite";if(!EXPLAIN_DIALECTS.has(driver))return null;let sqlText;if(driver==="mysql")sqlText=`EXPLAIN FORMAT=JSON ${query}`;else if(driver==="postgres")sqlText=`EXPLAIN (FORMAT JSON) ${query}`;else sqlText=`EXPLAIN QUERY PLAN ${query}`;const result=await db.unsafe?.(sqlText);if(!result)return null;const raw=result;const rows=Array.isArray(raw)?raw:raw?.rows??[];const planText=JSON.stringify(rows);const indexesUsed=[];const missingIndexes=[];if(driver==="sqlite"){for(const row of rows){const detail=row?.detail||"";const idxMatch=detail.match(/USING (?:COVERING )?INDEX (\w+)/i);if(idxMatch&&idxMatch[1]){indexesUsed.push(idxMatch[1])}else{const scanMatch=detail.match(/^SCAN\s+(\w+)/i);if(scanMatch&&scanMatch[1])missingIndexes.push(scanMatch[1])}}}else if(driver==="mysql"){const text=planText;for(const m of text.matchAll(/"key"\s*:\s*"([^"]+)"/g)){if(m[1])indexesUsed.push(m[1])}for(const m of text.matchAll(/"table_name"\s*:\s*"([^"]+)"[^}]*"access_type"\s*:\s*"ALL"/g)){if(m[1])missingIndexes.push(m[1])}}else if(driver==="postgres"){const text=planText;for(const m of text.matchAll(/"Index Name"\s*:\s*"([^"]+)"/g)){if(m[1])indexesUsed.push(m[1])}for(const m of text.matchAll(/"Node Type"\s*:\s*"Seq Scan"[^}]*"Relation Name"\s*:\s*"([^"]+)"/g)){if(m[1])missingIndexes.push(m[1])}}return{plan:planText.length>4000?`${planText.slice(0,4000)}…`:planText,indexesUsed:Array.from(new Set(indexesUsed)),missingIndexes:Array.from(new Set(missingIndexes))}}catch(err){log.debug("[query-logger] EXPLAIN failed (non-fatal):",err);return null}}function generateOptimizationSuggestions(explainResult,logRecord){const suggestions=[];if(explainResult.missingIndexes&&explainResult.missingIndexes.length>0){suggestions.push(`Consider adding index on ${explainResult.missingIndexes.join(", ")}`)}if(logRecord.status==="slow"){suggestions.push("Consider optimizing this query to reduce execution time");if(logRecord.query.toLowerCase().includes("select *")){suggestions.push("Specify only needed columns instead of using SELECT *")}if(!logRecord.query.toLowerCase().includes("limit")){suggestions.push("Consider adding LIMIT clause to reduce result set size")}}return suggestions}async function queryLogTableExists(){try{const dialect=getDatabaseDialect();const sql=dialect==="postgres"?`SELECT 1 AS present WHERE to_regclass('query_logs') IS NOT NULL`:dialect==="mysql"?`SELECT 1 AS present FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'query_logs'`:`SELECT 1 AS present FROM sqlite_master WHERE type = 'table' AND name = 'query_logs'`;const rows=await db.unsafe(sql).execute();return rows.length>0}catch{return false}}const QUERY_LOG_PRUNE_BATCH=5000;const QUERY_LOG_PRUNE_PAUSE_MS=10;let lastQueryLogPrune=0;let queryLogPruneInFlight=null;export function queryLogRetentionCutoff(retentionDays,now=Date.now()){const days=Number(retentionDays??7);if(!Number.isFinite(days)||days<=0)return null;return sqlDateTime(new Date(now-days*86400000))}function maybePruneQueryLogs(now=Date.now()){const logging=config.database?.queryLogging;const cutoff=queryLogRetentionCutoff(logging?.retention,now);if(!cutoff||queryLogPruneInFlight)return;const hours=Number(logging?.pruneFrequency??24);const interval=(Number.isFinite(hours)&&hours>0?hours:24)*3600000;if(lastQueryLogPrune&&now-lastQueryLogPrune<interval)return;lastQueryLogPrune=now;queryLogPruneInFlight=queryLogContext.run(true,()=>pruneExpiredQueryLogs(cutoff)).catch((error)=>{log.error("Failed to prune expired query logs:",error);return 0}).finally(()=>{queryLogPruneInFlight=null})}export function queryLogPruneSettled(){return queryLogPruneInFlight??Promise.resolve(0)}export async function pruneExpiredQueryLogs(cutoff,batch=QUERY_LOG_PRUNE_BATCH){let pruned=0;for(;;){const rows=await db.selectFrom("query_logs").select(["id"]).where("executed_at","<",cutoff).limit(batch).execute();if(rows.length===0)return pruned;await db.deleteFrom("query_logs").where("id","in",rows.map((row)=>row.id)).execute();pruned+=rows.length;if(rows.length<batch)return pruned;await new Promise((resolve)=>setTimeout(resolve,QUERY_LOG_PRUNE_PAUSE_MS))}}async function storeQueryLogs(logRecords,reportFailure=true){try{const values=logRecords;const libsql=isLibsqlDriver(config.database?.default);const batch=libsql?db.sql?.batch:undefined;if(values.length>1&&getDatabaseDialect()==="sqlite"&&batch){const columns=Object.keys(values[0]);const identifiers=columns.map((column)=>`"${column.replaceAll('"','""')}"`).join(", ");const row=`(${columns.map(()=>"?").join(", ")})`;const statement=`INSERT INTO query_logs (${identifiers}) VALUES ${values.map(()=>row).join(", ")}`;const bindings=values.flatMap((value)=>columns.map((column)=>value[column]??null));await batch([{sql:statement,args:bindings}])}else if(values.length>1&&getDatabaseDialect()==="sqlite"&&!libsql){const columns=Object.keys(values[0]);const identifiers=columns.map((column)=>`"${column.replaceAll('"','""')}"`).join(", ");const row=`(${columns.map(()=>"?").join(", ")})`;const statement=`INSERT INTO query_logs (${identifiers}) VALUES ${values.map(()=>row).join(", ")}`;const bindings=values.flatMap((value)=>columns.map((column)=>value[column]??null));db.unsafe("SAVEPOINT stacks_query_log_batch").executeSync();try{db.unsafe(statement,bindings).executeSync();db.unsafe("RELEASE SAVEPOINT stacks_query_log_batch").executeSync()}catch(error){db.unsafe("ROLLBACK TO SAVEPOINT stacks_query_log_batch").executeSync();db.unsafe("RELEASE SAVEPOINT stacks_query_log_batch").executeSync();throw error}}else{await db.insertInto("query_logs").values(values).execute()}return"stored"}catch(error){const message=error instanceof Error?error.message:String(error);if(/no such table|does not exist|doesn't exist/i.test(message)&&/query_logs/i.test(message)){if(!queryLogTableMissing)log.debug("Query logging will start after the query_logs table is migrated.");queryLogTableMissing=true;return"missing"}if(reportFailure)log.error("Failed to store query log:",error);return"failed"}}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@stacksjs/database",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "0.75.581",
5
+ "version": "0.75.584",
6
6
  "description": "The Stacks database integration.",
7
7
  "author": "Chris Breuer",
8
8
  "contributors": [
@@ -66,26 +66,26 @@
66
66
  "prepublishOnly": "bun run build"
67
67
  },
68
68
  "dependencies": {
69
- "@stacksjs/config": "0.75.581",
70
- "@stacksjs/env": "0.75.581",
71
- "@stacksjs/error-handling": "0.75.581",
72
- "@stacksjs/faker": "0.75.581",
73
- "@stacksjs/features": "0.75.581",
74
- "@stacksjs/logging": "0.75.581",
75
- "@stacksjs/model-meta": "0.75.581",
76
- "@stacksjs/path": "0.75.581",
77
- "@stacksjs/query-builder": "0.75.581",
78
- "@stacksjs/security": "0.75.581",
79
- "@stacksjs/storage": "0.75.581",
80
- "@stacksjs/strings": "0.75.581",
69
+ "@stacksjs/config": "0.75.584",
70
+ "@stacksjs/env": "0.75.584",
71
+ "@stacksjs/error-handling": "0.75.584",
72
+ "@stacksjs/faker": "0.75.584",
73
+ "@stacksjs/features": "0.75.584",
74
+ "@stacksjs/logging": "0.75.584",
75
+ "@stacksjs/model-meta": "0.75.584",
76
+ "@stacksjs/path": "0.75.584",
77
+ "@stacksjs/query-builder": "0.75.584",
78
+ "@stacksjs/security": "0.75.584",
79
+ "@stacksjs/storage": "0.75.584",
80
+ "@stacksjs/strings": "0.75.584",
81
81
  "@stacksjs/ts-validation": "^0.5.7",
82
82
  "bun-query-builder": "^0.6.0",
83
83
  "dynamodb-tooling": "^0.3.6"
84
84
  },
85
85
  "devDependencies": {
86
- "@stacksjs/cli": "0.75.581",
87
- "@stacksjs/router": "0.75.581",
88
- "@stacksjs/utils": "0.75.581",
86
+ "@stacksjs/cli": "0.75.584",
87
+ "@stacksjs/router": "0.75.584",
88
+ "@stacksjs/utils": "0.75.584",
89
89
  "better-dx": "^0.2.39"
90
90
  }
91
91
  }