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

Default Values #249

Closed
MrSwitch opened this issue Oct 10, 2022 · 1 comment · Fixed by #251
Closed

Default Values #249

MrSwitch opened this issue Oct 10, 2022 · 1 comment · Fixed by #251
Assignees
Labels

Comments

@MrSwitch
Copy link
Member

MrSwitch commented Oct 10, 2022

A Default value would automatically add an insert value and filter condition when inserting and matching records respectively.

model.schema[ field ] = {
   default = Boolean | "String" | null | Number
}

How this differs from SQL Schema Field Attributes DEFAULT [value]

Defined on the dare model, rather than the DB Schema, so:

  • Two models can share the same DB table, but with different defaults.
  • Applies to queries in the filter and join conditions, not just INSERT | UPDATE (SET field = DEFAULT) operations.

Example

e.g. Below the active status on a members model is used to enforce the condition on queries

// Set active status on the model
models.members.schema.status = {
   default: 'active'
}

// Assign model to Dare
const dare = new Dare({models});

// Make a query...
await dare.get({
   table: 'members',
   fields: ['name'],
   limit: 100
});

/*
-- The generated SQL would include the filter condition...
SELECT name FROM members WHERE status = 'active'
*/

The condition would also be enforced on other operations

  • patch: WHERE ... AND status = 'active'
  • del: WHERE ... AND status = 'active'
    Whilst POST operation automatically appends it to the insert
  • post: INSERT (..., status) VALUES (...'active')

Overriding this behaviour per query

Simply set the value e.g.

// Set's the range of status values
await dare.get({
   table: 'members',
   fields: ['name'],
   filter: {status: ['active', 'disabled']}
   limit: 100
});

TODO: should we permit a value of undefined to remove the condition altogether? e.g. (status: undefined)

Defining per method default rules

When it's not appropriate to have the rule applied to a particular method (or at all). It can be set using an object where the properties pertain to the name of the method.

For example: Where we want to restrict the del operation of a member record with a 'disabled' state.

// Set active status on the model
models.members.schema.status = {
   default: {
       del: 'disabled',
       get: 'active',
       post: 'active',
      // patch: undefined
   }
}

The above settings would result in the following rules being applied depending on the operation.

  • get: WHERE ... AND status = 'active'
  • del: WHERE ... AND status = 'disabled'
    Whilst POST operation automatically appends it to the insert
  • post: INSERT (..., status) VALUES (...'active')
    Whilst PATCH operation would not be filtered at all
  • patch: WHERE ... // no condition added because it's not defined
@MrSwitch MrSwitch self-assigned this Oct 10, 2022
MrSwitch added a commit that referenced this issue Oct 18, 2022
* feat(defaultValues): support schema defaultValues in post, #249

* feat(defaultValues): support schema defaultValues in get/patch/del, #249

* docs(defaultValue): describe new fieldAttribute, #249
5app-Machine added a commit that referenced this issue Oct 18, 2022
# [0.68.0](v0.67.0...v0.68.0) (2022-10-18)

### Features

* **defaultValues:** support schema defaultValues in post, [#249](#249) ([#250](#250)) ([aba049a](aba049a))
MrSwitch added a commit that referenced this issue Oct 19, 2022
…handler

fix(defaultValue): defaultValue in validateInput handler, fixes #249
5app-Machine added a commit that referenced this issue Oct 19, 2022
## [0.68.1](v0.68.0...v0.68.1) (2022-10-19)

### Bug Fixes

* **defaultValue:** defaultValue in validateInput handler, fixes [#249](#249) ([392ecc8](392ecc8))
@5app-Machine
Copy link
Contributor

🎉 This issue has been resolved in version 0.68.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants