Skip to content

Commit 5bb7cfe

Browse files
authored
Merge pull request #93 from malthe/node-test-runner
Use Node's built-in test runner
2 parents bd5b12e + 4051048 commit 5bb7cfe

24 files changed

Lines changed: 375 additions & 15906 deletions

.github/workflows/main.yml

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,32 @@ permissions:
1717
id-token: write
1818

1919
jobs:
20-
docs:
20+
build:
2121
runs-on: ubuntu-latest
2222
timeout-minutes: 1
2323
steps:
24+
- uses: actions/checkout@v4
25+
- name: NPM
26+
run: |
27+
set -ex
28+
npm i
29+
npm run prebuild
30+
npm run build:cjs
31+
npm run build:esm
32+
echo '{"type": "commonjs"}' > dist/commonjs/package.json
33+
echo '{"type": "module"}' > dist/module/package.json
34+
mkdir dest
35+
npm pack --pack-destination=dest
2436
- name: Docs
2537
run: npx typedoc
2638
- uses: actions/upload-pages-artifact@v2
2739
with:
2840
path: ./docs
41+
- name: Package artifact
42+
uses: actions/upload-artifact@v4
43+
with:
44+
name: pkg
45+
path: dest
2946
test:
3047
services:
3148
postgres:
@@ -42,11 +59,11 @@ jobs:
4259
--health-retries 5
4360
strategy:
4461
matrix:
45-
environment:
46-
- type: node
47-
version: 18.x
48-
- type: node
62+
version:
63+
- 18.x
64+
- 20.x
4965
runs-on: ubuntu-latest
66+
needs: [build]
5067
timeout-minutes: 3
5168
steps:
5269
- run: |
@@ -60,53 +77,43 @@ jobs:
6077
set full_page_writes off
6178
set synchronous_commit off
6279
docker kill --signal=SIGHUP ${{ job.services.postgres.id }}
63-
- uses: actions/checkout@v4
64-
- name: Use Node.js ${{ matrix.environment.version }}
65-
if: matrix.environment.type == 'node'
80+
- name: Use Node.js ${{ matrix.version }}
6681
uses: actions/setup-node@v3
6782
with:
68-
node-version: ${{ matrix.environment.version }}
83+
node-version: ${{ matrix.version }}
84+
- uses: actions/checkout@v4
6985
- name: Install dependencies
70-
if: matrix.environment.type == 'node'
71-
run: npm ci
86+
run: npm i
7287
- name: Run tests
73-
run: |
74-
case $ENVIRONMENT in
75-
node)
76-
npm run test:prod
77-
;;
78-
esac
88+
run: npm run test:prod
7989
env:
80-
ENVIRONMENT: ${{ matrix.environment.type }}
8190
PGSSLMODE: disable
8291
PGPORT: ${{ job.services.postgres.ports[5432] }}
8392
PGUSER: postgres
8493
PGPASSWORD: postgres
94+
- name: Extract SSL cert
95+
run: docker cp ${{ job.services.postgres.id }}:/etc/ssl/certs/ssl-cert-snakeoil.pem ./
8596
- name: Run tests (SSL)
97+
run: npm run test:prod
98+
env:
99+
NODE_EXTRA_CA_CERTS: ${{ github.workspace }}/ssl-cert-snakeoil.pem
100+
PGPORT: ${{ job.services.postgres.ports[5432] }}
101+
PGUSER: postgres
102+
PGPASSWORD: postgres
103+
- uses: actions/download-artifact@v4
104+
- name: "Example: Connect"
86105
run: |
87-
docker cp ${{ job.services.postgres.id }}:/etc/ssl/certs/ssl-cert-snakeoil.pem ./
88-
gen_code() {
89-
cat <<- EOF
90-
import { Client } from '$1';
91-
const client = new Client();
92-
const info = await client.connect();
93-
console.log("Encrypted: " + info.encrypted);
94-
await client.end();
95-
EOF
96-
}
106+
pkg_path=`ls $PWD/pkg/ts-postgres-*.tgz`
107+
cd examples/connect
108+
npm i . "$pkg_path"
97109
set -o pipefail
98-
case $ENVIRONMENT in
99-
node)
100-
npm run test:prod || exit 1
101-
npm run build
102-
gen_code ./dist/src/index.js > test.mjs
103-
node test.mjs | tee /dev/stderr | grep -q true
104-
;;
105-
esac
110+
PGSSLMODE=disable node main.cjs | tee /dev/stderr | grep -q false
111+
PGSSLMODE=require node main.cjs | tee /dev/stderr | grep -q true
112+
npx tsc
113+
PGSSLMODE=disable node main.mjs | tee /dev/stderr | grep -q false
114+
PGSSLMODE=require node main.mjs | tee /dev/stderr | grep -q true
106115
env:
107-
ENVIRONMENT: ${{ matrix.environment.type }}
108-
NODE_EXTRA_CA_CERTS: ssl-cert-snakeoil.pem
109-
PGSSLMODE: require
116+
NODE_EXTRA_CA_CERTS: ${{ github.workspace }}/ssl-cert-snakeoil.pem
110117
PGPORT: ${{ job.services.postgres.ports[5432] }}
111118
PGUSER: postgres
112119
PGPASSWORD: postgres
@@ -116,7 +123,7 @@ jobs:
116123
environment:
117124
name: github-pages
118125
url: ${{ steps.deployment.outputs.page_url }}
119-
needs: [test, docs]
126+
needs: [build, test]
120127
steps:
121128
- name: Deploy to GitHub Pages
122129
id: deployment

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
In the next release ...
22

3+
- Add support for ESM modules.
4+
5+
- The database host, port and connection timeout options can now be
6+
specified directly for `connect` (taking priority over the
7+
provided configuration).
8+
39
- Fix issue handling connection error during secure startup.
410

511
1.8.0 (2023-12-14)

README.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ Non-blocking PostgreSQL client for Node.js written in TypeScript.
99
To install the latest version of this library:
1010

1111
```sh
12-
$ npm install ts-postgres@latest
12+
$ npm install ts-postgres
1313
```
1414

1515
### Features
1616

1717
* Fast!
18-
* Supports both binary and text value formats
19-
* Result data is currently sent in binary format only
18+
* Supports binary and text value formats (result data always uses binary)
2019
* Multiple queries can be sent at once (pipeline)
2120
* Extensible value model
2221
* Hybrid query result object
2322
* Iterable (synchronous or asynchronous; one object at a time)
2423
* Rows and column names
2524
* Streaming data directly into a socket
25+
* Supports CommonJS and ESM modules
2626

2727
See the [documentation](https://malthe.github.io/ts-postgres/) for a complete reference.
2828

@@ -39,27 +39,23 @@ interface Greeting {
3939
message: string;
4040
}
4141

42-
async function main() {
43-
const client = new Client();
44-
await client.connect();
42+
const client = new Client();
43+
await client.connect();
4544

46-
try {
47-
// The query method is generic on the result row.
48-
const result = client.query<Greeting>(
49-
"SELECT 'Hello ' || $1 || '!' AS message",
50-
['world']
51-
);
45+
try {
46+
// The query method is generic on the result row.
47+
const result = client.query<Greeting>(
48+
"SELECT 'Hello ' || $1 || '!' AS message",
49+
['world']
50+
);
5251

53-
for await (const obj of result) {
54-
// 'Hello world!'
55-
console.log(obj.message);
56-
}
57-
} finally {
58-
await client.end();
52+
for await (const obj of result) {
53+
// 'Hello world!'
54+
console.log(obj.message);
5955
}
56+
} finally {
57+
await client.end();
6058
}
61-
62-
await main();
6359
```
6460
Waiting on the result (i.e., result iterator) returns the complete query result.
6561

examples/connect/main.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { Client } = require('ts-postgres');
2+
const client = new Client();
3+
module.exports = client.connect().then(
4+
(info) => {
5+
console.log("Encrypted: " + info.encrypted);
6+
return client.end();
7+
}
8+
);

examples/connect/main.mts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Client } from 'ts-postgres';
2+
const client = new Client();
3+
const info = await client.connect();
4+
console.log("Encrypted: " + info.encrypted);
5+
await client.end();

examples/connect/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"devDependencies": {
3+
"@types/node": "^20.0",
4+
"typescript": "^4.8"
5+
},
6+
"dependencies": {
7+
"ts-postgres": "^1.8"
8+
}
9+
}

examples/connect/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "Node16",
5+
"strict": true,
6+
"noImplicitAny": true
7+
}
8+
}

0 commit comments

Comments
 (0)