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: Support dynamic default values #527

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/api/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ The options are exported as a result type (the second value in the return tuple)

**Parameters:**

| Name | Type | Attribute |
| -------------------------- | ------------------------- | --------- |
| `properties` | `Record<string, unknown>` | required |
| `options` | `SchemaOptions` | optional |
| `options.defaults` | `Partial<TProperties>` | optional |
| `options.timestamps` | `TimestampSchemaOptions` | optional |
| `options.validationAction` | `VALIDATION_ACTIONS` | optional |
| `options.validationLevel` | `VALIDATION_LEVEL` | optional |
| Name | Type | Attribute |
| -------------------------- | ----------------------------- | --------- |
| `properties` | `Record<string, unknown>` | required |
| `options` | `SchemaOptions` | optional |
| `options.defaults` | `DefaultsOption<TProperties>` | optional |
| `options.timestamps` | `TimestampSchemaOptions` | optional |
| `options.validationAction` | `VALIDATION_ACTIONS` | optional |
| `options.validationLevel` | `VALIDATION_LEVEL` | optional |

**Returns:**

Expand Down
36 changes: 35 additions & 1 deletion docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ const exampleSchema = schema(

### Default Values

Papr does not support dynamic default values, but static default values can be used. Unlike Mongoose where default values are defined in the individual property options, Papr defines defaults in the schema options. An example of this can be seen below.
Unlike Mongoose where default values are defined in the individual property options, Papr defines defaults in the schema options.

Note: Default values are only applied to paths where no value is set at the time of insert.

#### Static Default Values

To set defaults you can supply an object in your schema with static values.

```js
import mongoose from 'mongoose';
Expand All @@ -113,6 +119,34 @@ const exampleSchema = schema(
);
```

#### Dynamic Default Values

Rather than supplying an object with your default values you can supply a function which will be executed at the time of insert and the returned values used as defaults.

```js
import mongoose from 'mongoose';
const { Schema } = mongoose;

const exampleSchema = new Schema({
birthday: { type: Date, default: Date.now, required: true },
});
```

```ts
import { schema, types } from 'papr';

const exampleSchema = schema(
{
birthday: types.date({ required: true }),
},
{
defaults: () => ({
birthday: new Date(),
}),
}
);
```

### Version Key

Mongoose automatically adds a `versionKey` to all of your schemas - you will need to either remove that value from your collections or include the matching key in your Papr schema when migrating. The default value for this property is `__v`, but may be changed in your Mongoose schema options. An example of this can be seen below.
Expand Down
Loading