@@ -27,9 +27,8 @@ export class RealtimeDbPersistenceProvider implements PersistenceProvider {
27
27
recordName : string ,
28
28
updaterFn : ( record : PersistenceRecord ) => PersistenceRecord ,
29
29
) : Promise < PersistenceRecord > {
30
- const refName = ` ${ collectionName } / ${ recordName } ` ;
30
+ const ref = this . getDatabaseRef ( collectionName , recordName ) ;
31
31
32
- const ref = this . database . ref ( refName ) ;
33
32
const response = await ref . transaction ( dataToUpdate => this . wrapUpdaterFn ( updaterFn ) ( dataToUpdate ) ) ;
34
33
const { snapshot, committed } = response ;
35
34
if ( ! snapshot ) throw new Error ( "RealtimeDbPersistenceProvider: realtime db didn't respond with data" ) ;
@@ -40,6 +39,15 @@ export class RealtimeDbPersistenceProvider implements PersistenceProvider {
40
39
else return data as PersistenceRecord ;
41
40
}
42
41
42
+ public async get ( collectionName : string , recordName : string ) : Promise < PersistenceRecord > {
43
+ const snapshot = await this . getDatabaseRef ( collectionName , recordName ) . once ( "value" ) ;
44
+ if ( ! snapshot ) return this . createEmptyRecord ( ) ;
45
+
46
+ const data = snapshot . val ( ) ;
47
+ if ( data === null ) return this . createEmptyRecord ( ) ;
48
+ else return data as PersistenceRecord ;
49
+ }
50
+
43
51
public setDebugFn ( debugFn : ( msg : string ) => void ) {
44
52
this . debugFn = debugFn ;
45
53
}
@@ -58,6 +66,11 @@ export class RealtimeDbPersistenceProvider implements PersistenceProvider {
58
66
} ;
59
67
}
60
68
69
+ private getDatabaseRef ( collectionName : string , recordName : string ) {
70
+ const refName = `${ collectionName } /${ recordName } ` ;
71
+ return this . database . ref ( refName ) ;
72
+ }
73
+
61
74
private createEmptyRecord ( ) : PersistenceRecord {
62
75
return {
63
76
u : [ ] ,
0 commit comments