Skip to content

Commit c3c49f0

Browse files
committed
Added key() method to database ref/DatabaseRef parent class
1 parent 55fc526 commit c3c49f0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/modules/base.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,20 @@ export class Base {
7777
export class ReferenceBase extends Base {
7878
constructor(firestack, path) {
7979
super(firestack);
80+
8081
this.path = Array.isArray(path) ?
8182
path :
8283
(typeof path == 'string' ?
8384
[path] : []);
85+
86+
// sanitize path, just in case
87+
this.path = this.path
88+
.filter(str => str !== "" );
89+
}
90+
91+
get key() {
92+
const path = this.path;
93+
return path.length === 0 ? '/' : path[path.length - 1];
8494
}
8595

8696
pathToString() {

lib/modules/database.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ class DatabaseRef extends ReferenceBase {
169169
push(value={}) {
170170
const path = this.dbPath();
171171
return promisify('push', FirestackDatabase)(path, value)
172-
.then(({ref}) => new DatabaseRef(this.db, ref))
172+
.then(({ref}) => {
173+
return new DatabaseRef(this.db, ref.split(separator))
174+
})
173175
}
174176

175177
on(evt, cb) {
@@ -247,7 +249,7 @@ class DatabaseRef extends ReferenceBase {
247249

248250
// attributes
249251
get fullPath() {
250-
return this.dbPath().path;
252+
return this.dbPath();
251253
}
252254

253255
get name() {

0 commit comments

Comments
 (0)