Skip to content

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