Skip to content
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

Allow merge in document updates #168

Open
tzvc opened this issue Jul 8, 2020 · 2 comments
Open

Allow merge in document updates #168

tzvc opened this issue Jul 8, 2020 · 2 comments
Labels
feature-request Feature Request help wanted Extra attention is needed

Comments

@tzvc
Copy link

tzvc commented Jul 8, 2020

Hi there,

I'm having troubles figuring out how to update a document without passing a complete object to the update() function.

Let's say I have a User model like so

// user_model.ts

@Collection(USERS_PUBLIC_COLLECTION)
export default class UserPublicModel {
  id!: string;
  display_name!: string;
  username!: string;
  pp!

later in my code I would like to be able to update lets say this user's display_name based only on its id.

// models
import UserPublicModel from '../models/user_public_model';

const repo = getBaseRepository(UserPublicModel);
const userId = "my_user_id";

repo.update({id: userId, display_name: "new display name"});

From the docs it seems like I need to fetch the document using findById(), update this record, and then pass that to update(). Is that the only way to do this ? It seems like a big limitation performancewise :/

Happy to help if needed :)

@sahil-23
Copy link

This isn't just an issue performance wise but can also cause a data consistency issue when different fields of a single document are being updated concurrently. Consider a document like {id:'xxxx', a:1, b:2}. If say there are two calls in parallel, one which updates 'a' to 3 and another updating 'b' to 4. If each gets the data from firestore before either has updated the data, then updates would like -
Update a to 3 -> {id:'xxxx', a:3, b:2}
Update b to 4 -> {id:'xxxx', a:1, b:4}

Now whatever the order of updates, it would result in wrong final state. This can of course be handled by writing firestore rules which don't allow such updates by keeping an 'update_time' field along with data but it's an overhead. The easiest way would be to allow partial updates, like firestore does.

@wovalle wovalle added feature-request Feature Request help wanted Extra attention is needed labels Feb 20, 2021
@wovalle
Copy link
Owner

wovalle commented Feb 20, 2021

The ideal implementetation would add a flag at initialization updateStrategy: 'replace', 'merge' where replace would be the default and the repository update method would also allow a config object (as a second parameter) to allow local override of the setting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Feature Request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants