Skip to content

Commit 8e00b5d

Browse files
committed
Postgres wire-compatible server for queryleaf
- working - has tests - Can't believe it
1 parent b04423f commit 8e00b5d

29 files changed

+7571
-72
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<!-- Fathom - beautiful, simple website analytics -->
2+
<script src="https://cdn.usefathom.com/script.js" data-spa="auto" data-site="SGMKGHHQ" defer></script> <!-- / Fathom -->

docs/usage/postgres-server.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# PostgreSQL Wire-Compatible Server
2+
3+
QueryLeaf includes a PostgreSQL wire-compatible server that allows you to connect to your MongoDB database using standard PostgreSQL clients like `psql`, pgAdmin, DBeaver, or any application that supports PostgreSQL connectivity.
4+
5+
This provides a convenient way to use SQL with your MongoDB database without requiring specialized drivers or custom integration work.
6+
7+
## Installation
8+
9+
The PostgreSQL server is included in the QueryLeaf package and can be installed via npm:
10+
11+
```bash
12+
npm install -g @queryleaf/postgres-server
13+
```
14+
15+
## Usage
16+
17+
### Starting the Server
18+
19+
You can start the server using the following command:
20+
21+
```bash
22+
queryleaf-pg-server --db your_database_name
23+
```
24+
25+
By default, the server will connect to MongoDB at `mongodb://localhost:27017` and listen for PostgreSQL connections on port 5432.
26+
27+
### Command Line Options
28+
29+
The server supports the following command line options:
30+
31+
| Option | Description | Default |
32+
| ------ | ----------- | ------- |
33+
| `--uri` | MongoDB connection URI | `mongodb://localhost:27017` |
34+
| `--db` | MongoDB database name (required) | - |
35+
| `--port` | PostgreSQL server port | `5432` |
36+
| `--host` | PostgreSQL server host | `localhost` |
37+
| `--max-connections` | Maximum number of connections | `100` |
38+
39+
Example with custom options:
40+
41+
```bash
42+
queryleaf-pg-server --db your_database_name --uri mongodb://user:[email protected]:27017 --port 5433 --host 0.0.0.0
43+
```
44+
45+
## Connecting with PostgreSQL Clients
46+
47+
Once the server is running, you can connect to it using any PostgreSQL client. Authentication is currently simplified - any username will be accepted.
48+
49+
### Using psql
50+
51+
```bash
52+
psql -h localhost -p 5432 -d your_database_name -U any_username
53+
```
54+
55+
### Connection String
56+
57+
The PostgreSQL connection string format is:
58+
59+
```
60+
postgresql://username@host:port/database
61+
```
62+
63+
For example:
64+
65+
```
66+
postgresql://user@localhost:5432/your_database_name
67+
```
68+
69+
## Supported Features
70+
71+
The PostgreSQL server supports:
72+
73+
- Basic SQL queries (SELECT, INSERT, UPDATE, DELETE)
74+
- Transaction management (BEGIN, COMMIT, ROLLBACK)
75+
- Connection pooling for multiple concurrent clients
76+
77+
## Limitations
78+
79+
The current implementation has the following limitations:
80+
81+
- Limited support for PostgreSQL-specific features and data types
82+
- Authentication is simplified (any username/password is accepted)
83+
- Some advanced PostgreSQL features may not be supported
84+
- Performance may differ from native PostgreSQL
85+
86+
## Programmatic Usage
87+
88+
You can also use the PostgreSQL server programmatically in your Node.js applications:
89+
90+
```typescript
91+
import { MongoClient } from 'mongodb';
92+
import { PostgresServer } from '@queryleaf/postgres-server';
93+
94+
async function startServer() {
95+
// Connect to MongoDB
96+
const mongoClient = new MongoClient('mongodb://localhost:27017');
97+
await mongoClient.connect();
98+
99+
// Start PostgreSQL server
100+
const server = new PostgresServer(mongoClient, 'your_database_name', {
101+
port: 5432,
102+
host: 'localhost',
103+
maxConnections: 100,
104+
});
105+
106+
// Server is now running
107+
console.log('PostgreSQL-compatible server started');
108+
109+
// To shutdown the server:
110+
// await server.shutdown();
111+
}
112+
113+
startServer().catch(console.error);
114+
```
115+
116+
## Use Cases
117+
118+
The PostgreSQL wire-compatible server is useful for:
119+
120+
- Connecting existing PostgreSQL-based applications to MongoDB
121+
- Using familiar SQL tools for data exploration and analysis
122+
- Providing a SQL interface to team members unfamiliar with MongoDB query syntax
123+
- Supporting applications that require a SQL interface but need MongoDB's document model

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@
1010
"clean:lib": "rm -rf packages/lib/dist",
1111
"clean:cli": "rm -rf packages/cli/dist",
1212
"clean:server": "rm -rf packages/server/dist",
13-
"clean": "yarn clean:lib && yarn clean:cli && yarn clean:server",
13+
"clean:pg-server": "rm -rf packages/postgres-server/dist",
14+
"clean": "yarn clean:lib && yarn clean:cli && yarn clean:server && yarn clean:pg-server",
1415
"build:lib": "cd packages/lib && npx tsc",
1516
"build:cli": "cd packages/cli && npx tsc",
1617
"build:server": "cd packages/server && npx tsc",
17-
"build": "yarn clean && yarn build:lib && yarn build:cli && yarn build:server",
18+
"build:pg-server": "cd packages/postgres-server && npx tsc",
19+
"build": "yarn clean && yarn build:lib && yarn build:cli && yarn build:server && yarn build:pg-server",
1820
"build:references": "tsc --build",
1921
"test": "jest",
2022
"test:lib": "cd packages/lib && npm test",
2123
"test:lib:unit": "cd packages/lib && npm run test:unit",
2224
"test:lib:integration": "cd packages/lib && npm run test:integration",
2325
"test:cli": "cd packages/cli && npm test",
2426
"test:server": "cd packages/server && npm test",
25-
"test:all": "npm run test:lib && npm run test:cli && npm run test:server",
26-
"typecheck": "cd packages/lib && npx tsc --noEmit && cd ../cli && npx tsc --noEmit && cd ../server && npx tsc --noEmit",
27+
"test:pg-server": "cd packages/postgres-server && npm test",
28+
"test:all": "npm run test:lib && npm run test:cli && npm run test:server && npm run test:pg-server",
29+
"typecheck": "cd packages/lib && npx tsc --noEmit && cd ../cli && npx tsc --noEmit && cd ../server && npx tsc --noEmit && cd ../postgres-server && npx tsc --noEmit",
2730
"lint": "eslint --ext .ts packages/*/src",
2831
"lint:fix": "eslint --ext .ts --fix packages/*/src",
2932
"format": "prettier --write \"packages/*/src/**/*.ts\"",
@@ -59,4 +62,4 @@
5962
"prettier": "^3.2.5",
6063
"typescript": "^5.8.2"
6164
}
62-
}
65+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env node
2+
3+
require("../dist/pg-server");

0 commit comments

Comments
 (0)