maze
  • Introduction
  • Getting Started
    • Quick Start
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params Validation
      • Cookies
      • Filters
      • Flash
      • Redirection
    • Views
      • View Helpers
    • Models
      • Granite
        • Granite's README
        • Migrations
        • Validations
        • Callbacks
        • Associations
        • Querying
        • Bulk Insertions
      • Crecto
        • Crecto's README
      • Jennifer
        • Jennifer Docs
        • Migrations
        • Models
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
      • Deliver a new Email
  • Testing
    • System Tests
  • Deployment
    • Digital Ocean
    • Heroku
  • CLI
    • New
    • Recipes
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
    • Deploy
  • Examples
    • Maze Auth
    • Crystal Debug
    • Minimal Configuration
  • Troubleshooting
  • Contributing
  • Code of Conduct
  • HAVE A QUESTION?
    • Ask on Gitter
    • Ask on StackOverflow
    • Follow on Twitter
    • Submit an issue
Powered by GitBook
On this page
  • What is Jennifer?
  • Installing Jennifer for Maze
  • Generate a normal maze app
  • Update your project shard.yml
  • Setup your database information
  • Create a jennifer.cr under the /config directory
  • Create a sam.cr in {project/src}
  • Edit your src/{project}.cr file
  1. Guides
  2. Models

Jennifer

PreviousCrectoNextMigrations

Last updated 7 years ago

What is Jennifer?

Jennifer is an ActiveRecord pattern for Crystal with great query DSL and migration mechanism.

This section is based on . Also see .

Installing Jennifer for Maze

Generate a normal maze app

maze new {project}

Update your project shard.yml

Add this to your application's

# Add the following dependencies
jennifer:
  github: imdrasil/jennifer.cr

sam:
  github: imdrasil/sam.cr

Choose one of existing adapters for your db: or .

Then in the console

shards update

Setup your database information

defaults : &defaults
  host: localhost
  adapter: postgres
  user: root
  password: somepassword
  migration_files_path: db/migrations # this is the default location for all migrations

development:
  db: blog_development
  <<: *defaults

test:
  db: blog_test
  <<: *defaults

Create a jennifer.cr under the /config directory

require "colorize"

Jennifer::Config.read("config/database.yml", MAZE_ENV)

Jennifer::Config.configure do |conf|
  conf.logger = Logger.new(STDOUT)

  conf.logger.formatter = Logger::Formatter.new do |severity, datetime, progname, message, io|
    io << datetime.colorize(:cyan) << ": \n" << message.colorize(:light_magenta)
  end
  conf.logger.level = Logger::DEBUG
end

Note that we pass the MAZE_ENV to Jennifer::Config.read this will allow Jennifer to use the correct database settings for the environment.

Create a sam.cr in {project/src}

# src/sam.cr
require "jennifer/adapter/postgres"
require "jennifer"

require "../config/jennifer"
require "../db/migrations/*"
require "sam"
require "jennifer/sam"
load_dependencies "jennifer"
Sam.help

Edit your src/{project}.cr file

This should be done before you load your application configurations (or at least models). With Maze this is very easy. Also the order is very important the adapter should come before Jennifer.

require "jennifer/adapter/postgres" # for postgres
require "jennifer"

require "maze"
require "./controllers/**"
require "./mailers/**"
require "./models/**"
require "./views/**"
require "../config/*"

Maze::Server.instance.run

You're all set with the configuration. Next using Jennifer Migrations and Models.

Jennifer uses Sam for running tasks pertinent to ORM operations. Sam is a Make-like utility which allows to specify tasks like Ruby's Rake do using plain Crystal. For how to use visit the Github repository

Jennifer's Docs
Maze Jennifer Example App
mysql
postgres
Sam
https://github.com/imdrasil/sam.cr