Saturday, 29 April 2017

storing data in firebase by id but ignoring duplicates

I have a concept of a show for an app that I'm building that looks like this:

{"band":"Jimmy Herring And The Invisible Whip","date":"2017-12-08T00:00:00-08:00","numberOfShows":"1 show","venue":"Warfield, S.F.","time":"7pm/8pm","soldOut":false,"pit":false,"multiDay":false,"ages":"a/a","price":"$100"}

each show has several attributes, including one band; several rows of shows can be the same event if the datetime and venue are the same, but each show is its own object.

I want to iterate through an array of shows and store bands on firebase in a namespace called artist.

This is my code for doing that:

        for (const parsedShow of parsedShows) {
            let artistref;
            let artist;
            artistref = this.db.ref(`artist`);
            artist = await artistref.push({ name: parsedShow.band });
        }

where the node.js function is getting passed an admin-enabled db connection as db

This stores data with a topology like

artist/{id}/[name: {name}]

which I assume will work fine if I need to populate a show namespace as well that has artist ids associated with a given show (and where id is auto-generated by firebase).

My question is, how do I prevent writes to my firebase db if a given band name is already in the database? I want to enforce unique bandnames only.

Also, is there a way to incorporate an id to be generated by firebase in a ref? I'd much rather have artist/name/{name: parsedShow.band, id: <some firebase-generated id>} if that's possible.



via fox

No comments:

Post a Comment