postgresdk 0.2.1-alpha.1 → 0.4.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/README.md +108 -0
- package/dist/cli.js +13 -7
- package/dist/index.js +13 -7
- package/package.json +1 -1
package/README.md
CHANGED
@@ -493,6 +493,114 @@ const sdk = new SDK({
|
|
493
493
|
});
|
494
494
|
```
|
495
495
|
|
496
|
+
## Database Drivers
|
497
|
+
|
498
|
+
The generated code works with any PostgreSQL client that implements a simple `query` interface. You can use the standard `pg` driver, Neon's serverless driver, or any other compatible client.
|
499
|
+
|
500
|
+
### Node.js `pg` Driver
|
501
|
+
|
502
|
+
The standard PostgreSQL driver for Node.js environments. Works everywhere Node.js runs.
|
503
|
+
|
504
|
+
Server setup:
|
505
|
+
```typescript
|
506
|
+
import { Hono } from "hono";
|
507
|
+
import { Client } from "pg";
|
508
|
+
import { createRouter } from "./generated/server/router";
|
509
|
+
|
510
|
+
const app = new Hono();
|
511
|
+
|
512
|
+
// Standard pg client
|
513
|
+
const pg = new Client({ connectionString: process.env.DATABASE_URL });
|
514
|
+
await pg.connect();
|
515
|
+
|
516
|
+
// Wire up the generated routes
|
517
|
+
const apiRouter = createRouter({ pg });
|
518
|
+
app.route("/", apiRouter);
|
519
|
+
```
|
520
|
+
|
521
|
+
### Neon Serverless Driver (Edge-Compatible)
|
522
|
+
|
523
|
+
For edge environments like Vercel Edge Functions or Cloudflare Workers. Uses HTTP/WebSocket instead of TCP connections.
|
524
|
+
|
525
|
+
Server setup:
|
526
|
+
```typescript
|
527
|
+
import { Hono } from "hono";
|
528
|
+
import { Pool } from "@neondatabase/serverless";
|
529
|
+
import { createRouter } from "./generated/server/router";
|
530
|
+
|
531
|
+
const app = new Hono();
|
532
|
+
|
533
|
+
// Neon's Pool is compatible with node-postgres
|
534
|
+
const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
|
535
|
+
|
536
|
+
// Wire up the generated routes (Pool has the same query interface)
|
537
|
+
const apiRouter = createRouter({ pg: pool });
|
538
|
+
app.route("/", apiRouter);
|
539
|
+
|
540
|
+
// Deploy to Vercel Edge
|
541
|
+
export const config = { runtime: 'edge' };
|
542
|
+
export default app;
|
543
|
+
```
|
544
|
+
|
545
|
+
### Which Driver Should I Use?
|
546
|
+
|
547
|
+
- **Use `pg` (default) when:**
|
548
|
+
- Running on traditional Node.js servers
|
549
|
+
- Using Docker, VPS, or dedicated hosting
|
550
|
+
- Need connection pooling or advanced PostgreSQL features
|
551
|
+
- Running on AWS Lambda, Google Cloud Functions (with Node.js runtime)
|
552
|
+
|
553
|
+
- **Use `neon` when:**
|
554
|
+
- Deploying to Vercel Edge Functions
|
555
|
+
- Deploying to Cloudflare Workers
|
556
|
+
- Need globally distributed edge computing
|
557
|
+
- Want to avoid TCP connection overhead
|
558
|
+
- Using Neon as your PostgreSQL provider
|
559
|
+
|
560
|
+
### Connection Pooling with `pg`
|
561
|
+
|
562
|
+
For production Node.js deployments, use connection pooling:
|
563
|
+
|
564
|
+
```typescript
|
565
|
+
import { Pool } from "pg";
|
566
|
+
import { createRouter } from "./generated/server/router";
|
567
|
+
|
568
|
+
const pool = new Pool({
|
569
|
+
connectionString: process.env.DATABASE_URL,
|
570
|
+
max: 20,
|
571
|
+
idleTimeoutMillis: 30000,
|
572
|
+
connectionTimeoutMillis: 2000,
|
573
|
+
});
|
574
|
+
|
575
|
+
// The generated routes work with both Client and Pool
|
576
|
+
const apiRouter = createRouter({ pg: pool });
|
577
|
+
```
|
578
|
+
|
579
|
+
### Custom Database Adapters
|
580
|
+
|
581
|
+
You can use any database client as long as it matches the expected interface:
|
582
|
+
|
583
|
+
```typescript
|
584
|
+
// Any client that implements this interface works
|
585
|
+
interface DatabaseAdapter {
|
586
|
+
query(text: string, params?: any[]): Promise<{ rows: any[] }>;
|
587
|
+
}
|
588
|
+
|
589
|
+
// Both pg and @neondatabase/serverless Pool/Client implement this interface natively
|
590
|
+
// For other ORMs, you may need to create an adapter:
|
591
|
+
|
592
|
+
// Example with a hypothetical ORM that doesn't match the interface
|
593
|
+
const customClient = new SomeORM();
|
594
|
+
const pg = {
|
595
|
+
async query(text: string, params?: any[]) {
|
596
|
+
const result = await customClient.raw(text, params);
|
597
|
+
return { rows: result };
|
598
|
+
}
|
599
|
+
};
|
600
|
+
|
601
|
+
const apiRouter = createRouter({ pg });
|
602
|
+
```
|
603
|
+
|
496
604
|
## Server Integration with Hono
|
497
605
|
|
498
606
|
The generated code integrates seamlessly with [Hono](https://hono.dev/), a lightweight web framework for the Edge.
|
package/dist/cli.js
CHANGED
@@ -1249,7 +1249,7 @@ export class ${Type}Client extends BaseClient {
|
|
1249
1249
|
function emitClientIndex(tables) {
|
1250
1250
|
let out = `/* Generated. Do not edit. */
|
1251
1251
|
`;
|
1252
|
-
out += `import { BaseClient, AuthConfig } from "./base-client";
|
1252
|
+
out += `import { BaseClient, type AuthConfig } from "./base-client";
|
1253
1253
|
`;
|
1254
1254
|
for (const t of tables) {
|
1255
1255
|
out += `import { ${pascal(t.name)}Client } from "./${t.name}";
|
@@ -2003,14 +2003,20 @@ ${hasAuth ? `export { authMiddleware } from "./auth";` : ""}
|
|
2003
2003
|
*
|
2004
2004
|
* @example
|
2005
2005
|
* import { Hono } from "hono";
|
2006
|
-
* import { Client } from "pg";
|
2007
2006
|
* import { createRouter } from "./generated/server/router";
|
2008
2007
|
*
|
2009
|
-
*
|
2008
|
+
* // Using pg driver (Node.js)
|
2009
|
+
* import { Client } from "pg";
|
2010
2010
|
* const pg = new Client({ connectionString: process.env.DATABASE_URL });
|
2011
2011
|
* await pg.connect();
|
2012
2012
|
*
|
2013
|
-
* //
|
2013
|
+
* // OR using Neon driver (Edge-compatible)
|
2014
|
+
* import { Pool } from "@neondatabase/serverless";
|
2015
|
+
* const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
|
2016
|
+
* const pg = pool; // Pool already has the compatible query method
|
2017
|
+
*
|
2018
|
+
* // Mount all generated routes
|
2019
|
+
* const app = new Hono();
|
2014
2020
|
* const apiRouter = createRouter({ pg });
|
2015
2021
|
* app.route("/api", apiRouter);
|
2016
2022
|
*
|
@@ -2058,12 +2064,12 @@ ${registrations}
|
|
2058
2064
|
*
|
2059
2065
|
* @example
|
2060
2066
|
* import { Hono } from "hono";
|
2061
|
-
* import { Client } from "pg";
|
2062
2067
|
* import { registerAllRoutes } from "./generated/server/router";
|
2063
2068
|
*
|
2064
2069
|
* const app = new Hono();
|
2065
|
-
*
|
2066
|
-
*
|
2070
|
+
*
|
2071
|
+
* // Setup database connection (see createRouter example for both pg and Neon options)
|
2072
|
+
* const pg = yourDatabaseClient;
|
2067
2073
|
*
|
2068
2074
|
* // Register all routes at once
|
2069
2075
|
* registerAllRoutes(app, { pg });
|
package/dist/index.js
CHANGED
@@ -1012,7 +1012,7 @@ export class ${Type}Client extends BaseClient {
|
|
1012
1012
|
function emitClientIndex(tables) {
|
1013
1013
|
let out = `/* Generated. Do not edit. */
|
1014
1014
|
`;
|
1015
|
-
out += `import { BaseClient, AuthConfig } from "./base-client";
|
1015
|
+
out += `import { BaseClient, type AuthConfig } from "./base-client";
|
1016
1016
|
`;
|
1017
1017
|
for (const t of tables) {
|
1018
1018
|
out += `import { ${pascal(t.name)}Client } from "./${t.name}";
|
@@ -1766,14 +1766,20 @@ ${hasAuth ? `export { authMiddleware } from "./auth";` : ""}
|
|
1766
1766
|
*
|
1767
1767
|
* @example
|
1768
1768
|
* import { Hono } from "hono";
|
1769
|
-
* import { Client } from "pg";
|
1770
1769
|
* import { createRouter } from "./generated/server/router";
|
1771
1770
|
*
|
1772
|
-
*
|
1771
|
+
* // Using pg driver (Node.js)
|
1772
|
+
* import { Client } from "pg";
|
1773
1773
|
* const pg = new Client({ connectionString: process.env.DATABASE_URL });
|
1774
1774
|
* await pg.connect();
|
1775
1775
|
*
|
1776
|
-
* //
|
1776
|
+
* // OR using Neon driver (Edge-compatible)
|
1777
|
+
* import { Pool } from "@neondatabase/serverless";
|
1778
|
+
* const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
|
1779
|
+
* const pg = pool; // Pool already has the compatible query method
|
1780
|
+
*
|
1781
|
+
* // Mount all generated routes
|
1782
|
+
* const app = new Hono();
|
1777
1783
|
* const apiRouter = createRouter({ pg });
|
1778
1784
|
* app.route("/api", apiRouter);
|
1779
1785
|
*
|
@@ -1821,12 +1827,12 @@ ${registrations}
|
|
1821
1827
|
*
|
1822
1828
|
* @example
|
1823
1829
|
* import { Hono } from "hono";
|
1824
|
-
* import { Client } from "pg";
|
1825
1830
|
* import { registerAllRoutes } from "./generated/server/router";
|
1826
1831
|
*
|
1827
1832
|
* const app = new Hono();
|
1828
|
-
*
|
1829
|
-
*
|
1833
|
+
*
|
1834
|
+
* // Setup database connection (see createRouter example for both pg and Neon options)
|
1835
|
+
* const pg = yourDatabaseClient;
|
1830
1836
|
*
|
1831
1837
|
* // Register all routes at once
|
1832
1838
|
* registerAllRoutes(app, { pg });
|