Skip to content

Commit

Permalink
GITBOOK-23: No subject
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbook-bot committed Aug 23, 2024
1 parent aa8873d commit 8bc9d0b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 40 deletions.
Binary file added docs/.gitbook/assets/cql-banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ title: Cql

CQL is a powerful library designed to simplify and enhance the management and execution of SQL queries in the Crystal programming language. It provides utilities for building, validating, and executing SQL statements, ensuring better performance and code maintainability.

<figure><img src=".gitbook/assets/cql-banner.png" alt=""><figcaption></figcaption></figure>

## Features

* **Query Builder**: Programmatically create complex SQL queries.
Expand Down
72 changes: 32 additions & 40 deletions docs/guides/active-record-with-cql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Getting Started with Active Record, Relations, and Migrations in CQL

In this guide, we'll walk through using CQL with Crystal for setting up a database schema, defining records (models), establishing relationships between them, and handling migrations. This will be a foundational guide for developers who are familiar with Object-Relational Mapping (ORM) concepts from other frameworks like ActiveRecord (Rails), Ecto, or Hibernate, but are now learning CQL with Crystal.

---
***

### Prerequisites

Before getting started, ensure you have the following:

- Crystal language installed (latest stable version).
- PostgreSQL or MySQL set up locally or in the cloud.
- CQL installed in your Crystal project.
* Crystal language installed (latest stable version).
* PostgreSQL or MySQL set up locally or in the cloud.
* CQL installed in your Crystal project.

You can add CQL to your project by including it in your `shard.yml`:

Expand All @@ -32,7 +32,7 @@ dependencies:
Run `shards install` to add the library to your project.

---
***

### Setting up CQL

Expand All @@ -49,7 +49,7 @@ end

Replace the database connection string with your actual PostgreSQL or MySQL credentials.

---
***

### Creating the Database Schema

Expand All @@ -58,7 +58,7 @@ CQL uses migrations to manage the database schema, similar to Rails' ActiveRecor
1. **Create a migration**: Let's define a `users` table in this migration:

Create a file in the `db/migrate` directory, for example, `20230817000000_create_users.cr`\
&#x20;


```crystal
# db/migrate/20230817000000_create_users.cr
Expand All @@ -84,15 +84,15 @@ CQL uses migrations to manage the database schema, similar to Rails' ActiveRecor

Here, we create a `users` table with columns `name`, `email`, and `timestamps`.

1. **Run the migration**: After defining your migration, you can run it with:\
1. **Run the migration**: After defining your migration, you can run it with:\\

```crystal
AcmeDB.migrator.up
```

This command will execute all pending migrations and update your database schema.

---
***

### Defining Records (Models)

Expand All @@ -114,7 +114,6 @@ Now that the schema is ready, we can define the `User` record (model) that maps
```

The `User` model is mapped to the `users` table. Here, we've define the fields for `id`, `name`, `email`, and timestamps. We also added basic validations for `name` and `email`.

2. **Create Records**: You can now create user records using the `User` model.

```crystal
Expand All @@ -123,7 +122,6 @@ Now that the schema is ready, we can define the `User` record (model) that maps
```

This will insert a new record into the `users` table.

3. **Query Records**: You can query users using the `User` model.

```crystal
Expand All @@ -133,7 +131,7 @@ Now that the schema is ready, we can define the `User` record (model) that maps

`User.all` fetches all users, and `User.find(1)` fetches the user with ID `1`.

---
***

### Establishing Relations

Expand Down Expand Up @@ -172,8 +170,7 @@ CQL supports associations similar to ActiveRecord, Ecto, and other ORMs. Let's d
end
end
```

2. **db_context the Post Model**:
2. **db\_context the Post Model**:

Now, let's define the `Post` record and establish the relationships.

Expand All @@ -194,8 +191,7 @@ CQL supports associations similar to ActiveRecord, Ecto, and other ORMs. Let's d
```

Here, the `Post` model includes a foreign key `user_id` and define a `belongs_to` association to the `User` model.

3. **db_context the `User` model's association**:
3. **db\_context the `User` model's association**:

Update the `User` model to reflect the relationship with `Post`.

Expand All @@ -215,28 +211,25 @@ CQL supports associations similar to ActiveRecord, Ecto, and other ORMs. Let's d
```

This define a `has_many` association on `User` so that each user can have multiple posts.
4. **Working with Relations**:
* Create a user and associate posts with them:

4. **Working with Relations**:

- Create a user and associate posts with them:

```crystal
user = User.create(name: "Jane Doe", email: "[email protected]")
post = Post.new(title: "First Post", body: "This is the first post", user: user)
post.save
```

- Access posts through the user:
```crystal
user = User.create(name: "Jane Doe", email: "[email protected]")
post = Post.new(title: "First Post", body: "This is the first post", user: user)
post.save
```
* Access posts through the user:

```crystal
user = User.find(1)
```crystal
user = User.find(1)
user.posts.each do |post|
puts post.title
end
```
user.posts.each do |post|
puts post.title
end
```

---
***

### Handling Migrations

Expand Down Expand Up @@ -267,7 +260,6 @@ CQL migrations allow you to create and alter your database schema easily. Here a
end
end
```

2. **Rolling Back Migrations**:

If something goes wrong with a migration, you can roll it back using:
Expand All @@ -278,16 +270,16 @@ CQL migrations allow you to create and alter your database schema easily. Here a

This will undo the last migration that was applied.

---
***

### Conclusion

This guide has provided a basic overview of using CQL with Crystal to define records (models), create relationships, and handle migrations. You've learned how to:

- Set up CQL and connect it to a database.
- Create and run migrations to define your schema.
- Define records and establish relationships using `has_many` and `belongs_to`.
- Manage your database schema with migrations.
* Set up CQL and connect it to a database.
* Create and run migrations to define your schema.
* Define records and establish relationships using `has_many` and `belongs_to`.
* Manage your database schema with migrations.

With this foundation, you can now expand your models, add validations, and explore more advanced querying and relationships in CQL.\
\
Expand Down

0 comments on commit 8bc9d0b

Please sign in to comment.