Simplify db code

This commit is contained in:
Zed 2019-09-08 12:22:52 +02:00
parent c7a2387aeb
commit d7e0fa6059
6 changed files with 21 additions and 15 deletions

View File

@ -11,7 +11,7 @@ bin = @["nitter"]
# Dependencies
requires "nim >= 0.19.9"
requires "norm >= 1.0.13"
requires "norm >= 1.0.15"
requires "jester >= 0.4.3"
requires "regex >= 0.11.2"
requires "q >= 0.0.7"

View File

@ -102,7 +102,7 @@ proc getVideoVar(tweet: Tweet): var Option[Video] =
return tweet.video
proc getVideo*(tweet: Tweet; agent, token: string; force=false) {.async.} =
withDb:
withCustomDb("cache.db", "", "", ""):
try:
getVideoVar(tweet) = some(Video.getOne("videoId = ?", tweet.id))
except KeyError:

View File

@ -1,7 +1,9 @@
import asyncdispatch, times
import asyncdispatch, times, strutils
import types, api
withCustomDb("cache.db", "", "", ""):
dbFromTypes("cache.db", "", "", "", [Profile, Video])
withDb:
try:
createTables()
except DbError:
@ -13,7 +15,7 @@ proc isOutdated*(profile: Profile): bool =
getTime() - profile.updated > profileCacheTime
proc cache*(profile: var Profile) =
withCustomDb("cache.db", "", "", ""):
withDb:
try:
let p = Profile.getOne("lower(username) = ?", toLower(profile.username))
profile.id = p.id
@ -23,7 +25,7 @@ proc cache*(profile: var Profile) =
profile.insert()
proc hasCachedProfile*(username: string): Option[Profile] =
withCustomDb("cache.db", "", "", ""):
withDb:
try:
let p = Profile.getOne("lower(username) = ?", toLower(username))
doAssert not p.isOutdated
@ -32,7 +34,7 @@ proc hasCachedProfile*(username: string): Option[Profile] =
result = none(Profile)
proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} =
withCustomDb("cache.db", "", "", ""):
withDb:
try:
result.getOne("lower(username) = ?", toLower(username))
doAssert not result.isOutdated

View File

@ -1,6 +1,5 @@
import sequtils, macros
import types
import prefs_impl
import strutils, sequtils, macros
import prefs_impl, types
export genUpdatePrefs
@ -15,14 +14,16 @@ static:
if missing.len > 0:
raiseAssert("{$1} missing from the Prefs type" % missing.join(", "))
withCustomDb("prefs.db", "", "", ""):
dbFromTypes("prefs.db", "", "", "", [Prefs])
withDb:
try:
createTables()
except DbError:
discard
proc cache*(prefs: var Prefs) =
withCustomDb("prefs.db", "", "", ""):
withDb:
try:
doAssert prefs.id != 0
discard Prefs.getOne("id = ?", prefs.id)
@ -33,7 +34,7 @@ proc cache*(prefs: var Prefs) =
proc getPrefs*(id: string): Prefs =
if id.len == 0: return genDefaultPrefs()
withCustomDb("prefs.db", "", "", ""):
withDb:
try:
result.getOne("id = ?", id)
except KeyError:

View File

@ -7,7 +7,7 @@ type
VideoType* = enum
vmap, m3u8, mp4
db("cache.db", "", "", ""):
dbTypes:
type
Profile* = object
username*: string
@ -62,6 +62,9 @@ db("cache.db", "", "", ""):
replaceYouTube*: string
replaceTwitter*: string
dbFromTypes("cache.db", "", "", "", [Profile, Video])
type
QueryKind* = enum
replies, media, multi, custom = "search"

View File

@ -1,4 +1,4 @@
import tables, macros, strformat, xmltree
import tables, macros, strformat, strutils, xmltree
import karax/[karaxdsl, vdom, vstyles]
import renderutils