@dockstat/sqlite-wrapper 1.2.8 → 1.3.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.
package/utils/index.ts ADDED
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Utilities for sqlite-wrapper
3
+ *
4
+ * Re-exports all utility modules for easy importing.
5
+ */
6
+
7
+ // Logger utilities
8
+ export {
9
+ addLoggerParents,
10
+ createLogger,
11
+ createSqliteBaseLogger,
12
+ logger,
13
+ SqliteLogger,
14
+ setSqliteLogHook,
15
+ } from "./logger"
16
+
17
+ // SQL utilities
18
+ export {
19
+ buildBetweenClause,
20
+ buildCondition,
21
+ buildDeleteSQL,
22
+ buildInClause,
23
+ buildInsertSQL,
24
+ buildPlaceholders,
25
+ buildSelectSQL,
26
+ buildSetClause,
27
+ buildUpdateSQL,
28
+ escapeValue,
29
+ isSQLFunction,
30
+ normalizeOperator,
31
+ quoteIdentifier,
32
+ quoteIdentifiers,
33
+ quoteString,
34
+ } from "./sql"
35
+
36
+ // Transformer utilities
37
+ export {
38
+ getParserSummary,
39
+ hasTransformations,
40
+ type RowData,
41
+ type TransformOptions,
42
+ transformFromDb,
43
+ transformRowsFromDb,
44
+ transformRowsToDb,
45
+ transformToDb,
46
+ } from "./transformer"
@@ -0,0 +1,216 @@
1
+ import { Logger, type LogHook } from "@dockstat/logger"
2
+
3
+ /**
4
+ * Centralized logging for sqlite-wrapper
5
+ *
6
+ * This module provides a clean, consistent logging interface
7
+ * for all sqlite-wrapper operations.
8
+ */
9
+
10
+ /**
11
+ * Truncate a string to a maximum length with ellipsis
12
+ */
13
+ function truncate(str: string, maxLength: number): string {
14
+ if (str.length <= maxLength) return str
15
+ return `${str.slice(0, maxLength)}...`
16
+ }
17
+
18
+ /**
19
+ * Format query parameters for logging (truncated for readability)
20
+ */
21
+ function formatParams(params?: unknown[]): string {
22
+ if (!params || params.length === 0) return ""
23
+ const str = JSON.stringify(params)
24
+ return truncate(str, 60)
25
+ }
26
+
27
+ /**
28
+ * SqliteLogger - A wrapper around @dockstat/logger with sqlite-specific helpers
29
+ */
30
+ export class SqliteLogger {
31
+ private logger: Logger
32
+ private tableName?: string
33
+
34
+ constructor(name: string, parent?: Logger, tableName?: string, logHook?: LogHook) {
35
+ try {
36
+ this.logger =
37
+ typeof parent?.spawn === "function" ? parent.spawn(name) : new Logger(name, [], logHook)
38
+ } catch (error) {
39
+ this.logger = new Logger(name, [], logHook)
40
+ console.error(error)
41
+ }
42
+
43
+ this.tableName = tableName
44
+ }
45
+
46
+ /**
47
+ * Create a child logger for a specific component
48
+ */
49
+ child(name: string): SqliteLogger {
50
+ return new SqliteLogger(name, this.logger, this.tableName)
51
+ }
52
+
53
+ /**
54
+ * Create a table-scoped logger
55
+ */
56
+ forTable(tableName: string): SqliteLogger {
57
+ const child = new SqliteLogger(tableName, this.logger, tableName)
58
+ return child
59
+ }
60
+
61
+ // ===== Standard log methods =====
62
+
63
+ debug(message: string): void {
64
+ this.logger.debug(message)
65
+ }
66
+
67
+ info(message: string): void {
68
+ this.logger.info(message)
69
+ }
70
+
71
+ warn(message: string): void {
72
+ this.logger.warn(message)
73
+ }
74
+
75
+ error(message: string): void {
76
+ this.logger.error(message)
77
+ }
78
+
79
+ // ===== Sqlite-specific log helpers =====
80
+
81
+ /**
82
+ * Log a database connection event
83
+ */
84
+ connection(path: string, action: "open" | "close"): void {
85
+ this.logger.info(`Database ${action}: ${path}`)
86
+ }
87
+
88
+ /**
89
+ * Log a SQL query execution
90
+ */
91
+ query(operation: string, sql: string, params?: unknown[]): void {
92
+ const paramStr = formatParams(params)
93
+ const sqlStr = truncate(sql.replace(/\s+/g, " ").trim(), 100)
94
+ const msg = paramStr
95
+ ? `${operation} | ${sqlStr} | params=${paramStr}`
96
+ : `${operation} | ${sqlStr}`
97
+ this.logger.debug(msg)
98
+ }
99
+
100
+ /**
101
+ * Log query results
102
+ */
103
+ result(operation: string, rowCount: number): void {
104
+ this.logger.debug(`${operation} | rows=${rowCount}`)
105
+ }
106
+
107
+ /**
108
+ * Log a table creation
109
+ */
110
+ tableCreate(tableName: string, columns: string[]): void {
111
+ this.logger.debug(`CREATE TABLE ${tableName} | columns=[${columns.join(", ")}]`)
112
+ }
113
+
114
+ /**
115
+ * Log backup operations
116
+ */
117
+ backup(action: "create" | "restore" | "list" | "delete", path?: string): void {
118
+ const msg = path ? `Backup ${action}: ${path}` : `Backup ${action}`
119
+ this.logger.info(msg)
120
+ }
121
+
122
+ /**
123
+ * Log row transformation
124
+ */
125
+ transform(direction: "serialize" | "deserialize", columnTypes: string[]): void {
126
+ if (columnTypes.length === 0) return
127
+ this.logger.debug(`Transform ${direction}: [${columnTypes.join(", ")}]`)
128
+ }
129
+
130
+ /**
131
+ * Log parser configuration
132
+ */
133
+ parserConfig(json: string[], boolean: string[], module: string[]): void {
134
+ const parts: string[] = []
135
+ if (json.length > 0) parts.push(`JSON=[${json.join(",")}]`)
136
+ if (boolean.length > 0) parts.push(`BOOL=[${boolean.join(",")}]`)
137
+ if (module.length > 0) parts.push(`MODULE=[${module.join(",")}]`)
138
+ if (parts.length > 0) {
139
+ this.logger.debug(`Parser config: ${parts.join(" ")}`)
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Log transaction events
145
+ */
146
+ transaction(action: "begin" | "commit" | "rollback" | "savepoint", name?: string): void {
147
+ const msg = name ? `Transaction ${action}: ${name}` : `Transaction ${action}`
148
+ this.logger.debug(msg)
149
+ }
150
+
151
+ /**
152
+ * Get the underlying @dockstat/logger instance
153
+ */
154
+ getBaseLogger(): Logger {
155
+ return this.logger
156
+ }
157
+
158
+ /**
159
+ * Add parent loggers for chaining
160
+ */
161
+ addParents(parents: string[]): void {
162
+ this.logger.addParents(parents)
163
+ }
164
+
165
+ /**
166
+ * Get parents for logger chaining
167
+ */
168
+ getParentsForLoggerChaining(): string[] {
169
+ return this.logger.getParentsForLoggerChaining()
170
+ }
171
+ }
172
+
173
+ // ===== Module exports =====
174
+
175
+ /**
176
+ * Main logger instance for sqlite-wrapper
177
+ */
178
+ export const logger = new SqliteLogger("Sqlite")
179
+
180
+ /**
181
+ * Create a new logger for a specific module
182
+ */
183
+ export function createLogger(name: string, baseLogger?: Logger): SqliteLogger {
184
+ // If a base logger is provided (recommended for apps/packages that want shared LogHook/parents),
185
+ // derive a sqlite-scoped child logger from it.
186
+ if (baseLogger) return new SqliteLogger(name, baseLogger)
187
+
188
+ // Fallback to the package-level logger (works, but won't automatically inherit app hooks)
189
+ return logger.child(name)
190
+ }
191
+
192
+ export function addLoggerParents(parents: string[]): void {
193
+ logger.addParents(parents)
194
+ }
195
+
196
+ /**
197
+ * Configure a shared LogHook for sqlite-wrapper's root logger.
198
+ *
199
+ * Useful when consumers (e.g. `apps/api`, `packages/db`) want sqlite-wrapper logs
200
+ * to be routed through the same sink/formatter as the rest of the app.
201
+ */
202
+ export function setSqliteLogHook(logHook: LogHook): void {
203
+ logger.getBaseLogger().setLogHook(logHook)
204
+ }
205
+
206
+ /**
207
+ * Optional convenience for consumers: create a sqlite-scoped base Logger that shares a LogHook.
208
+ *
209
+ * This is handy if you want to pass a base logger into `createLogger()` / `DB` and have
210
+ * everything inherit the same hook.
211
+ */
212
+ export function createSqliteBaseLogger(logHook?: LogHook): Logger {
213
+ return new Logger("Sqlite", [], logHook)
214
+ }
215
+
216
+ export default logger
package/utils/sql.ts ADDED
@@ -0,0 +1,241 @@
1
+ import type { SQLQueryBindings } from "bun:sqlite"
2
+
3
+ /**
4
+ * SQL Utilities for sqlite-wrapper
5
+ *
6
+ * Common SQL operations and helpers used across the package.
7
+ */
8
+
9
+ /**
10
+ * Quote a SQL identifier (table name, column name) to prevent injection
11
+ * and handle special characters.
12
+ *
13
+ * @example
14
+ * quoteIdentifier("users") // "users"
15
+ * quoteIdentifier("user name") // "user name"
16
+ * quoteIdentifier('with"quote') // "with""quote"
17
+ */
18
+ export function quoteIdentifier(identifier: string): string {
19
+ return `"${identifier.replace(/"/g, '""')}"`
20
+ }
21
+
22
+ /**
23
+ * Quote a SQL string literal value
24
+ *
25
+ * @example
26
+ * quoteString("hello") // 'hello'
27
+ * quoteString("it's") // 'it''s'
28
+ */
29
+ export function quoteString(value: string): string {
30
+ return `'${value.replace(/'/g, "''")}'`
31
+ }
32
+
33
+ /**
34
+ * Build a comma-separated list of quoted identifiers
35
+ *
36
+ * @example
37
+ * quoteIdentifiers(["id", "name", "email"]) // "id", "name", "email"
38
+ */
39
+ export function quoteIdentifiers(identifiers: string[]): string {
40
+ return identifiers.map(quoteIdentifier).join(", ")
41
+ }
42
+
43
+ /**
44
+ * Build placeholders for parameterized queries
45
+ *
46
+ * @example
47
+ * buildPlaceholders(3) // "?, ?, ?"
48
+ * buildPlaceholders(["a", "b"]) // "?, ?"
49
+ */
50
+ export function buildPlaceholders(countOrArray: number | unknown[]): string {
51
+ const count = typeof countOrArray === "number" ? countOrArray : countOrArray.length
52
+ return Array(count).fill("?").join(", ")
53
+ }
54
+
55
+ /**
56
+ * Build a SET clause for UPDATE statements
57
+ *
58
+ * @example
59
+ * buildSetClause(["name", "email"]) // "name" = ?, "email" = ?
60
+ */
61
+ export function buildSetClause(columns: string[]): string {
62
+ return columns.map((col) => `${quoteIdentifier(col)} = ?`).join(", ")
63
+ }
64
+
65
+ /**
66
+ * Build an INSERT statement
67
+ */
68
+ export function buildInsertSQL(
69
+ table: string,
70
+ columns: string[],
71
+ conflictResolution?: "IGNORE" | "REPLACE" | "ABORT" | "FAIL" | "ROLLBACK"
72
+ ): string {
73
+ const insertType = conflictResolution ? `INSERT OR ${conflictResolution}` : "INSERT"
74
+ const quotedColumns = quoteIdentifiers(columns)
75
+ const placeholders = buildPlaceholders(columns)
76
+
77
+ return `${insertType} INTO ${quoteIdentifier(table)} (${quotedColumns}) VALUES (${placeholders})`
78
+ }
79
+
80
+ /**
81
+ * Build a simple SELECT statement
82
+ */
83
+ export function buildSelectSQL(
84
+ table: string,
85
+ columns: string[] | "*",
86
+ options?: {
87
+ where?: string
88
+ orderBy?: string
89
+ orderDirection?: "ASC" | "DESC"
90
+ limit?: number
91
+ offset?: number
92
+ }
93
+ ): string {
94
+ const cols = columns === "*" ? "*" : quoteIdentifiers(columns)
95
+ let sql = `SELECT ${cols} FROM ${quoteIdentifier(table)}`
96
+
97
+ if (options?.where) {
98
+ sql += ` WHERE ${options.where}`
99
+ }
100
+
101
+ if (options?.orderBy) {
102
+ sql += ` ORDER BY ${quoteIdentifier(options.orderBy)} ${options.orderDirection || "ASC"}`
103
+ }
104
+
105
+ if (options?.limit !== undefined) {
106
+ sql += ` LIMIT ${options.limit}`
107
+ }
108
+
109
+ if (options?.offset !== undefined) {
110
+ sql += ` OFFSET ${options.offset}`
111
+ }
112
+
113
+ return sql
114
+ }
115
+
116
+ /**
117
+ * Build an UPDATE statement
118
+ */
119
+ export function buildUpdateSQL(table: string, columns: string[], where: string): string {
120
+ const setClause = buildSetClause(columns)
121
+ return `UPDATE ${quoteIdentifier(table)} SET ${setClause} WHERE ${where}`
122
+ }
123
+
124
+ /**
125
+ * Build a DELETE statement
126
+ */
127
+ export function buildDeleteSQL(table: string, where: string): string {
128
+ return `DELETE FROM ${quoteIdentifier(table)} WHERE ${where}`
129
+ }
130
+
131
+ /**
132
+ * Check if a string looks like a SQL function call
133
+ *
134
+ * @example
135
+ * isSQLFunction("datetime('now')") // true
136
+ * isSQLFunction("CURRENT_TIMESTAMP") // true
137
+ * isSQLFunction("hello") // false
138
+ */
139
+ export function isSQLFunction(value: string): boolean {
140
+ const functionPatterns = [
141
+ /^\w+\s*\(/i, // function(...)
142
+ /^CURRENT_TIME(STAMP)?$/i,
143
+ /^CURRENT_DATE$/i,
144
+ /^NULL$/i,
145
+ ]
146
+ return functionPatterns.some((pattern) => pattern.test(value.trim()))
147
+ }
148
+
149
+ /**
150
+ * Escape a value for safe inclusion in SQL
151
+ * Returns the SQLite-safe representation
152
+ */
153
+ export function escapeValue(value: SQLQueryBindings): string {
154
+ if (value === null) return "NULL"
155
+ if (typeof value === "number") return String(value)
156
+ if (typeof value === "boolean") return value ? "1" : "0"
157
+ if (typeof value === "string") return quoteString(value)
158
+ if (value instanceof Uint8Array) return `X'${Buffer.from(value).toString("hex")}'`
159
+ return quoteString(String(value))
160
+ }
161
+
162
+ /**
163
+ * Normalize a comparison operator
164
+ */
165
+ export function normalizeOperator(op: string): string {
166
+ const normalized = op.toUpperCase().trim()
167
+ const allowed = ["=", "!=", "<>", "<", "<=", ">", ">=", "LIKE", "GLOB", "IS", "IS NOT"]
168
+
169
+ if (!allowed.includes(normalized)) {
170
+ throw new Error(`Invalid SQL operator: "${op}"`)
171
+ }
172
+
173
+ return normalized
174
+ }
175
+
176
+ /**
177
+ * Build a WHERE condition for a single column
178
+ */
179
+ export function buildCondition(
180
+ column: string,
181
+ operator: string,
182
+ value: SQLQueryBindings | null | undefined
183
+ ): { sql: string; params: SQLQueryBindings[] } {
184
+ const normalizedOp = normalizeOperator(operator)
185
+ const quotedCol = quoteIdentifier(column)
186
+
187
+ // Handle NULL special cases
188
+ if (value === null || value === undefined) {
189
+ if (normalizedOp === "=" || normalizedOp === "IS") {
190
+ return { sql: `${quotedCol} IS NULL`, params: [] }
191
+ }
192
+ if (normalizedOp === "!=" || normalizedOp === "<>" || normalizedOp === "IS NOT") {
193
+ return { sql: `${quotedCol} IS NOT NULL`, params: [] }
194
+ }
195
+ }
196
+
197
+ return {
198
+ sql: `${quotedCol} ${normalizedOp} ?`,
199
+ params: [value as SQLQueryBindings],
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Build an IN clause
205
+ */
206
+ export function buildInClause(
207
+ column: string,
208
+ values: SQLQueryBindings[],
209
+ negate = false
210
+ ): { sql: string; params: SQLQueryBindings[] } {
211
+ if (values.length === 0) {
212
+ throw new Error("IN clause requires at least one value")
213
+ }
214
+
215
+ const quotedCol = quoteIdentifier(column)
216
+ const placeholders = buildPlaceholders(values)
217
+ const keyword = negate ? "NOT IN" : "IN"
218
+
219
+ return {
220
+ sql: `${quotedCol} ${keyword} (${placeholders})`,
221
+ params: values,
222
+ }
223
+ }
224
+
225
+ /**
226
+ * Build a BETWEEN clause
227
+ */
228
+ export function buildBetweenClause(
229
+ column: string,
230
+ min: SQLQueryBindings,
231
+ max: SQLQueryBindings,
232
+ negate = false
233
+ ): { sql: string; params: SQLQueryBindings[] } {
234
+ const quotedCol = quoteIdentifier(column)
235
+ const keyword = negate ? "NOT BETWEEN" : "BETWEEN"
236
+
237
+ return {
238
+ sql: `${quotedCol} ${keyword} ? AND ?`,
239
+ params: [min, max],
240
+ }
241
+ }