8000 feat(models): move column naming description before date column · adonisjs/lucid.adonisjs.com@2b87e6d · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b87e6d

Browse files
committed
feat(models): move column naming description before date column
1 parent 1166408 commit 2b87e6d

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

content/docs/models/introduction.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ The `@column` decorator additionally accepts options to configure the property b
152152
- The `isPrimary` option marks the property as the primary key for the given database table.
153153
- The `serializeAs: null` option removes the property when you serialize the model to JSON.
154154

155+
### Column names
156+
157+
Lucid assumes that your database columns names are defined as `snake_case` and automatically converts the model properties to snake case during database queries. For example:
158+
159+
```ts
160+
await User.create({ avatarUrl: 'foo.jpg' })
161+
162+
// EXECUTED QUERY
163+
// insert into "users" ("avatar_url") values (?)
164+
```
165+
166+
If you are not using the `snake_case` convention in your database, then you can override the default behavior of Lucid by defining a custom [Naming Strategy](./naming_strategy.md)
167+
168+
You can also define the database column names explicitly within the `@column` decorator. This is usually helpful for bypassing the convention in specific use cases.
169+
170+
```ts
171+
@column({ columnName: 'user_id', isPrimary: true })
172+
declare id: number
173+
```
174+
155175
### Date columns
156176

157177
Lucid further enhances the date and the date-time properties and converts the database driver values to an instance of [luxon.DateTime](https://moment.github.io/luxon/).
@@ -176,25 +196,6 @@ export default class User extends BaseModel {
176196

177197
Optionally, you can pass the `autoCreate` and `autoUpdate` options to always define the timestamps during the creation and the update operations. **Do note, setting these options doesn't modify the database table or its triggers.**
178198

179-
### Column names
180-
181-
Lucid assumes that your database columns names are defined as `snake_case` and automatically converts the model properties to snake case during database queries. For example:
182-
183-
```ts
184-
await User.create({ avatarUrl: 'foo.jpg' })
185-
186-
// EXECUTED QUERY
187-
// insert into "users" ("avatar_url") values (?)
188-
```
189-
190-
If you are not using the `snake_case` convention in your database, then you can override the default behavior of Lucid by defining a custom [Naming Strategy](./naming_strategy.md)
191-
192-
You can also define the database column names explicitly within the `@column` decorator. This is usually helpful for bypassing the convention in specific use cases.
193-
194-
```ts
195-
@column({ columnName: 'user_id', isPrimary: true })
196-
declare id: number
197-
```
198199

199200
## Models config
200201

0 commit comments

Comments
 (0)
0