Skip to content

Commit 955dbd8

Browse files
committed
Merge branch 'release'
2 parents a42f2dc + 45781a1 commit 955dbd8

File tree

5 files changed

+81
-27
lines changed

5 files changed

+81
-27
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## 3.0.0
2+
3+
Thanks to @billwashere, we upgraded to a more modern version of `pg` (4.5.1 ->
4+
7.4.1), which means that the way the database information is provided has
5+
changed. Previously, you could pass a URL to sharedb-postgres, like:
6+
7+
```js
8+
const db = require('sharedb-postgres')('postgres://localhost/mydb');
9+
```
10+
11+
This is no longer supported, and you must instead pass a config object:
12+
13+
```js
14+
const db = require('sharedb-postgres')({host: 'localhost', database: 'mydb'});
15+
```
16+
17+
See the [node-postgres](https://node-postgres.com/features/connecting)
18+
documentation for more details about what can be passed in the config object.
19+
Additionally, if no object is provided, `pg` will use the same environment
20+
variables as `libpq` (`PGUSER`, `PGHOST` and so on).

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,15 @@ PGUSER=dbuser PGPASSWORD=secretpassword PGHOST=database.server.com PGDATABASE=m
5151
Example using an object
5252

5353
```js
54-
var db = require('sharedb-postgres')({
55-
user: 'dbuser',
56-
host: 'database.server.com',
57-
database: 'mydb',
58-
password: 'secretpassword',
59-
port: 5433,
60-
});
54+
var db = require('sharedb-postgres')({host: 'localhost', database: 'mydb'});
6155
var backend = require('sharedb')({db: db})
6256
```
6357

6458
## Error codes
6559

6660
PostgreSQL errors are passed back directly.
61+
62+
## Changelog
63+
64+
Note that version 3.0.0 introduces breaking changes in how you specify
65+
connection parameters. See the [changelog](CHANGELOG.md) for more info.

index.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ function PostgresDB(options) {
1010
this.closed = false;
1111

1212
this.pg_config = options;
13-
this.pool = new pg.Pool(this.pg_config)
13+
this.pool = new pg.Pool(options);
1414
};
1515
module.exports = PostgresDB;
1616

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

1919
PostgresDB.prototype.close = function(callback) {
2020
this.closed = true;
21+
this.pool.end();
22+
2123
if (callback) callback();
2224
};
2325

@@ -41,30 +43,63 @@ PostgresDB.prototype.commit = function(collection, id, op, snapshot, options, ca
4143
callback(err);
4244
return;
4345
}
44-
/*const*/ var query = {
46+
/*
47+
* This query uses common table expression to upsert the snapshot table
48+
* (iff the new version is exactly 1 more than the latest table or if
49+
* the document id does not exists)
50+
*
51+
* It will then insert into the ops table if it is exactly 1 more than the
52+
* latest table or it the first operation and iff the previous insert into
53+
* the snapshot table is successful.
54+
*
55+
* This result of this query the version of the newly inserted operation
56+
* If either the ops or the snapshot insert fails then 0 rows are returned
57+
*
58+
* If 0 zeros are return then the callback must return false
59+
*
60+
* Casting is required as postgres thinks that collection and doc_id are
61+
* not varchar
62+
*/
63+
const query = {
4564
name: 'sdb-commit-op-and-snap',
46-
text: `With snaps as (
47-
Insert into snapshots (collection,doc_id,doc_type, version,data)
48-
Select n.* From ( select $1 c, $2 d, $4 t, $3::integer v, $5::jsonb daa)
49-
n
50-
where v = (select version+1 v from snapshots where collection = $1 and doc_id = $2 for update) or not exists (select 1 from snapshots where collection = $1 and doc_id = $2 for update)
51-
On conflict(collection, doc_id) do update set version = $3, data = $5 , doc_type = $4
52-
Returning version
53-
)
54-
Insert into ops (collection,doc_id, version,operation)
55-
Select n.* From ( select $1 c, $2 t, $3::integer v, $6::jsonb daa)
56-
n
57-
where (v = (select max(version)+1 v from ops where collection = $1 and doc_id = $2) or not exists (select 1 from ops where collection = $1 and doc_id = $2 for update)) and exists (select 1 from snaps)
58-
Returning version`,
65+
text: `WITH snapshot_id AS (
66+
INSERT INTO snapshots (collection, doc_id, doc_type, version, data)
67+
SELECT $1::varchar collection, $2::varchar doc_id, $4 doc_type, $3 v, $5 d
68+
WHERE $3 = (
69+
SELECT version+1 v
70+
FROM snapshots
71+
WHERE collection = $1 AND doc_id = $2
72+
FOR UPDATE
73+
) OR NOT EXISTS (
74+
SELECT 1
75+
FROM snapshots
76+
WHERE collection = $1 AND doc_id = $2
77+
FOR UPDATE
78+
)
79+
ON CONFLICT (collection, doc_id) DO UPDATE SET version = $3, data = $5, doc_type = $4
80+
RETURNING version
81+
)
82+
INSERT INTO ops (collection, doc_id, version, operation)
83+
SELECT $1::varchar collection, $2::varchar doc_id, $3 v, $6 operation
84+
WHERE (
85+
$3 = (
86+
SELECT max(version)+1
87+
FROM ops
88+
WHERE collection = $1 AND doc_id = $2
89+
) OR NOT EXISTS (
90+
SELECT 1
91+
FROM ops
92+
WHERE collection = $1 AND doc_id = $2
93+
)
94+
) AND EXISTS (SELECT 1 FROM snapshot_id)
95+
RETURNING version`,
5996
values: [collection,id,snapshot.v, snapshot.type, snapshot.data,op]
6097
}
6198
client.query(query, (err, res) => {
6299
if (err) {
63-
console.log(err.stack)
64100
callback(err)
65101
} else if(res.rows.length === 0) {
66102
done(client);
67-
console.log("Unable to commit, not the latest version")
68103
callback(null,false)
69104
}
70105
else {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sharedb-postgres",
3-
"version": "2.0.0",
3+
"version": "3.0.0",
44
"description": "PostgreSQL adapter for ShareDB",
55
"main": "index.js",
66
"scripts": {

structure.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CREATE TABLE IF NOT EXISTS ops (
22
collection character varying(255) not null,
33
doc_id character varying(255) not null,
44
version integer not null,
5-
operation json not null, -- {v:0, create:{...}} or {v:n, op:[...]}
5+
operation jsonb not null, -- {v:0, create:{...}} or {v:n, op:[...]}
66
PRIMARY KEY (collection, doc_id, version)
77
);
88

@@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS snapshots (
1111
doc_id character varying(255) not null,
1212
doc_type character varying(255) not null,
1313
version integer not null,
14-
data json not null,
14+
data jsonb not null,
1515
PRIMARY KEY (collection, doc_id)
1616
);
1717

0 commit comments

Comments
 (0)