Skip to content

Commit fbacee8

Browse files
committed
feat: implement PersistenceProvider#get in all providers
1 parent c1b99a9 commit fbacee8

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/persistence/FirestorePersistenceProvider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export class FirestorePersistenceProvider implements PersistenceProvider {
4040
return result;
4141
}
4242

43+
public async get(collectionName: string, recordName: string): Promise<PersistenceRecord> {
44+
return await this.getRecord(collectionName, recordName);
45+
}
46+
4347
public setDebugFn(debugFn: (msg: string) => void) {
4448
this.debugFn = debugFn;
4549
}

src/persistence/PersistenceProviderMock.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ export class PersistenceProviderMock implements PersistenceProvider {
2020
return result;
2121
}
2222

23+
public async get(collectionName: string, recordName: string): Promise<PersistenceRecord> {
24+
return await this.getRecord(collectionName, recordName);
25+
}
26+
2327
public setDebugFn(debugFn: (msg: string) => void) {
2428
//
2529
}

src/persistence/RealtimeDbPersistenceProvider.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ export class RealtimeDbPersistenceProvider implements PersistenceProvider {
2727
recordName: string,
2828
updaterFn: (record: PersistenceRecord) => PersistenceRecord,
2929
): Promise<PersistenceRecord> {
30-
const refName = `${collectionName}/${recordName}`;
30+
const ref = this.getDatabaseRef(collectionName, recordName);
3131

32-
const ref = this.database.ref(refName);
3332
const response = await ref.transaction(dataToUpdate => this.wrapUpdaterFn(updaterFn)(dataToUpdate));
3433
const { snapshot, committed } = response;
3534
if (!snapshot) throw new Error("RealtimeDbPersistenceProvider: realtime db didn't respond with data");
@@ -40,6 +39,15 @@ export class RealtimeDbPersistenceProvider implements PersistenceProvider {
4039
else return data as PersistenceRecord;
4140
}
4241

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+
4351
public setDebugFn(debugFn: (msg: string) => void) {
4452
this.debugFn = debugFn;
4553
}
@@ -58,6 +66,11 @@ export class RealtimeDbPersistenceProvider implements PersistenceProvider {
5866
};
5967
}
6068

69+
private getDatabaseRef(collectionName: string, recordName: string) {
70+
const refName = `${collectionName}/${recordName}`;
71+
return this.database.ref(refName);
72+
}
73+
6174
private createEmptyRecord(): PersistenceRecord {
6275
return {
6376
u: [],

src/types/RealtimeDbEquivalent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export namespace RealtimeDbEquivalent {
88
updateFn: (data: any) => any,
99
completeFn?: any,
1010
): Promise<{ committed: boolean; snapshot: DataSnapshot | null }>;
11+
once(eventType: "value"): Promise<DataSnapshot>;
1112
}
1213

1314
export interface DataSnapshot {

0 commit comments

Comments
 (0)