Skip to content

Commit

Permalink
martenframework#47 - Add model table options reference to the documen…
Browse files Browse the repository at this point in the history
…tation
  • Loading branch information
ellmetha committed Apr 10, 2023
1 parent b8d0a5a commit ba9688b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/docs/models-and-databases.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ Models define what data can be persisted and manipulated by a Marten application
<article key="fields" className="col col--6">
<DocCard item={{type: "link", href: "./models-and-databases/reference/fields", label: "Model fields", docId: "models-and-databases/reference/fields" }} />
</article>
<article key="table-options" className="col col--6">
<DocCard item={{type: "link", href: "./models-and-databases/reference/table-options", label: "Table options", docId: "models-and-databases/reference/table-options" }} />
</article>
<article key="query-set" className="col col--6">
<DocCard item={{type: "link", href: "./models-and-databases/reference/query-set", label: "Query set", docId: "models-and-databases/reference/query-set" }} />
</article>
Expand Down
57 changes: 57 additions & 0 deletions docs/docs/models-and-databases/reference/table-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: Table options
description: Table options reference.
---

This page provides a reference for all the table options that can be leveraged when defining models.

## Table name

Table names for models are automatically generated from the model name and the label of the associated application. That being said, it is possible to specifically override the name of a model table by leveraging the [`#db_table`](pathname:https:///api/dev/Marten/DB/Model/Table/ClassMethods.html#db_table(db_table%3AString|Symbol)-instance-method) class method, which requires a table name string or symbol.

For example:

```crystal
class Article < Marten::Model
field :id, :big_int, primary_key: true, auto: true
field :title, :string, max_size: 255
field :content, :text
// highlight-next-line
db_table :articles
end
```

## Table indexes

Multifields indexes can be configured in a model by leveraging the [`#db_index`](pathname:https:///api/dev/Marten/DB/Model/Table/ClassMethods.html#db_index(name%3AString|Symbol%2Cfield_names%3AArray(String)|Array(Symbol))%3ANil-instance-method) class method. This method requires an index name argument as well as an array of targeted field names.

For example:

```crystal
class Person < Marten::Model
field :id, :int, primary_key: true, auto: true
field :first_name, :string, max_size: 50
field :last_name, :string, max_size: 50
// highlight-next-line
db_index :person_full_name_index, field_names: [:first_name, :last_name]
end
```

## Table unique constraints

Multifields unique constraints can be configured in a model by leveraging the [`#db_unique_constraint`](pathname:https:///api/dev/Marten/DB/Model/Table/ClassMethods.html#db_unique_constraint(name%3AString|Symbol%2Cfield_names%3AArray(String)|Array(Symbol))%3ANil-instance-method) class method. This method requires an index name argument as well as an array of targeted field names.

For example:

```crystal
class Booking < Marten::Model
field :id, :int, primary_key: true, auto: true
field :room, :string, max_size: 50
field :date, :date, max_size: 50
// highlight-next-line
db_unique_constraint :booking_room_date_constraint, field_names: [:room, :date]
end
```
1 change: 1 addition & 0 deletions docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
label: 'Reference',
items: [
'models-and-databases/reference/fields',
'models-and-databases/reference/table-options',
'models-and-databases/reference/query-set',
'models-and-databases/reference/migration-operations',
],
Expand Down

0 comments on commit ba9688b

Please sign in to comment.