Skip to content

Commit

Permalink
Merge pull request #16 from aniravi24/add-support-for-multiple-databases
Browse files Browse the repository at this point in the history
add support for multiple databases
  • Loading branch information
aniravi24 authored Sep 28, 2024
2 parents 593e97d + 7a24bb5 commit e357032
Show file tree
Hide file tree
Showing 33 changed files with 1,755 additions and 880 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-hotels-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"typefusion": patch
---

Support multiple databases and change script configuration
8 changes: 5 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: "Start setting up postgres container"
- name: "Start setting up database containers"
run: |
# start preparing the postgres container, if you run tests too soon, it will fail
docker run -d -p 5432:5432 -e POSTGRES_DB=typefusion -e POSTGRES_PASSWORD=password postgres:14-alpine
# start preparing the database containers, if you run tests before these start up, you may get an error
docker run -d -p 5432:5432 -e POSTGRES_DB=typefusion -e POSTGRES_PASSWORD=password postgres:alpine
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=typefusion mysql:latest
- name: Checkout repo
uses: actions/checkout@v4
Expand All @@ -39,6 +40,7 @@ jobs:
run: pnpm build && pnpm turbo --env-mode=loose lint test-ci typecheck
env:
PG_DATABASE_URL: postgres://postgres:password@localhost:5432/typefusion?sslmode=disable
MYSQL_DATABASE_URL: mysql://root:password@localhost:3306/typefusion

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
Expand Down
60 changes: 31 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

## Introduction

Typefusion allows you to run TypeScript scripts and materialize the results into a database (currently PostgreSQL only). It enables you to create complex workflows by allowing scripts to reference each other's results. Inspired by [Data Build Tool (DBT)](https://www.getdbt.com/).
Typefusion allows you to run TypeScript scripts and materialize the results into a database. It enables you to create complex workflows by allowing scripts to reference each other's results. Inspired by [Data Build Tool (DBT)](https://www.getdbt.com/).

## Key Features

- Execute TypeScript scripts and store results in PostgreSQL
- Execute TypeScript scripts and store results in your database
- Create complex workflows with script dependencies
- Type-safe references between scripts
- Flexible usage through CLI and library modes
Expand All @@ -49,10 +49,10 @@ To begin using Typefusion, follow these steps:
bun add typefusion
```

2. Configure your database connection using one of these methods:
2. Configure your database connection using one of these methods (PostgreSQL and MySQL are supported):

- Set a full connection string in the `PG_DATABASE_URL` environment variable.
- Set individual environment variables: `PGDATABASE`, `PGHOST`, `PGPORT`, `PGPASSWORD`, and `PGUSER`.
- Set a full connection string in the `PG_DATABASE_URL` or `MYSQL_DATABASE_URL` environment variable.
- Set individual environment variables: `PG_DATABASE`, `PG_HOST`, `PG_PORT`, `PG_PASSWORD`, and `PG_USER` (for postgres) or `MYSQL_DATABASE`, `MYSQL_HOST`, `MYSQL_PORT`, `MYSQL_PASSWORD`, and `MYSQL_USER` (for mysql).

3. Create a directory for your scripts (e.g., `workflows`).

Expand All @@ -65,33 +65,34 @@ To begin using Typefusion, follow these steps:
After following the above instructions, create a script file in the directory, for example, `main.ts`:

```ts
import { pgType, TypefusionPgResult } from "typefusion";
// or mySqlType for mysql
import { pgType, typefusionRef, TypefusionDbResult } from "typefusion";

export const mainSchema = {
const mainSchema = {
id: pgType.integer().notNull(),
name: pgType.text().notNull(),
age: pgType.integer().notNull(),
email: pgType.text().notNull(),
address: pgType.text().notNull(),
};

export default async function main(): Promise<
TypefusionPgResult<typeof mainSchema>
> {
console.log("running main");
return {
types: mainSchema,
data: [
export default {
name: "main",
schema: mainSchema,
resultDatabase: "postgresql",
run: async () => {
console.log("running main");
return [
{
id: 1,
name: "John Doe",
age: 30,
email: "[email protected]",
address: "123 Main St",
},
],
};
}
];
},
} satisfies TypefusionDbResult<typeof mainSchema>;
```

**Warning:** Typefusion is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and does not provide a CommonJS export.
Expand Down Expand Up @@ -143,27 +144,28 @@ This approach allows for more programmatic control and integration with your exi
Typefusion Refs enable you to reference the results of one script in another, facilitating the creation of complex workflows. Here's an example:

```ts
import { pgType, typefusionRef, TypefusionPgResult } from "typefusion";
// or mySqlType for mysql
import { pgType, typefusionRef, TypefusionDbResult } from "typefusion";
import main from "./main.js";

const smallSchema = {
small: pgType.text().notNull(),
};

export default async function typefusion_ref(): Promise<
TypefusionPgResult<typeof smallSchema>
> {
const result = await typefusionRef(main);
console.log("typefusion ref main result", result);
return {
types: smallSchema,
data: [
export default {
name: "typefusion_ref",
schema: smallSchema,
resultDatabase: "postgresql",
run: async () => {
const result = await typefusionRef(main);
console.log("typefusion ref main result", result);
return [
{
small: "smallString" as const,
},
],
};
}
];
},
} satisfies TypefusionDbResult<typeof smallSchema>;
```

For cases where you only need the table name without fetching the full data, use the `typefusionRefTableName` function:
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
"@commitlint/cli": "19.5.0",
"@commitlint/config-conventional": "19.5.0",
"@manypkg/cli": "0.21.4",
"@typescript-eslint/eslint-plugin": "7.18.0",
"@typescript-eslint/parser": "7.18.0",
"commitizen": "4.3.0",
"@typescript-eslint/eslint-plugin": "8.7.0",
"@typescript-eslint/parser": "8.7.0",
"commitizen": "4.3.1",
"cz-git": "1.9.4",
"eslint": "8.57.1",
"eslint": "9.11.1",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.30.0",
"eslint-plugin-simple-import-sort": "12.1.1",
Expand Down
22 changes: 11 additions & 11 deletions packages/typefusion/example/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pgType } from "../src/db/postgres/types.js";
import { TypefusionPgResult } from "../src/index.js";
import { TypefusionDbResult } from "../src/index.js";

export const mainSchema = {
id: pgType.integer().notNull(),
Expand All @@ -9,20 +9,20 @@ export const mainSchema = {
address: pgType.text().notNull(),
};

export default async function main(): Promise<
TypefusionPgResult<typeof mainSchema>
> {
console.log("MAIN IS RUN");
return {
types: mainSchema,
data: [
export default {
name: "main",
schema: mainSchema,
resultDatabase: "postgresql",
run: async () => {
console.log("MAIN IS RUN");
return [
{
id: 1,
name: "John Doe",
age: 30,
email: "[email protected]",
address: "123 Main St",
},
],
};
}
];
},
} satisfies TypefusionDbResult<typeof mainSchema>;
47 changes: 47 additions & 0 deletions packages/typefusion/example/mysql/typefusion_all_mysql_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { mySqlType, TypefusionDbResult } from "../../src/index.js";

const allMySqlTypes = {
text: mySqlType.text().notNull(),
int: mySqlType.int().notNull(),
boolean: mySqlType.boolean().notNull(),
date: mySqlType.date().notNull(),
dateTime: mySqlType.dateTime().notNull(),
bigint: mySqlType.bigint().notNull(),
smallint: mySqlType.smallint().notNull(),
float: mySqlType.float().notNull(),
double: mySqlType.double().notNull(),
decimal: mySqlType.decimal().notNull(),
char: mySqlType.char(10).notNull(),
varchar: mySqlType.varchar(50).notNull(),
time: mySqlType.time().notNull(),
json: mySqlType.json().notNull(),
binary: mySqlType.binary().notNull(),
};

export default {
name: "typefusion_all_mysql_types",
schema: allMySqlTypes,
resultDatabase: "mysql",
run: async () => {
console.log("TYPEFUSION ALL MYSQL TYPES IS RUN");
return [
{
text: "Sample text",
int: 42,
boolean: true,
date: new Date("2023-05-17"),
dateTime: new Date("2023-05-17T12:34:56"),
bigint: BigInt("9007199254740991"),
smallint: 32767,
float: 3.14,
double: 3.141592653589793,
decimal: 123.45,
char: "Fixed ",
varchar: "Variable length text",
time: "12:34:56",
json: { key: "value" },
binary: new Uint8Array([0x12]),
},
];
},
} satisfies TypefusionDbResult<typeof allMySqlTypes>;
25 changes: 25 additions & 0 deletions packages/typefusion/example/mysql/typefusion_mysql_result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
typefusionRef,
TypefusionDbResult,
mySqlType,
} from "../../src/index.js";
import main from "../main.js";

const smallSchema = {
small: mySqlType.text().notNull(),
};

export default {
name: "typefusion_mysql_result",
resultDatabase: "mysql",
schema: smallSchema,
run: async () => {
const result = await typefusionRef(main);
console.log("TYPEFUSION PG RESULT IS RUN", result);
return [
{
small: "smallString" as const,
},
];
},
} satisfies TypefusionDbResult<typeof smallSchema>;
19 changes: 0 additions & 19 deletions packages/typefusion/example/options/typefusion_data_only.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/typefusion/example/options/typefusion_no_types.ts

This file was deleted.

21 changes: 0 additions & 21 deletions packages/typefusion/example/options/typefusion_pg_result.ts

This file was deleted.

17 changes: 0 additions & 17 deletions packages/typefusion/example/options/typefusion_result.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pgType, TypefusionPgResult } from "../../src/index.js";
import { pgType, TypefusionDbResult } from "../../src/index.js";

const allPgTypes = {
text: pgType.text().notNull(),
Expand Down Expand Up @@ -38,13 +38,13 @@ const allPgTypes = {
bytea: pgType.bytea().notNull(),
};

export default async function typefusion_all_pg_types(): Promise<
TypefusionPgResult<typeof allPgTypes>
> {
console.log("TYPEFUSION ALL PG TYPES IS RUN");
return {
types: allPgTypes,
data: [
export default {
name: "typefusion_all_pg_types",
schema: allPgTypes,
resultDatabase: "postgresql",
run: async () => {
console.log("TYPEFUSION ALL PG TYPES IS RUN");
return [
{
text: "Sample text",
integer: 42,
Expand Down Expand Up @@ -82,6 +82,6 @@ export default async function typefusion_all_pg_types(): Promise<
xml: "<root><element>content</element></root>",
bytea: new Uint8Array([0x12, 0x34, 0x56, 0x78]),
},
],
};
}
];
},
} satisfies TypefusionDbResult<typeof allPgTypes>;
Loading

0 comments on commit e357032

Please sign in to comment.