Norman is a scaffolder and migration manager for Norm ORM.
Norman provides a CLI tool to manage migrations and a normanpkg/prelude module that helps writing migrations.
- Install Norman with Nimble:
$ nimble install -y norman
- Add Norman to your .nimble file:
requires "norman >= 2.1.0"- Create a blank Nimble package with
nimble init. Choose package type "binary". - Run
norman initinside the package directory:
$ norman init
Creating folders and files:
migrations
migrations/config.nims
src/foo/models
src/foo/db_backend.nim
.env
- Add your first model with
norman model:
$ norman model -n user
Creating blank model and init migration:
src/foo/models/user.nim
migrations/m1595536838_init_user.nim
- Open the model in your favorite editor and add fields to it:
import norm/model
type
User* = ref object of Model
func newUser*: User =
newUser()⏬⏬⏬
import norm/model
type
User* = ref object of Model
email*: string
func newUser*(email: string): User =
User(email: email)
func newUser*: User =
newUser("")- Apply migrations with
norman migrate:
$ norman migrate
Applying migrations:
migrations/m1595536838_init_user.nim
This creates the table for your new model.
- Generate a migration with
norman generate:
$ norman generate -m "seed users"
Creating blank migration:
migrations/m1595537495_seed_users.nim
- Edit the migration to actually insert rows into the DB:
include normanpkg/prelude
import foo/db_backend
migrate:
withDb:
discard "Your migration code goes here."
undo:
withDb:
discard "Your undo migration code goes here."⏬⏬⏬
include normanpkg/prelude
import strutils
import sugar
import foo/db_backend
import foo/models/user
migrate:
withDb:
for i in 1..10:
discard newUser("user$#@example.com" % $i).dup:
db.insert
undo:
withDb:
discard @[newUser()].dup:
db.select("1")
db.delete- Apply the new migration:
$ norman migrate
Applying migrations:
migrations/m1595537495_seed_users.nim
- To undo the last applied migration, run
norman undo:
$ norman undo
Undoing migration:
migrations/m1595537495_seed_users.nim