Norman: Scaffolder and Migration Manager for Norm

Build StatusNimble

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.

Quickstart

1. Install Norman with Nimble:

$ nimble install -y norman

2. Add Norman to your .nimble file:

requires "norman >= 2.1.0"

Usage

1. Create a blank Nimble package with nimble init. Choose package type "binary".

2. Run norman init inside the package directory:

$ norman init
Creating folders and files:
    migrations
    migrations/config.nims
    src/foo/models
    src/foo/db_backend.nim
    .env

3. 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

4. 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("")

5. Apply migrations with norman migrate:

$ norman migrate
Applying migrations:
    migrations/m1595536838_init_user.nim

This creates the table for your new model.

6. Generate a migration with norman generate:

$ norman generate -m "seed users"
Creating blank migration:
    migrations/m1595537495_seed_users.nim

7. 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

8. Apply the new migration:

$ norman migrate
Applying migrations:
    migrations/m1595537495_seed_users.nim

9. To undo the last applied migration, run norman undo:

$ norman undo

Undoing migration:
    migrations/m1595537495_seed_users.nim