How to Effectively Use Granite ORM in Crystal for Your Projects

May 11, 2024 | Programming

Granite is a powerful web framework crafted in the Crystal programming language. This framework promises to simplify your development process by offering a robust Object-Relational Mapping (ORM) solution. Whether you’re maintaining existing features, contributing new ones, or simply running tests, this guide will walk you through the steps effectively and seamlessly.

Seeking Maintainers

Granite is on the lookout for volunteers to take over the maintainership of this repository. This includes reviewing and merging pull requests, as well as ensuring that the framework stays up-to-date with the latest changes in the Crystal language. If you’re interested in contributing, check out more information here.

Documentation

For comprehensive insights into all features of Granite, ensure to refer to the Documentation.

Contributing to Granite

Are you ready to contribute? Here’s how you can do it:

  1. Fork the project: Fork it.
  2. Create your feature branch: git checkout -b my-new-feature.
  3. Commit your changes: git commit -am "Add some feature".
  4. Push to the branch: git push origin my-new-feature.
  5. Create a new Pull Request.

Running Tests

Testing is essential to confirm that everything is functioning smoothly. Granite uses Crystal’s built-in testing framework, and you can run tests either in a dockerized testing environment or locally.

Docker Setup

You can use Docker to establish a self-contained testing environment. Here’s how:

  1. Ensure Docker is installed on your machine.
  2. Choose the appropriate docker-compose file based on the database you want to test against:

Environment Variable Setup

Set up your environment variables:

  1. Option 1: Export environment variables with $ source .export.sh or $ source .env.
  2. Option 2: Modify the default .env file for docker-compose as needed.

Running Tests

Use the following command format to run your tests for different databases:

$ docker-compose -f docker/docker-compose.database_type.yml run spec

Cleanup

When you’re done testing:

$ docker-compose -f docker/docker-compose.database_type.yml down

Local Setup

If you prefer to run tests locally, follow these steps:

  1. Install the necessary dependencies: $ shards install.
  2. Make sure to configure the .env file correctly or create the required databases.
  3. For setting up databases:
    • PostgreSQL:
      CREATE USER granite WITH PASSWORD password;
      CREATE DATABASE granite_db;
      GRANT ALL PRIVILEGES ON DATABASE granite_db TO granite;
    • MySQL:
      CREATE USER granite@localhost IDENTIFIED BY password;
      CREATE DATABASE granite_db;
      GRANT ALL PRIVILEGES ON granite_db.* TO granite@localhost WITH GRANT OPTION;
  4. Export your environment variables using $ source .export.sh or $ source .env.
  5. Run the tests with: $ crystal spec.

Troubleshooting

If you encounter any issues during the setup or testing processes, here are some ideas to help you out:

  • Ensure that you have the correct database credentials in your .env file.
  • Make sure Docker is properly installed and running.
  • Verify that your Crystal environment is functioning correctly.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox