docs: intro + basic chapter fixes (#10361)
This commit is contained in:
@@ -28,10 +28,12 @@ Modules are created in a sub-directory of `src/modules`. So, start by creating t
|
||||
|
||||
### 1. Create Data Model
|
||||
|
||||
A data model represents a table in the database. You create data models using Medusa's data modeling utility, which is built to improve readability and provide an intuitive developer experience. It simplifies defining a table's columns, relations, and indexes with straightforward methods and configurations.
|
||||
A data model represents a table in the database. You create data models using Medusa's data modeling utility. It simplifies defining a table's columns, relations, and indexes with straightforward methods and configurations.
|
||||
|
||||
You create a data model in a TypeScript or JavaScript file under the `models` directory of a module. So, to create a `Post` data model in the Blog Module, create the file `src/modules/blog/models/post.ts` with the following content:
|
||||
|
||||

|
||||
|
||||
```ts title="src/modules/blog/models/post.ts"
|
||||
import { model } from "@medusajs/framework/utils"
|
||||
|
||||
@@ -61,12 +63,10 @@ The code snippet above defines a `Post` data model with `id` and `title` propert
|
||||
|
||||
You perform database operations on your data models in a service, which is a class exported by the module and acts like an interface to its functionalities. Medusa registers the service in its [container](../medusa-container/page.mdx), allowing you to resolve and use it when building custom commerce flows.
|
||||
|
||||
In other commerce platforms, you have to write the methods to manage each data model, such as to create or retrieve a post. This process is inefficient and wastes your time that can be spent on building custom business logic.
|
||||
|
||||
Medusa saves your time by generating these methods for you. Your service can extend a `MedusaService` utility, which is a function that generates a class with read and write methods for every data model in your module. Your efforts only go into building custom business logic.
|
||||
|
||||
You define a service in a `service.ts` or `service.js` file at the root of your module's directory. So, to create the Blog Module's service, create the file `src/modules/blog/service.ts` with the following content:
|
||||
|
||||

|
||||
|
||||
export const highlights = [
|
||||
["4", "MedusaService", "The service factory function."],
|
||||
["5", "MyCustom", "The data models to generate data-management methods for."]
|
||||
@@ -84,7 +84,9 @@ class BlogModuleService extends MedusaService({
|
||||
export default BlogModuleService
|
||||
```
|
||||
|
||||
Your module's service extends a class returned by the `MedusaService` utility function. The `MedusaService` function accepts an object of data models, and returns a class with generated methods for data-management Create, Read, Update, and Delete (CRUD) operations on those data models. You can pass all data models in your module in this object.
|
||||
Your module's service extends a class generated by the `MedusaService` utility function. This class comes with generated methods for data-management Create, Read, Update, and Delete (CRUD) operations on each of your modules, saving your time that can be spent on building custom business logic.
|
||||
|
||||
The `MedusaService` function accepts an object of data models to generate methods for. You can pass all data models in your module in this object.
|
||||
|
||||
For example, the `BlogModuleService` now has a `createPosts` method to create post records, and a `retrievePost` method to retrieve a post record. The suffix of each method (except for `retrieve`) is the pluralized name of the data model.
|
||||
|
||||
@@ -102,6 +104,8 @@ The final piece to a module is its definition, which is exported in an `index.ts
|
||||
|
||||
So, to export the definition of the Blog Module, create the file `src/modules/blog/index.ts` with the following content:
|
||||
|
||||

|
||||
|
||||
export const moduleDefinitionHighlights = [
|
||||
["4", "BLOG_MODULE", "Export the module's name to reference it in other customizations."],
|
||||
["6", "BLOG_MODULE", "Specify the module's name."],
|
||||
|
||||
Reference in New Issue
Block a user