- What does it do?
- How to specify schema?
- Relations
- How to run migrations
- How to run generator
- How to run clean
- Creates migrations using schema specified in YAML file
- Generates structs using specified schema
- Cleans up the names inside schema to match the rules
You can specify schema in YAML files. Here is an example of the schema file:
connection:
provider: postgresql
url: env("DATABASE_URL")
models:
- name: User
properties:
- name: id
type: int
default: autoincrement()
id: true
- name: email
type: string
unique: true
- name: username
type: string?
- name: isVerified
type: boolean
default: false
- name: userType
type: UserRole
- name: todos
type: Todo[]
- name: videos
type: UserToVideoRelation[]
- name: Todo
properties:
- name: id
type: string
default: uuid()
id: true
- name: title
type: string
- name: userId
type: int
- name: user
type: User
relationField: userId
referenceField: id
- name: note
type: Note
- name: Note
properties:
- name: id
type: string
default: uuid()
id: true
- name: text
type: string
- name: todo
type: Todo
relationField: id
referenceField: id
- name: UserToVideoRelation
properties:
- name: id
type: int
default: autoincrement()
id: true
- name: userId
type: int
- name: videoId
type: int
- name: user
type: User
relationField: userId
referenceField: id
- name: video
type: Video
relationField: videoId
referenceField: id
- name: Video
properties:
- name: id
type: int
default: autoincrement()
id: true
- name: title
type: string
- name: users
type: UserToVideoRelation[]
enums:
- name: UserRole
values:
- Admin
- User
Let's dive into some details:
-
-
- Here you can specify your db provider as well as connection string (url). postgresql is the only available option for now.
- Instead of specifying url explicitly you can use env("YOUR_ENV_VARIABLE_NAME") function to load env variable and use it as url.
-
- Should have name and url property
-
-
-
- Here you can specify models and properties that will correspond to them. For each of these models, new table will be created with name you specified in 'name' option.
-
- Should have name and properties property
- Should have 2 or more properties and one of them should have id property set to true
-
-
-
- Here you can specify enums with corresponding values, that will be created.
-
- Should have name and values property with 2 or more string values
-
-
-
- Here you can specify properties on model (columns in db)
-
- Should have name and type properties.
-
Type property should have one of these values
- int
- boolean
- float
- string
- dateTime
- Models defined in schema
- Enums defined in schema
- Arrays (T[])
- Nullable types (T?)
-
- id
- Defines if field is an id field or not (id field == primary key field)
- Default
- Defines default value which will be assigned to cell, when row will be created
-
Possible values
- int
- boolean
- float
- string
- dateTime
- Enums defined in schema
- now() function
- uuid() function
- autoincrement() function
- id
-
-
-
- Relations can be defined using relationField and referenceField properties
- relationField should be a field on model that you are defining relation on
- referenceField should be a field on model that you are referencing
- The referenced model should have a field with the type of the model that you are defining relation on
-
-
- One to many
-
- Should have relationField and referenceField properties
- relationField should be an array type
- referenceField should be a unique type
-
- One to one
-
- Should have relationField and referenceField properties
- relationField should be a unique type
- referenceField should be a unique type
-
- Many to many
-
- Separate linking model should be created, where you define 2 fields, that will reference models that you want to link
- Both fields should have relationField and referenceField properties
- Both fields should be an array type
- Linking table should have id field
- Example
models: - name: User properties: - name: id type: int default: autoincrement() id: true - name: email type: string unique: true - name: username type: string? - name: isVerified type: boolean default: false - name: videos type: UserToVideoRelation[] - name: UserToVideoRelation properties: - name: id type: int default: autoincrement() id: true - name: userId type: int - name: videoId type: int - name: user type: User relationField: userId referenceField: id - name: video type: Video relationField: videoId referenceField: id - name: Video properties: - name: id type: int default: autoincrement() id: true - name: title type: string - name: users type: UserToVideoRelation[]
-
- One to many
- Download latest executable from GitHub
- Run command in command line
./FOLDER_WHERE_EXECUTABLE_EXISTS/GoRelCli.exe migrate --path="./FOLDER_WHERE_GOREL_SCHEMA_EXISTS/gorel_schema.yml"
- Download latest executable from GitHub
- Run command in command line
./FOLDER_WHERE_EXECUTABLE_EXISTS/GoRelCli.exe generate --path="./FOLDER_WHERE_GOREL_SCHEMA_EXISTS/gorel_schema.yml" --project_path="./ROOT_PROJECT_FOLDER"
- Download latest executable from GitHub
- Run command in command line
./FOLDER_WHERE_EXECUTABLE_EXISTS/GoRelCli.exe clean --path="./FOLDER_WHERE_GOREL_SCHEMA_EXISTS/gorel_schema.yml"