Skip to content

Commit f177ac0

Browse files
authored
Modify DeltaSnapshot constructor to only take in path, delta, and data (#117)
1 parent 1b631c3 commit f177ac0

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

spec/providers/database.spec.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,13 @@ describe('DeltaSnapshot', () => {
8989
const apps = new appsNamespace.Apps(fakeConfig());
9090

9191
let populate = (old: any, change: any) => {
92-
subject = new database.DeltaSnapshot(apps.admin, apps.admin, {
93-
resource: 'projects/_/instances/mySubdomain/refs/foo',
94-
data: {
95-
data: old,
96-
delta: change,
97-
},
98-
auth: {admin: false},
99-
});
92+
subject = new database.DeltaSnapshot(
93+
apps.admin,
94+
apps.admin,
95+
old,
96+
change,
97+
database.resourceToPath('projects/_/instances/mySubdomain/refs/foo')
98+
);
10099
};
101100

102101
describe('#val(): any', () => {
@@ -264,22 +263,24 @@ describe('DeltaSnapshot', () => {
264263
});
265264

266265
it('should return null for the root', () => {
267-
const snapshot = new database.DeltaSnapshot(apps.admin, apps.admin, {
268-
resource: 'projects/_/instances/foo/refs',
269-
data: {}},
266+
const snapshot = new database.DeltaSnapshot(
267+
apps.admin,
268+
apps.admin,
269+
null,
270+
null,
271+
database.resourceToPath('projects/_/instances/foo/refs')
270272
);
271273
expect(snapshot.key).to.be.null;
272274
});
273275

274276
it('should return null for explicit root', () => {
275-
expect(new database.DeltaSnapshot(apps.admin, apps.admin, {
276-
resource: 'projects/_/instances/foo/refs',
277-
data: {
278-
data: null,
279-
delta: {},
280-
},
281-
auth: {admin: false},
282-
}).key).to.be.null;
277+
expect(new database.DeltaSnapshot(
278+
apps.admin,
279+
apps.admin,
280+
null,
281+
{},
282+
database.resourceToPath('projects/_/instances/foo/refs')
283+
).key).to.be.null;
283284
});
284285

285286
it('should work for child paths', () => {

src/providers/database.ts

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ export class RefBuilder {
7878
if (raw.data instanceof DeltaSnapshot) {
7979
return raw.data;
8080
}
81-
return new DeltaSnapshot(this.apps.forMode(raw.auth), this.apps.admin, raw);
81+
return new DeltaSnapshot(
82+
this.apps.forMode(raw.auth),
83+
this.apps.admin,
84+
raw.data.data,
85+
raw.data.delta,
86+
resourceToPath(raw.resource),
87+
);
8288
};
8389
return makeCloudFunction({
8490
provider, handler,
@@ -96,6 +102,22 @@ export class RefBuilder {
96102
}
97103
}
98104

105+
/* Utility function to extract database reference from resource string */
106+
/** @internal */
107+
export function resourceToPath(resource) {
108+
let resourceRegex = `projects/([^/]+)/instances/([^/]+)/refs(/.+)?`;
109+
let match = resource.match(new RegExp(resourceRegex));
110+
if (!match) {
111+
throw new Error(`Unexpected resource string for Firebase Realtime Database event: ${resource}. ` +
112+
'Expected string in the format of "projects/_/instances/{firebaseioSubdomain}/refs/{ref=**}"');
113+
}
114+
let [, project, /* instance */ , path] = match;
115+
if (project !== '_') {
116+
throw new Error(`Expect project to be '_' in a Firebase Realtime Database event`);
117+
}
118+
return path;
119+
}
120+
99121
export class DeltaSnapshot implements firebase.database.DataSnapshot {
100122
private _adminRef: firebase.database.Reference;
101123
private _ref: firebase.database.Reference;
@@ -107,22 +129,17 @@ export class DeltaSnapshot implements firebase.database.DataSnapshot {
107129
private _childPath: string;
108130
private _isPrevious: boolean;
109131

110-
constructor(private app: firebase.app.App, private adminApp: firebase.app.App, event: Event<any>) {
111-
if (event) {
112-
let resourceRegex = `projects/([^/]+)/instances/([^/]+)/refs(/.+)?`;
113-
let match = event.resource.match(new RegExp(resourceRegex));
114-
if (!match) {
115-
throw new Error(`Unexpected resource string for Firebase Realtime Database event: ${event.resource}. ` +
116-
'Expected string in the format of "projects/_/instances/{firebaseioSubdomain}/refs/{ref=**}"');
117-
}
118-
let [, project, /* instance */ , ref] = match;
119-
if (project !== '_') {
120-
throw new Error(`Expect project to be '_' in a Firebase Realtime Database event`);
121-
}
122-
123-
this._path = normalizePath(ref);
124-
this._data = event.data.data;
125-
this._delta = event.data.delta;
132+
constructor(
133+
private app: firebase.app.App,
134+
private adminApp: firebase.app.App,
135+
data: any,
136+
delta: any,
137+
path?: string // path will be undefined for the database root
138+
) {
139+
if (delta !== undefined) {
140+
this._path = path;
141+
this._data = data;
142+
this._delta = delta;
126143
this._newData = applyChange(this._data, this._delta);
127144
}
128145
}
@@ -243,7 +260,7 @@ export class DeltaSnapshot implements firebase.database.DataSnapshot {
243260
}
244261

245262
private _dup(previous: boolean, childPath?: string): DeltaSnapshot {
246-
let dup = new DeltaSnapshot(this.app, this.adminApp, null);
263+
let dup = new DeltaSnapshot(this.app, this.adminApp, undefined, undefined);
247264
[dup._path, dup._data, dup._delta, dup._childPath, dup._newData] =
248265
[this._path, this._data, this._delta, this._childPath, this._newData];
249266

0 commit comments

Comments
 (0)