papr icon indicating copy to clipboard operation
papr copied to clipboard

Feature Request: Dynamic Defaults

Open despairblue opened this issue 3 years ago • 3 comments

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.

despairblue avatar Jun 24 '21 14:06 despairblue

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 avatar Jun 30 '21 14:06 avaly

@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 avatar May 25 '22 12:05 BahaaZidan

@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.

avaly avatar May 25 '22 12:05 avaly