-
-
Notifications
You must be signed in to change notification settings - Fork 333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it easy to access the per item firebase refs. #180
Comments
mmh, this is interesting. Any situation when |
It's just easier access. Make the resulting user code much more readable IMHO. |
oh wait, you're asking to add a ref for every single element? That is too much. I will have to think about it. I like how practical it is but it introduces other problems |
would making it a function like: Help your concerns? |
@posva any updates? |
not yet, I'll look into this later |
@anachirino or it could be a getter with Object.defineProperty function createRecord (snapshot) {
var value = snapshot.val()
var res
if (isObject(value)) {
res = value
} else {
res = {}
Object.defineProperty(res, '.value', {
value: value
})
}
Object.defineProperty(res, '.key', {
value: _getKey(snapshot)
})
Object.defineProperty(res, '.ref', {
get: function () {
return _getRef(snapshot)
}
})
return res
} |
For the moment, this is now achievable with the serialize option (https://vuefire.vuejs.org/api/vuefire.html#options-serialize) |
I was able to achieve the result by doing this in my import { VueFire, VueFireAuth, globalFirestoreOptions, firestoreDefaultConverter } from 'vuefire'
globalFirestoreOptions.converter = {
// the default converter just returns the data: (data) => data
toFirestore: firestoreDefaultConverter.toFirestore,
fromFirestore: (snapshot, options) => {
const data = firestoreDefaultConverter.fromFirestore(snapshot, options)
// if the document doesn't exist, return null
if (!data) return null
// add anything custom to the returned object
// data.metadata = snapshot.metadata
data.ref = snapshot.ref
return data
},
} note the line: Sadly, it does not do the same for the referenced children which are loaded. Those end up not having a ref. UPDATE: I later noticed that this also get's pushed to the firestore upon update. So it's not read only and I don't know how I would make it read only 🙈. |
Use Object.defineProperty to make it non enumerable 😉 |
How does this work with TypeScript? I have tried this without any luck. globalFirestoreOptions.converter = {
// the default converter just returns the data: (data) => data
toFirestore: firestoreDefaultConverter.toFirestore,
fromFirestore: (snapshot, options) => {
const data = firestoreDefaultConverter.fromFirestore(snapshot, options);
// if the document doesn't exist, return null
if (!data) return null;
// add anything custom to the returned object
// Adding a ref (aka DocumentReference)
Object.defineProperty(data, "ref", {
value: snapshot.ref,
enumerable: false,
writable: true,
configurable: true,
});
return data as DocumentData & {
readonly id: string;
readonly ref: DocumentReference<DocumentData>;
};
},
}; The code works, because I can access For example, when I try to access |
Also I have noticed this does not work for When using |
For who is looking for the serialize option document, it is moved to here now. |
If you add
res['.ref'] = _getRef(snapshot)
to the createRecord function. Like:Then we can more easily access the firebase reference that is associated with a given item. The example in the readme could be changed from:
to:
The text was updated successfully, but these errors were encountered: