Skip to content
Branch: develop
Clone or download
Latest commit ff612e9 Mar 21, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Update code sample. Mar 21, 2019
LICENSE Initial commit Sep 25, 2018
docker-compose.yml Add Dockerfile. Feb 27, 2019

Norm, a Nim ORM

Norm is a lightweight ORM written in Nim programming language. It enables you to store Nim's objects as DB rows and fetch data from DB as objects. So that your business logic is driven with objects, and the storage aspect is decoupled from it.

Norm supports SQLite and PostgreSQL.


Install Norm with Nimble:

$ nimble install norm


import norm / sqlite              # Import SQLite backend. Another option is ``norm / postgres``.
import logging                    # Import logging to inspect the generated SQL statements.
import unicode, sugar

db("petshop.db", "", "", ""):     # Set DB connection credentials.
  type                            # Describe object model in an ordinary type section.
    User = object
      age: int                    # Nim types are automatically converted into SQL types and back.
                                  # You can specify how types are converted using ``parser``,
                                  # ``formatter``, ``parseIt``, and ``formatIt`` pragmas.
      name {.
        formatIt: capitalize(it)  # Guarantee that ``name`` is stored in DB capitalized.
      .}: string

addHandler newConsoleLogger()

withDb:                           # Start a DB session.
  createTables(force=true)        # Create tables for all objects. Drop tables if they exist.

  var bob = User(                 # Create a ``User`` instance as you normally would.
    name: "bob",                  # Note that the instance is mutable. This is mandatory!
    age: 23
  bob.insert()                    # Insert ``bob`` into DB.
  dump                     # ``id`` attr is added by Norm and updated on insertion.

  var bobby = User(name: "bobby", age: 34)

  var alice = User(name: "alice", age: 12)

  let bobs = User.getMany(        # Read records from DB:
    100,                          # - only the first 100 records
    where="name LIKE 'Bob%'",     # - find by condition
    orderBy="age DESC"            # - order by age from oldest to youngest

  dump bobs

  var bob = User.getOne(1)        # Fetch record from DB and store it as ``User`` instance.
  bob.age += 10                   # Change attr value.
  bob.update()                    # Update the record in DB.

  bob.delete()                    # Delete the record.
  dump                     # ``id`` is 0 for objects not stored in DB.

  dropTables()                    # Drop all tables.


My goal with Norm was to lubricate the routine of working with DB: creating DB schema from the object model and converting data between DB and object representations. It's a tool for common cases not for all cases. Norm's builtin CRUD procs will help you write a typical RESTful API, but as your app grows more complex, you will have to write SQL queries manually (btw Norm can help with that too).

Using any ORM, Norm included, doesn't free a programmer from having to learn SQL!

You can’t perform that action at this time.