@dockstat/sqlite-wrapper 1.2.8 → 1.3.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/package.json CHANGED
@@ -1,54 +1,60 @@
1
- {
2
- "name": "@dockstat/sqlite-wrapper",
3
- "version": "1.2.8",
4
- "description": "A TypeScript wrapper around bun:sqlite with type-safe query building",
5
- "type": "module",
6
- "main": "./index.ts",
7
- "types": "./index.ts",
8
- "files": [
9
- "index.ts",
10
- "query-builder/**/*",
11
- "README.md",
12
- "types.ts"
13
- ],
14
- "scripts": {
15
- "dev": "bun build index.ts",
16
- "lint": "biome lint .",
17
- "lint:fix": "biome lint --write .",
18
- "lint:gh": "turbo run lint -- --reporter=github",
19
- "check-types": "bunx tsc --noEmit",
20
- "test": "bun run test.ts"
21
- },
22
- "keywords": [
23
- "sqlite",
24
- "database",
25
- "typescript",
26
- "bun",
27
- "query-builder",
28
- "orm"
29
- ],
30
- "author": "Its4Nik",
31
- "license": "MPL-2.0",
32
- "repository": {
33
- "type": "git",
34
- "url": "https://github.com/Its4Nik/DockStat/tree/main/packages/sqlite-wrapper",
35
- "directory": "packages/sqlite-wrapper"
36
- },
37
- "bugs": {
38
- "url": "https://github.com/Its4Nik/DockStat/issues"
39
- },
40
- "homepage": "https://outline.itsnik.de/s/9d88c471-373e-4ef2-a955-b1058eb7dc99/doc/dockstatsqlite-wrapper-Lxt4IphXI5",
41
- "engines": {
42
- "bun": ">=1.0.0"
43
- },
44
- "dependencies": {
45
- "@dockstat/logger": "latest"
46
- },
47
- "peerDependencies": {
48
- "typescript": "^5.9.3"
49
- },
50
- "devDependencies": {
51
- "@types/bun": "latest",
52
- "@types/dockerode": "^3.3.44"
53
- }
54
- }
1
+ {
2
+ "name": "@dockstat/sqlite-wrapper",
3
+ "version": "1.3.0",
4
+ "description": "A TypeScript wrapper around bun:sqlite with type-safe query building",
5
+ "type": "module",
6
+ "main": "./index.ts",
7
+ "types": "./index.ts",
8
+ "exports": {
9
+ ".": "./index.ts",
10
+ "./types": "./types.ts",
11
+ "./utils": "./utils/index.ts"
12
+ },
13
+ "files": [
14
+ "index.ts",
15
+ "query-builder/**/*",
16
+ "utils/**/*",
17
+ "README.md",
18
+ "types.ts"
19
+ ],
20
+ "scripts": {
21
+ "dev": "bun build index.ts",
22
+ "lint": "biome lint .",
23
+ "lint:fix": "biome lint --write .",
24
+ "lint:gh": "turbo run lint -- --reporter=github",
25
+ "check-types": "bunx tsc --noEmit",
26
+ "test": "DOCKSTAT_LOGGER_IGNORE_MESSAGES='Logger Status: active - ignoring messages:' bun test"
27
+ },
28
+ "keywords": [
29
+ "sqlite",
30
+ "database",
31
+ "typescript",
32
+ "bun",
33
+ "query-builder",
34
+ "orm"
35
+ ],
36
+ "author": "Its4Nik",
37
+ "license": "MPL-2.0",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/Its4Nik/DockStat/tree/main/packages/sqlite-wrapper",
41
+ "directory": "packages/sqlite-wrapper"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/Its4Nik/DockStat/issues"
45
+ },
46
+ "homepage": "https://outline.itsnik.de/s/9d88c471-373e-4ef2-a955-b1058eb7dc99/doc/dockstatsqlite-wrapper-Lxt4IphXI5",
47
+ "engines": {
48
+ "bun": ">=1.0.0"
49
+ },
50
+ "dependencies": {
51
+ "@dockstat/logger": "1.0.1"
52
+ },
53
+ "peerDependencies": {
54
+ "typescript": "^5.9.3"
55
+ },
56
+ "devDependencies": {
57
+ "@types/bun": "latest",
58
+ "@types/dockerode": "^3.3.44"
59
+ }
60
+ }
@@ -1,221 +1,183 @@
1
- import type { Database, SQLQueryBindings } from 'bun:sqlite'
2
- import { createLogger } from '@dockstat/logger'
3
- import type {
4
- ColumnNames,
5
- DatabaseRowData,
6
- JsonColumnConfig,
7
- OrderDirection,
8
- QueryBuilderState,
9
- } from '../types'
10
-
11
- /**
12
- * Base QueryBuilder class that manages core state and shared functionality.
13
- * This class provides the foundation for all query operations.
14
- */
15
- export abstract class BaseQueryBuilder<T extends Record<string, unknown>> {
16
- private logger = createLogger('BaseQueryBuilder')
17
- protected state: QueryBuilderState<T>
18
-
19
- /**
20
- * Get the logger instance
21
- */
22
- protected getLogger() {
23
- return this.logger
24
- }
25
-
26
- constructor(
27
- db: Database,
28
- tableName: string,
29
- jsonConfig?: JsonColumnConfig<T>
30
- ) {
31
- this.state = {
32
- db,
33
- tableName,
34
- whereConditions: [],
35
- whereParams: [],
36
- regexConditions: [],
37
- jsonColumns: jsonConfig,
38
- }
39
- }
40
-
41
- /**
42
- * Reset query builder state
43
- */
44
- protected reset(): void {
45
- this.state.whereConditions = []
46
- this.state.whereParams = []
47
- this.state.regexConditions = []
48
- // Reset any ordering, limit, offset, selected columns if present
49
- if ('orderColumn' in this) (this as any).orderColumn = undefined
50
- if ('orderDirection' in this) (this as any).orderDirection = 'ASC'
51
- if ('limitValue' in this) (this as any).limitValue = undefined
52
- if ('offsetValue' in this) (this as any).offsetValue = undefined
53
- if ('selectedColumns' in this) (this as any).selectedColumns = ['*']
54
- }
55
-
56
- /**
57
- * Get the database instance
58
- */
59
- protected getDb(): Database {
60
- return this.state.db
61
- }
62
-
63
- /**
64
- * Get the table name
65
- */
66
- protected getTableName(): string {
67
- return this.state.tableName
68
- }
69
-
70
- /**
71
- * Build the WHERE clause portion of a SQL query.
72
- * @returns Tuple of [whereClause, parameters] where whereClause includes "WHERE" prefix
73
- */
74
- protected buildWhereClause(): [string, SQLQueryBindings[]] {
75
- if (this.state.whereConditions.length === 0) {
76
- return ['', []]
77
- }
78
- return [
79
- ` WHERE ${this.state.whereConditions.join(' AND ')}`,
80
- this.state.whereParams.slice(),
81
- ]
82
- }
83
-
84
- /**
85
- * Check if there are any regex conditions that require client-side filtering.
86
- */
87
- protected hasRegexConditions(): boolean {
88
- return this.state.regexConditions.length > 0
89
- }
90
-
91
- /**
92
- * Apply client-side regex filtering to a set of rows.
93
- * This is used when regex conditions are present.
94
- */
95
- protected applyRegexFiltering(rows: T[]): T[] {
96
- if (this.state.regexConditions.length === 0) {
97
- return rows
98
- }
99
-
100
- return rows.filter((row) =>
101
- this.state.regexConditions.every(({ column, regex }) => {
102
- const value = row[String(column)]
103
- if (value === null || value === undefined) return false
104
- return regex.test(String(value))
105
- })
106
- )
107
- }
108
-
109
- /**
110
- * Validate that WHERE conditions exist for operations that require them.
111
- * Throws an error if no WHERE conditions are present.
112
- */
113
- protected requireWhereClause(operation: string): void {
114
- if (
115
- this.state.whereConditions.length === 0 &&
116
- this.state.regexConditions.length === 0
117
- ) {
118
- const error = `${operation} operation requires at least one WHERE condition. Use where(), whereRaw(), whereIn(), whereOp(), or whereRgx() to add conditions.`
119
- this.logger.error(error)
120
- throw new Error(error)
121
- }
122
- }
123
-
124
- /**
125
- * Quote SQL identifiers to prevent injection and handle special characters.
126
- */
127
- protected quoteIdentifier(identifier: string): string {
128
- return `"${identifier.replace(/"/g, '""')}"`
129
- }
130
-
131
- /**
132
- * Reset all WHERE conditions and parameters.
133
- * Useful for reusing the same builder instance.
134
- */
135
- protected resetWhereConditions(): void {
136
- this.state.whereConditions = []
137
- this.state.whereParams = []
138
- this.state.regexConditions = []
139
- }
140
-
141
- /**
142
- * Transform row data after fetching from database (deserialize JSON columns).
143
- */
144
- protected transformRowFromDb(row: unknown): T {
145
- if (!this.state.jsonColumns || !row) return row as T
146
-
147
- try {
148
- const transformed = { ...row } as DatabaseRowData
149
- for (const column of this.state.jsonColumns) {
150
- const columnKey = String(column)
151
- if (
152
- transformed[columnKey] !== null &&
153
- transformed[columnKey] !== undefined &&
154
- typeof transformed[columnKey] === 'string'
155
- ) {
156
- try {
157
- transformed[columnKey] = JSON.parse(
158
- transformed[columnKey] as string
159
- )
160
- } catch (parseError) {
161
- // Keep original value if JSON parsing fails
162
- this.logger.warn(
163
- `JSON parse failed for column ${columnKey}: ${parseError}`
164
- )
165
- }
166
- }
167
- }
168
- return transformed as T
169
- } catch (error) {
170
- this.logger.error(`Error in transformRowFromDb: ${error}`)
171
- return row as T
172
- }
173
- }
174
-
175
- /**
176
- * Transform multiple rows after fetching from database.
177
- */
178
- protected transformRowsFromDb(rows: unknown[]): T[] {
179
- if (!this.state.jsonColumns || !Array.isArray(rows)) return rows as T[]
180
-
181
- try {
182
- return rows.map((row, index) => {
183
- try {
184
- return this.transformRowFromDb(row)
185
- } catch (error) {
186
- this.logger.error(`Error transforming row ${index}: ${error}`)
187
- return row as T
188
- }
189
- })
190
- } catch (error) {
191
- this.logger.error(`Error in transformRowsFromDb: ${error}`)
192
- return rows as T[]
193
- }
194
- }
195
-
196
- /**
197
- * Transform row data before inserting/updating to database (serialize JSON columns).
198
- */
199
- protected transformRowToDb(row: Partial<T>): DatabaseRowData {
200
- this.logger.debug(`Transforming row (${JSON.stringify(row)}) to row Data`)
201
- if (!this.state.jsonColumns || !row) {
202
- return row as DatabaseRowData
203
- }
204
-
205
- const transformed: DatabaseRowData = { ...row } as DatabaseRowData
206
-
207
- for (const column of this.state.jsonColumns) {
208
- const columnKey = String(column)
209
- this.logger.debug(`Checking: ${columnKey}`)
210
- if (
211
- transformed[columnKey] !== undefined &&
212
- transformed[columnKey] !== null
213
- ) {
214
- if (typeof transformed[columnKey] === 'object') {
215
- transformed[columnKey] = JSON.stringify(transformed[columnKey])
216
- }
217
- }
218
- }
219
- return transformed
220
- }
221
- }
1
+ import type { Database, SQLQueryBindings } from "bun:sqlite"
2
+ import type { Parser, QueryBuilderState } from "../types"
3
+ import {
4
+ createLogger,
5
+ quoteIdentifier,
6
+ type RowData,
7
+ transformFromDb,
8
+ transformRowsFromDb,
9
+ transformToDb,
10
+ } from "../utils"
11
+
12
+ /**
13
+ * Base QueryBuilder class that manages core state and shared functionality.
14
+ *
15
+ * This class provides the foundation for all query operations including:
16
+ * - Database connection and table name management
17
+ * - WHERE clause building and management
18
+ * - Regex condition handling (client-side filtering)
19
+ * - Row transformation (JSON/Boolean serialization)
20
+ */
21
+ export abstract class BaseQueryBuilder<T extends Record<string, unknown>> {
22
+ protected state: QueryBuilderState<T>
23
+ protected log = createLogger("query")
24
+
25
+ constructor(db: Database, tableName: string, parser?: Parser<T>) {
26
+ this.state = {
27
+ db,
28
+ tableName,
29
+ whereConditions: [],
30
+ whereParams: [],
31
+ regexConditions: [],
32
+ parser,
33
+ }
34
+
35
+ this.log.debug(`QueryBuilder initialized for table: ${tableName}`)
36
+ }
37
+
38
+ // ===== State Accessors =====
39
+
40
+ /**
41
+ * Get the database instance
42
+ */
43
+ protected getDb(): Database {
44
+ return this.state.db
45
+ }
46
+
47
+ /**
48
+ * Get the table name
49
+ */
50
+ protected getTableName(): string {
51
+ return this.state.tableName
52
+ }
53
+
54
+ /**
55
+ * Get the parser configuration
56
+ */
57
+ protected getParser(): Parser<T> | undefined {
58
+ return this.state.parser
59
+ }
60
+
61
+ // ===== State Management =====
62
+
63
+ /**
64
+ * Reset query builder state to initial values
65
+ */
66
+ protected reset(): void {
67
+ this.state.whereConditions = []
68
+ this.state.whereParams = []
69
+ this.state.regexConditions = []
70
+
71
+ // Reset any additional state in subclasses
72
+ if ("orderColumn" in this) this.orderColumn = undefined
73
+ if ("orderDirection" in this) this.orderDirection = "ASC"
74
+ if ("limitValue" in this) this.limitValue = undefined
75
+ if ("offsetValue" in this) this.offsetValue = undefined
76
+ if ("selectedColumns" in this) this.selectedColumns = ["*"]
77
+ }
78
+
79
+ /**
80
+ * Reset only WHERE conditions (useful for reusing builder)
81
+ */
82
+ protected resetWhereConditions(): void {
83
+ this.state.whereConditions = []
84
+ this.state.whereParams = []
85
+ this.state.regexConditions = []
86
+ }
87
+
88
+ // ===== SQL Building Helpers =====
89
+
90
+ /**
91
+ * Quote a SQL identifier to prevent injection
92
+ */
93
+ protected quoteIdentifier(identifier: string): string {
94
+ return quoteIdentifier(identifier)
95
+ }
96
+
97
+ /**
98
+ * Build the WHERE clause from accumulated conditions
99
+ *
100
+ * @returns Tuple of [whereClause, parameters]
101
+ */
102
+ protected buildWhereClause(): [string, SQLQueryBindings[]] {
103
+ if (this.state.whereConditions.length === 0) {
104
+ return ["", []]
105
+ }
106
+
107
+ const clause = ` WHERE ${this.state.whereConditions.join(" AND ")}`
108
+ const params = [...this.state.whereParams]
109
+
110
+ return [clause, params]
111
+ }
112
+
113
+ // ===== Regex Condition Handling =====
114
+
115
+ /**
116
+ * Check if there are any regex conditions requiring client-side filtering
117
+ */
118
+ protected hasRegexConditions(): boolean {
119
+ return this.state.regexConditions.length > 0
120
+ }
121
+
122
+ /**
123
+ * Apply regex filtering to rows (client-side)
124
+ */
125
+ protected applyRegexFiltering(rows: T[]): T[] {
126
+ if (!this.hasRegexConditions()) {
127
+ return rows
128
+ }
129
+
130
+ return rows.filter((row) =>
131
+ this.state.regexConditions.every(({ column, regex }) => {
132
+ const value = row[String(column)]
133
+ if (value === null || value === undefined) return false
134
+ return regex.test(String(value))
135
+ })
136
+ )
137
+ }
138
+
139
+ // ===== Validation =====
140
+
141
+ /**
142
+ * Validate that WHERE conditions exist for destructive operations
143
+ *
144
+ * @throws Error if no WHERE conditions are present
145
+ */
146
+ protected requireWhereClause(operation: string): void {
147
+ const hasWhere = this.state.whereConditions.length > 0
148
+ const hasRegex = this.state.regexConditions.length > 0
149
+
150
+ if (!hasWhere && !hasRegex) {
151
+ const message =
152
+ `${operation} requires at least one WHERE condition. ` +
153
+ `Use where(), whereRaw(), whereIn(), whereOp(), or whereRgx().`
154
+ this.log.error(message)
155
+ throw new Error(message)
156
+ }
157
+ }
158
+
159
+ // ===== Row Transformation =====
160
+
161
+ /**
162
+ * Transform a single row FROM the database
163
+ * (deserialize JSON, convert booleans, etc.)
164
+ */
165
+ protected transformRowFromDb(row: unknown): T {
166
+ return transformFromDb<T>(row, { parser: this.state.parser })
167
+ }
168
+
169
+ /**
170
+ * Transform multiple rows FROM the database
171
+ */
172
+ protected transformRowsFromDb(rows: unknown[]): T[] {
173
+ return transformRowsFromDb<T>(rows, { parser: this.state.parser })
174
+ }
175
+
176
+ /**
177
+ * Transform a row TO the database
178
+ * (serialize JSON, stringify functions, etc.)
179
+ */
180
+ protected transformRowToDb(row: Partial<T>): RowData {
181
+ return transformToDb<T>(row, { parser: this.state.parser })
182
+ }
183
+ }