src/norm/model

Types

ConflictPolicy = enum
  cpRaise, cpIgnore, cpReplace
Model = ref object of RootObj
  id* {.pk, ro.}: int64

Base type for models.

id corresponds to row id in DB. Updated automatically, do not update manually!

Procs

proc checkRo(T: typedesc[Model])
Stop compilation if an object has ro pragma.
func col(T: typedesc[Model]; fld: string): string
Get column name for a Model field, which is just the field name.
func col[T: Model](obj: T; fld: string): string
Get column name for a Model instance field.
func cols[T: Model](obj: T; force = false): seq[string]

Get columns for Model instance.

If force is true, fields with ro are included.

func fCol(T: typedesc[Model]; fld, tAls: string): string
Get fully qualified column name with an alias instead of the actual table name: alias.col.
func fCol(T: typedesc[Model]; fld: string): string
Get fully qualified column name for a Model field: table.col.
func fCol[T: Model](obj: T; fld, tAls: string): string
Get fully qualified column name with an alias instead of the actual table name for a Model instance field.
func fCol[T: Model](obj: T; fld: string): string
Get fully qualified column name for a Model instance field.
proc getRelatedFieldNameTo[S: Model; T: Model](source: typedesc[S];
    target: typedesc[T]): string {.compileTime.}
A compile time proc that searches the given source Model type for any foreign key field that points to the table of the targetmodel type. Breaks at compile time if sourcedoes not have exactly one foreign key field to that table, as otherwise the desired field name to use can't be inferred.
func isModel[T: Model](val: Option[T]): bool
func isModel[T: Model](val: T): bool
func isModel[T](val: T): bool
func joinGroups[T: Model](obj: T; flds: seq[string] = @[]): seq[
    tuple[tbl, tAls, lFld, rFld: string]]
For each Model field of Model instance, get:
  • table name for the field type
  • full column name for the field
  • full column name for id field of the field type

Used to construct JOIN statements: JOIN {tbl} AS {tAls} ON {lFld} = {rFld}

func model[T: Model](val: Option[T]): Option[T]
func model[T: Model](val: T): Option[T]
func model[T](val: T): Option[Model]
This is never called and exists only to please the compiler.
func rfCols[T: Model](obj: T; flds: seq[string] = @[]): seq[string]
Recursively get fully qualified column names for Model instance and its Model fields.
func schema(T: typedesc[Model]): Option[string]

Get schema name for Model, which is the value of schemaName pragma.

none(string) means default schema.

Ignored in SQLite.

func table(T: typedesc[Model]): string

Get table name for Model, which is the value of tableName pragma or the type name in double quotes.

If schemaName is set, it's prepended to the table name.

proc validateFkField[S, T: Model](fkFieldName: static string;
                                  source: typedesc[S]; target: typedesc[T]): bool {.
    compileTime.}
Checks at compile time whether the field with the name fkFieldName is a valid foreign key field on the given source model to the table of the given target model. Specifically checks 1) if the field exists, 2) if it has either an fk pragma, or is a model type or an option of a model type, and 3) if the table associated with that field is equivalent to that of the table of the target model. If any of these conditions are false, this proc will intentionally fail to compile
proc validateJoinModelFkField[S, T: Model](fkFieldName: static string;
    joinModel: typedesc[S]; target: typedesc[T]): bool {.compileTime.}
Checks at compile time whether the field with the name fkFieldName is a valid foreign key field on the given joinModel model to the given target model. Ensures that the type in the field fkFieldName is target If it isn't the code won't compile as that Model type is required for a useful Many-To-Many query.