kolam
kolam

Reputation: 741

Initialise SQLite Database in Yesod Scaffolding Site

I created a yesod project with yesod init dans chose SQLite database.

My question : How do we initialise the SQLite Database with initial entries in the Yesod Scaffoled Site at the launch? Where do we put our insert instructions in this Yesod Templace Site?

I'm talking about actually adding records to some existing tables.

The Persistence chapter in the Yesod book explains how to insert a line in the database in the Handler of a ressource, but it doesn't explain how to add database entries at the start of the Yesod site (when the Yesod site is launched using yesod devel for example).

This is probably not an efficient way to proceed, so any suggestions on the completion of this task would be helpful.

Thank you.

Upvotes: 2

Views: 351

Answers (1)

unfoldr
unfoldr

Reputation: 141

I've never done this before since in my usual use case the DB is used for persistence across runs.

However I think the most appropriate place to do this would be in the makeFoundation function inside the Application module

Thats where your DB resources (connection pool etc...) are initialised.

Theres a line that looks something like this:

-- Perform database migration using our application's logging settings.
runLoggingT
    (Database.Persist.runPool dbconf (runMigration migrateAll) p)
    (messageLoggerSource foundation logger)

That sets up your tables, its probably just after this that you want to add your records.

I think the link in your question needs to be corrected, but the Persistent chapter of the Yesod book that you referred to does have valid examples for inserting records in an IO () action, so it should be able to get you the rest of the way.

Hope this helped

Upvotes: 1

Related Questions