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

feat(insert): adds custom ID capabilities #223

Merged
merged 2 commits into from
Jan 2, 2023
Merged

Conversation

micheleriva
Copy link
Member

@micheleriva micheleriva commented Jan 2, 2023

Highly requested feature, will solve both #220 and #112.

By default, Lyra will check if a field id exists in the document we're trying to insert:

insert(db, {
  id: 'foo-0123', // Will use this ID
  user: 'mitch'
})

if the id field does not exist, it will generate a random one.

We can also override the default id field by passing a function to the insert configuration:

insert(db, {
  id: 'foo-0123',
  user: 'mitch'
}, {
  id: (doc /* the entire document */) => `${doc.user}.01`
})

in that case, the id will be mitch.01. The id configuration function can be either sync or async.

Copy link
Contributor

@ShogunPanda ShogunPanda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@vassbence
Copy link

In the second example is it required to have the id prop on the doc?

@micheleriva
Copy link
Member Author

In the second example is it required to have the id prop on the doc?

@vassbence nope, as it will get overwritten by the function 🙂 it's only needed if defined in the schema definition

@micheleriva micheleriva merged commit bf82b60 into main Jan 2, 2023
@micheleriva micheleriva deleted the feat/custom-id branch January 2, 2023 11:18
@Zombobot1
Copy link

Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants