Skip to content

Commit

Permalink
expose DB credentials
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <georgelemon@protonmail.com>
  • Loading branch information
georgelemon committed Apr 13, 2024
1 parent 998c009 commit 43ccda1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/enimsql/meta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export Port, `$`, `%`

type
DBConnection = ref object
address, name, user, password: string
address*, name*, user*, password*: string
port: Port

DBConnections = OrderedTableRef[string, DBConnection]
Enimsql = ref object
## Database manager
dbs: DBConnections
maindb: DBConnection
maindb*: DBConnection

var DB*: Enimsql

Expand Down
32 changes: 6 additions & 26 deletions src/enimsql/private/query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ when not defined release:
proc `$`*(node: Node): string =
result = pretty(node.toJson, 2)

proc `$`*(x: SqlQuery): string = x.string

proc getTableName*(id: string): string =
add result, id[0].toLowerAscii
for c in id[1..^1]:
Expand All @@ -137,30 +139,6 @@ proc getDefaultValue*(col: SQLColumn): string =
## Returns default value of `sqlv`
if col.cDefault != nil:
result = col.cDefault.value
# case col.cDefault.dt
# of BigInt:
# $(col.cDefault.vBigInt)
# of BigSerial:
# $(col.cDefault.vBigSerial)
# of Bit:
# $(col.cDefault.vBit)
# of BitVarying:
# $(col.cDefault.vBitVarying)
# of Box:
# $(col.cDefault.vBox)
# of Bytea:
# $(col.cDefault.vBytea)
# of Char:
# $(col.cDefault.vChar)
# of Varchar:
# $(col.cDefault.vVarchar)
# of Boolean:
# $(col.cDefault.vBool)
# of Int:
# $(col.cDefault.vInt)
# of Text:
# "'" & $(col.cDefault.vText) & "'"
# else: ""

proc newCreateStmt*: Node =
result = Node(nt: ntCreate)
Expand Down Expand Up @@ -370,11 +348,13 @@ template checkConstraints(constr: openarray[Constraints], stmt: typed) {.dirty.}

template checkModelIdent(x) {.dirty.} =
if unlikely(models.hasKey(x)):
raise newException(EnimsqlModelDefect, "Duplicate model `" & x & "`")
raise newException(EnimsqlModelDefect,
"Duplicate model `" & x & "`")

template checkModelExists(x) {.dirty.} =
if unlikely(not models.hasKey(x)):
raise newException(EnimsqlModelDefect, "Unknown model `" & x & "`")
raise newException(EnimsqlModelDefect,
"Unknown model `" & x & "`")

proc create*(models: SchemaTable,
modelName: string, callbackBuilder: SchemaBuilder): SQLQuery =
Expand Down
60 changes: 50 additions & 10 deletions tests/test1.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
# This is just an example to get you started. You may wish to put all of your
# tests into a single file, or separate them into multiple `test1`, `test2`
# etc. files (better names are recommended, just make sure the name starts with
# the letter 't').
#
# To run these tests, simply execute `nimble test`.

import unittest
import db_connector/db_postgres

import enimsql

newModel Users:
id {.pk, notnull.}: Serial
name: Varchar(255)
email {.unique, notnull.}: Text
is_confirmed {.notnull.}: Boolean = false
is_muted {.notnull.}: Boolean = false
total_posts {.notnull, inc.}: Int = 0
total_replies {.notnull, inc.}: Int = 0
last_active: Date
last_published_reply: Date
last_published_topic: Date

# newModel Sessions:
# id {.pk, notnull.}: Serial
# author_id: Users.id
# user_ip: Int
# user_agent: Text
# payload: Text
# last_activity: Date

test "can init database":
initdb("georgelemon", "georgelemon", "")
assert DB.maindb.user == "georgelemon"
assert DB.maindb.name == "georgelemon"
assert DB.maindb.password.len == 0

test "runtime create table":
let q = Models.create("sessions") do(schema: Schema):
schema.add("id", Serial).primaryKey
schema.add("user_ip", Int)
schema.add("user_agent", Text)
schema.add("payload", Text)
schema.add("last_activity", Date)
echo q

test "runtime check tables":
try:
discard Models.create("sessions") do(schema: Schema):
schema.add("id", Serial).primaryKey
except EnimsqlModelDefect as e:
assert e.msg == "Duplicate model `sessions`"

import v2
test "can add":
check add(5, 5) == 10
try:
discard Models.create("users") do(schema: Schema):
schema.add("id", Serial).primaryKey
except EnimsqlModelDefect as e:
assert e.msg == "Duplicate model `users`"

0 comments on commit 43ccda1

Please sign in to comment.