// Init initializes schema func (schema *Schema) Init() error { if schema.IsAbstract() { return nil } jsonSchema := schema.JSONSchema parent := schema.Parent required := util.MaybeStringList(jsonSchema["required"]) properties := util.MaybeMap(jsonSchema["properties"]) propertiesOrder := util.MaybeStringList(jsonSchema["propertiesOrder"]) if parent != "" && properties[FormatParentID(parent)] == nil { properties[FormatParentID(parent)] = getParentPropertyObj(parent, parent) propertiesOrder = append(propertiesOrder, FormatParentID(parent)) required = append(required, FormatParentID(parent)) } jsonSchema["required"] = required schema.JSONSchemaOnCreate = filterSchemaByPermission(jsonSchema, "create") schema.JSONSchemaOnUpdate = filterSchemaByPermission(jsonSchema, "update") schema.Properties = []Property{} for key := range properties { if !util.ContainsString(propertiesOrder, key) { propertiesOrder = append(propertiesOrder, key) } } jsonSchema["propertiesOrder"] = propertiesOrder for _, id := range propertiesOrder { property, ok := properties[id] if !ok { continue } propertyRequired := util.ContainsString(required, id) propertyObj, err := NewPropertyFromObj(id, property, propertyRequired) if err != nil { return fmt.Errorf("Invalid schema: Properties is missing %v", err) } schema.Properties = append(schema.Properties, *propertyObj) } return nil }
func fixEnumDefaultValue(node map[string]interface{}) { if defaultValue, ok := node["default"]; ok { if enums, ok := node["enum"]; ok { if defaultValueStr, ok := defaultValue.(string); ok { enumsArr := util.MaybeStringList(enums) if !util.ContainsString(enumsArr, defaultValueStr) { delete(node, "default") } } } } }
func removeNotSupportedFormat(node map[string]interface{}) { const formatProperty string = "format" var allowedFormats = []string{"uri", "uuid", "email", "int32", "int64", "float", "double", "byte", "binary", "date", "date-time", "password"} if format, ok := node[formatProperty]; ok { if format, ok := format.(string); ok { if !util.ContainsString(allowedFormats, format) { delete(node, formatProperty) } } } }
func (db *DB) genTableCols(s *schema.Schema, cascade bool, exclude []string) ([]string, []string) { var cols []string var relations []string schemaManager := schema.GetManager() for _, property := range s.Properties { if util.ContainsString(exclude, property.ID) { continue } handler := db.handlers[property.Type] dataType := property.SQLType if db.sqlType == "sqlite3" { dataType = strings.Replace(dataType, "auto_increment", "autoincrement", 1) } if dataType == "" { dataType = handler.dataType(&property) if property.ID == "id" { dataType += " primary key" } else { if property.Nullable { dataType += " null" } else { dataType += " not null" } if property.Unique { dataType += " unique" } } } sql := "`" + property.ID + "`" + dataType cols = append(cols, sql) if property.Relation != "" { foreignSchema, _ := schemaManager.Schema(property.Relation) if foreignSchema != nil { cascadeString := "" if cascade || property.OnDeleteCascade { cascadeString = "on delete cascade" } relations = append(relations, fmt.Sprintf("foreign key(`%s`) REFERENCES `%s`(id) %s", property.ID, foreignSchema.GetDbTableName(), cascadeString)) } } } return cols, relations }
func (db *DB) genTableCols(s *schema.Schema, cascade bool, exclude []string) ([]string, []string, []string) { var cols []string var relations []string var indices []string schemaManager := schema.GetManager() for _, property := range s.Properties { if util.ContainsString(exclude, property.ID) { continue } handler := db.handlers[property.Type] sqlDataType := property.SQLType sqlDataProperties := "" if db.sqlType == "sqlite3" { sqlDataType = strings.Replace(sqlDataType, "auto_increment", "autoincrement", 1) } if sqlDataType == "" { sqlDataType = handler.dataType(&property) if property.ID == "id" { sqlDataProperties = " primary key" } else { if property.Nullable { sqlDataProperties = " null" } else { sqlDataProperties = " not null" } if property.Unique { sqlDataProperties = " unique" } } } sql := "`" + property.ID + "` " + sqlDataType + sqlDataProperties cols = append(cols, sql) if property.Relation != "" { foreignSchema, _ := schemaManager.Schema(property.Relation) if foreignSchema != nil { cascadeString := "" if cascade || property.OnDeleteCascade { cascadeString = "on delete cascade" } relationColumn := "id" if property.RelationColumn != "" { relationColumn = property.RelationColumn } relations = append(relations, fmt.Sprintf("foreign key(`%s`) REFERENCES `%s`(%s) %s", property.ID, foreignSchema.GetDbTableName(), relationColumn, cascadeString)) } } if property.Indexed { prefix := "" if sqlDataType == "text" { prefix = "(255)" } indices = append(indices, fmt.Sprintf("CREATE INDEX %s_%s_idx ON `%s`(`%s`%s);", s.Plural, property.ID, s.Plural, property.ID, prefix)) } } return cols, relations, indices }