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

feat(index): Provide a similar API to Query #9193

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
106 changes: 68 additions & 38 deletions integration-tests/modules/__tests__/index/search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,33 @@ medusaIntegrationTestRunner({
// Timeout to allow indexing to finish
await setTimeout(2000)

const [results, count] = await indexEngine.queryAndCount(
{
select: {
const [results, count] = await indexEngine.queryAndCount({
fields: [
"product.*",
"product.variants.*",
"product.variants.prices.*",
],
filters: {
product: {
variants: {
prices: {
amount: { $gt: 50 },
},
},
},
},
pagination: {
orderBy: {
product: {
variants: {
prices: true,
prices: {
amount: "DESC",
},
},
},
},
where: {
"product.variants.prices.amount": { $gt: 50 },
},
},
{
orderBy: [{ "product.variants.prices.amount": "DESC" }],
}
)
})

expect(count).toBe(1)

Expand Down Expand Up @@ -118,24 +128,34 @@ medusaIntegrationTestRunner({
// Timeout to allow indexing to finish
await setTimeout(2000)

const [results, count] = await indexEngine.queryAndCount(
{
select: {
product: {
variants: {
prices: true,
const [results, count] = await indexEngine.queryAndCount({
fields: [
"product.*",
"product.variants.*",
"product.variants.prices.*",
],
filters: {
product: {
variants: {
prices: {
amount: { $gt: 50 },
currency_code: { $eq: "AUD" },
},
},
},
where: {
"product.variants.prices.amount": { $gt: 50 },
"product.variants.prices.currency_code": { $eq: "AUD" },
pagination: {
orderBy: {
product: {
variants: {
prices: {
amount: "DESC",
},
},
},
},
},
},
{
orderBy: [{ "product.variants.prices.amount": "DESC" }],
}
)
})

expect(count).toBe(1)

Expand Down Expand Up @@ -179,29 +199,39 @@ medusaIntegrationTestRunner({

await setTimeout(5000)

const queryArgs = [
{
select: {
const queryArgs = {
fields: [
"product.*",
"product.variants.*",
"product.variants.prices.*",
],
filters: {
product: {
variants: {
prices: {
amount: { $gt: 50 },
currency_code: { $eq: "AUD" },
},
},
},
},
pagination: {
orderBy: {
product: {
variants: {
prices: true,
prices: {
amount: "DESC",
},
},
},
},
where: {
"product.variants.prices.amount": { $gt: 50 },
"product.variants.prices.currency_code": { $eq: "AUD" },
},
},
{
orderBy: [{ "product.variants.prices.amount": "DESC" }],
},
]
}

await indexEngine.queryAndCount(...queryArgs)
await indexEngine.queryAndCount(queryArgs)

const [results, count, perf] = await indexEngine.queryAndCount(
...queryArgs
queryArgs
)

console.log(perf)
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"private": true,
"scripts": {
"test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage",
"test:integration": "NODE_OPTIONS=--experimental-vm-modules jest --no-cache --maxWorkers=50% --bail --detectOpenHandles --forceExit --logHeapUsage -- __tests__/index/*.spec.ts",
"test:integration:chunk": "NODE_OPTIONS=--experimental-vm-modules jest --silent --no-cache --bail --maxWorkers=50% --forceExit --testPathPattern=$(echo $CHUNKS | jq -r \".[${CHUNK}] | .[]\")",
"build": "tsc --allowJs --outDir ./dist"
},
Expand Down
22 changes: 20 additions & 2 deletions packages/core/types/src/index/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { IModuleService } from "../modules-sdk"

type OrderBy = {
[key: string]: OrderBy | "ASC" | "DESC" | 1 | -1 | true | false
}

export type IndexQueryConfig = {
fields: string[]
filters?: Record<string, any>
joinFilters?: Record<string, any>
pagination?: {
skip?: number
take?: number
orderBy?: OrderBy
}
keepFilteredEntities?: boolean
}

export interface IIndexService extends IModuleService {
query(...args): Promise<any>
queryAndCount(...args): Promise<any>
query(config: IndexQueryConfig): Promise<any[]>
queryAndCount(
config: IndexQueryConfig
): Promise<[any[], number, PerformanceMeasure]>
}
18 changes: 18 additions & 0 deletions packages/modules/index/coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1726733616211" clover="3.2.0">
<project timestamp="1726733616211" name="All files">
<metrics statements="9" coveredstatements="9" conditionals="4" coveredconditionals="3" methods="2" coveredmethods="2" elements="15" coveredelements="14" complexity="0" loc="9" ncloc="9" packages="1" files="1" classes="1"/>
<file name="flatten-object-keys.ts" path="/Users/adriendeperetti/Documents/02-personnel/01-projects/medusa/packages/modules/index/src/utils/flatten-object-keys.ts">
<metrics statements="9" coveredstatements="9" conditionals="4" coveredconditionals="3" methods="2" coveredmethods="2"/>
<line num="22" count="1" type="stmt"/>
<line num="23" count="1" type="stmt"/>
<line num="26" count="2" type="stmt"/>
<line num="27" count="5" type="cond" truecount="2" falsecount="0"/>
<line num="28" count="1" type="cond" truecount="1" falsecount="1"/>
<line num="29" count="1" type="stmt"/>
<line num="31" count="4" type="stmt"/>
<line num="36" count="1" type="stmt"/>
<line num="37" count="1" type="stmt"/>
</file>
</project>
</coverage>
2 changes: 2 additions & 0 deletions packages/modules/index/coverage/coverage-final.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"/Users/adriendeperetti/Documents/02-personnel/01-projects/medusa/packages/modules/index/src/utils/flatten-where-clauses.ts": {"path":"/Users/adriendeperetti/Documents/02-personnel/01-projects/medusa/packages/modules/index/src/utils/flatten-where-clauses.ts","statementMap":{"0":{"start":{"line":22,"column":16},"end":{"line":22,"column":36}},"1":{"start":{"line":23,"column":38},"end":{"line":23,"column":null}},"2":{"start":{"line":26,"column":4},"end":{"line":33,"column":null}},"3":{"start":{"line":27,"column":6},"end":{"line":32,"column":null}},"4":{"start":{"line":28,"column":24},"end":{"line":28,"column":null}},"5":{"start":{"line":29,"column":8},"end":{"line":29,"column":null}},"6":{"start":{"line":31,"column":8},"end":{"line":31,"column":null}},"7":{"start":{"line":36,"column":2},"end":{"line":36,"column":null}},"8":{"start":{"line":37,"column":2},"end":{"line":37,"column":null}}},"fnMap":{"0":{"name":"flattenWhereClauses","decl":{"start":{"line":22,"column":16},"end":{"line":22,"column":36}},"loc":{"start":{"line":22,"column":62},"end":{"line":38,"column":null}}},"1":{"name":"flatten","decl":{"start":{"line":25,"column":11},"end":{"line":25,"column":19}},"loc":{"start":{"line":25,"column":58},"end":{"line":34,"column":null}}}},"branchMap":{"0":{"loc":{"start":{"line":27,"column":6},"end":{"line":32,"column":null}},"type":"if","locations":[{"start":{"line":27,"column":6},"end":{"line":32,"column":null}},{"start":{"line":30,"column":13},"end":{"line":32,"column":null}}]},"1":{"loc":{"start":{"line":28,"column":24},"end":{"line":28,"column":null}},"type":"cond-expr","locations":[{"start":{"line":28,"column":31},"end":{"line":28,"column":47}},{"start":{"line":28,"column":50},"end":{"line":28,"column":null}}]}},"s":{"0":1,"1":1,"2":2,"3":5,"4":1,"5":1,"6":4,"7":1,"8":1},"f":{"0":1,"1":2},"b":{"0":[1,4],"1":[0,1]}}
}
Loading
Loading