This is a guide about how I use Ormin as my project dependency.
Initialize the Project
Initialize your new project with Nimble:
$ nimble init <myproject>
Or, you can also make the directory first and run:
$ mkdir <myproject>
$ nimble init
After that, nimble init will interactively ask you about the metadata of your project, such as package type (library, binary, or hybrid), initial version, description, etc.
Initialize Ormin
Clone Ormin’s repository from GitHub:
$ git clone git@github:Araq/ormin.git
Or using HTTPS:
$ git clone https://github.com/Araq/ormin.git
(Optional, because we will override this later). Add Ormin as your project dependency on the <myproject>.nimble file:
$ nimble add ormin
While using Ormin, db_connector is still needed, so:
$ nimble add db_connector
Run nimble setup and Nimble will automatically create config.nims and nimble.paths for you.
Edit the nimble.paths and change the ormin path to this:
--noNimblePath
--path:"/home/yourusername/.nimble/pkgs2/db_connector-0.1.0-xxx"
--path:"ormin" # this one
Pro-Tip: Simplifying Paths You can safely delete
--noNimblePathand the hardcodeddb_connectorpath. By removing them, the Nim compiler will automatically find the correct versions of your dependencies in$HOME/.nimble/, keeping your configuration clean and portable.
The last step is to compile the ormin_importer.nim and copy the binary to either $HOME/.local/bin/ or /usr/local/bin/. I chose my $HOME local binary directory.
# inside your project root
$ cd ormin/tools
$ nim c ormin_importer.nim
$ cp ormin_importer $HOME/.local/bin/
$ cd ../.. # return to project root
Create the Database
Create your SQL file inside the src/ directory.
For example, this is my schema:
-- src/user_model.sql
create table if not exists users (
id integer primary key,
name varchar(20) not null
);
And then, run:
$ ormin_importer src/user_model.sql
This will create the .nim file equivalent of your SQL file.
This is how the project directory would look like:
myproject/
├── ormin/ <-- Cloned repo
├── src/
│ ├── myproject.nim <-- Main code
│ ├── user_model.sql <-- Schema
│ └── user_model.nim <-- Generated by ormin_importer
├── nimble.paths <-- Path config
└── myproject.nimble <-- Metadata
Now, open your project’s main entry; it would be inside src/<myproject>.nim and edit the file.
import ormin
import ormin/db_utils
from db_connector/db_sqlite import exec
importModel(DbBackend.sqlite, "user_model") # this loads the generated .nim file
let
db {.global.} = open("test.db", "", "", "")
srcDir = currentSourcePath.parentDir()
sqlFile = Path(srcDir / "user_model.sql")
insertUser = sql"insert into users(id, name) values (?, ?)"
db.droptable(sqlFile, "users")
db.createTable(sqlFile, "users")
db.exec(insertUser, 1, "rayfadh") # inserts id, and name
let users = query:
select users(name)
echo users # @["rayfadh"]
Now, run this inside your project root:
$ nimble run
Going Further
For more information, the test specs of Ormin are easy to read and understand, so if you need more examples, just take a look at the test specs.
So that’s how I use Ormin on my Nim project. Hope this helps!