Skip to content

Commit fc0a30d

Browse files
committed
👷‍♀️ Add CI build
1 parent 4b5b537 commit fc0a30d

File tree

7 files changed

+1719
-100
lines changed

7 files changed

+1719
-100
lines changed

.github/workflows/test.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- setup-ci # TODO: Remove
8+
pull_request:
9+
branches:
10+
- main
11+
12+
jobs:
13+
test:
14+
name: Node.js ${{ matrix.node }} + PostgreSQL ${{ matrix.postgres }}
15+
runs-on: ubuntu-latest
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
node:
20+
- 16
21+
- 18
22+
- 20
23+
postgres:
24+
- 13
25+
- 14
26+
- 15
27+
- 16
28+
services:
29+
postgres:
30+
image: postgres:${{ matrix.postgres }}
31+
options: >-
32+
--health-cmd pg_isready
33+
--health-interval 10s
34+
--health-timeout 5s
35+
--health-retries 5
36+
ports:
37+
- 5432:5432
38+
timeout-minutes: 10
39+
steps:
40+
- uses: actions/checkout@v4
41+
- uses: actions/setup-node@v4
42+
with:
43+
node-version: ${{ matrix.node }}
44+
- name: Install
45+
run: npm install
46+
- name: Test
47+
run: npm test

.mocharc.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
timeout: 5_000,
3+
bail: true, // TODO: Remove
4+
};

index.js

+30-31
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@ module.exports = PostgresDB;
1616

1717
PostgresDB.prototype = Object.create(DB.prototype);
1818

19-
PostgresDB.prototype.close = function(callback) {
20-
this.closed = true;
21-
this.pool.end();
22-
23-
if (callback) callback();
19+
PostgresDB.prototype.close = async function(callback) {
20+
let error;
21+
try {
22+
if (!this.closed) {
23+
this.closed = true;
24+
await this.pool.end();
25+
}
26+
} catch (err) {
27+
error = err;
28+
}
29+
30+
// FIXME: Don't swallow errors. Emit 'error' event?
31+
if (callback) callback(error);
2432
};
2533

2634

2735
// Persists an op and snapshot if it is for the next version. Calls back with
2836
// callback(err, succeeded)
29-
PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, callback) {
37+
PostgresDB.prototype.commit = async function(collection, id, op, snapshot, options, callback) {
38+
let client;
39+
try {
3040
/*
3141
* op: CreateOp {
3242
* src: '24545654654646',
@@ -37,29 +47,24 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
3747
* }
3848
* snapshot: PostgresSnapshot
3949
*/
40-
this.pool.connect((err, client, done) => {
41-
if (err) {
42-
done(client);
43-
callback(err);
44-
return;
45-
}
50+
client = await this.pool.connect();
4651
/*
47-
* This query uses common table expression to upsert the snapshot table
52+
* This query uses common table expression to upsert the snapshot table
4853
* (iff the new version is exactly 1 more than the latest table or if
4954
* the document id does not exists)
5055
*
51-
* It will then insert into the ops table if it is exactly 1 more than the
56+
* It will then insert into the ops table if it is exactly 1 more than the
5257
* latest table or it the first operation and iff the previous insert into
5358
* the snapshot table is successful.
5459
*
5560
* This result of this query the version of the newly inserted operation
5661
* If either the ops or the snapshot insert fails then 0 rows are returned
5762
*
58-
* If 0 zeros are return then the callback must return false
63+
* If 0 zeros are return then the callback must return false
5964
*
6065
* Casting is required as postgres thinks that collection and doc_id are
61-
* not varchar
62-
*/
66+
* not varchar
67+
*/
6368
const query = {
6469
name: 'sdb-commit-op-and-snap',
6570
text: `WITH snapshot_id AS (
@@ -95,20 +100,14 @@ WHERE (
95100
RETURNING version`,
96101
values: [collection,id,snapshot.v, snapshot.type, snapshot.data,op]
97102
}
98-
client.query(query, (err, res) => {
99-
if (err) {
100-
callback(err)
101-
} else if(res.rows.length === 0) {
102-
done(client);
103-
callback(null,false)
104-
}
105-
else {
106-
done(client);
107-
callback(null,true)
108-
}
109-
})
110-
111-
})
103+
const result = await client.query(query);
104+
const success = result.rowCount > 0;
105+
callback(null, success);
106+
} catch (error) {
107+
callback(error);
108+
} finally {
109+
if (client) client.release(true);
110+
}
112111
};
113112

114113
// Get the named document from the database. The callback is called with (err,

0 commit comments

Comments
 (0)