chore(docs): added eslint to lint documentation code blocks (#2920)
* docs: added rule for code length * chore: fixes based on vale errors * changed to use eslint * fixes using eslint * added github action for documentation eslint * changed allowed max-length * fixed incorrect heading level * removed comment
This commit is contained in:
@@ -6,15 +6,15 @@ In this document, you’ll learn how you can create an [Entity](overview.md).
|
||||
|
||||
To create an entity, create a TypeScript file in `src/models`. For example, here’s a `Post` entity defined in the file `src/models/post.ts`:
|
||||
|
||||
```tsx title=src/models/post.ts
|
||||
import { BeforeInsert, Column, Entity, PrimaryColumn } from "typeorm";
|
||||
import { BaseEntity} from "@medusajs/medusa";
|
||||
```ts title=src/models/post.ts
|
||||
import { BeforeInsert, Column, Entity, PrimaryColumn } from "typeorm"
|
||||
import { BaseEntity } from "@medusajs/medusa"
|
||||
import { generateEntityId } from "@medusajs/medusa/dist/utils"
|
||||
|
||||
@Entity()
|
||||
export class Post extends BaseEntity {
|
||||
@Column({type: 'varchar'})
|
||||
title: string | null;
|
||||
@Column({ type: "varchar" })
|
||||
title: string | null
|
||||
|
||||
@BeforeInsert()
|
||||
private beforeInsert(): void {
|
||||
@@ -31,12 +31,12 @@ To generate an ID for your entity that matches the IDs generated for Medusa’s
|
||||
|
||||
If you want the entity to also be soft deletable then it should extend `SoftDeletableEntity` instead:
|
||||
|
||||
```tsx
|
||||
import { SoftDeletableEntity } from "@medusajs/medusa";
|
||||
```ts
|
||||
import { SoftDeletableEntity } from "@medusajs/medusa"
|
||||
|
||||
@Entity()
|
||||
export class Post extends SoftDeletableEntity {
|
||||
//...
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
@@ -52,7 +52,7 @@ You can learn more about Migrations, how to create them, and how to run them in
|
||||
|
||||
Entities data can be easily accessed and modified using Typeorm [Repositories](https://typeorm.io/working-with-repository). To create a repository, create a file in `src/repositories`. For example, here’s a repository `PostRepository` created in `src/repositories/post.ts`:
|
||||
|
||||
```tsx title=src/repositories/post.ts
|
||||
```ts title=src/repositories/post.ts
|
||||
import { EntityRepository, Repository } from "typeorm"
|
||||
|
||||
import { Post } from "../models/post"
|
||||
@@ -85,24 +85,25 @@ npm run build
|
||||
|
||||
You can access your custom entity data in the database in services or subscribers using the repository. For example, here’s a service that lists all posts:
|
||||
|
||||
```tsx
|
||||
import { TransactionBaseService } from '@medusajs/medusa';
|
||||
```ts
|
||||
import { TransactionBaseService } from "@medusajs/medusa"
|
||||
|
||||
class PostService extends TransactionBaseService {
|
||||
constructor({ postRepository, manager }) {
|
||||
super({ postRepository, manager });
|
||||
super({ postRepository, manager })
|
||||
|
||||
this.postRepository = postRepository;
|
||||
this.manager_ = manager;
|
||||
this.postRepository = postRepository
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async list() {
|
||||
const postRepository = this.manager_.getCustomRepository(this.postRepository);
|
||||
return await postRepository.find();
|
||||
const postRepository = this.manager_
|
||||
.getCustomRepository(this.postRepository)
|
||||
return await postRepository.find()
|
||||
}
|
||||
}
|
||||
|
||||
export default PostService;
|
||||
export default PostService
|
||||
```
|
||||
|
||||
In the constructor, you can use dependency injection to get access to instances of services and repositories. Here, you initialize class fields `postRepository` and `manager`. The `manager` is a [Typeorm Entity Manager](https://typeorm.io/working-with-entity-manager).
|
||||
@@ -123,8 +124,8 @@ This same usage of repositories can be done in subscribers as well.
|
||||
|
||||
To delete soft-deletable entities that extend the `SoftDeletableEntity` class, you can use the repository method `softDelete` method:
|
||||
|
||||
```tsx
|
||||
await postRepository.softDelete(post.id);
|
||||
```ts
|
||||
await postRepository.softDelete(post.id)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user