Here’s an example. Suppose we need to implement a product sale specification list that includes selectable shard numbers, shard capacity, and replica numbers, as shown in the image below (non-production code, for example learning only):

    Try to use JSON for complex types for storage, facilitating automatic conversion to objects upon scanning, avoiding custom parsing - 图1

    Our table design is as follows:

    1. CREATE TABLE `sell_spec` (
    2. `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Primary Key',
    3. `product` varchar(45) NOT NULL COMMENT 'Product Name',
    4. `resources` json NOT NULL COMMENT 'Resource specifications (cpu:memory), for example: ["0:0.25", "0:1", "1:2"]',
    5. `disk_min` int(10) DEFAULT NULL COMMENT 'Minimum disk capacity',
    6. `disk_max` int(10) DEFAULT NULL COMMENT 'Maximum disk capacity',
    7. `disk_step` int(10) DEFAULT NULL COMMENT 'Disk increment size',
    8. `shards` json NOT NULL COMMENT 'Shard specifications, for example: [1,3,5,8,12,16,24,32,40,48,64,80,96,128]',
    9. `replicas` json NOT NULL COMMENT 'Replica specifications, for example: [1,2,3,4,5,6,7,8,9,12]',
    10. `created_at` datetime DEFAULT NULL COMMENT 'Creation Time',
    11. `updated_at` datetime DEFAULT NULL COMMENT 'Update Time',
    12. PRIMARY KEY (`id`)
    13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Sale Specification Configuration';

    We define resources, shards, replicas in json format to store custom lists of specifications for resources, shards, and replicas (non-sequentially). The go struct definition is as follows:

    1. // SellSpec is a data structure automatically generated by the GoFrame tool and maintained by the tool.
    2. type SellSpec struct {
    3. Id uint `description:"Primary Key"`
    4. Product string `description:"Product Name"`
    5. Resources string `description:"Resource specifications (cpu:memory), for example: [\"0:0.25\", \"0:1\", \"1:2\"]"`
    6. DiskMin int `description:"Minimum disk capacity"`
    7. DiskMax int `description:"Maximum disk capacity"`
    8. DiskStep int `description:"Disk increment size"`
    9. Shards string `description:"Shard specifications, for example: [1,3,5,8,12,16,24,32,40,48,64,80,96,128]"`
    10. Replicas string `description:"Replica specifications, for example: [1,2,3,4,5,6,7,8,9,12]"`
    11. CreatedAt *gtime.Time `description:"Creation Time"`
    12. UpdatedAt *gtime.Time `description:"Update Time"`
    13. }
    14. // SellSpecItem is a custom data structure extending the entity,
    15. // some fields such as Resources/Shards/Replicas are overridden to array types for automatic type conversion when operating with ORM.
    16. type SellSpecItem struct {
    17. entity.SellSpec
    18. Resources []string `dc:"Resource specifications"`
    19. Shards []int `dc:"Shard specifications"`
    20. Replicas []int `dc:"Replica specifications"`
    21. }

    In the program, we can write and query data records as follows.

    Data writing:

    1. _, err = dao.SellSpec.Ctx(ctx).Data(v1.SellSpecItem{
    2. SellSpec: entity.SellSpec{
    3. Product: "redis",
    4. DiskMin: 50,
    5. DiskMax: 1000,
    6. DiskStep: 10,
    7. },
    8. Resources: []string{"1:2", "2:4", "4:8"},
    9. Shards: []int{1, 3, 5, 8, 12, 16, 24, 32, 40, 48, 64, 80, 96, 128},
    10. Replicas: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 12},
    11. }).Insert()

    Data querying, the ORM component will automatically convert records in the data table to array type attributes corresponding to the go struct:

    1. var items []v1.SellSpecItem
    2. err = dao.SellSpec.Ctx(ctx).Scan(&items)