hazo_connect 2.2.0 → 2.3.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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +563 -0
  3. package/dist/adapters/sqlite-adapter.d.ts.map +1 -1
  4. package/dist/adapters/sqlite-adapter.js +37 -2
  5. package/dist/adapters/sqlite-adapter.js.map +1 -1
  6. package/dist/factory.d.ts +1 -0
  7. package/dist/factory.d.ts.map +1 -1
  8. package/dist/factory.js +51 -9
  9. package/dist/factory.js.map +1 -1
  10. package/dist/nextjs/index.d.ts +2 -0
  11. package/dist/nextjs/index.d.ts.map +1 -1
  12. package/dist/nextjs/index.js +10 -1
  13. package/dist/nextjs/index.js.map +1 -1
  14. package/dist/nextjs/route-setup.d.ts +46 -0
  15. package/dist/nextjs/route-setup.d.ts.map +1 -0
  16. package/dist/nextjs/route-setup.js +141 -0
  17. package/dist/nextjs/route-setup.js.map +1 -0
  18. package/dist/nextjs/setup-helpers.d.ts +86 -0
  19. package/dist/nextjs/setup-helpers.d.ts.map +1 -0
  20. package/dist/nextjs/setup-helpers.js +174 -0
  21. package/dist/nextjs/setup-helpers.js.map +1 -0
  22. package/dist/server/index.d.ts +2 -1
  23. package/dist/server/index.d.ts.map +1 -1
  24. package/dist/server/index.js +7 -1
  25. package/dist/server/index.js.map +1 -1
  26. package/dist/sqlite/admin-service.d.ts +8 -0
  27. package/dist/sqlite/admin-service.d.ts.map +1 -1
  28. package/dist/sqlite/admin-service.js +39 -1
  29. package/dist/sqlite/admin-service.js.map +1 -1
  30. package/dist/utils/config-validator.d.ts +39 -0
  31. package/dist/utils/config-validator.d.ts.map +1 -0
  32. package/dist/utils/config-validator.js +78 -0
  33. package/dist/utils/config-validator.js.map +1 -0
  34. package/docs/examples/nextjs-admin-ui-setup.ts +199 -0
  35. package/docs/examples/nextjs-api-route.ts +205 -0
  36. package/docs/examples/nextjs-crud-service.ts +257 -0
  37. package/docs/examples/nextjs-server-component.tsx +123 -0
  38. package/docs/examples/nextjs-singleton-pattern.ts +166 -0
  39. package/docs/migration-guide.md +272 -0
  40. package/docs/nextjs-setup.md +471 -0
  41. package/docs/techdoc.md +824 -0
  42. package/docs/troubleshooting.md +442 -0
  43. package/docs/types.md +490 -0
  44. package/package.json +16 -4
  45. package/scripts/postinstall-setup.js +72 -0
  46. package/scripts/setup-routes.js +123 -0
package/docs/types.md ADDED
@@ -0,0 +1,490 @@
1
+ # Type Definitions Reference for hazo_connect
2
+
3
+ Complete TypeScript type reference for `hazo_connect`.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Core Types](#core-types)
8
+ - [Configuration Types](#configuration-types)
9
+ - [Query Builder Types](#query-builder-types)
10
+ - [Adapter Types](#adapter-types)
11
+ - [Error Types](#error-types)
12
+ - [SQLite Admin Types](#sqlite-admin-types)
13
+
14
+ ## Core Types
15
+
16
+ ### HazoConnectConfig
17
+
18
+ Main configuration interface for creating adapters.
19
+
20
+ ```typescript
21
+ import type { HazoConnectConfig } from 'hazo_connect'
22
+
23
+ interface HazoConnectConfig {
24
+ type: ConnectionType
25
+ logger?: Logger
26
+ configProvider?: ConfigProvider
27
+ enable_admin_ui?: boolean // For SQLite admin UI
28
+ [key: string]: any // Provider-specific config
29
+ }
30
+ ```
31
+
32
+ **Example:**
33
+ ```typescript
34
+ const config: HazoConnectConfig = {
35
+ type: 'sqlite',
36
+ enable_admin_ui: true,
37
+ sqlite: {
38
+ database_path: './database.sqlite'
39
+ }
40
+ }
41
+ ```
42
+
43
+ ### ConnectionType
44
+
45
+ Supported database connection types.
46
+
47
+ ```typescript
48
+ type ConnectionType = 'postgrest' | 'supabase' | 'sqlite' | 'file'
49
+ ```
50
+
51
+ ### HazoConnectAdapter
52
+
53
+ Interface that all adapters implement.
54
+
55
+ ```typescript
56
+ import type { HazoConnectAdapter } from 'hazo_connect'
57
+
58
+ interface HazoConnectAdapter {
59
+ query(
60
+ builder: QueryBuilder,
61
+ method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE',
62
+ body?: any
63
+ ): Promise<any>
64
+
65
+ rawQuery(endpoint: string, options?: RequestInit): Promise<any>
66
+
67
+ getConfig(): Promise<any>
68
+ }
69
+ ```
70
+
71
+ **Return Types:**
72
+ - `query()`: Returns `Promise<any[]>` for GET requests, `Promise<any>` for mutations
73
+ - `rawQuery()`: Returns `Promise<any[]>` for SELECT, `Promise<any>` for mutations
74
+ - `getConfig()`: Returns `Promise<AdapterSpecificConfig>`
75
+
76
+ ## Configuration Types
77
+
78
+ ### PostgREST Configuration
79
+
80
+ ```typescript
81
+ interface PostgrestConfig {
82
+ postgrest: {
83
+ base_url: string
84
+ api_key: string
85
+ }
86
+ }
87
+ ```
88
+
89
+ ### SQLite Configuration
90
+
91
+ ```typescript
92
+ interface SqliteConfig {
93
+ sqlite: {
94
+ database_path?: string // Optional - in-memory if omitted
95
+ read_only?: boolean // Default: false
96
+ initial_sql?: string[] // Optional seed statements
97
+ wasm_directory?: string // Optional WASM file directory
98
+ }
99
+ }
100
+ ```
101
+
102
+ ### Supabase Configuration
103
+
104
+ ```typescript
105
+ interface SupabaseConfig {
106
+ supabase: {
107
+ url: string
108
+ anon_key: string
109
+ service_role_key?: string
110
+ }
111
+ }
112
+ ```
113
+
114
+ ## Query Builder Types
115
+
116
+ ### QueryOperator
117
+
118
+ Supported query operators.
119
+
120
+ ```typescript
121
+ type QueryOperator =
122
+ | 'eq' // equals
123
+ | 'neq' // not equals
124
+ | 'gt' // greater than
125
+ | 'gte' // greater than or equal
126
+ | 'lt' // less than
127
+ | 'lte' // less than or equal
128
+ | 'like' // like (case-sensitive)
129
+ | 'ilike' // like (case-insensitive)
130
+ | 'in' // in array
131
+ | 'is' // is null/not null
132
+ | 'or' // or condition
133
+ ```
134
+
135
+ ### WhereCondition
136
+
137
+ Condition structure for WHERE clauses.
138
+
139
+ ```typescript
140
+ interface WhereCondition {
141
+ field: string
142
+ operator: QueryOperator
143
+ value: any
144
+ }
145
+ ```
146
+
147
+ ### OrderDirection
148
+
149
+ Sort direction.
150
+
151
+ ```typescript
152
+ type OrderDirection = 'asc' | 'desc'
153
+ ```
154
+
155
+ ### JoinType
156
+
157
+ Join types for table joins.
158
+
159
+ ```typescript
160
+ type JoinType = 'inner' | 'left' | 'right'
161
+ ```
162
+
163
+ ### NestedSelect
164
+
165
+ Nested select structure (PostgREST).
166
+
167
+ ```typescript
168
+ interface NestedSelect {
169
+ table: string
170
+ fields: string[]
171
+ }
172
+ ```
173
+
174
+ ## Adapter Types
175
+
176
+ ### PostgrestAdapter
177
+
178
+ ```typescript
179
+ import { PostgrestAdapter } from 'hazo_connect/server'
180
+
181
+ const adapter = new PostgrestAdapter(config, logger)
182
+ ```
183
+
184
+ ### SqliteAdapter
185
+
186
+ ```typescript
187
+ import { SqliteAdapter } from 'hazo_connect/server'
188
+
189
+ const adapter = new SqliteAdapter(config, logger)
190
+ ```
191
+
192
+ ## Error Types
193
+
194
+ ### ErrorCode
195
+
196
+ Error code enumeration.
197
+
198
+ ```typescript
199
+ import { ErrorCode } from 'hazo_connect/server'
200
+
201
+ enum ErrorCode {
202
+ CONFIG_ERROR = 'HAZO_CONNECT_CONFIG_ERROR',
203
+ CONNECTION_FAILED = 'HAZO_CONNECT_CONNECTION_FAILED',
204
+ QUERY_ERROR = 'HAZO_CONNECT_QUERY_ERROR',
205
+ NOT_IMPLEMENTED = 'HAZO_CONNECT_NOT_IMPLEMENTED',
206
+ VALIDATION_ERROR = 'HAZO_CONNECT_VALIDATION_ERROR'
207
+ }
208
+ ```
209
+
210
+ ### HazoConnectError
211
+
212
+ Standardized error response.
213
+
214
+ ```typescript
215
+ interface HazoConnectError {
216
+ code: string // ErrorCode enum value
217
+ message: string
218
+ statusCode?: number
219
+ originalError?: any
220
+ }
221
+ ```
222
+
223
+ **Error Handling Example:**
224
+ ```typescript
225
+ try {
226
+ const result = await hazo.query(builder)
227
+ } catch (error) {
228
+ if (error instanceof Error) {
229
+ console.error('Error code:', error.code)
230
+ console.error('Error message:', error.message)
231
+ }
232
+ }
233
+ ```
234
+
235
+ ## SQLite Admin Types
236
+
237
+ ### TableSummary
238
+
239
+ Summary information about a table.
240
+
241
+ ```typescript
242
+ import type { TableSummary } from 'hazo_connect/ui'
243
+
244
+ interface TableSummary {
245
+ name: string
246
+ type: 'table' | 'view'
247
+ row_count: number | null
248
+ }
249
+ ```
250
+
251
+ ### TableSchema
252
+
253
+ Complete table schema information.
254
+
255
+ ```typescript
256
+ import type { TableSchema } from 'hazo_connect/ui'
257
+
258
+ interface TableSchema {
259
+ columns: TableColumn[]
260
+ foreign_keys: TableForeignKey[]
261
+ }
262
+ ```
263
+
264
+ ### TableColumn
265
+
266
+ Column definition.
267
+
268
+ ```typescript
269
+ import type { TableColumn } from 'hazo_connect/ui'
270
+
271
+ interface TableColumn {
272
+ cid: number
273
+ name: string
274
+ type: string
275
+ notnull: boolean
276
+ default_value: unknown
277
+ primary_key_position: number
278
+ }
279
+ ```
280
+
281
+ ### TableForeignKey
282
+
283
+ Foreign key definition.
284
+
285
+ ```typescript
286
+ import type { TableForeignKey } from 'hazo_connect/ui'
287
+
288
+ interface TableForeignKey {
289
+ id: number
290
+ seq: number
291
+ table: string
292
+ from: string
293
+ to: string
294
+ on_update: string
295
+ on_delete: string
296
+ match: string
297
+ }
298
+ ```
299
+
300
+ ### RowPage
301
+
302
+ Paginated row data.
303
+
304
+ ```typescript
305
+ import type { RowPage } from 'hazo_connect/ui'
306
+
307
+ interface RowPage {
308
+ rows: Record<string, unknown>[]
309
+ total: number
310
+ }
311
+ ```
312
+
313
+ ### SqliteWhereFilter
314
+
315
+ Filter for SQLite queries.
316
+
317
+ ```typescript
318
+ import type { SqliteWhereFilter } from 'hazo_connect/ui'
319
+
320
+ interface SqliteWhereFilter {
321
+ column: string
322
+ operator: SqliteFilterOperator
323
+ value: unknown
324
+ }
325
+ ```
326
+
327
+ ### SqliteFilterOperator
328
+
329
+ Supported filter operators for SQLite.
330
+
331
+ ```typescript
332
+ import type { SqliteFilterOperator } from 'hazo_connect/ui'
333
+
334
+ type SqliteFilterOperator =
335
+ | 'eq'
336
+ | 'neq'
337
+ | 'gt'
338
+ | 'gte'
339
+ | 'lt'
340
+ | 'lte'
341
+ | 'like'
342
+ | 'ilike'
343
+ | 'is'
344
+ ```
345
+
346
+ ## Logger Interface
347
+
348
+ ### Logger
349
+
350
+ Optional logger interface for dependency injection.
351
+
352
+ ```typescript
353
+ import type { Logger } from 'hazo_connect'
354
+
355
+ interface Logger {
356
+ debug(message: string, data?: Record<string, unknown>): void
357
+ info(message: string, data?: Record<string, unknown>): void
358
+ warn(message: string, data?: Record<string, unknown>): void
359
+ error(message: string, data?: Record<string, unknown>): void
360
+ }
361
+ ```
362
+
363
+ **Example:**
364
+ ```typescript
365
+ const logger: Logger = {
366
+ debug: (msg, data) => console.log('[DEBUG]', msg, data),
367
+ info: (msg, data) => console.log('[INFO]', msg, data),
368
+ warn: (msg, data) => console.warn('[WARN]', msg, data),
369
+ error: (msg, data) => console.error('[ERROR]', msg, data),
370
+ }
371
+
372
+ const hazo = createHazoConnect({ type: 'sqlite', logger, ... })
373
+ ```
374
+
375
+ ## Next.js Helper Types
376
+
377
+ ### CreateHazoConnectFromEnvOptions
378
+
379
+ Options for environment-based adapter creation.
380
+
381
+ ```typescript
382
+ import type { CreateHazoConnectFromEnvOptions } from 'hazo_connect/nextjs/setup'
383
+
384
+ interface CreateHazoConnectFromEnvOptions {
385
+ type?: 'sqlite' | 'postgrest' | 'supabase' | 'file'
386
+ sqlitePath?: string
387
+ readOnly?: boolean
388
+ enableAdminUi?: boolean
389
+ logger?: Logger
390
+ }
391
+ ```
392
+
393
+ ### ApiRouteHandler
394
+
395
+ Handler function for API route helpers.
396
+
397
+ ```typescript
398
+ import type { ApiRouteHandler } from 'hazo_connect/nextjs'
399
+
400
+ type ApiRouteHandler = (
401
+ hazo: HazoConnectAdapter,
402
+ request: NextRequest
403
+ ) => Promise<NextResponse | Response>
404
+ ```
405
+
406
+ ## Type Usage Examples
407
+
408
+ ### Type-Safe Configuration
409
+
410
+ ```typescript
411
+ import type { HazoConnectConfig } from 'hazo_connect'
412
+ import { createHazoConnect } from 'hazo_connect/server'
413
+
414
+ const config: HazoConnectConfig = {
415
+ type: 'sqlite',
416
+ sqlite: {
417
+ database_path: './db.sqlite'
418
+ }
419
+ }
420
+
421
+ const hazo = createHazoConnect(config)
422
+ ```
423
+
424
+ ### Type-Safe Query Results
425
+
426
+ ```typescript
427
+ interface User {
428
+ id: number
429
+ name: string
430
+ email: string
431
+ }
432
+
433
+ const users = await hazo.query(
434
+ new QueryBuilder().from('users').select('*')
435
+ ) as User[]
436
+ ```
437
+
438
+ ### Type-Safe Error Handling
439
+
440
+ ```typescript
441
+ import { ErrorCode } from 'hazo_connect/server'
442
+
443
+ try {
444
+ await hazo.query(builder)
445
+ } catch (error: any) {
446
+ if (error.code === ErrorCode.CONFIG_ERROR) {
447
+ // Handle configuration error
448
+ } else if (error.code === ErrorCode.QUERY_ERROR) {
449
+ // Handle query error
450
+ }
451
+ }
452
+ ```
453
+
454
+ ## Import Paths
455
+
456
+ ### Server-Side Types
457
+
458
+ ```typescript
459
+ import type {
460
+ HazoConnectConfig,
461
+ HazoConnectAdapter,
462
+ Logger,
463
+ ConnectionType
464
+ } from 'hazo_connect/server'
465
+ ```
466
+
467
+ ### Client-Side Types
468
+
469
+ ```typescript
470
+ import type {
471
+ HazoConnectConfig,
472
+ HazoConnectAdapter
473
+ } from 'hazo_connect'
474
+
475
+ import type {
476
+ TableSummary,
477
+ TableSchema,
478
+ TableColumn
479
+ } from 'hazo_connect/ui'
480
+ ```
481
+
482
+ ### Next.js Helper Types
483
+
484
+ ```typescript
485
+ import type {
486
+ CreateHazoConnectFromEnvOptions,
487
+ ApiRouteHandler
488
+ } from 'hazo_connect/nextjs'
489
+ ```
490
+
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "hazo_connect",
3
- "version": "2.2.0",
3
+ "version": "2.3.2",
4
4
  "description": "Module to connect to the data store (postgres via postgrest, supabase, etc)",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
8
8
  "dist",
9
9
  "app",
10
+ "scripts",
11
+ "docs",
10
12
  "README.md"
11
13
  ],
14
+ "bin": {
15
+ "hazo-connect-setup-routes": "./scripts/setup-routes.js"
16
+ },
12
17
  "exports": {
13
18
  ".": {
14
19
  "types": "./dist/index.d.ts",
@@ -22,6 +27,10 @@
22
27
  "types": "./dist/nextjs/index.d.ts",
23
28
  "default": "./dist/nextjs/index.js"
24
29
  },
30
+ "./nextjs/setup": {
31
+ "types": "./dist/nextjs/setup-helpers.d.ts",
32
+ "default": "./dist/nextjs/setup-helpers.js"
33
+ },
25
34
  "./ui": {
26
35
  "types": "./dist/ui/index.d.ts",
27
36
  "default": "./dist/ui/index.js"
@@ -42,7 +51,7 @@
42
51
  "test:integration": "POSTGREST_INTEGRATION=true jest --runInBand tests/integration/postgrest.integration.test.ts",
43
52
  "test:integration:sqlite": "jest --runInBand tests/integration/sqlite.integration.test.ts",
44
53
  "prepublishOnly": "npm run build:lib",
45
- "postinstall": "patch-package"
54
+ "postinstall": "patch-package && node scripts/postinstall-setup.js"
46
55
  },
47
56
  "repository": {
48
57
  "type": "git",
@@ -85,11 +94,11 @@
85
94
  "@types/sql.js": "^1.4.9",
86
95
  "autoprefixer": "^10.4.19",
87
96
  "eslint": "^8.57.0",
88
- "eslint-config-next": "14.2.5",
97
+ "eslint-config-next": "14.2.33",
89
98
  "jest": "^30.2.0",
90
99
  "jest-environment-node": "^30.2.0",
91
100
  "lucide-react": "^0.553.0",
92
- "next": "14.2.5",
101
+ "next": "14.2.33",
93
102
  "patch-package": "^8.0.1",
94
103
  "postcss": "^8.4.39",
95
104
  "react": "18.3.1",
@@ -100,5 +109,8 @@
100
109
  "ts-jest": "^29.4.5",
101
110
  "tsx": "^4.17.0",
102
111
  "typescript": "^5.5.4"
112
+ },
113
+ "overrides": {
114
+ "glob": "^10.4.6"
103
115
  }
104
116
  }
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Purpose: Postinstall script to optionally setup SQLite admin routes
5
+ *
6
+ * This script runs after npm install and checks if HAZO_CONNECT_ENABLE_ADMIN_UI
7
+ * is set to 'true'. If so, it automatically sets up the admin routes.
8
+ *
9
+ * This is called from package.json postinstall script.
10
+ */
11
+
12
+ const fs = require('fs')
13
+ const path = require('path')
14
+
15
+ // Only run if we're in a Next.js project (has app or pages directory)
16
+ function isNextJsProject() {
17
+ const cwd = process.cwd()
18
+ return fs.existsSync(path.join(cwd, 'app')) || fs.existsSync(path.join(cwd, 'pages'))
19
+ }
20
+
21
+ // Check if admin UI is enabled
22
+ function isAdminUiEnabled() {
23
+ // Check environment variable
24
+ if (process.env.HAZO_CONNECT_ENABLE_ADMIN_UI === 'true') {
25
+ return true
26
+ }
27
+
28
+ // Check .env files
29
+ const envFiles = ['.env.local', '.env', '.env.development']
30
+ for (const envFile of envFiles) {
31
+ const envPath = path.join(process.cwd(), envFile)
32
+ if (fs.existsSync(envPath)) {
33
+ const content = fs.readFileSync(envPath, 'utf-8')
34
+ if (content.includes('HAZO_CONNECT_ENABLE_ADMIN_UI=true')) {
35
+ return true
36
+ }
37
+ }
38
+ }
39
+
40
+ return false
41
+ }
42
+
43
+ function main() {
44
+ // Only run in Next.js projects
45
+ if (!isNextJsProject()) {
46
+ return
47
+ }
48
+
49
+ // Only setup routes if admin UI is enabled
50
+ if (!isAdminUiEnabled()) {
51
+ return
52
+ }
53
+
54
+ // Check if routes already exist
55
+ const targetRoutesDir = path.join(process.cwd(), 'app', 'hazo_connect')
56
+ if (fs.existsSync(targetRoutesDir)) {
57
+ return
58
+ }
59
+
60
+ // Run the setup script
61
+ try {
62
+ process.env.SILENT = '1' // Suppress output during postinstall
63
+ const setupModule = require('./setup-routes.js')
64
+ setupModule.main()
65
+ } catch (error) {
66
+ // Silently fail - user can run setup manually if needed
67
+ // Don't log errors during postinstall to avoid noise
68
+ }
69
+ }
70
+
71
+ main()
72
+