-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from aniravi24/add-support-for-multiple-databases
add support for multiple databases
- Loading branch information
Showing
33 changed files
with
1,755 additions
and
880 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"typefusion": patch | ||
--- | ||
|
||
Support multiple databases and change script configuration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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`). | ||
|
||
|
@@ -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. | ||
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
|
@@ -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
47
packages/typefusion/example/mysql/typefusion_all_mysql_types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
packages/typefusion/example/mysql/typefusion_mysql_result.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
packages/typefusion/example/options/typefusion_data_only.ts
This file was deleted.
Oops, something went wrong.
18 changes: 0 additions & 18 deletions
18
packages/typefusion/example/options/typefusion_no_types.ts
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
packages/typefusion/example/options/typefusion_pg_result.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.