Add an importer binary.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
module Config
|
||||
( parserOpts
|
||||
, AppConfig(..)
|
||||
, importerParserOpts
|
||||
, ImporterConfig(..)
|
||||
) where
|
||||
|
||||
import Options.Applicative
|
||||
@@ -43,3 +45,29 @@ parserOpts = info (appConfig <**> helper)
|
||||
( fullDesc
|
||||
<> progDesc "Serve Quotes API"
|
||||
<> header "quotes api" )
|
||||
|
||||
data ImporterConfig = ImporterConfig
|
||||
{ ioAppDbFile :: FilePath
|
||||
, ioReadwiseFile :: FilePath
|
||||
} deriving (Show, Eq)
|
||||
|
||||
importerConfig :: Parser ImporterConfig
|
||||
importerConfig = ImporterConfig
|
||||
<$> strOption
|
||||
( long "dbpath"
|
||||
<> help "sqlite db file path"
|
||||
<> showDefault
|
||||
<> value "quotes.db"
|
||||
<> metavar "TARGET")
|
||||
<*> strOption
|
||||
( long "readwise"
|
||||
<> help "readwise export file path"
|
||||
<> showDefault
|
||||
<> value "readwise.csv"
|
||||
<> metavar "RWCSV")
|
||||
|
||||
importerParserOpts :: ParserInfo ImporterConfig
|
||||
importerParserOpts = info (importerConfig <**> helper)
|
||||
( fullDesc
|
||||
<> progDesc "Import data into db"
|
||||
<> header "importer API")
|
||||
|
25
lib/Database.hs
Normal file
25
lib/Database.hs
Normal file
@@ -0,0 +1,25 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
|
||||
module Database where
|
||||
|
||||
import Database.SQLite.Simple.QQ
|
||||
import Database.SQLite.Simple
|
||||
|
||||
import Api.Types
|
||||
|
||||
initDb :: FilePath -> IO ()
|
||||
initDb dbFile = withConnection dbFile $ \conn ->
|
||||
execute_ conn
|
||||
[sql|CREATE TABLE IF NOT EXISTS quotes ( quote text non null
|
||||
, author text
|
||||
, title text
|
||||
, page text
|
||||
, chapter text
|
||||
, created_on integer);|]
|
||||
|
||||
insertQts :: FilePath -> [Quote] -> IO ()
|
||||
insertQts db qts = do
|
||||
withConnection db $ \c ->
|
||||
executeMany c qry qts
|
||||
where
|
||||
qry = [sql|INSERT INTO quotes VALUES (?,?,?,?,?,?);|]
|
Reference in New Issue
Block a user