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 # Dependencies
requires "nim >= 0.19.9" requires "nim >= 0.19.9"
requires "norm >= 1.0.13" requires "norm >= 1.0.15"
requires "jester >= 0.4.3" requires "jester >= 0.4.3"
requires "regex >= 0.11.2" requires "regex >= 0.11.2"
requires "q >= 0.0.7" requires "q >= 0.0.7"

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@ type
VideoType* = enum VideoType* = enum
vmap, m3u8, mp4 vmap, m3u8, mp4
db("cache.db", "", "", ""): dbTypes:
type type
Profile* = object Profile* = object
username*: string username*: string
@ -62,6 +62,9 @@ db("cache.db", "", "", ""):
replaceYouTube*: string replaceYouTube*: string
replaceTwitter*: string replaceTwitter*: string
dbFromTypes("cache.db", "", "", "", [Profile, Video])
type type
QueryKind* = enum QueryKind* = enum
replies, media, multi, custom = "search" 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 karax/[karaxdsl, vdom, vstyles]
import renderutils import renderutils