Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] develop from medusajs:develop #634

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/great-badgers-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@medusajs/types": patch
"@medusajs/utils": patch
---

fix: relationships to accept ids
58 changes: 58 additions & 0 deletions .github/workflows/generate-doc-files.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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
28 changes: 28 additions & 0 deletions packages/core/types/src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,27 @@ export type InferSchemaFields<Schema extends DMLSchema> = Prettify<
} & InferForeignKeys<Schema>
>

/**
* Infers the types of the schema fields from the DML entity
* for module services
*/
export type InferSchemaFieldsForModuleServices<Schema extends DMLSchema> =
Prettify<
{
[K in keyof Schema]: Schema[K] extends RelationshipType<any>
? 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<Schema>
>

/**
* Infers the schema properties without the relationships
*/
Expand Down Expand Up @@ -247,6 +268,13 @@ export type Infer<T> = T extends IDmlEntity<infer Schema, any>
? EntityConstructor<InferSchemaFields<Schema>>
: never

export type InferEntityForModuleService<T> = T extends IDmlEntity<
infer Schema,
any
>
? InferSchemaFieldsForModuleServices<Schema>
: never

/**
* The actions to cascade from a given entity to its
* relationship.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,14 +74,16 @@ const containerMock = {
describe("Medusa Service typings", () => {
describe("create<Service>", () => {
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<
| [
Partial<{
id: string | undefined
title: string | undefined
comments: string[] | undefined
tags: string[] | undefined
description: string | null | undefined
}>,
...rest: any[]
Expand All @@ -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<InferTypeOf<typeof Blog>> | Promise<InferTypeOf<typeof Blog>[]>
>()

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[]
Expand Down Expand Up @@ -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[]
Expand All @@ -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
}>[]
| {
Expand All @@ -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
}>[]
}
Expand All @@ -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
}>[]
}[]
Expand Down
5 changes: 3 additions & 2 deletions packages/core/utils/src/modules-sdk/types/medusa-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {
Context,
FindConfig,
IDmlEntity,
InferEntityType,
Pluralize,
Prettify,
RestoreReturn,
SoftDeleteReturn,
InferEntityType,
InferEntityForModuleService,
} from "@medusajs/types"
import { DmlEntity } from "../../dml"

Expand Down Expand Up @@ -45,7 +46,7 @@ export type ModelConfigurationsToConfigTemplate<T extends ModelEntries> = {
? InstanceType<T[Key]>
: any
inputDto: T[Key] extends DmlEntity<any, any>
? Omit<InferEntityType<T[Key]>, DMLDTOExcludeProperties>
? Omit<InferEntityForModuleService<T[Key]>, DMLDTOExcludeProperties>
: T[Key] extends Constructor<any>
? InstanceType<T[Key]>
: any
Expand Down
6 changes: 3 additions & 3 deletions www/apps/book/app/learn/update/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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
```
Loading