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
@@ -0,0 +1,442 @@
1
+ # Troubleshooting Guide for hazo_connect
2
+
3
+ This guide helps you resolve common issues when using `hazo_connect` in Next.js projects.
4
+
5
+ ## Table of Contents
6
+
7
+ - [Common Errors](#common-errors)
8
+ - [Configuration Issues](#configuration-issues)
9
+ - [Admin UI Issues](#admin-ui-issues)
10
+ - [Verification Steps](#verification-steps)
11
+ - [Debugging Tips](#debugging-tips)
12
+
13
+ ## Common Errors
14
+
15
+ ### Error: "Cannot set properties of undefined (setting 'exports')"
16
+
17
+ **Symptoms:**
18
+ ```
19
+ TypeError: Cannot set properties of undefined (setting 'exports')
20
+ ```
21
+
22
+ **Cause:** `sql.js` is being bundled by webpack, which doesn't support Node.js `module.exports`.
23
+
24
+ **Solution:**
25
+
26
+ 1. **Check `next.config.mjs`** - Ensure `sql.js` is excluded from bundling:
27
+
28
+ ```javascript
29
+ webpack: (config, { isServer }) => {
30
+ if (isServer) {
31
+ config.externals = config.externals || []
32
+ if (Array.isArray(config.externals)) {
33
+ config.externals.push("sql.js")
34
+ } else {
35
+ config.externals = [config.externals, "sql.js"]
36
+ }
37
+ }
38
+ return config
39
+ }
40
+ ```
41
+
42
+ 2. **Add `serverComponentsExternalPackages`** (if using Next.js 13+):
43
+
44
+ ```javascript
45
+ serverComponentsExternalPackages: [
46
+ "sql.js",
47
+ "better-sqlite3",
48
+ ]
49
+ ```
50
+
51
+ 3. **Restart your development server** after making changes.
52
+
53
+ **Verification:**
54
+ - Check that `sql.js` is not in your `.next` bundle
55
+ - Verify the error no longer occurs
56
+
57
+ ### Error: SQLite works in API routes but not in Server Components
58
+
59
+ **Symptoms:**
60
+ - API routes work correctly
61
+ - Server Components throw errors or fail to load data
62
+
63
+ **Cause:** Server Components still go through webpack bundling, which can cause issues with `sql.js`.
64
+
65
+ **Solution:**
66
+
67
+ **Pattern 1: Fetch from API routes (Recommended)**
68
+
69
+ Instead of calling `hazo_connect` directly in Server Components:
70
+
71
+ ```typescript
72
+ // ❌ DON'T DO THIS in Server Components
73
+ import { createHazoConnect } from 'hazo_connect/server'
74
+
75
+ export default async function Page() {
76
+ const hazo = createHazoConnect({ ... })
77
+ const data = await hazo.query(...)
78
+ }
79
+ ```
80
+
81
+ Do this:
82
+
83
+ ```typescript
84
+ // ✅ DO THIS - Fetch from API route
85
+ import { headers } from 'next/headers'
86
+
87
+ export default async function Page() {
88
+ const headersList = await headers()
89
+ const host = headersList.get('host') || 'localhost:3000'
90
+ const response = await fetch(`http://${host}/api/data`, {
91
+ cache: 'no-store'
92
+ })
93
+ const { data } = await response.json()
94
+ return <div>{/* render data */}</div>
95
+ }
96
+ ```
97
+
98
+ **Pattern 2: Use API route handler**
99
+
100
+ Create an API route that uses `hazo_connect`, then fetch from it in your Server Component.
101
+
102
+ ### Error: Admin UI not loading
103
+
104
+ **Symptoms:**
105
+ - Admin UI page shows error message
106
+ - API routes return 403 or error about admin UI not enabled
107
+
108
+ **Cause:** Admin service not initialized or adapter not created.
109
+
110
+ **Solution:**
111
+
112
+ 1. **Enable admin UI in configuration:**
113
+
114
+ ```typescript
115
+ const hazo = createHazoConnect({
116
+ type: 'sqlite',
117
+ enable_admin_ui: true, // ← This is required
118
+ sqlite: {
119
+ database_path: './database.sqlite'
120
+ }
121
+ })
122
+ ```
123
+
124
+ 2. **Or set environment variable:**
125
+
126
+ ```bash
127
+ HAZO_CONNECT_ENABLE_ADMIN_UI=true
128
+ ```
129
+
130
+ 3. **Ensure adapter is created before accessing admin service:**
131
+
132
+ ```typescript
133
+ // app/api/sqlite/tables/route.ts
134
+ import { getSqliteAdminService } from 'hazo_connect/server'
135
+ import { createHazoConnect } from 'hazo_connect/server'
136
+
137
+ export async function GET() {
138
+ // Create adapter first (this initializes admin service if enabled)
139
+ const hazo = createHazoConnect({
140
+ type: 'sqlite',
141
+ enable_admin_ui: true,
142
+ sqlite: {
143
+ database_path: process.env.HAZO_CONNECT_SQLITE_PATH || './database.sqlite'
144
+ }
145
+ })
146
+
147
+ // Now get admin service
148
+ const service = getSqliteAdminService()
149
+ const tables = await service.listTables()
150
+
151
+ return NextResponse.json({ data: tables })
152
+ }
153
+ ```
154
+
155
+ **Verification:**
156
+ - Check that `enable_admin_ui: true` is set
157
+ - Verify environment variable is set correctly
158
+ - Ensure adapter is created before calling `getSqliteAdminService()`
159
+
160
+ ### Error: "SQLite database not found"
161
+
162
+ **Symptoms:**
163
+ ```
164
+ Error: SQLite database not found at '/path/to/database.sqlite'
165
+ ```
166
+
167
+ **Cause:** Database file doesn't exist and `read_only: true` is set.
168
+
169
+ **Solution:**
170
+
171
+ 1. **Create the database file first:**
172
+ - Run a migration script
173
+ - Or create it programmatically with `read_only: false`
174
+
175
+ 2. **Or set `read_only: false`** to allow automatic creation:
176
+
177
+ ```typescript
178
+ const hazo = createHazoConnect({
179
+ type: 'sqlite',
180
+ sqlite: {
181
+ database_path: './database.sqlite',
182
+ read_only: false // Allows creation if file doesn't exist
183
+ }
184
+ })
185
+ ```
186
+
187
+ ### Error: "hazo_connect/server can only be used on the server"
188
+
189
+ **Symptoms:**
190
+ ```
191
+ Error: hazo_connect/server can only be used on the server.
192
+ ```
193
+
194
+ **Cause:** Trying to use server-side code in a client component.
195
+
196
+ **Solution:**
197
+
198
+ 1. **Use API routes** - Create API routes that use `hazo_connect`, then fetch from them in client components.
199
+
200
+ 2. **Use Server Components** - If you need server-side data fetching, use Server Components (not Client Components).
201
+
202
+ 3. **Import types only** - In client components, only import types:
203
+
204
+ ```typescript
205
+ // ✅ In client components
206
+ import type { HazoConnectConfig } from 'hazo_connect'
207
+ import type { TableSummary } from 'hazo_connect/ui'
208
+
209
+ // ❌ DON'T import runtime code
210
+ import { createHazoConnect } from 'hazo_connect/server' // This will fail
211
+ ```
212
+
213
+ ## Configuration Issues
214
+
215
+ ### Issue: Environment variables not being read
216
+
217
+ **Symptoms:**
218
+ - Configuration defaults are used instead of environment variables
219
+ - Changes to `.env.local` don't take effect
220
+
221
+ **Solution:**
222
+
223
+ 1. **Check file location:**
224
+ - `.env.local` should be in the project root (same level as `package.json`)
225
+
226
+ 2. **Restart development server:**
227
+ - Environment variables are loaded at startup
228
+ - Changes require a restart
229
+
230
+ 3. **Check variable names:**
231
+ - Use exact names: `HAZO_CONNECT_SQLITE_PATH` (not `SQLITE_PATH`)
232
+ - Case-sensitive
233
+
234
+ 4. **Verify in `next.config.mjs`:**
235
+ - If using `env` field, ensure variables are listed there
236
+
237
+ ### Issue: Database path resolution problems
238
+
239
+ **Symptoms:**
240
+ - Database file not found even though path looks correct
241
+ - Different behavior in development vs production
242
+
243
+ **Solution:**
244
+
245
+ 1. **Use absolute paths:**
246
+ ```typescript
247
+ import path from 'path'
248
+
249
+ const dbPath = path.resolve(process.cwd(), 'database.sqlite')
250
+ ```
251
+
252
+ 2. **Or use environment variable with resolution:**
253
+ ```typescript
254
+ const dbPath = process.env.HAZO_CONNECT_SQLITE_PATH
255
+ ? path.isAbsolute(process.env.HAZO_CONNECT_SQLITE_PATH)
256
+ ? process.env.HAZO_CONNECT_SQLITE_PATH
257
+ : path.resolve(process.cwd(), process.env.HAZO_CONNECT_SQLITE_PATH)
258
+ : path.resolve(process.cwd(), 'database.sqlite')
259
+ ```
260
+
261
+ 3. **Use setup helper:**
262
+ ```typescript
263
+ import { createHazoConnectFromEnv } from 'hazo_connect/nextjs/setup'
264
+
265
+ const hazo = createHazoConnectFromEnv() // Handles path resolution automatically
266
+ ```
267
+
268
+ ## Admin UI Issues
269
+
270
+ ### Issue: Admin UI routes return 403
271
+
272
+ **Cause:** Admin UI is not enabled.
273
+
274
+ **Solution:**
275
+
276
+ 1. Set `enable_admin_ui: true` when creating adapter
277
+ 2. Or set `HAZO_CONNECT_ENABLE_ADMIN_UI=true` environment variable
278
+ 3. Restart development server
279
+
280
+ ### Issue: Admin UI shows "Failed to list SQLite tables"
281
+
282
+ **Cause:** Database path not configured or database doesn't exist.
283
+
284
+ **Solution:**
285
+
286
+ 1. Verify `HAZO_CONNECT_SQLITE_PATH` is set correctly
287
+ 2. Ensure database file exists
288
+ 3. Check file permissions
289
+
290
+ ## Verification Steps
291
+
292
+ ### 1. Verify Next.js Configuration
293
+
294
+ Check `next.config.mjs`:
295
+
296
+ ```javascript
297
+ // Should have:
298
+ webpack: (config, { isServer }) => {
299
+ if (isServer) {
300
+ config.externals.push("sql.js")
301
+ }
302
+ return config
303
+ }
304
+
305
+ serverComponentsExternalPackages: ["sql.js"]
306
+ ```
307
+
308
+ ### 2. Verify Environment Variables
309
+
310
+ Create a test API route:
311
+
312
+ ```typescript
313
+ // app/api/test-config/route.ts
314
+ import { NextResponse } from 'next/server'
315
+
316
+ export async function GET() {
317
+ return NextResponse.json({
318
+ HAZO_CONNECT_TYPE: process.env.HAZO_CONNECT_TYPE,
319
+ HAZO_CONNECT_SQLITE_PATH: process.env.HAZO_CONNECT_SQLITE_PATH,
320
+ HAZO_CONNECT_ENABLE_ADMIN_UI: process.env.HAZO_CONNECT_ENABLE_ADMIN_UI,
321
+ })
322
+ }
323
+ ```
324
+
325
+ Visit `/api/test-config` to verify variables are loaded.
326
+
327
+ ### 3. Verify Database Connection
328
+
329
+ Create a test API route:
330
+
331
+ ```typescript
332
+ // app/api/test-connection/route.ts
333
+ import { createHazoConnectFromEnv } from 'hazo_connect/nextjs/setup'
334
+ import { QueryBuilder } from 'hazo_connect/server'
335
+ import { NextResponse } from 'next/server'
336
+
337
+ export async function GET() {
338
+ try {
339
+ const hazo = createHazoConnectFromEnv()
340
+ const result = await hazo.query(
341
+ new QueryBuilder().rawQuery("SELECT 1 as test")
342
+ )
343
+ return NextResponse.json({ success: true, result })
344
+ } catch (error) {
345
+ return NextResponse.json(
346
+ { success: false, error: error instanceof Error ? error.message : String(error) },
347
+ { status: 500 }
348
+ )
349
+ }
350
+ }
351
+ ```
352
+
353
+ ### 4. Verify Admin UI
354
+
355
+ 1. Navigate to `/hazo_connect/sqlite_admin`
356
+ 2. Should see list of tables (or empty state if no tables)
357
+ 3. If error, check browser console and server logs
358
+
359
+ ## Debugging Tips
360
+
361
+ ### 1. Enable Debug Logging
362
+
363
+ Add a logger to see what's happening:
364
+
365
+ ```typescript
366
+ const hazo = createHazoConnect({
367
+ type: 'sqlite',
368
+ sqlite: { database_path: './database.sqlite' },
369
+ logger: {
370
+ debug: (msg, data) => console.log('[DEBUG]', msg, data),
371
+ info: (msg, data) => console.log('[INFO]', msg, data),
372
+ warn: (msg, data) => console.warn('[WARN]', msg, data),
373
+ error: (msg, data) => console.error('[ERROR]', msg, data),
374
+ }
375
+ })
376
+ ```
377
+
378
+ ### 2. Check Server Logs
379
+
380
+ - Look for errors in terminal where `next dev` is running
381
+ - Check for webpack bundling warnings
382
+ - Look for SQLite initialization errors
383
+
384
+ ### 3. Test in Isolation
385
+
386
+ Create a minimal test file:
387
+
388
+ ```typescript
389
+ // test-hazo.ts
390
+ import { createHazoConnect } from 'hazo_connect/server'
391
+ import { QueryBuilder } from 'hazo_connect/server'
392
+
393
+ async function test() {
394
+ const hazo = createHazoConnect({
395
+ type: 'sqlite',
396
+ sqlite: {
397
+ database_path: './test.db'
398
+ }
399
+ })
400
+
401
+ const result = await hazo.query(
402
+ new QueryBuilder().rawQuery("SELECT 1")
403
+ )
404
+ console.log('Success:', result)
405
+ }
406
+
407
+ test().catch(console.error)
408
+ ```
409
+
410
+ Run with: `npx tsx test-hazo.ts`
411
+
412
+ ### 4. Check Package Versions
413
+
414
+ Ensure compatible versions:
415
+
416
+ ```bash
417
+ npm list hazo_connect next sql.js
418
+ ```
419
+
420
+ ### 5. Clear Next.js Cache
421
+
422
+ If issues persist:
423
+
424
+ ```bash
425
+ rm -rf .next
426
+ npm run dev
427
+ ```
428
+
429
+ ## Getting Help
430
+
431
+ If you're still experiencing issues:
432
+
433
+ 1. Check the [Next.js Setup Guide](./nextjs-setup.md)
434
+ 2. Review [Code Examples](./examples/)
435
+ 3. Check [Migration Guide](./migration-guide.md) if migrating from older code
436
+ 4. Open an issue on GitHub with:
437
+ - Error message
438
+ - Next.js version
439
+ - `next.config.mjs` configuration
440
+ - Environment variables (redacted)
441
+ - Steps to reproduce
442
+