diff --git a/.changeset/great-badgers-play.md b/.changeset/great-badgers-play.md new file mode 100644 index 0000000000000..0adb7261bcd3e --- /dev/null +++ b/.changeset/great-badgers-play.md @@ -0,0 +1,6 @@ +--- +"@medusajs/types": patch +"@medusajs/utils": patch +--- + +fix: relationships to accept ids diff --git a/.github/workflows/generate-doc-files.yml b/.github/workflows/generate-doc-files.yml new file mode 100644 index 0000000000000..584e6ba114ac1 --- /dev/null +++ b/.github/workflows/generate-doc-files.yml @@ -0,0 +1,58 @@ +name: Generate Documentation Prep Files +on: + pull_request: + paths: + - www/apps/** + +jobs: + check_files: + runs-on: ubuntu-latest + permissions: + contents: write # Required to push changes + steps: + - name: Cancel Previous Runs + uses: styfle/cancel-workflow-action@0.11.0 + with: + access_token: ${{ github.token }} + + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 2 # Ensures we can check the diff + ref: ${{ github.event.pull_request.head.ref }} # Checkout PR branch + + - name: Setup Node.js environment + uses: actions/setup-node@v3 + with: + node-version: 20 + cache: "yarn" + + - name: Install Workspace dependencies + working-directory: www + run: yarn install + + - name: Build packages + working-directory: www + run: yarn build:packages + + - name: Run yarn prep in book + working-directory: www/apps/book + run: yarn prep + + - name: Run yarn prep in resources + working-directory: www/apps/resources + run: yarn prep + + - name: Commit and push changes (if any) + run: | + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + + if [[ -n $(git status --porcelain www/apps/book/generated www/apps/book/public www/apps/resources/generated) ]]; then + echo "Ran prep and generated files, committing and pushing..." + git add www/apps/book/generated www/apps/book/public www/apps/resources/generated + git commit -m "chore: run yarn prep automatically" + git push origin ${{ github.event.pull_request.head.ref }} + else + echo "No generated files, skipping commit." + fi \ No newline at end of file diff --git a/packages/core/types/src/dml/index.ts b/packages/core/types/src/dml/index.ts index 3f25730a070ef..adff56b039636 100644 --- a/packages/core/types/src/dml/index.ts +++ b/packages/core/types/src/dml/index.ts @@ -213,6 +213,27 @@ export type InferSchemaFields = Prettify< } & InferForeignKeys > +/** + * Infers the types of the schema fields from the DML entity + * for module services + */ +export type InferSchemaFieldsForModuleServices = + Prettify< + { + [K in keyof Schema]: Schema[K] extends RelationshipType + ? Schema[K]["type"] extends "belongsTo" + ? string + : Schema[K]["type"] extends "hasOne" | "hasOneWithFK" + ? string + : Schema[K]["type"] extends "hasMany" + ? string[] + : Schema[K]["type"] extends "manyToMany" + ? string[] + : never + : Schema[K]["$dataType"] + } & InferForeignKeys + > + /** * Infers the schema properties without the relationships */ @@ -247,6 +268,13 @@ export type Infer = T extends IDmlEntity ? EntityConstructor> : never +export type InferEntityForModuleService = T extends IDmlEntity< + infer Schema, + any +> + ? InferSchemaFieldsForModuleServices + : never + /** * The actions to cascade from a given entity to its * relationship. diff --git a/packages/core/utils/src/modules-sdk/__tests__/medusa-service-typings.spec.ts b/packages/core/utils/src/modules-sdk/__tests__/medusa-service-typings.spec.ts index 47dc55e549248..2037c89b4fdaa 100644 --- a/packages/core/utils/src/modules-sdk/__tests__/medusa-service-typings.spec.ts +++ b/packages/core/utils/src/modules-sdk/__tests__/medusa-service-typings.spec.ts @@ -6,9 +6,28 @@ import { InferTypeOf } from "@medusajs/types" const Blog = model.define("Blog", { id: model.text(), title: model.text(), + tags: model.manyToMany(() => Tag), + comments: model.hasMany(() => Comment), description: model.text().nullable(), }) +const Tag = model.define("Tag", { + id: model.text(), + title: model.text(), +}) + +const Comment = model.define("Comment", { + id: model.text(), + post: model.belongsTo(() => Blog), + author: model.belongsTo(() => User), + description: model.text().nullable(), +}) + +const User = model.define("User", { + id: model.text(), + username: model.text(), +}) + type BlogDTO = { id: number title: string @@ -55,7 +74,7 @@ const containerMock = { describe("Medusa Service typings", () => { describe("create", () => { test("type-hint model properties", () => { - class BlogService extends MedusaService({ Blog }) {} + class BlogService extends MedusaService({ Blog, Comment }) {} const blogService = new BlogService(containerMock) expectTypeOf(blogService.createBlogs).parameters.toEqualTypeOf< @@ -63,6 +82,8 @@ describe("Medusa Service typings", () => { Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined description: string | null | undefined }>, ...rest: any[] @@ -71,6 +92,36 @@ describe("Medusa Service typings", () => { Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined + description: string | null | undefined + }>[], + ...rest: any[] + ] + >() + expectTypeOf(blogService.createBlogs).returns.toEqualTypeOf< + Promise> | Promise[]> + >() + + expectTypeOf(blogService.createComments).parameters.toEqualTypeOf< + | [ + Partial<{ + id: string | undefined + post: string | undefined + author: string | undefined + post_id: string | undefined + author_id: string | undefined + description: string | null | undefined + }>, + ...rest: any[] + ] + | [ + Partial<{ + id: string | undefined + post: string | undefined + author: string | undefined + post_id: string | undefined + author_id: string | undefined description: string | null | undefined }>[], ...rest: any[] @@ -143,6 +194,8 @@ describe("Medusa Service typings", () => { Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined description: string | null | undefined }>, ...rest: any[] @@ -152,6 +205,8 @@ describe("Medusa Service typings", () => { | Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined description: string | null | undefined }>[] | { @@ -160,11 +215,15 @@ describe("Medusa Service typings", () => { | Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined description: string | null | undefined }> | Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined description: string | null | undefined }>[] } @@ -174,11 +233,15 @@ describe("Medusa Service typings", () => { | Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined description: string | null | undefined }> | Partial<{ id: string | undefined title: string | undefined + comments: string[] | undefined + tags: string[] | undefined description: string | null | undefined }>[] }[] diff --git a/packages/core/utils/src/modules-sdk/types/medusa-service.ts b/packages/core/utils/src/modules-sdk/types/medusa-service.ts index eb66ceeae9796..f6ec59eb59eed 100644 --- a/packages/core/utils/src/modules-sdk/types/medusa-service.ts +++ b/packages/core/utils/src/modules-sdk/types/medusa-service.ts @@ -3,11 +3,12 @@ import { Context, FindConfig, IDmlEntity, - InferEntityType, Pluralize, Prettify, RestoreReturn, SoftDeleteReturn, + InferEntityType, + InferEntityForModuleService, } from "@medusajs/types" import { DmlEntity } from "../../dml" @@ -45,7 +46,7 @@ export type ModelConfigurationsToConfigTemplate = { ? InstanceType : any inputDto: T[Key] extends DmlEntity - ? Omit, DMLDTOExcludeProperties> + ? Omit, DMLDTOExcludeProperties> : T[Key] extends Constructor ? InstanceType : any diff --git a/www/apps/book/app/learn/update/page.mdx b/www/apps/book/app/learn/update/page.mdx index 096ddcea7c6d7..4c3135fefd6ae 100644 --- a/www/apps/book/app/learn/update/page.mdx +++ b/www/apps/book/app/learn/update/page.mdx @@ -61,7 +61,7 @@ Before updating a Medusa application, make sure to check the [release notes](htt Then, to update your Medusa application, bump the version of all `@medusajs/*` dependencies in your `package.json`. Then, re-install dependencies: ```bash npm2yarn -npm run install +npm install ``` This will update all Medusa packages to the latest version. @@ -91,7 +91,7 @@ npx medusa db:rollback cart product Then, revert the update by changing the version of all `@medusajs/*` dependencies in your `package.json` to the previous version and re-installing dependencies: ```bash npm2yarn -npm run install +npm install ``` Finally, run the migrations to sync link changes: @@ -116,5 +116,5 @@ In the Medusa codebase, our team uses the following [TSDoc](https://tsdoc.org/) If you have a Medusa plugin project, you only need to update its `@medusajs/*` dependencies in the `package.json` file to the latest version. Then, re-install dependencies: ```bash npm2yarn -npm run install +npm install ```