papr
papr copied to clipboard
Feature Request: Dynamic Defaults
Hey, I was wondering if this would be something you'd be interested to have integrated:
import { schema, types } from 'papr';
const userSchema = schema({
active: types.boolean(),
age: types.number(),
firstName: types.string({ required: true }),
lastName: types.string({ required: true }),
abTestGroup: types.enum(["A", "B"]),
}, {
defaults: {
abTestGroup: () => Math.random > 0.5 ? "A" : "B"
}
});
Depending on your answer I would try to implement it.
Can you please elaborate with a different real world example for this feature?
I worry a bit about the performance impact this will have. Feel free to try it on a fork and report back some benchmarks.
@avaly An example would be the default Date.now
function. We use it in mongoose when you want a field to have the current date at the moment of creation. As I understand, this is not possible in Papr.
@BahaaZidan papr
supports two default timestamp attributes: createdAt
and updatedAt
. These attributes are pre-populated with the timestamp of the insert/update operation of the document. You can enable these attributes with the timestamp
option in the schema()
definition.
Unless there's a real-world use case of having dynamic defaults, I don't think this feature will get added in papr
.