postgresdk 0.1.1-alpha.0 → 0.1.1-alpha.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 +248 -5
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,15 +1,258 @@
|
|
1
|
-
#
|
1
|
+
# postgresdk
|
2
2
|
|
3
|
-
|
3
|
+
Generate a fully-typed, production-ready SDK from your PostgreSQL database schema. Automatically creates both server-side REST API routes and client-side SDK with TypeScript types, Zod validation, and support for complex relationships.
|
4
|
+
|
5
|
+
## Features
|
6
|
+
|
7
|
+
- 🚀 **Instant SDK Generation** - Point at your PostgreSQL database and get a complete SDK
|
8
|
+
- 🔒 **Type Safety** - Full TypeScript types derived from your database schema
|
9
|
+
- ✅ **Runtime Validation** - Zod schemas for request/response validation
|
10
|
+
- 🔗 **Smart Relationships** - Automatic handling of 1:N and M:N relationships with eager loading
|
11
|
+
- 🎯 **Zero Config** - Works out of the box with sensible defaults
|
12
|
+
- 🏗️ **Framework Ready** - Server routes built for Hono, client SDK works anywhere
|
13
|
+
- 📦 **Lightweight** - Minimal dependencies, optimized for production
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
```bash
|
18
|
+
npm install -g postgresdk
|
19
|
+
# or
|
20
|
+
npx postgresdk
|
21
|
+
```
|
22
|
+
|
23
|
+
## Quick Start
|
24
|
+
|
25
|
+
1. Create a configuration file `postgresdk.config.ts`:
|
26
|
+
|
27
|
+
```typescript
|
28
|
+
export default {
|
29
|
+
connectionString: "postgres://user:pass@localhost:5432/mydb",
|
30
|
+
schema: "public",
|
31
|
+
outServer: "./generated/server",
|
32
|
+
outClient: "./generated/client",
|
33
|
+
};
|
34
|
+
```
|
35
|
+
|
36
|
+
2. Run the generator:
|
37
|
+
|
38
|
+
```bash
|
39
|
+
postgresdk
|
40
|
+
```
|
41
|
+
|
42
|
+
3. Use the generated SDK:
|
43
|
+
|
44
|
+
```typescript
|
45
|
+
// Server (Hono)
|
46
|
+
import { Hono } from "hono";
|
47
|
+
import { Client } from "pg";
|
48
|
+
import { registerUsersRoutes } from "./generated/server/routes/users";
|
49
|
+
|
50
|
+
const app = new Hono();
|
51
|
+
const pg = new Client({ connectionString: "..." });
|
52
|
+
await pg.connect();
|
53
|
+
|
54
|
+
registerUsersRoutes(app, { pg });
|
55
|
+
|
56
|
+
// Client
|
57
|
+
import { SDK } from "./generated/client";
|
58
|
+
|
59
|
+
const sdk = new SDK({ baseUrl: "http://localhost:3000" });
|
60
|
+
|
61
|
+
// Full CRUD operations with TypeScript types
|
62
|
+
const user = await sdk.users.create({ name: "Alice", email: "alice@example.com" });
|
63
|
+
const users = await sdk.users.list({ include: { posts: true } });
|
64
|
+
await sdk.users.update(user.id, { name: "Alice Smith" });
|
65
|
+
await sdk.users.delete(user.id);
|
66
|
+
```
|
67
|
+
|
68
|
+
## Configuration
|
69
|
+
|
70
|
+
Create a `postgresdk.config.ts` file in your project root:
|
71
|
+
|
72
|
+
```typescript
|
73
|
+
export default {
|
74
|
+
// Required
|
75
|
+
connectionString: "postgres://user:pass@localhost:5432/dbname",
|
76
|
+
|
77
|
+
// Optional (with defaults)
|
78
|
+
schema: "public", // Database schema to introspect
|
79
|
+
outServer: "./generated/server", // Server code output directory
|
80
|
+
outClient: "./generated/client", // Client SDK output directory
|
81
|
+
softDeleteColumn: null, // Column name for soft deletes (e.g., "deleted_at")
|
82
|
+
includeDepthLimit: 3, // Max depth for nested includes
|
83
|
+
dateType: "date", // "date" | "string" - How to handle timestamps
|
84
|
+
};
|
85
|
+
```
|
86
|
+
|
87
|
+
## Generated SDK Features
|
88
|
+
|
89
|
+
### CRUD Operations
|
90
|
+
|
91
|
+
Every table gets a complete set of CRUD operations:
|
92
|
+
|
93
|
+
```typescript
|
94
|
+
// Create
|
95
|
+
const user = await sdk.users.create({ name: "Bob", email: "bob@example.com" });
|
96
|
+
|
97
|
+
// Read
|
98
|
+
const user = await sdk.users.getByPk(123);
|
99
|
+
const users = await sdk.users.list();
|
100
|
+
|
101
|
+
// Update
|
102
|
+
const updated = await sdk.users.update(123, { name: "Robert" });
|
103
|
+
|
104
|
+
// Delete
|
105
|
+
const deleted = await sdk.users.delete(123);
|
106
|
+
```
|
107
|
+
|
108
|
+
### Relationships & Eager Loading
|
109
|
+
|
110
|
+
Automatically handles relationships with the `include` parameter:
|
111
|
+
|
112
|
+
```typescript
|
113
|
+
// 1:N relationship - Get authors with their books
|
114
|
+
const authors = await sdk.authors.list({
|
115
|
+
include: { books: true }
|
116
|
+
});
|
117
|
+
|
118
|
+
// M:N relationship - Get books with their tags
|
119
|
+
const books = await sdk.books.list({
|
120
|
+
include: { tags: true }
|
121
|
+
});
|
122
|
+
|
123
|
+
// Nested includes - Get authors with books and their tags
|
124
|
+
const authors = await sdk.authors.list({
|
125
|
+
include: {
|
126
|
+
books: {
|
127
|
+
include: {
|
128
|
+
tags: true
|
129
|
+
}
|
130
|
+
}
|
131
|
+
}
|
132
|
+
});
|
133
|
+
```
|
134
|
+
|
135
|
+
### Filtering & Pagination
|
136
|
+
|
137
|
+
```typescript
|
138
|
+
const users = await sdk.users.list({
|
139
|
+
where: { status: "active" },
|
140
|
+
orderBy: "created_at",
|
141
|
+
order: "desc",
|
142
|
+
limit: 20,
|
143
|
+
offset: 40
|
144
|
+
});
|
145
|
+
```
|
146
|
+
|
147
|
+
### Type Safety
|
148
|
+
|
149
|
+
All operations are fully typed based on your database schema:
|
150
|
+
|
151
|
+
```typescript
|
152
|
+
// TypeScript will enforce correct types
|
153
|
+
const user = await sdk.users.create({
|
154
|
+
name: "Alice", // ✅ string required
|
155
|
+
email: "alice@...", // ✅ string required
|
156
|
+
age: "30" // ❌ Type error: should be number
|
157
|
+
});
|
158
|
+
```
|
159
|
+
|
160
|
+
## Server Integration
|
161
|
+
|
162
|
+
The generated server code is designed for [Hono](https://hono.dev/) but can be adapted to other frameworks:
|
163
|
+
|
164
|
+
```typescript
|
165
|
+
import { Hono } from "hono";
|
166
|
+
import { serve } from "@hono/node-server";
|
167
|
+
import { Client } from "pg";
|
168
|
+
|
169
|
+
// Import generated route registrations
|
170
|
+
import { registerUsersRoutes } from "./generated/server/routes/users";
|
171
|
+
import { registerPostsRoutes } from "./generated/server/routes/posts";
|
172
|
+
|
173
|
+
const app = new Hono();
|
174
|
+
const pg = new Client({ connectionString: process.env.DATABASE_URL });
|
175
|
+
await pg.connect();
|
176
|
+
|
177
|
+
// Register routes
|
178
|
+
registerUsersRoutes(app, { pg });
|
179
|
+
registerPostsRoutes(app, { pg });
|
180
|
+
|
181
|
+
// Start server
|
182
|
+
serve({ fetch: app.fetch, port: 3000 });
|
183
|
+
```
|
184
|
+
|
185
|
+
## CLI Options
|
186
|
+
|
187
|
+
```bash
|
188
|
+
postgresdk [options]
|
189
|
+
|
190
|
+
Options:
|
191
|
+
-c, --config <path> Path to config file (default: postgresdk.config.ts)
|
192
|
+
-v, --version Show version
|
193
|
+
-h, --help Show help
|
194
|
+
```
|
195
|
+
|
196
|
+
## How It Works
|
197
|
+
|
198
|
+
1. **Introspection** - Connects to your PostgreSQL database and reads the schema
|
199
|
+
2. **Relationship Detection** - Analyzes foreign keys to understand table relationships
|
200
|
+
3. **Code Generation** - Generates TypeScript code for:
|
201
|
+
- Type definitions from table schemas
|
202
|
+
- Zod validation schemas
|
203
|
+
- REST API route handlers
|
204
|
+
- Client SDK with full typing
|
205
|
+
- Include/eager-loading system
|
206
|
+
4. **Output** - Writes generated files to specified directories
|
207
|
+
|
208
|
+
## Requirements
|
209
|
+
|
210
|
+
- Node.js 20+
|
211
|
+
- PostgreSQL 12+
|
212
|
+
- TypeScript project (for using generated code)
|
213
|
+
|
214
|
+
## Development
|
4
215
|
|
5
216
|
```bash
|
217
|
+
# Clone the repository
|
218
|
+
git clone https://github.com/adpharm/postgresdk.git
|
219
|
+
cd postgresdk
|
220
|
+
|
221
|
+
# Install dependencies
|
6
222
|
bun install
|
223
|
+
|
224
|
+
# Run tests (starts PostgreSQL in Docker)
|
225
|
+
bun test
|
226
|
+
|
227
|
+
# Build
|
228
|
+
bun run build
|
229
|
+
|
230
|
+
# Publish new version
|
231
|
+
./publish.sh
|
7
232
|
```
|
8
233
|
|
9
|
-
|
234
|
+
## Testing
|
235
|
+
|
236
|
+
The test suite automatically manages a PostgreSQL Docker container:
|
10
237
|
|
11
238
|
```bash
|
12
|
-
bun
|
239
|
+
bun test
|
13
240
|
```
|
14
241
|
|
15
|
-
|
242
|
+
Tests cover:
|
243
|
+
- CRUD operations for all entities
|
244
|
+
- 1:N and M:N relationships
|
245
|
+
- Nested includes
|
246
|
+
- Validation and error handling
|
247
|
+
|
248
|
+
## License
|
249
|
+
|
250
|
+
MIT
|
251
|
+
|
252
|
+
## Contributing
|
253
|
+
|
254
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
255
|
+
|
256
|
+
## Support
|
257
|
+
|
258
|
+
For issues and feature requests, please [create an issue](https://github.com/adpharm/postgresdk/issues).
|