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
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.
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](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 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.