Default value feature
Operations may be easier if the ability to assign default values to fields in models is added.
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:
- Add or modify data type option type with appropriate default value field
- Add any validations
validateField(valid integers and enums are the only ones I can think of right now) - Update
src/api/schema/implementations/localStorage/v1/schema.jtd.jsonto 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)
- 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
- Update
FieldFieldSetto support user input - Update
FieldViewto 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.
@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.