Skip to content

Commit 69c868a

Browse files
committed
Prepare 0.5.0 release
1 parent 9aaac06 commit 69c868a

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Marten release: 0.5
2+
3+
We are pleased to announce the release of [Marten 0.5](https://martenframework.com/docs/the-marten-project/release-notes/0.5)!
4+
5+
**Marten 0.5** brings significant enhancements, including relations pre-fetching, model scopes, enum fields for models and schemas, and support for raw SQL predicates in query sets. These improvements enhance performance, flexibility, and ease of use. For a detailed overview of all new features and changes, check out the [full changelog](https://martenframework.com/docs/the-marten-project/release-notes/0.5).
6+
7+
## New features and highlights
8+
9+
### Relations pre-fetching
10+
11+
Marten now provides the ability to prefetch relations when using [query sets](https://martenframework.com/docs/models-and-databases/queries.md) through the use of the new [`#prefetch`](https://martenframework.com/docs/models-and-databases/reference/query-set.md#prefetch) method. When using [`#prefetch`](https://martenframework.com/docs/models-and-databases/reference/query-set.md#prefetch), the records corresponding to the specified relationships will be prefetched in single batches and each record returned by the original query set will have the corresponding related objects already selected and populated.
12+
13+
For example:
14+
15+
```crystal
16+
posts_1 = Post.all.to_a
17+
# hits the database to retrieve the related "tags" (many-to-many relation)
18+
puts posts_1[0].tags.to_a
19+
20+
posts_2 = Post.all.prefetch(:tags).to_a
21+
# doesn't hit the database since the related "tags" relation was already prefetched
22+
puts posts_2[0].tags.to_a
23+
```
24+
25+
Like the existing [`#join`](https://martenframework.com/docs/models-and-databases/reference/query-set.md#join) method, this allows to alleviate N+1 issues commonly encountered when accessing related objects. However, unlike [`#join`](https://martenframework.com/docs/models-and-databases/reference/query-set.md#join) (which can only be used with single-valued relationships), [`#prefetch`](https://martenframework.com/docs/models-and-databases/reference/query-set.md#prefetch) can be used with both single-valued relationships and multi-valued relationships (such as [many-to-many](https://martenframework.com/docs/models-and-databases/relationships.md#many-to-many-relationships) relationships, [reverse many-to-many](https://martenframework.com/docs/models-and-databases/relationships.md#backward-relations-2) relationships, and [reverse many-to-one](https://martenframework.com/docs/models-and-databases/relationships.md#backward-relations) relationships).
26+
27+
Please refer to [Pre-fetching relations](https://martenframework.com/docs/models-and-databases/queries.md#pre-fetching-relations) to learn more about this new capability.
28+
29+
### Model scopes
30+
31+
It is now possible to define [scopes](https://martenframework.com/docs/models-and-databases/queries.md#scopes) in model classes. Scopes allow to pre-define specific filtered query sets, which can be easily applied to model classes and model query sets.
32+
33+
Such scopes can be defined through the use of the [`#scope`](https://martenframework.com/docs/api/0.5/Marten/DB/Model/Querying.html#scope(name%2C%26block)-macro) macro, which expects a scope name (string literal or symbol) as first argument and requires a block where the query set filtering logic is defined:
34+
35+
```crystal
36+
class Post < Marten::Model
37+
field :id, :big_int, primary_key: true, auto: true
38+
field :title, :string, max_size: 255
39+
field :is_published, :bool, default: false
40+
field :created_at, :date_time
41+
42+
scope :published { filter(is_published: true) }
43+
scope :unpublished { filter(is_published: false) }
44+
scope :recent { filter(created_at__gt: 1.year.ago) }
45+
end
46+
47+
Post.published # => Post::QuerySet [...]>
48+
```
49+
50+
It is also possible to override the default scope through the use of the [`#default_scope`](https://martenframework.com/docs/api/0.5/Marten/DB/Model/Querying.html#default_scope-macro) macro. This macro requires a block where the query set filtering logic is defined:
51+
52+
```crystal
53+
class Post < Marten::Model
54+
field :id, :big_int, primary_key: true, auto: true
55+
field :title, :string, max_size: 255
56+
field :is_published, :bool, default: false
57+
field :created_at, :date_time
58+
59+
default_scope { filter(is_published: true) }
60+
end
61+
```
62+
63+
Please refer to [Scopes](https://martenframework.com/docs/models-and-databases/queries.md#scopes) for more details on how to define scopes.
64+
65+
### Enum field for models and schemas
66+
67+
It is now possible to define `enum` fields in [models](https://martenframework.com/docs/models-and-databases/reference/fields.md#enum) and [schemas](https://martenframework.com/docs/schemas/reference/fields.md#enum). For models, such fields allow you to store valid enum values, with validation enforced at the database level. When validating data with schemas, they allow you to expect valid string values that match those of the configured enum.
68+
69+
For example:
70+
71+
```crystal
72+
enum Category
73+
NEWS
74+
BLOG
75+
end
76+
77+
class Article < Marten::Model
78+
field :id, :big_int, primary_key: true, auto: true
79+
field :category, :enum, values: Category
80+
end
81+
82+
article = Article.last!
83+
article.category # => Category::BLOG
84+
```
85+
86+
### Raw SQL predicate filtering
87+
88+
Marten now provides the ability to filter [query sets](https://martenframework.com/docs/models-and-databases/queries.md) using [raw SQL predicates](https://martenframework.com/docs/models-and-databases/raw-sql.md#filtering-with-raw-sql-predicates) through the use of the `#filter` method. This is useful when you want to leverage the flexibility of SQL for specific conditions, but still want Marten to handle the column selection and query building for the rest of the query.
89+
90+
For example:
91+
92+
```crystal
93+
Author.filter("first_name = :first_name", first_name: "John")
94+
Author.filter("first_name = ?", "John")
95+
Author.filter { q("first_name = :first_name", first_name: "John") }
96+
```
97+
98+
Please refer to [Filtering with raw SQL predicates](https://martenframework.com/docs/models-and-databases/raw-sql.md#filtering-with-raw-sql-predicates) to learn more about this new capability.
99+
100+
## Other changes
101+
102+
Please head over to the [official Marten 0.5 release notes](https://martenframework.com/docs/the-marten-project/release-notes/0.5) for an overview of all the changes that are part of this release.

news/manifest.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
news:
2+
- title: "Marten 0.5: Relations pre-fetching, model scopes, enum fields, and more!"
3+
description: "Check out the new 0.5 release: Leverage pre-fetched relations in querysets, model scopes, enum fields for models and schemas, raw SQL predicates, and more!"
4+
file: 2024/2024-07-13-marten-release-0.5.md
5+
author:
6+
name: Morgan Aubert
7+
github: https://github.com/ellmetha
28
- title: "Marten bugfix release: 0.4.5"
39
description: Marten 0.4.5 introduces bug fixes, enhancing the stability and performance of the framework for a more reliable development experience.
410
file: 2024/2024-05-05-marten-bugfix-release-0.4.5.md

scripts/build_docs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git clone https://github.com/martenframework/marten ./docs/src
88
cd ./docs/src
99
shards install
1010
crystal docs --output=docs/static/api/dev
11-
for version in 0.1.5 0.2.4 0.3.0 0.4.0
11+
for version in 0.3.4 0.4.5 0.5.0
1212
do
1313
git checkout v$version
1414
doc_version=$(echo $version | rev | cut -c3- | rev)

0 commit comments

Comments
 (0)