Skip to content

Commit

Permalink
Migrate to ESM (#478)
Browse files Browse the repository at this point in the history
* Migrates all packages to use ESM by default.
* Isolate runtime depedencies into @pgtyped/runtime module from @pgtyped/query.
* Migrate to a more performant worker pool (Piscina).
* Bundle a CommonJS version of @pgtyped/runtime for backward compatibility
with CommonJS users.
  • Loading branch information
adelsz authored Jan 26, 2023
1 parent 96f3380 commit e81ae5e
Show file tree
Hide file tree
Showing 92 changed files with 1,853 additions and 737 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['16', '18', '19']
node-version: ['16', '18']
name: Test (node ${{ matrix.node-version }}.x)
steps:
- uses: actions/checkout@v3
Expand Down
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ No need to map or translate your DB schema to TypeScript, PgTyped automatically
4. Useful parameter interpolation helpers for arrays and objects.
5. No need to define your DB schema in TypeScript, your running DB is the live source of type data.
6. Prevents SQL injections by not doing explicit parameter substitution. Instead, queries and parameters are sent separately to the DB driver, allowing parameter substitution to be safely done by the PostgreSQL server.
7. Native ESM support. Runtime dependencies are also provided as CommonJS.

### Documentation

Visit our new documentation page at [https://pgtyped.now.sh/](https://pgtyped.now.sh/)
Visit our documentation page at [https://pgtyped.vercel.app/](https://pgtyped.vercel.app/)

### Getting started

1. `npm install @pgtyped/cli @pgtyped/query typescript` (typescript is a required peer dependency for pgtyped)
2. Create a PgTyped `config.json` file.
3. Run `npx pgtyped -w -c config.json` to start PgTyped in watch mode.
1. `npm install -D @pgtyped/cli typescript` (typescript is a required peer dependency for pgtyped)
2. `npm install @pgtyped/runtime` (`@pgtyped/runtime` is the only required runtime dependency of pgtyped)
3. Create a PgTyped `config.json` file.
4. Run `npx pgtyped -w -c config.json` to start PgTyped in watch mode.

Refer to the [example app](./packages/example/README.md) for a preconfigured example.
More info on getting started can be found in the [Getting Started](https://pgtyped.vercel.app/docs/getting-started) page.
You can also refer to the [example app](./packages/example/README.md) for a preconfigured example.

### Example

Expand Down Expand Up @@ -101,7 +104,7 @@ main();

### Resources

1. [Configuring Pgtyped](https://pgtyped.vercel.app/docs/cli)
1. [Configuring pgTyped](https://pgtyped.vercel.app/docs/cli)
2. [Writing queries in SQL files](https://pgtyped.vercel.app/docs/sql-file-intro)
3. [Advanced queries and parameter expansions in SQL files](https://pgtyped.vercel.app/docs/sql-file)
4. [Writing queries in TS files](https://pgtyped.vercel.app/docs/ts-file-intro)
Expand Down
3 changes: 2 additions & 1 deletion docs-new/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ sidebar_label: Getting Started

### Installation

1. `npm install @pgtyped/cli @pgtyped/query typescript` (TS is a required peer dependency)
1. `npm install -D @pgtyped/cli typescript` (typescript is a required peer dependency for pgtyped)
2. `npm install @pgtyped/runtime` (runtime is the only required runtime dependency for pgtyped)
2. Create a PgTyped `config.json` file.
3. Run `npx pgtyped -w -c config.json` to start PgTyped in watch mode.

Expand Down
4 changes: 2 additions & 2 deletions docs-new/docs/ts-file-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ PgTyped supports inlined queries using the `sql` template literal.
To see how that works lets write some queries in `users/queries.ts`:

```ts title="users/queries.ts"
import { sql } from '@pgtyped/query';
import { sql } from '@pgtyped/runtime';
import { ISelectUserIdsQuery } from './queries.types.ts';

export const selectUserIds = sql<
Expand Down Expand Up @@ -45,7 +45,7 @@ export interface ISelectUserIdsResult {
We can now pass the `ISelectUserIdsQuery` as a generic parameter to our query in `users/queries.ts`:

```ts title="users/queries.ts"
import { sql } from '@pgtyped/query';
import { sql } from '@pgtyped/runtime';
import { ISelectUserIdsQuery } from './queries.types.ts';

export const selectUserIds = sql<
Expand Down
4 changes: 2 additions & 2 deletions docs-new/docs/ts-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PgTyped also supports parsing queries from TS files.
Such queries must be tagged with an `sql` template literal, like this:

```ts
import { sql } from '@pgtyped/query';
import { sql } from '@pgtyped/runtime';

const getUsersWithComments = sql`
SELECT u.* FROM users u
Expand All @@ -21,7 +21,7 @@ PgTyped will then scan your project for such `sql` tags and generate types for e
Once the type files have been generated you can import them to type your query:

```ts
import { sql } from '@pgtyped/query';
import { sql } from '@pgtyped/runtime';
import { IGetUsersWithCommentsQuery } from './sample.types';

const getUsersWithComments = sql<IGetUsersWithCommentsQuery>`
Expand Down
24 changes: 24 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { Config } from 'jest';

const config: Config = {
snapshotFormat: {
escapeString: true,
printBasicPrototype: true,
},
roots: ['src'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
useESM: true,
},
],
},
preset: 'ts-jest/presets/default-esm',
testRegex: '\\.test\\.tsx?$',
};

export default config;
Loading

1 comment on commit e81ae5e

@vercel
Copy link

@vercel vercel bot commented on e81ae5e Jan 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pgtyped – ./

pgtyped-adels.vercel.app
pgtyped-git-master-adels.vercel.app
pgtyped.vercel.app

Please sign in to comment.