sequelize-ui icon indicating copy to clipboard operation
sequelize-ui copied to clipboard

Default value feature

Open GreXLin85 opened this issue 3 years ago • 1 comments

Operations may be easier if the ability to assign default values ​​to fields in models is added.

GreXLin85 avatar Apr 05 '22 20:04 GreXLin85

I would love to expand support for default values for more data types! Currently we only have default support for UUID version for the UUID type and "now" for all date/time types.

I think adding/extending DataType options as follows would support to almost all use cases:

type BooleanOptions = { defaultValue: boolean | null }
type EnumOptions = { values: string[]; defaultValue: string | null }
type NumberOptions = { unsigned: boolean; defaultValue: number | null }
type StringTextOptions = { defaultValue: string | null }
type ArrayOptions = { arrayType: DataType; defaultEmptyArray: boolean }
type JsonOptions = { defaultEmptyObject: boolean }

For each of the data type groups we'd need the following tasks:

  1. Add or modify data type option type with appropriate default value field
  2. Add any validations validateField (valid integers and enums are the only ones I can think of right now)
  3. Update src/api/schema/implementations/localStorage/v1/schema.jtd.json to support persisting the default values into local storage.
    • The workflow for managing this schema/generating types isn't documented (yet), but you need to update the JTD schema from the referenced file and run npm run generate:jtd-schema:schema (pre-req: json-typedef-codegen)
  4. Update FieldFieldSet to support user input
  5. Update FieldView to support displaying field default configuration

What do you think? I see you're off to a great start in https://github.com/tomjschuster/sequelize-ui/pull/87. Would you be interested in continuing to work on this with something along the lines of what I've described here? If not, hopefully I can start chipping away at this in the near future.

tomjschuster avatar Jun 01 '22 04:06 tomjschuster

@GreXLin85 #110 implements type-specific defaults. If you want to take a look, you can try it out at https://default-values.sequelize-ui.pages.dev/ or you can look at the examples in the PR description.

For the JSON/Array types, I'm currently only supporting defaulting to empty arrays or objects since I figured those were the most common use cases and didn't think it would be worth the effort/complexity to deal with validating JSON input fields and generating the code in a nice way. This is similar to how for the UUID/Date types I only support the generated UUID type or defaulting to the current time (rather than dealing with date time parsing, etc.).

If you have any feedback, I'll leave that PR up for about a week before merging.

tomjschuster avatar Mar 08 '24 03:03 tomjschuster