Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
b496b10578 | |||
c6430baa9a | |||
cd52c9860d | |||
af755a8a8d | |||
1f57e0dccc | |||
61e06842af | |||
934bb26cf3 | |||
126c4f1c7c | |||
56a257be2e | |||
35af299fdc |
@ -1,6 +1,6 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.1.0"
|
version = "0.2.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"
|
@ -2,7 +2,7 @@ import db_postgres, macros, options, sequtils, strutils, uuids
|
|||||||
|
|
||||||
from unicode import capitalize
|
from unicode import capitalize
|
||||||
|
|
||||||
import ./fiber_orm_nim/util
|
import ./fiber_orm/util
|
||||||
|
|
||||||
type NotFoundError* = object of CatchableError
|
type NotFoundError* = object of CatchableError
|
||||||
|
|
||||||
@ -50,8 +50,8 @@ template getRecord*(db: DbConn, modelType: type, id: typed): untyped =
|
|||||||
" FROM " & tableName(modelType) &
|
" FROM " & tableName(modelType) &
|
||||||
" WHERE id = ?"), @[$id])
|
" WHERE id = ?"), @[$id])
|
||||||
|
|
||||||
if row.allIt(it.len == 0):
|
if allIt(row, it.len == 0):
|
||||||
raise newException(NotFoundError, "no record for id " & $id)
|
raise newException(NotFoundError, "no " & modelName(modelType) & " record for id " & $id)
|
||||||
|
|
||||||
rowToModel(modelType, row)
|
rowToModel(modelType, row)
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ template findRecordsBy*(db: DbConn, modelType: type, lookups: seq[tuple[field: s
|
|||||||
lookups.mapIt(it.value))
|
lookups.mapIt(it.value))
|
||||||
.mapIt(rowToModel(modelType, it))
|
.mapIt(rowToModel(modelType, it))
|
||||||
|
|
||||||
macro generateProcsForModels*(modelTypes: openarray[type]): untyped =
|
macro generateProcsForModels*(dbType: type, modelTypes: openarray[type]): untyped =
|
||||||
result = newStmtList()
|
result = newStmtList()
|
||||||
|
|
||||||
for t in modelTypes:
|
for t in modelTypes:
|
||||||
@ -89,22 +89,22 @@ macro generateProcsForModels*(modelTypes: openarray[type]): untyped =
|
|||||||
let deleteName = ident("delete" & modelName)
|
let deleteName = ident("delete" & modelName)
|
||||||
let idType = typeOfColumn(t, "id")
|
let idType = typeOfColumn(t, "id")
|
||||||
result.add quote do:
|
result.add quote do:
|
||||||
proc `getName`*(db: PMApiDb, id: `idType`): `t` = getRecord(db.conn, `t`, id)
|
proc `getName`*(db: `dbType`, id: `idType`): `t` = getRecord(db.conn, `t`, id)
|
||||||
proc `getAllName`*(db: PMApiDb): seq[`t`] = getAllRecords(db.conn, `t`)
|
proc `getAllName`*(db: `dbType`): seq[`t`] = getAllRecords(db.conn, `t`)
|
||||||
proc `findWhereName`*(db: PMApiDb, whereClause: string, values: varargs[string, dbFormat]): seq[`t`] =
|
proc `findWhereName`*(db: `dbType`, whereClause: string, values: varargs[string, dbFormat]): seq[`t`] =
|
||||||
return findRecordsWhere(db.conn, `t`, whereClause, values)
|
return findRecordsWhere(db.conn, `t`, whereClause, values)
|
||||||
proc `createName`*(db: PMApiDb, rec: `t`): `t` = createRecord(db.conn, rec)
|
proc `createName`*(db: `dbType`, rec: `t`): `t` = createRecord(db.conn, rec)
|
||||||
proc `updateName`*(db: PMApiDb, rec: `t`): bool = updateRecord(db.conn, rec)
|
proc `updateName`*(db: `dbType`, rec: `t`): bool = updateRecord(db.conn, rec)
|
||||||
proc `deleteName`*(db: PMApiDb, rec: `t`): bool = deleteRecord(db.conn, rec)
|
proc `deleteName`*(db: `dbType`, rec: `t`): bool = deleteRecord(db.conn, rec)
|
||||||
proc `deleteName`*(db: PMApiDb, id: `idType`): bool = deleteRecord(db.conn, `t`, id)
|
proc `deleteName`*(db: `dbType`, id: `idType`): bool = deleteRecord(db.conn, `t`, id)
|
||||||
|
|
||||||
macro generateLookup*(modelType: type, fields: seq[string]): untyped =
|
macro generateLookup*(dbType: type, modelType: type, fields: seq[string]): untyped =
|
||||||
let fieldNames = fields[1].mapIt($it)
|
let fieldNames = fields[1].mapIt($it)
|
||||||
let procName = ident("find" & $modelType.getType[1] & "sBy" & fieldNames.mapIt(it.capitalize).join("And"))
|
let procName = ident("find" & pluralize($modelType.getType[1]) & "By" & fieldNames.mapIt(it.capitalize).join("And"))
|
||||||
|
|
||||||
# Create proc skeleton
|
# Create proc skeleton
|
||||||
result = quote do:
|
result = quote do:
|
||||||
proc `procName`*(db: PMApiDb): seq[`modelType`] =
|
proc `procName`*(db: `dbType`): seq[`modelType`] =
|
||||||
return findRecordsBy(db.conn, `modelType`)
|
return findRecordsBy(db.conn, `modelType`)
|
||||||
|
|
||||||
var callParams = quote do: @[]
|
var callParams = quote do: @[]
|
||||||
@ -120,7 +120,7 @@ macro generateLookup*(modelType: type, fields: seq[string]): untyped =
|
|||||||
|
|
||||||
result[6][0][0].add(callParams)
|
result[6][0][0].add(callParams)
|
||||||
|
|
||||||
macro generateProcsForFieldLookups*(modelsAndFields: openarray[tuple[t: type, fields: seq[string]]]): untyped =
|
macro generateProcsForFieldLookups*(dbType: type, modelsAndFields: openarray[tuple[t: type, fields: seq[string]]]): untyped =
|
||||||
result = newStmtList()
|
result = newStmtList()
|
||||||
|
|
||||||
for i in modelsAndFields:
|
for i in modelsAndFields:
|
||||||
@ -131,7 +131,7 @@ macro generateProcsForFieldLookups*(modelsAndFields: openarray[tuple[t: type, fi
|
|||||||
|
|
||||||
# Create proc skeleton
|
# Create proc skeleton
|
||||||
let procDefAST = quote do:
|
let procDefAST = quote do:
|
||||||
proc `procName`*(db: PMApiDb): seq[`modelType`] =
|
proc `procName`*(db: `dbType`): seq[`modelType`] =
|
||||||
return findRecordsBy(db.conn, `modelType`)
|
return findRecordsBy(db.conn, `modelType`)
|
||||||
|
|
||||||
var callParams = quote do: @[]
|
var callParams = quote do: @[]
|
@ -1,14 +1,16 @@
|
|||||||
import json, macros, options, sequtils, strutils, times, timeutils, unicode,
|
import json, macros, options, sequtils, strutils, times, timeutils, unicode,
|
||||||
uuids
|
uuids
|
||||||
|
|
||||||
|
import nre except toSeq
|
||||||
|
|
||||||
const UNDERSCORE_RUNE = "_".toRunes[0]
|
const UNDERSCORE_RUNE = "_".toRunes[0]
|
||||||
const PG_TIMESTAMP_FORMATS = [
|
const PG_TIMESTAMP_FORMATS = [
|
||||||
"yyyy-MM-dd HH:mm:sszz",
|
"yyyy-MM-dd HH:mm:sszz",
|
||||||
"yyyy-MM-dd HH:mm:ss'.'fzz",
|
|
||||||
"yyyy-MM-dd HH:mm:ss'.'ffzz",
|
|
||||||
"yyyy-MM-dd HH:mm:ss'.'fffzz"
|
"yyyy-MM-dd HH:mm:ss'.'fffzz"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
var PG_PARTIAL_FORMAT_REGEX = re"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.)(\d{1,3})(\S+)?"
|
||||||
|
|
||||||
type
|
type
|
||||||
MutateClauses* = object
|
MutateClauses* = object
|
||||||
columns*: seq[string]
|
columns*: seq[string]
|
||||||
@ -17,7 +19,7 @@ type
|
|||||||
|
|
||||||
# TODO: more complete implementation
|
# TODO: more complete implementation
|
||||||
# see https://github.com/blakeembrey/pluralize
|
# see https://github.com/blakeembrey/pluralize
|
||||||
proc pluralize(name: string): string =
|
proc pluralize*(name: string): string =
|
||||||
if name[^2..^1] == "ey": return name[0..^3] & "ies"
|
if name[^2..^1] == "ey": return name[0..^3] & "ies"
|
||||||
if name[^1] == 'y': return name[0..^2] & "ies"
|
if name[^1] == 'y': return name[0..^2] & "ies"
|
||||||
return name & "s"
|
return name & "s"
|
||||||
@ -69,9 +71,27 @@ type DbArrayParseState = enum
|
|||||||
|
|
||||||
proc parsePGDatetime*(val: string): DateTime =
|
proc parsePGDatetime*(val: string): DateTime =
|
||||||
var errStr = ""
|
var errStr = ""
|
||||||
|
|
||||||
|
# Try to parse directly using known format strings.
|
||||||
for df in PG_TIMESTAMP_FORMATS:
|
for df in PG_TIMESTAMP_FORMATS:
|
||||||
try: return val.parse(df)
|
try: return val.parse(df)
|
||||||
except: errStr &= "\n" & getCurrentExceptionMsg()
|
except: errStr &= "\n\t" & getCurrentExceptionMsg()
|
||||||
|
|
||||||
|
# PostgreSQL will truncate any trailing 0's in the millisecond value leading
|
||||||
|
# to values like `2020-01-01 16:42.3+00`. This cannot currently be parsed by
|
||||||
|
# the standard times format as it expects exactly three digits for
|
||||||
|
# millisecond values. So we have to detect this and pad out the millisecond
|
||||||
|
# value to 3 digits.
|
||||||
|
let match = val.match(PG_PARTIAL_FORMAT_REGEX)
|
||||||
|
if match.isSome:
|
||||||
|
let c = match.get.captures
|
||||||
|
try:
|
||||||
|
let corrected = c[0] & alignLeft(c[1], 3, '0') & c[2]
|
||||||
|
return corrected.parse(PG_TIMESTAMP_FORMATS[1])
|
||||||
|
except:
|
||||||
|
errStr &= "\n\t" & PG_TIMESTAMP_FORMATS[1] &
|
||||||
|
" after padding out milliseconds to full 3-digits"
|
||||||
|
|
||||||
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
|
raise newException(ValueError, "Cannot parse PG date. Tried:" & errStr)
|
||||||
|
|
||||||
proc parseDbArray*(val: string): seq[string] =
|
proc parseDbArray*(val: string): seq[string] =
|
||||||
@ -144,7 +164,11 @@ proc createParseStmt*(t, value: NimNode): NimNode =
|
|||||||
result = quote do: parsePGDatetime(`value`)
|
result = quote do: parsePGDatetime(`value`)
|
||||||
|
|
||||||
elif t.getTypeInst == Option.getType:
|
elif t.getTypeInst == Option.getType:
|
||||||
let innerType = t.getTypeImpl[2][0][0][1]
|
var innerType = t.getTypeImpl[2][0] # start at the first RecList
|
||||||
|
# If the value is a non-pointer type, there is another inner RecList
|
||||||
|
if innerType.kind == nnkRecList: innerType = innerType[0]
|
||||||
|
innerType = innerType[1] # now we can take the field type from the first symbol
|
||||||
|
|
||||||
let parseStmt = createParseStmt(innerType, value)
|
let parseStmt = createParseStmt(innerType, value)
|
||||||
result = quote do:
|
result = quote do:
|
||||||
if `value`.len == 0: none[`innerType`]()
|
if `value`.len == 0: none[`innerType`]()
|
||||||
@ -176,6 +200,10 @@ proc createParseStmt*(t, value: NimNode): NimNode =
|
|||||||
elif t.typeKind == ntyBool:
|
elif t.typeKind == ntyBool:
|
||||||
result = quote do: "true".startsWith(`value`.toLower)
|
result = quote do: "true".startsWith(`value`.toLower)
|
||||||
|
|
||||||
|
elif t.typeKind == ntyEnum:
|
||||||
|
let innerType = t.getTypeInst
|
||||||
|
result = quote do: parseEnum[`innerType`](`value`)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
error "Unknown value type: " & $t.typeKind
|
error "Unknown value type: " & $t.typeKind
|
||||||
|
|
||||||
@ -241,11 +269,13 @@ proc typeOfColumn*(modelType: NimNode, colName: string): NimNode =
|
|||||||
else: error "Unknown column type: " & $fieldType.getTypeInst
|
else: error "Unknown column type: " & $fieldType.getTypeInst
|
||||||
|
|
||||||
else: return fieldType
|
else: return fieldType
|
||||||
|
|
||||||
raise newException(Exception,
|
raise newException(Exception,
|
||||||
"model of type '" & $modelType & "' has no column named '" & colName & "'")
|
"model of type '" & $modelType & "' has no column named '" & colName & "'")
|
||||||
|
|
||||||
proc isZero(val: int): bool = return val == 0
|
proc isEmpty(val: int): bool = return val == 0
|
||||||
|
proc isEmpty(val: UUID): bool = return val.isZero
|
||||||
|
proc isEmpty(val: string): bool = return val.isNilOrWhitespace
|
||||||
|
|
||||||
macro populateMutateClauses*(t: typed, newRecord: bool, mc: var MutateClauses): untyped =
|
macro populateMutateClauses*(t: typed, newRecord: bool, mc: var MutateClauses): untyped =
|
||||||
|
|
||||||
@ -257,14 +287,14 @@ macro populateMutateClauses*(t: typed, newRecord: bool, mc: var MutateClauses):
|
|||||||
# grab the field, it's string name, and it's type
|
# grab the field, it's string name, and it's type
|
||||||
let fieldName = $fieldIdent
|
let fieldName = $fieldIdent
|
||||||
|
|
||||||
# we do not update the ID, but we do check: if we're creating a new
|
# We only add clauses for the ID field if we're creating a new record and
|
||||||
# record, we should not have an existing ID
|
# the caller provided a value..
|
||||||
if fieldName == "id":
|
if fieldName == "id":
|
||||||
result.add quote do:
|
result.add quote do:
|
||||||
if `newRecord` and not `t`.id.isZero:
|
if `newRecord` and not `t`.id.isEmpty:
|
||||||
raise newException(
|
`mc`.columns.add(identNameToDb(`fieldName`))
|
||||||
AssertionError,
|
`mc`.placeholders.add("?")
|
||||||
"Trying to create a new record, but the record already has an ID (" & $(`t`.id) & ").")
|
`mc`.values.add(dbFormat(`t`.`fieldIdent`))
|
||||||
|
|
||||||
# if we're looking at an optional field, add logic to check for presence
|
# if we're looking at an optional field, add logic to check for presence
|
||||||
elif fieldType.kind == nnkBracketExpr and
|
elif fieldType.kind == nnkBracketExpr and
|
||||||
@ -273,7 +303,7 @@ macro populateMutateClauses*(t: typed, newRecord: bool, mc: var MutateClauses):
|
|||||||
|
|
||||||
result.add quote do:
|
result.add quote do:
|
||||||
`mc`.columns.add(identNameToDb(`fieldName`))
|
`mc`.columns.add(identNameToDb(`fieldName`))
|
||||||
if `t`.`fieldIdent`.isSome:
|
if isSome(`t`.`fieldIdent`):
|
||||||
`mc`.placeholders.add("?")
|
`mc`.placeholders.add("?")
|
||||||
`mc`.values.add(dbFormat(`t`.`fieldIdent`.get))
|
`mc`.values.add(dbFormat(`t`.`fieldIdent`.get))
|
||||||
else:
|
else:
|
Reference in New Issue
Block a user