Skip to content

Commit

Permalink
Merge pull request #40 from wsporto/postgres
Browse files Browse the repository at this point in the history
Postgres
  • Loading branch information
wsporto authored Dec 18, 2024
2 parents 21fa5c7 + 86040b1 commit 770502a
Show file tree
Hide file tree
Showing 64 changed files with 8,914 additions and 439 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Access your database directly without a heavy ORM, gain effortless type-safety,

TypeSQL supports multiple SQL database backends:

##### PostgreSQL (Experimental)

- [pg](https://www.npmjs.com/package/pg) - PostgreSQL client for node.js.

##### MySQL / MariaDB

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

1. _npm install -g typesql-cli_

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.
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.

```json
{
Expand Down
18 changes: 17 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ services:
environment:
MYSQL_ROOT_PASSWORD: "password"
MYSQL_DATABASE: "mydb"
postgres-dev:
image: "postgres:12.21"
container_name: "postgres-dev"
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
flyway:
image: flyway/flyway:7.15
container_name: "flyway"
Expand All @@ -31,4 +39,12 @@ services:
command: -url=jdbc:sqlite:/flyway/db/users.db migrate
volumes:
- .:/flyway/db
- ./sqlite-attached-migrations:/flyway/sql
- ./sqlite-attached-migrations:/flyway/sql
postgres-flyway:
image: flyway/flyway:7.15
container_name: "postgres_flyway"
command: -url=jdbc:postgresql://postgres-dev:5432/postgres -schemas=public -user=postgres -password=password -connectRetries=60 migrate
volumes:
- ./migrations/postgres:/flyway/sql
depends_on:
- postgres-dev
50 changes: 50 additions & 0 deletions migrations/postgres/V1__create_initial_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
CREATE TABLE mytable1 (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
value INTEGER
);

CREATE TABLE mytable2 (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT,
descr TEXT
);

CREATE TABLE mytable3 (
id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
double_value real,
name TEXT NOT NULL
);

CREATE TABLE mytable4 (
id TEXT PRIMARY KEY,
name TEXT,
year INTEGER
);

CREATE TABLE mytable5 (
id INTEGER PRIMARY KEY,
name TEXT,
year INTEGER
);

CREATE TABLE all_types (
bool_column BOOL,
bytea_column BYTEA,
char_column CHAR(1),
name_column NAME,
int8_column INT8,
int2_column INT2,
int4_column INT4,
text_column TEXT,
varchar_column VARCHAR(255),
date_column DATE,
bit_column BIT(1),
numeric_column NUMERIC,
uuid_column UUID,
float4_column FLOAT4,
float8_column FLOAT8,
timestamp_column timestamp,
timestamp_not_null_column timestamp NOT NULL,
timestamptz_column timestamptz,
timestamptz_not_null_column timestamptz NOT NULL
);
109 changes: 94 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typesql-cli",
"version": "0.14.3",
"version": "0.15.0",
"description": "",
"main": "index.js",
"bin": {
Expand All @@ -11,6 +11,7 @@
"test": "mocha --enable-source-maps dist/tests/**/*.js --watch --watch-files **/*.js",
"antlr4ts": "antlr4ts -Xexact-output-dir -o ./parser/ ./grammar/*.g4",
"dist": "tsc && shx cp ./package.json ./README.md ./dist/src && cd ./dist/src && npm publish",
"dist-exp": "tsc && shx cp ./package.json ./README.md ./dist/src && cd ./dist/src && npm publish --tag experimental",
"gen": "node ./dist/src/cli.js compile"
},
"keywords": [],
Expand All @@ -34,7 +35,7 @@
"typescript": "^5.5.3"
},
"dependencies": {
"@wsporto/ts-mysql-parser": "^0.4.0",
"@wsporto/typesql-parser": "^0.0.3",
"better-sqlite3": "^11.1.2",
"camelcase": "^6.3.0",
"chokidar": "^3.6.0",
Expand All @@ -44,6 +45,8 @@
"libsql": "^0.4.4",
"moment": "^2.30.1",
"mysql2": "^3.10.3",
"neverthrow": "^8.1.1",
"postgres": "^3.4.5",
"yargs": "^17.7.2"
}
}
16 changes: 13 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createMysqlClient, loadMysqlSchema, loadMySqlTableSchema, selectTablesF
import { generateInsertStatement, generateUpdateStatement, generateDeleteStatement, generateSelectStatement } from './sql-generator';
import type { ColumnSchema, Table } from './mysql-query-analyzer/types';
import type { TypeSqlConfig, SqlGenOption, DatabaseClient, TypeSqlDialect, TypeSqlError, SQLiteClient, QueryType } from './types';
import { type Either, isLeft, left } from 'fp-ts/lib/Either';
import { type Either, isLeft, left, right } from 'fp-ts/lib/Either';
import CodeBlockWriter from 'code-block-writer';
import { globSync } from 'glob';
import {
Expand All @@ -19,6 +19,7 @@ import {
} from './sqlite-query-analyzer/query-executor';
import { createLibSqlClient } from './drivers/libsql';
import { generateCrud } from './sqlite-query-analyzer/code-generator';
import { createPostgresClient } from './drivers/postgres';

const CRUD_FOLDER = 'crud';

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

async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach?: string[], loadExtensions?: string[], authToken?: string) {
async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach?: string[], loadExtensions?: string[], authToken?: string): Promise<Either<TypeSqlError, DatabaseClient>> {
switch (dialect) {
case 'mysql2':
return createMysqlClient(databaseUri);
Expand All @@ -302,6 +303,8 @@ async function createClient(databaseUri: string, dialect: TypeSqlDialect, attach
return createLibSqlClient(databaseUri, attach || [], loadExtensions || [], authToken || '');
case 'd1':
return createD1Client(dialect, databaseUri, loadExtensions || []);
case 'pg':
return createPostgresClient(databaseUri);
}
}
async function loadSchema(databaseClient: DatabaseClient): Promise<Either<TypeSqlError, ColumnSchema[]>> {
Expand All @@ -313,6 +316,9 @@ async function loadSchema(databaseClient: DatabaseClient): Promise<Either<TypeSq
case 'bun:sqlite':
case 'd1':
return loadDbSchema(databaseClient.client);
case 'pg':
return right([]);

}
}

Expand All @@ -325,10 +331,12 @@ async function loadTableSchema(databaseClient: DatabaseClient, tableName: string
case 'bun:sqlite':
case 'd1':
return await loadDbSchema(databaseClient.client);
case 'pg':
return right([]);
}
}

async function selectTables(databaseClient: DatabaseClient) {
async function selectTables(databaseClient: DatabaseClient): Promise<Either<TypeSqlError, Table[]>> {
switch (databaseClient.type) {
case 'mysql2':
return await selectTablesFromSchema(databaseClient.client);
Expand All @@ -337,6 +345,8 @@ async function selectTables(databaseClient: DatabaseClient) {
case 'bun:sqlite':
case 'd1':
return selectSqliteTablesFromSchema(databaseClient.client);
case 'pg':
return right([])
}
}

Expand Down
Loading

0 comments on commit 770502a

Please sign in to comment.