postgresdk 0.1.1-alpha.1 → 0.1.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/README.md +157 -1
- package/dist/cli.js +252 -4777
- package/dist/emit-auth.d.ts +2 -0
- package/dist/emit-routes.d.ts +1 -0
- package/dist/index.js +248 -4772
- package/dist/types.d.ts +20 -0
- package/package.json +5 -4
package/README.md
CHANGED
@@ -8,6 +8,7 @@ Generate a fully-typed, production-ready SDK from your PostgreSQL database schem
|
|
8
8
|
- 🔒 **Type Safety** - Full TypeScript types derived from your database schema
|
9
9
|
- ✅ **Runtime Validation** - Zod schemas for request/response validation
|
10
10
|
- 🔗 **Smart Relationships** - Automatic handling of 1:N and M:N relationships with eager loading
|
11
|
+
- 🔐 **Built-in Auth** - API key and JWT authentication with zero configuration
|
11
12
|
- 🎯 **Zero Config** - Works out of the box with sensible defaults
|
12
13
|
- 🏗️ **Framework Ready** - Server routes built for Hono, client SDK works anywhere
|
13
14
|
- 📦 **Lightweight** - Minimal dependencies, optimized for production
|
@@ -72,7 +73,7 @@ Create a `postgresdk.config.ts` file in your project root:
|
|
72
73
|
```typescript
|
73
74
|
export default {
|
74
75
|
// Required
|
75
|
-
connectionString: "postgres://user:pass@localhost:5432/dbname",
|
76
|
+
connectionString: process.env.DATABASE_URL || "postgres://user:pass@localhost:5432/dbname",
|
76
77
|
|
77
78
|
// Optional (with defaults)
|
78
79
|
schema: "public", // Database schema to introspect
|
@@ -81,9 +82,27 @@ export default {
|
|
81
82
|
softDeleteColumn: null, // Column name for soft deletes (e.g., "deleted_at")
|
82
83
|
includeDepthLimit: 3, // Max depth for nested includes
|
83
84
|
dateType: "date", // "date" | "string" - How to handle timestamps
|
85
|
+
|
86
|
+
// Authentication (optional)
|
87
|
+
auth: {
|
88
|
+
strategy: "none" | "api-key" | "jwt-hs256", // Default: "none"
|
89
|
+
|
90
|
+
// For API key auth
|
91
|
+
apiKeyHeader: "x-api-key", // Header name for API key
|
92
|
+
apiKeys: ["key1", "key2"], // Array of valid keys
|
93
|
+
|
94
|
+
// For JWT auth (HS256)
|
95
|
+
jwt: {
|
96
|
+
sharedSecret: "your-secret", // Shared secret for HS256
|
97
|
+
issuer: "your-app", // Optional: validate issuer claim
|
98
|
+
audience: "your-audience" // Optional: validate audience claim
|
99
|
+
}
|
100
|
+
}
|
84
101
|
};
|
85
102
|
```
|
86
103
|
|
104
|
+
Environment variables work directly in the config file - no function wrapper needed.
|
105
|
+
|
87
106
|
## Generated SDK Features
|
88
107
|
|
89
108
|
### CRUD Operations
|
@@ -157,6 +176,142 @@ const user = await sdk.users.create({
|
|
157
176
|
});
|
158
177
|
```
|
159
178
|
|
179
|
+
## Authentication
|
180
|
+
|
181
|
+
postgresdk supports three authentication strategies out of the box:
|
182
|
+
|
183
|
+
### No Authentication (Default)
|
184
|
+
|
185
|
+
```typescript
|
186
|
+
// postgresdk.config.ts
|
187
|
+
export default {
|
188
|
+
connectionString: "...",
|
189
|
+
// No auth config needed - routes are unprotected
|
190
|
+
};
|
191
|
+
```
|
192
|
+
|
193
|
+
### API Key Authentication
|
194
|
+
|
195
|
+
```typescript
|
196
|
+
// postgresdk.config.ts
|
197
|
+
export default {
|
198
|
+
connectionString: "...",
|
199
|
+
auth: {
|
200
|
+
strategy: "api-key",
|
201
|
+
apiKeyHeader: "x-api-key", // Optional, defaults to "x-api-key"
|
202
|
+
apiKeys: [
|
203
|
+
"your-api-key-1",
|
204
|
+
"your-api-key-2",
|
205
|
+
// Can also use environment variables
|
206
|
+
"env:API_KEYS" // Reads comma-separated keys from process.env.API_KEYS
|
207
|
+
]
|
208
|
+
}
|
209
|
+
};
|
210
|
+
|
211
|
+
// Client SDK usage
|
212
|
+
const sdk = new SDK({
|
213
|
+
baseUrl: "http://localhost:3000",
|
214
|
+
auth: { apiKey: "your-api-key-1" }
|
215
|
+
});
|
216
|
+
```
|
217
|
+
|
218
|
+
### JWT Authentication (HS256)
|
219
|
+
|
220
|
+
```typescript
|
221
|
+
// postgresdk.config.ts
|
222
|
+
export default {
|
223
|
+
connectionString: "...",
|
224
|
+
auth: {
|
225
|
+
strategy: "jwt-hs256",
|
226
|
+
jwt: {
|
227
|
+
sharedSecret: process.env.JWT_SECRET || "your-secret-key",
|
228
|
+
issuer: "my-app", // Optional: validates 'iss' claim
|
229
|
+
audience: "my-users" // Optional: validates 'aud' claim
|
230
|
+
}
|
231
|
+
}
|
232
|
+
};
|
233
|
+
|
234
|
+
// Client SDK usage with static token
|
235
|
+
const sdk = new SDK({
|
236
|
+
baseUrl: "http://localhost:3000",
|
237
|
+
auth: { jwt: "eyJhbGciOiJIUzI1NiIs..." }
|
238
|
+
});
|
239
|
+
|
240
|
+
// Or with dynamic token provider
|
241
|
+
const sdk = new SDK({
|
242
|
+
baseUrl: "http://localhost:3000",
|
243
|
+
auth: {
|
244
|
+
jwt: async () => {
|
245
|
+
// Refresh token if needed
|
246
|
+
return await getAccessToken();
|
247
|
+
}
|
248
|
+
}
|
249
|
+
});
|
250
|
+
|
251
|
+
// Or with custom auth headers
|
252
|
+
const sdk = new SDK({
|
253
|
+
baseUrl: "http://localhost:3000",
|
254
|
+
auth: async () => ({
|
255
|
+
"Authorization": `Bearer ${await getToken()}`,
|
256
|
+
"X-Tenant-ID": "tenant-123"
|
257
|
+
})
|
258
|
+
});
|
259
|
+
```
|
260
|
+
|
261
|
+
### Environment Variables in Auth Config
|
262
|
+
|
263
|
+
The auth configuration supports environment variables with the `env:` prefix:
|
264
|
+
|
265
|
+
```typescript
|
266
|
+
export default {
|
267
|
+
auth: {
|
268
|
+
strategy: "api-key",
|
269
|
+
apiKeys: ["env:API_KEYS"], // Reads from process.env.API_KEYS
|
270
|
+
|
271
|
+
// Or for JWT
|
272
|
+
strategy: "jwt-hs256",
|
273
|
+
jwt: {
|
274
|
+
sharedSecret: "env:JWT_SECRET" // Reads from process.env.JWT_SECRET
|
275
|
+
}
|
276
|
+
}
|
277
|
+
};
|
278
|
+
```
|
279
|
+
|
280
|
+
### How Auth Works
|
281
|
+
|
282
|
+
When authentication is configured:
|
283
|
+
|
284
|
+
1. **Server Side**: All generated routes are automatically protected with the configured auth middleware
|
285
|
+
2. **Client Side**: The SDK handles auth headers transparently
|
286
|
+
3. **Type Safety**: Auth configuration is fully typed
|
287
|
+
4. **Zero Overhead**: When `strategy: "none"`, no auth code is included
|
288
|
+
|
289
|
+
### JWT Token Generation Example
|
290
|
+
|
291
|
+
```typescript
|
292
|
+
// Install jose for JWT generation: npm install jose
|
293
|
+
import { SignJWT } from 'jose';
|
294
|
+
|
295
|
+
const secret = new TextEncoder().encode('your-secret-key');
|
296
|
+
|
297
|
+
const token = await new SignJWT({
|
298
|
+
sub: 'user123',
|
299
|
+
email: 'user@example.com',
|
300
|
+
roles: ['admin']
|
301
|
+
})
|
302
|
+
.setProtectedHeader({ alg: 'HS256' })
|
303
|
+
.setIssuer('my-app')
|
304
|
+
.setAudience('my-users')
|
305
|
+
.setExpirationTime('2h')
|
306
|
+
.sign(secret);
|
307
|
+
|
308
|
+
// Use with SDK
|
309
|
+
const sdk = new SDK({
|
310
|
+
baseUrl: "http://localhost:3000",
|
311
|
+
auth: { jwt: token }
|
312
|
+
});
|
313
|
+
```
|
314
|
+
|
160
315
|
## Server Integration
|
161
316
|
|
162
317
|
The generated server code is designed for [Hono](https://hono.dev/) but can be adapted to other frameworks:
|
@@ -210,6 +365,7 @@ Options:
|
|
210
365
|
- Node.js 20+
|
211
366
|
- PostgreSQL 12+
|
212
367
|
- TypeScript project (for using generated code)
|
368
|
+
- Optional: `jose` package for JWT authentication (auto-installed when using JWT auth)
|
213
369
|
|
214
370
|
## Development
|
215
371
|
|