gdg-tangier / vue-firestore

:cloud: Cloud Firestore binding in realtime with Vuejs
MIT License
235 stars 28 forks source link

Select type of collection #20

Closed asankulov closed 5 years ago

asankulov commented 6 years ago

Is it possible to make selectable type of collection like array or object. Example from VueFire is below:

var db = firebaseApp.database()

var vm = new Vue({
  el: '#demo',
  firebase: {
    // simple syntax, bind as an array by default
    anArray: db.ref('url/to/my/collection'),
    // can also bind to a query
    // anArray: db.ref('url/to/my/collection').limitToLast(25)
    // full syntax
    anObject: {
      source: db.ref('url/to/my/object'),
      // optionally bind as an object
      asObject: true,
      // optionally provide the cancelCallback
      cancelCallback: function () {},
      // this is called once the data has been retrieved from firebase
      readyCallback: function () {}
    }
  }
})
amranidev commented 6 years ago

Hi @asankulov.

When you define your source in the firestore function, vue-firestore automatically binds this source as an array if it is a collection, otherwise vue-firestore will bind it as an object.

asankulov commented 6 years ago

May you explain about binding as object or give some example?

amranidev commented 6 years ago

Hello @asankulov.

Originally Vue-firestore binds collections as an array of documents (Array of objects). [{obj1}, {obj2}, {obj3}]

and binds documnets as a single object.

Example:

....
firestore() {
    return {
        cars: firestore.collection('cars'), // Array of documents.
        ford: firesotre.doc('cars/ford') // Single document.
    }
}
....

Results:

console.log(this.cars)
// [{.key: "mercedess", model: "2015"}, {.key: 'ADF783Sf1lz40zvVv1d2', model: "2001"}, ......]

console.log(this.ford)
// {.key: "ford", model: "2014"}

And bear in mind that each record we get form the firestore will be indecated with .key property.

Thank you :)

asankulov commented 6 years ago

Ok but I meant not single document I mean full collection. Like {'dx3fe4cdef54xsz970': {model: 'BMW'}, 'cr773qcr366crf244fc': {model: 'Nissan'} }

amranidev commented 6 years ago

yeah, I see, this will be a decent new feature to be implemented, Thanks a lot!

amranidev commented 5 years ago

@asankulov, binding collections as an object is available with the new release 0.3.1.

Thanks.