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"If you are using Nim 2.0 or later, you might need to enable deepcopy support in your project's nim.cfg or config.nims file:
deepcopy:on
- 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:
src/foo/models/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 =
User()⏬⏬⏬
import norm/model
type
User* = ref object of Model
email*: string
func newUser*(email: string): User =
User(email: email)
func newUser*: User =
newUser("")- Generate a migration for the new model with
norman generate:
$ norman generate -i user
Creating blank migration:
migrations/1595536838_init_user/migration.nim
Copying models:
src/foo/models
- Apply migrations with
norman migrate:
$ norman migrate
Applying migrations:
migrations/1595536838_init_user
This creates the table for your new model.
- Generate a migration with
norman generate:
$ norman generate -m "seed users"
Creating blank migration:
migrations/1595537495_seed_users/migration.nim
Copying models:
src/foo/models
- Edit the migration to actually insert rows into the DB:
include normanpkg/prelude
import foo/db_backend
migrate:
withDb:
db.transaction:
let qry = """ALTER TABLE "Table" ADD COLUMN column TYPE NOT NULL DEFAULT value"""
debug qry
db.exec sql qry
undo:
withDb:
db.transaction:
let qry = """ALTER TABLE "Table" DROP COLUMN column"""
debug qry
db.exec sql qry⏬⏬⏬
include normanpkg/prelude
import strutils
import sugar
import foo/db_backend
import models/user
migrate:
withDb:
db.transaction:
for i in 1..10:
discard newUser("user$#@example.com" % $i).dup:
db.insert
undo:
withDb:
db.transaction:
discard @[newUser()].dup:
db.select("1")
db.delete- Apply the new migration:
$ norman migrate
Applying migrations:
migrations/1595537495_seed_users
- To undo the last applied migration, run
norman undo:
$ norman undo
Undoing migration:
migrations/1595537495_seed_users
If you see this error, it is likely that you are using nimble setup (project isolation) but haven't re-run it after adding norman to your dependencies.
Solution: Run nimble setup again in your project root.
$ nimble setup
This updates nimble.paths to include norman, allowing migrations to find it.
If the error persists or you need to manually configure paths, ensure migrations/config.nims includes nimble.paths correctly:
when fileExists("$projectDir/../../nimble.paths"):
include "$projectDir/../../nimble.paths"And ensure the path to your source is correct:
switch("path", "$projectDir/../../src")If you are using Nim 2.0 or newer, you must enable deepcopy for migrations to work.
Add this line to your migrations/config.nims:
switch("deepcopy", "on")