Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
bb36bba864 | |||
f54bf6e974 |
@@ -1,6 +1,6 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "4.0.0"
|
version = "4.1.0"
|
||||||
author = "Jonathan Bernard"
|
author = "Jonathan Bernard"
|
||||||
description = "Lightweight Postgres ORM for Nim."
|
description = "Lightweight Postgres ORM for Nim."
|
||||||
license = "GPL-3.0"
|
license = "GPL-3.0"
|
||||||
|
@@ -334,7 +334,6 @@ proc createRecord*[D: DbConnType, T](db: D, rec: T): T =
|
|||||||
" RETURNING " & columnNamesForModel(rec).join(",")
|
" RETURNING " & columnNamesForModel(rec).join(",")
|
||||||
|
|
||||||
logQuery("createRecord", sqlStmt)
|
logQuery("createRecord", sqlStmt)
|
||||||
debug(getLogger("query"), %*{ "values": mc.values })
|
|
||||||
|
|
||||||
let newRow = db.getRow(sql(sqlStmt), mc.values)
|
let newRow = db.getRow(sql(sqlStmt), mc.values)
|
||||||
|
|
||||||
@@ -403,6 +402,19 @@ template getRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
|
|||||||
|
|
||||||
rowToModel(modelType, row)
|
rowToModel(modelType, row)
|
||||||
|
|
||||||
|
template tryGetRecord*[D: DbConnType](db: D, modelType: type, id: typed): untyped =
|
||||||
|
## Fetch a record by id.
|
||||||
|
let sqlStmt =
|
||||||
|
"SELECT " & columnNamesForModel(modelType).join(",") &
|
||||||
|
" FROM " & tableName(modelType) &
|
||||||
|
" WHERE id = ?"
|
||||||
|
|
||||||
|
logQuery("tryGetRecord", sqlStmt, [("id", $id)])
|
||||||
|
let row = db.getRow(sql(sqlStmt), @[$id])
|
||||||
|
|
||||||
|
if allIt(row, it.len == 0): none[modelType]()
|
||||||
|
else: some(rowToModel(modelType, row))
|
||||||
|
|
||||||
template findRecordsWhere*[D: DbConnType](
|
template findRecordsWhere*[D: DbConnType](
|
||||||
db: D,
|
db: D,
|
||||||
modelType: type,
|
modelType: type,
|
||||||
@@ -603,6 +615,7 @@ macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untype
|
|||||||
|
|
||||||
let modelName = $(t.getType[1])
|
let modelName = $(t.getType[1])
|
||||||
let getName = ident("get" & modelName)
|
let getName = ident("get" & modelName)
|
||||||
|
let tryGetName = ident("tryGet" & modelName)
|
||||||
let getIfExistsName = ident("get" & modelName & "IfItExists")
|
let getIfExistsName = ident("get" & modelName & "IfItExists")
|
||||||
let getAllName = ident("getAll" & pluralize(modelName))
|
let getAllName = ident("getAll" & pluralize(modelName))
|
||||||
let findWhereName = ident("find" & pluralize(modelName) & "Where")
|
let findWhereName = ident("find" & pluralize(modelName) & "Where")
|
||||||
@@ -615,6 +628,9 @@ macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untype
|
|||||||
proc `getName`*(db: `dbType`, id: `idType`): `t` =
|
proc `getName`*(db: `dbType`, id: `idType`): `t` =
|
||||||
db.withConnection conn: result = getRecord(conn, `t`, id)
|
db.withConnection conn: result = getRecord(conn, `t`, id)
|
||||||
|
|
||||||
|
proc `tryGetName`*(db: `dbType`, id: `idType`): Option[`t`] =
|
||||||
|
db.withConnection conn: result = tryGetRecord(conn, `t`, id)
|
||||||
|
|
||||||
proc `getIfExistsName`*(db: `dbType`, id: `idType`): Option[`t`] =
|
proc `getIfExistsName`*(db: `dbType`, id: `idType`): Option[`t`] =
|
||||||
db.withConnection conn:
|
db.withConnection conn:
|
||||||
try: result = some(getRecord(conn, `t`, id))
|
try: result = some(getRecord(conn, `t`, id))
|
||||||
|
@@ -256,6 +256,12 @@ func createParseStmt*(t, value: NimNode): NimNode =
|
|||||||
|
|
||||||
else: error "Cannot parse column with unknown generic instance type: " & $t.getTypeInst
|
else: error "Cannot parse column with unknown generic instance type: " & $t.getTypeInst
|
||||||
|
|
||||||
|
elif t.typeKind == ntyDistinct:
|
||||||
|
result = quote do:
|
||||||
|
block:
|
||||||
|
let tmp: `t` = `value`
|
||||||
|
tmp
|
||||||
|
|
||||||
elif t.typeKind == ntyRef:
|
elif t.typeKind == ntyRef:
|
||||||
|
|
||||||
if $t.getTypeInst == "JsonNode":
|
if $t.getTypeInst == "JsonNode":
|
||||||
|
Reference in New Issue
Block a user