fiber-orm-nim/src/fiber_orm.nim

328 lines
11 KiB
Nim
Raw Normal View History

2022-09-02 23:25:52 -05:00
# Fiber ORM
#
# Copyright 2019 Jonathan Bernard <jonathan@jdbernard.com>
## Lightweight ORM supporting the `Postgres`_ and `SQLite`_ databases in Nim.
## It supports a simple, opinionated model mapper to generate SQL queries based
## on Nim objects. It also includes a simple connection pooling implementation.
##
## .. _Postgres: https://nim-lang.org/docs/db_postgres.html
## .. _SQLite: https://nim-lang.org/docs/db_sqlite.html
##
## Basic Usage
## ===========
##
## Object-Relational Modeling
## ==========================
##
## Model Class
## -----------
##
## Table Name
## ``````````
##
## Column Names
## ````````````
##
## ID Field
## ````````
##
## Supported Data Types
## --------------------
##
## Database Object
## ===============
import std/db_postgres, std/macros, std/options, std/sequtils, std/strutils
import namespaced_logging, uuids
from std/unicode import capitalize
import ./fiber_orm/pool
import ./fiber_orm/util
export
pool,
util.columnNamesForModel,
util.dbFormat,
util.dbNameToIdent,
util.identNameToDb,
util.modelName,
util.rowToModel,
util.tableName
2022-09-02 23:25:52 -05:00
type NotFoundError* = object of CatchableError ##\
## Error type raised when no record matches a given ID
2022-01-22 20:20:02 -06:00
var logNs {.threadvar.}: LoggingNamespace
template log(): untyped =
if logNs.isNil: logNs = initLoggingNamespace(name = "fiber_orm", level = lvlNotice)
2022-01-22 20:20:02 -06:00
logNs
proc newMutateClauses(): MutateClauses =
return MutateClauses(
columns: @[],
placeholders: @[],
values: @[])
proc createRecord*[T](db: DbConn, rec: T): T =
2022-09-02 23:25:52 -05:00
## Create a new record. `rec` is expected to be a `model class`_. The `id
## field`_ is only set if it is `non-empty`_
##
## Returns the newly created record.
##
## .. _model class: #objectminusrelational-modeling-model-class
## .. _id field: #model-class-id-field
## .. _non-empty:
var mc = newMutateClauses()
populateMutateClauses(rec, true, mc)
let sqlStmt =
"INSERT INTO " & tableName(rec) &
" (" & mc.columns.join(",") & ") " &
" VALUES (" & mc.placeholders.join(",") & ") " &
" RETURNING " & columnNamesForModel(rec).join(",")
2022-01-22 20:20:02 -06:00
log().debug "createRecord: [" & sqlStmt & "]"
let newRow = db.getRow(sql(sqlStmt), mc.values)
result = rowToModel(T, newRow)
2021-04-02 13:58:08 -05:00
proc updateRecord*[T](db: DbConn, rec: T): bool =
2022-09-02 23:25:52 -05:00
## Update a record by id. `rec` is expected to be a `model class`_.
##
## .. _model class: #objectminusrelational-modeling-model-class
var mc = newMutateClauses()
populateMutateClauses(rec, false, mc)
2021-04-02 13:58:08 -05:00
let setClause = zip(mc.columns, mc.placeholders).mapIt(it[0] & " = " & it[1]).join(",")
let sqlStmt =
"UPDATE " & tableName(rec) &
" SET " & setClause &
" WHERE id = ? "
2022-01-22 20:20:02 -06:00
log().debug "updateRecord: [" & sqlStmt & "] id: " & $rec.id
let numRowsUpdated = db.execAffectedRows(sql(sqlStmt), mc.values.concat(@[$rec.id]))
return numRowsUpdated > 0;
template deleteRecord*(db: DbConn, modelType: type, id: typed): untyped =
2022-09-02 23:25:52 -05:00
## Delete a record by id.
let sqlStmt = "DELETE FROM " & tableName(modelType) & " WHERE id = ?"
2022-01-22 20:20:02 -06:00
log().debug "deleteRecord: [" & sqlStmt & "] id: " & $id
db.tryExec(sql(sqlStmt), $id)
proc deleteRecord*[T](db: DbConn, rec: T): bool =
2022-09-02 23:25:52 -05:00
## Delete a record by `id`_.
##
## .. _id: #model-class-id-field
let sqlStmt = "DELETE FROM " & tableName(rec) & " WHERE id = ?"
2022-01-22 20:20:02 -06:00
log().debug "deleteRecord: [" & sqlStmt & "] id: " & $rec.id
return db.tryExec(sql(sqlStmt), $rec.id)
template getRecord*(db: DbConn, modelType: type, id: typed): untyped =
2022-09-02 23:25:52 -05:00
## Fetch a record by id.
let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType) &
" WHERE id = ?"
2022-01-22 20:20:02 -06:00
log().debug "getRecord: [" & sqlStmt & "] id: " & $id
let row = db.getRow(sql(sqlStmt), @[$id])
if allIt(row, it.len == 0):
raise newException(NotFoundError, "no " & modelName(modelType) & " record for id " & $id)
rowToModel(modelType, row)
template findRecordsWhere*(db: DbConn, modelType: type, whereClause: string, values: varargs[string, dbFormat]): untyped =
2022-09-02 23:25:52 -05:00
## Find all records matching a given `WHERE` clause. The number of elements in
## the `values` array must match the number of placeholders (`?`) in the
## provided `WHERE` clause.
let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType) &
" WHERE " & whereClause
2022-01-22 20:20:02 -06:00
log().debug "findRecordsWhere: [" & sqlStmt & "] values: (" & values.join(", ") & ")"
db.getAllRows(sql(sqlStmt), values).mapIt(rowToModel(modelType, it))
template getAllRecords*(db: DbConn, modelType: type): untyped =
2022-09-02 23:25:52 -05:00
## Fetch all records of the given type.
let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType)
2022-01-22 20:20:02 -06:00
log().debug "getAllRecords: [" & sqlStmt & "]"
db.getAllRows(sql(sqlStmt)).mapIt(rowToModel(modelType, it))
template findRecordsBy*(db: DbConn, modelType: type, lookups: seq[tuple[field: string, value: string]]): untyped =
2022-09-02 23:25:52 -05:00
## Find all records matching the provided lookup values.
let sqlStmt =
"SELECT " & columnNamesForModel(modelType).join(",") &
" FROM " & tableName(modelType) &
" WHERE " & lookups.mapIt(it.field & " = ?").join(" AND ")
let values = lookups.mapIt(it.value)
2022-01-22 20:20:02 -06:00
log().debug "findRecordsBy: [" & sqlStmt & "] values (" & values.join(", ") & ")"
db.getAllRows(sql(sqlStmt), values).mapIt(rowToModel(modelType, it))
macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untyped =
2022-09-02 23:25:52 -05:00
## Generate all standard access procedures for the given model types. For a
## `model class`_ named `SampleRecord`, this will generate the following
## procedures:
##
## .. code-block:: Nim
## proc getSampleRecord*(db: dbType): SampleRecord;
## proc getAllSampleRecords*(db: dbType): SampleRecord;
## proc createSampleRecord*(db: dbType, rec: SampleRecord): SampleRecord;
## proc deleteSampleRecord*(db: dbType, rec: SampleRecord): bool;
## proc deleteSampleRecord*(db: dbType, id: idType): bool;
## proc updateSampleRecord*(db: dbType, rec: SampleRecord): bool;
##
## proc findSampleRecordsWhere*(
## db: dbType, whereClause: string, values: varargs[string]): SampleRecord;
##
## `dbType` is expected to be some type that has a defined `withConn`_
## procedure.
result = newStmtList()
for t in modelTypes:
let modelName = $(t.getType[1])
let getName = ident("get" & modelName)
let getAllName = ident("getAll" & modelName & "s")
let findWhereName = ident("find" & modelName & "sWhere")
let createName = ident("create" & modelName)
let updateName = ident("update" & modelName)
let deleteName = ident("delete" & modelName)
let idType = typeOfColumn(t, "id")
result.add quote do:
proc `getName`*(db: `dbType`, id: `idType`): `t` =
db.withConn: result = getRecord(conn, `t`, id)
proc `getAllName`*(db: `dbType`): seq[`t`] =
db.withConn: result = getAllRecords(conn, `t`)
proc `findWhereName`*(db: `dbType`, whereClause: string, values: varargs[string, dbFormat]): seq[`t`] =
db.withConn:
result = findRecordsWhere(conn, `t`, whereClause, values)
proc `createName`*(db: `dbType`, rec: `t`): `t` =
db.withConn: result = createRecord(conn, rec)
proc `updateName`*(db: `dbType`, rec: `t`): bool =
db.withConn: result = updateRecord(conn, rec)
proc `deleteName`*(db: `dbType`, rec: `t`): bool =
db.withConn: result = deleteRecord(conn, rec)
proc `deleteName`*(db: `dbType`, id: `idType`): bool =
db.withConn: result = deleteRecord(conn, `t`, id)
macro generateLookup*(dbType: type, modelType: type, fields: seq[string]): untyped =
2022-09-02 23:25:52 -05:00
## Create a lookup procedure for a given set of field names. For example,
##
## .. code-block:: Nim
## generateLookup(SampleDB, SampleRecord, ["name", "location"])
##
## will generate the following procedure:
##
## .. code-block:: Nim
## proc findSampleRecordsByNameAndLocation*(db: SampleDB,
let fieldNames = fields[1].mapIt($it)
let procName = ident("find" & pluralize($modelType.getType[1]) & "By" & fieldNames.mapIt(it.capitalize).join("And"))
# Create proc skeleton
result = quote do:
proc `procName`*(db: `dbType`): seq[`modelType`] =
db.withConn: result = findRecordsBy(conn, `modelType`)
var callParams = quote do: @[]
# Add dynamic parameters for the proc definition and inner proc call
for n in fieldNames:
let paramTuple = newNimNode(nnkPar)
paramTuple.add(newColonExpr(ident("field"), newLit(identNameToDb(n))))
paramTuple.add(newColonExpr(ident("value"), ident(n)))
# Add the parameter to the outer call (the generated proc)
# result[3] is ProcDef -> [3]: FormalParams
result[3].add(newIdentDefs(ident(n), ident("string")))
# Build up the AST for the inner procedure call
callParams[1].add(paramTuple)
# Add the call params to the inner procedure call
# result[6][0][1][0][1] is
# ProcDef -> [6]: StmtList (body) -> [0]: Call ->
# [1]: StmtList (withConn body) -> [0]: Asgn (result =) ->
# [1]: Call (inner findRecords invocation)
result[6][0][1][0][1].add(callParams)
macro generateProcsForFieldLookups*(dbType: type, modelsAndFields: openarray[tuple[t: type, fields: seq[string]]]): untyped =
result = newStmtList()
for i in modelsAndFields:
var modelType = i[1][0]
let fieldNames = i[1][1][1].mapIt($it)
let procName = ident("find" & $modelType & "sBy" & fieldNames.mapIt(it.capitalize).join("And"))
# Create proc skeleton
let procDefAST = quote do:
proc `procName`*(db: `dbType`): seq[`modelType`] =
db.withConn: result = findRecordsBy(conn, `modelType`)
var callParams = quote do: @[]
# Add dynamic parameters for the proc definition and inner proc call
for n in fieldNames:
let paramTuple = newNimNode(nnkPar)
paramTuple.add(newColonExpr(ident("field"), newLit(identNameToDb(n))))
paramTuple.add(newColonExpr(ident("value"), ident(n)))
procDefAST[3].add(newIdentDefs(ident(n), ident("string")))
callParams[1].add(paramTuple)
procDefAST[6][0][1][0][1].add(callParams)
result.add procDefAST
proc initPool*(
connect: proc(): DbConn,
poolSize = 10,
hardCap = false,
healthCheckQuery = "SELECT 'true' AS alive"): DbConnPool =
2022-09-02 23:25:52 -05:00
## Initialize a new DbConnPool.
##
## * `connect` must be a factory which creates a new `DbConn`
## * `poolSize` sets the desired capacity of the connection pool.
## * `hardCap` defaults to `false`.
##
## When `false`, the pool can grow beyond the configured capacity, but will
## release connections down to the its capacity (no less than `poolSize`).
##
## When `true` the pool will not create more than its configured capacity.
## It a connection is requested, none are free, and the pool is at
## capacity, this will result in an Error being raised.
## * `healthCheckQuery` should be a simple and fast SQL query that the pool
## can use to test the liveliness of pooled connections.
initDbConnPool(DbConnPoolConfig(
connect: connect,
poolSize: poolSize,
hardCap: hardCap,
healthCheckQuery: healthCheckQuery))
template inTransaction*(db: DbConnPool, body: untyped) =
pool.withConn(db):
conn.exec(sql"BEGIN TRANSACTION")
try:
body
conn.exec(sql"COMMIT")
except:
conn.exec(sql"ROLLBACK")
raise getCurrentException()