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

Support function type as the default schema option #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

unpin
Copy link
Contributor

@unpin unpin commented Aug 22, 2022

Checklist

  • Bugfix
  • New feature
  • Refactor

Related Issue

A function cannot be assigned to the default schema option, which makes it impossible to set dynamic values such as timestamps to a newly created document.

Solution

This PR provides the ability to assign a function to the default schema option. The function will be called and the returned value will be assigned to the document.

const userSchema = dango.schema({
  name: { type: 'string', required: true },
  createdAt: {
    type: 'date',
    default: () => Date.now(),
  },
});
const User = dango.model('user', userSchema);

const _id = await User.insertOne({ name: 'Jane' });
console.log(await User.findById(_id));
{
  name: "Jane",
  createdAt: 2022-08-22T10:52:30.699Z
}

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.

1 participant