Skip to content

Commit 770502a

Browse files
authored
Merge pull request #40 from wsporto/postgres
Postgres
2 parents 21fa5c7 + 86040b1 commit 770502a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+8914
-439
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Access your database directly without a heavy ORM, gain effortless type-safety,
66

77
TypeSQL supports multiple SQL database backends:
88

9+
##### PostgreSQL (Experimental)
10+
11+
- [pg](https://www.npmjs.com/package/pg) - PostgreSQL client for node.js.
12+
913
##### MySQL / MariaDB
1014

1115
- [mysql2](https://www.npmjs.com/package/mysql2) - the standard driver for mysql in NodeJS
@@ -57,7 +61,7 @@ deno syntax:
5761

5862
1. _npm install -g typesql-cli_
5963

60-
2. Add the `typesql.json` configuration file in project root folder. You can generate an template with cli command `typesql init`. The client option can be: 'mysql2', 'better-sqlite3', 'libsql', 'bun:sqlite' or 'd1'. The `authToken` configuration is used only for the libsql client.
64+
2. Add the `typesql.json` configuration file in project root folder. You can generate an template with cli command `typesql init`. The client option can be: 'pg', 'mysql2', 'better-sqlite3', 'libsql', 'bun:sqlite' or 'd1'. The `authToken` configuration is used only for the libsql client.
6165

6266
```json
6367
{

docker-compose.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ services:
1010
environment:
1111
MYSQL_ROOT_PASSWORD: "password"
1212
MYSQL_DATABASE: "mydb"
13+
postgres-dev:
14+
image: "postgres:12.21"
15+
container_name: "postgres-dev"
16+
ports:
17+
- "5432:5432"
18+
environment:
19+
POSTGRES_USER: postgres
20+
POSTGRES_PASSWORD: password
1321
flyway:
1422
image: flyway/flyway:7.15
1523
container_name: "flyway"
@@ -31,4 +39,12 @@ services:
3139
command: -url=jdbc:sqlite:/flyway/db/users.db migrate
3240
volumes:
3341
- .:/flyway/db
34-
- ./sqlite-attached-migrations:/flyway/sql
42+
- ./sqlite-attached-migrations:/flyway/sql
43+
postgres-flyway:
44+
image: flyway/flyway:7.15
45+
container_name: "postgres_flyway"
46+
command: -url=jdbc:postgresql://postgres-dev:5432/postgres -schemas=public -user=postgres -password=password -connectRetries=60 migrate
47+
volumes:
48+
- ./migrations/postgres:/flyway/sql
49+
depends_on:
50+
- postgres-dev
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CREATE TABLE mytable1 (
2+
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3+
value INTEGER
4+
);
5+
6+
CREATE TABLE mytable2 (
7+
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
8+
name TEXT,
9+
descr TEXT
10+
);
11+
12+
CREATE TABLE mytable3 (
13+
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
14+
double_value real,
15+
name TEXT NOT NULL
16+
);
17+
18+
CREATE TABLE mytable4 (
19+
id TEXT PRIMARY KEY,
20+
name TEXT,
21+
year INTEGER
22+
);
23+
24+
CREATE TABLE mytable5 (
25+
id INTEGER PRIMARY KEY,
26+
name TEXT,
27+
year INTEGER
28+
);
29+
30+
CREATE TABLE all_types (
31+
bool_column BOOL,
32+
bytea_column BYTEA,
33+
char_column CHAR(1),
34+
name_column NAME,
35+
int8_column INT8,
36+
int2_column INT2,
37+
int4_column INT4,
38+
text_column TEXT,
39+
varchar_column VARCHAR(255),
40+
date_column DATE,
41+
bit_column BIT(1),
42+
numeric_column NUMERIC,
43+
uuid_column UUID,
44+
float4_column FLOAT4,
45+
float8_column FLOAT8,
46+
timestamp_column timestamp,
47+
timestamp_not_null_column timestamp NOT NULL,
48+
timestamptz_column timestamptz,
49+
timestamptz_not_null_column timestamptz NOT NULL
50+
);

package-lock.json

Lines changed: 94 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typesql-cli",
3-
"version": "0.14.3",
3+
"version": "0.15.0",
44
"description": "",
55
"main": "index.js",
66
"bin": {
@@ -11,6 +11,7 @@
1111
"test": "mocha --enable-source-maps dist/tests/**/*.js --watch --watch-files **/*.js",
1212
"antlr4ts": "antlr4ts -Xexact-output-dir -o ./parser/ ./grammar/*.g4",
1313
"dist": "tsc && shx cp ./package.json ./README.md ./dist/src && cd ./dist/src && npm publish",
14+
"dist-exp": "tsc && shx cp ./package.json ./README.md ./dist/src && cd ./dist/src && npm publish --tag experimental",
1415
"gen": "node ./dist/src/cli.js compile"
1516
},
1617
"keywords": [],
@@ -34,7 +35,7 @@
3435
"typescript": "^5.5.3"
3536
},
3637
"dependencies": {
37-
"@wsporto/ts-mysql-parser": "^0.4.0",
38+
"@wsporto/typesql-parser": "^0.0.3",
3839
"better-sqlite3": "^11.1.2",
3940
"camelcase": "^6.3.0",
4041
"chokidar": "^3.6.0",
@@ -44,6 +45,8 @@
4445
"libsql": "^0.4.4",
4546
"moment": "^2.30.1",
4647
"mysql2": "^3.10.3",
48+
"neverthrow": "^8.1.1",
49+
"postgres": "^3.4.5",
4750
"yargs": "^17.7.2"
4851
}
4952
}

src/cli.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createMysqlClient, loadMysqlSchema, loadMySqlTableSchema, selectTablesF
88
import { generateInsertStatement, generateUpdateStatement, generateDeleteStatement, generateSelectStatement } from './sql-generator';
99
import type { ColumnSchema, Table } from './mysql-query-analyzer/types';
1010
import type { TypeSqlConfig, SqlGenOption, DatabaseClient, TypeSqlDialect, TypeSqlError, SQLiteClient, QueryType } from './types';
11-
import { type Either, isLeft, left } from 'fp-ts/lib/Either';
11+
import { type Either, isLeft, left, right } from 'fp-ts/lib/Either';
1212
import CodeBlockWriter from 'code-block-writer';
1313
import { globSync } from 'glob';
1414
import {
@@ -19,6 +19,7 @@ import {
1919
} from './sqlite-query-analyzer/query-executor';
2020
import { createLibSqlClient } from './drivers/libsql';
2121
import { generateCrud } from './sqlite-query-analyzer/code-generator';
22+
import { createPostgresClient } from './drivers/postgres';
2223

2324
const CRUD_FOLDER = 'crud';
2425

@@ -291,7 +292,7 @@ async function selectAllTables(client: DatabaseClient): Promise<Either<string, T
291292
return selectTablesResult;
292293
}
293294

294-
async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach?: string[], loadExtensions?: string[], authToken?: string) {
295+
async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach?: string[], loadExtensions?: string[], authToken?: string): Promise<Either<TypeSqlError, DatabaseClient>> {
295296
switch (dialect) {
296297
case 'mysql2':
297298
return createMysqlClient(databaseUri);
@@ -302,6 +303,8 @@ async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach
302303
return createLibSqlClient(databaseUri, attach || [], loadExtensions || [], authToken || '');
303304
case 'd1':
304305
return createD1Client(dialect, databaseUri, loadExtensions || []);
306+
case 'pg':
307+
return createPostgresClient(databaseUri);
305308
}
306309
}
307310
async function loadSchema(databaseClient: DatabaseClient): Promise<Either<TypeSqlError, ColumnSchema[]>> {
@@ -313,6 +316,9 @@ async function loadSchema(databaseClient: DatabaseClient): Promise<Either<TypeSq
313316
case 'bun:sqlite':
314317
case 'd1':
315318
return loadDbSchema(databaseClient.client);
319+
case 'pg':
320+
return right([]);
321+
316322
}
317323
}
318324

@@ -325,10 +331,12 @@ async function loadTableSchema(databaseClient: DatabaseClient, tableName: string
325331
case 'bun:sqlite':
326332
case 'd1':
327333
return await loadDbSchema(databaseClient.client);
334+
case 'pg':
335+
return right([]);
328336
}
329337
}
330338

331-
async function selectTables(databaseClient: DatabaseClient) {
339+
async function selectTables(databaseClient: DatabaseClient): Promise<Either<TypeSqlError, Table[]>> {
332340
switch (databaseClient.type) {
333341
case 'mysql2':
334342
return await selectTablesFromSchema(databaseClient.client);
@@ -337,6 +345,8 @@ async function selectTables(databaseClient: DatabaseClient) {
337345
case 'bun:sqlite':
338346
case 'd1':
339347
return selectSqliteTablesFromSchema(databaseClient.client);
348+
case 'pg':
349+
return right([])
340350
}
341351
}
342352

0 commit comments

Comments
 (0)