@@ -10,6 +10,7 @@ class PostgresDB {
10
10
this . closed = false ;
11
11
12
12
this . pg_config = options ;
13
+
13
14
}
14
15
15
16
close ( callback ) {
@@ -36,77 +37,35 @@ class PostgresDB {
36
37
callback ( err ) ;
37
38
return ;
38
39
}
39
- function commit ( ) {
40
- client . query ( 'COMMIT' , err => {
41
- done ( err ) ;
42
- if ( err ) {
43
- callback ( err ) ;
44
- } else {
45
- callback ( null , true ) ;
46
- }
47
- } )
40
+ /*const*/ query = {
41
+ // TODO: investigate if ops should use on conflict
42
+ name : 'sdb-commit-op-and-snap' ,
43
+ text : `With snaps as (
44
+ Insert into snapshots (collection,doc_id,doc_type, version,data)
45
+ Select n.* From ( select $1 c, $2 d, $4 t, $3 v, $5 daa)
46
+ n
47
+ 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)
48
+ On conflict(collection, doc_id) do update set version = $3, data = $5 , doc_type = $2
49
+ Returning version
50
+ )
51
+ Insert into ops (collection,doc_id, version,operation)
52
+ Select n.* From ( select $1 c, $2 t, $3 v, $6 daa)
53
+ n
54
+ where (v = (select version+1 v from ops where collection = $1 and doc_id = $2 for update) or not exists (select 1 from ops where collection = $1 and doc_id = $2 for update)) and exists (select 1 from snaps)
55
+ On conflict(collection, doc_id, version) do update set version = $3, operation = $6
56
+ Returning version` ,
57
+ values : [ collection , id , snapshot . v , snapshot . type , snapshot . data , op ]
58
+ }
59
+ client . query ( query , ( err , res ) => {
60
+ if ( err ) {
61
+ console . log ( err . stack )
62
+ callback ( err )
63
+ } else {
64
+ console . log ( res . rows [ 0 ] )
65
+ callback ( null , true )
48
66
}
49
- client . query (
50
- 'SELECT max(version) AS max_version FROM ops WHERE collection = $1 AND doc_id = $2' ,
51
- [ collection , id ] ,
52
- ( err , res ) => {
53
- let max_version = res . rows [ 0 ] . max_version ;
54
- if ( max_version == null )
55
- max_version = 0 ;
56
- if ( snapshot . v !== max_version + 1 ) {
57
- return callback ( null , false ) ;
58
- }
59
- client . query ( 'BEGIN' , err => {
60
- client . query (
61
- 'INSERT INTO ops (collection, doc_id, version, operation) VALUES ($1, $2, $3, $4)' ,
62
- [ collection , id , snapshot . v , op ] ,
63
- ( err , res ) => {
64
- if ( err ) {
65
- // TODO: if err is "constraint violation", callback(null, false) instead
66
- rollback ( client , done ) ;
67
- callback ( err ) ;
68
- return ;
69
- }
70
- if ( snapshot . v === 1 ) {
71
- client . query (
72
- 'INSERT INTO snapshots (collection, doc_id, doc_type, version, data) VALUES ($1, $2, $3, $4, $5)' ,
73
- [ collection , id , snapshot . type , snapshot . v , snapshot . data ] ,
74
- ( err , res ) => {
75
- // TODO:
76
- // if the insert was successful and did insert, callback(null, true)
77
- // if the insert was successful and did not insert, callback(null, false)
78
- // if there was an error, rollback and callback(error)
79
- if ( err ) {
80
- rollback ( client , done ) ;
81
- callback ( err ) ;
82
- return ;
83
- }
84
- commit ( ) ;
85
- }
86
- )
87
- } else {
88
- client . query (
89
- 'UPDATE snapshots SET doc_type = $3, version = $4, data = $5 WHERE collection = $1 AND doc_id = $2 AND version = ($4 - 1)' ,
90
- [ collection , id , snapshot . type , snapshot . v , snapshot . data ] ,
91
- ( err , res ) => {
92
- // TODO:
93
- // if any rows were updated, success
94
- // if 0 rows were updated, rollback and not success
95
- // if error, rollback and not success
96
- if ( err ) {
97
- rollback ( client , done ) ;
98
- callback ( err ) ;
99
- return ;
100
- }
101
- commit ( ) ;
102
- }
103
- )
104
- }
105
- }
106
- )
107
- } )
108
- }
109
- )
67
+ } )
68
+
110
69
} )
111
70
}
112
71
@@ -188,16 +147,12 @@ class PostgresDB {
188
147
189
148
export default PostgresDB ;
190
149
191
- PostgresDB . prototype = Object . create ( DB . prototype ) ;
192
-
193
- function rollback ( client , done ) {
194
- client . query ( 'ROLLBACK' , err => done ( err ) )
195
- }
196
-
197
- function PostgresSnapshot ( id , version , type , data , meta ) {
198
- this . id = id ;
199
- this . v = version ;
200
- this . type = type ;
201
- this . data = data ;
202
- this . m = meta ;
150
+ class PostgresSnapshot {
151
+ constructor ( id , version , type , data , meta ) {
152
+ this . id = id ;
153
+ this . v = version ;
154
+ this . type = type ;
155
+ this . data = data ;
156
+ this . m = meta ;
157
+ }
203
158
}
0 commit comments