-
Notifications
You must be signed in to change notification settings - Fork 76
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
How to create a new document with a reference? #118
Comments
I understand the problem but what is what you're trying to achieve? If you want If what you want is to stablish a 1:1 relationship between your entities I would start removing the Apart from that, I see that there might be a bug with the deserialization/serialization of the firestore entities when the values are class instances. I could recommend adding |
@wovalle I think that @lucasdidur is doing reference to: https://firebase.google.com/docs/firestore/data-model#references for me it's more easy can related documents with references that as subcollections. |
@lucasdidur @wovalle @nietzscheson |
Thanks! Didn't know about firestorm (and I'm mad because that name is awesome :D). I updated my roadmap (#1) to increase the compatibility with raw firestore documents. For now I'll close this issue. |
Would you like to consider re-opening this issue @wovalle ? |
Here's some documentation about searching by reference: https://fireorm.js.org/#/Read_Data?id=search-by-document-reference What other do you guys think are missing? |
@wovalle thank you! It would be really great if we could this: @Collection()
class Artist {
id: string;
name: string;
// some other useful props
}
@Collection()
class Band {
id: string;
@DocumentReference()
artist: Artist;
} Then this: getRepository(Band).create( {
id: 'fireorm rocks!',
artist: '/artists/wovalle' // Making a reference to a Collection ```artist``` to a document ```wovalle```
}); That way, when we create a new band document, getRepository(Band).find().forEach((band) => console.log(band.artist.name)); As you can see, it is really easy to access the artist of a particular band without having the need to go fetch the artist or making a subcollection. Let me know what you think and I will be happy to help you |
That API looks nice indeed! Some questions that are still unanswered: In your example artist was declared as It would look like this: const artist = new Artist()
artist.id = 'new-artist'
artist.name = "New Artist"
getRepository(Band).create({
id: 'fireorm rocks!',
artist,
}); What would happen if you try to save an
const artist = new Artist()
artist.id = 'new-artist'
artist.name = "New Artist"
getRepository(Band).create({
id: 'fireorm rocks!',
artist: setReference(artist),
}); |
Yes, I set the artist in the create method as a string. By adding the
I believe that fireorm should just let you or throw an error at best given that firestore allows you to set a reference to a non-existent document when the reference is of type string. It should be the developer's job to ensure he's doing a valid reference IMO. The reason I assigned the getRepository(Band).create( {
id: 'fireorm rocks!',
artist: '/artists/wovalle' // Making a reference to a Collection ```artist``` to a document ```wovalle```
}); is to be able to check to see if the
This way, we let the /* Create example with a string */
getRepository(Band).create({
id: 'fireorm rocks!',
artist: '/artists/wovalle' // I'm a string, the create method should do the insert normally!
// (And I'll still be able to appear as a firestore document reference data type)
});
/* Create example with an object */
getRepository(Band).create({
id: 'fireorm rocks!',
artist: {
id: 'new artist from a newly created band',
name: 'cisco'
} // In here, I'm an artist object, maybe fireorm can create me first and then create the band and
// automatically reference me! (And still be able to appear as a firestore document reference data type)
}); Let me know what you think 🙏🏻 |
Instead of forcing the user to declare all of the references @Collection()
class Band {
id: string;
@DocumentReference()
artist: Reference<Artist>;
} Given that the reference field must be decorated with And one problem I find here is that we're using plain objects as the parameter for Another problem is that as of today, there's no easy way to get the path (ref?) of a fireorm entity. To achive this something must be done. Maybe adding a |
@DocumentReference()
artist: Reference<Artist>; Looks really great! Couldn't agree more.
This is what the Google API NodeJS Firestore repository does to validate a path: export function validateResourcePath(
arg: string | number,
resourcePath: string
): void {
if (typeof resourcePath !== 'string' || resourcePath === '') {
throw new Error(
`${invalidArgumentMessage(
arg,
'resource path'
)} Path must be a non-empty string.`
);
}
if (resourcePath.indexOf('//') >= 0) {
throw new Error(
`${invalidArgumentMessage(
arg,
'resource path'
)} Paths must not contain //.`
);
} They just check if the path is a valid string and does not contains any double slash.
The return this.className(?) + '/' + this.id; // I actually don't know how you take the classname (or the collection name if overriden) of the entity lol
Yes! This link might be helpful as well |
About the validation: That's easy then 🙌 , In the future I imagine adding an option to prevent adding a relationship that doesn't exist yet. About getPath: every repository has access to the path |
Awesome!
You mean from this right (the casting)? I really don't know a clean solution for that |
Yeah I'll have to rethink the api. The only thing that occurs to me at the moment is to leave it as Something like: /* Create example with an object */
getRepository(Band).create({
id: 'fireorm rocks!',
artist: {
id: 'new artist from a newly created band',
name: 'cisco',
},
});
getRepository(Band).create(
{
id: 'fireorm rocks!',
},
{ refs: { artist: '/artists/wovalle' } }
); |
It could be an alternative. Right now, I'm looking at what Firebase Firestorm does to create a document reference when creating a new document. They have this: export default class Post extends Entity {
@documentRef({
name: 'author',
entity: Author, // we must define the entity class due to limitations in Typescript's reflection capabilities. Progress should be made on this issue in future releases.
})
author!: IDocumentRef<Author>;
...
} So I'll look if they have a way of creating a document reference inside the |
any update on this? |
Hello, how can I create a new ProdutorDadosAdicionais with a referencied property produtor of Produtor class?
I tried this, but no gave me a error:
The text was updated successfully, but these errors were encountered: