One of the feedback items for Nexus was the ability to define models with Nim 
code, particularly Nim objects. Defining with Nim object types isn't ideal, 
because there's no validation or any way to provide defaults, so for the 
prototype code below I went with procs that help you define objects.

Any feedback on this method would be very helpful. The current way of doing 
this is by defining the models with YAML.
    
    
    import options
    import schema_types
    
    
    proc setupModes*(context: var MyContext) =
      
      contexts.models.add(
        newModel(
          name = "Account User",
          description = "This is to manage users in the system, by account.",
          tableOptions = newTableOptions(
            namingCase = snakeCase),
          fields = @[
            newField(
              name = "Id",
              fieldType = stringType,
              constraints = @[
                autoGenerateConstraint,
                notNullConstraint
              ],
              autoGenerate = some(ulidGenerator)),
            newField(
              name = "Account Id",
              fieldType = stringType,
              fkReference =
                some(newFkReference(
                       model = "Account",
                       refCol = "Id"))),
            newField(
              name = "Name",
              fieldType = stringType),
            newField(
              name = "Email",
              fieldType = stringType,
              constraints = @[
                notNullConstraint
              ])
          ],
          indexes = @[
            newIndex(
              name = some("Account User Ix 1"),
              columns = @[
                "Email"
              ]
            )
          ]))
    
    
    Run

Reply via email to