feat: Add basic CRUD functionality to store module (#6510)

Adds the core model and basic CRUD around it.

Note: The store model is not complete, but I prefer doing smaller PRs so it's easier to do a proper review. Adding currencies will be a follow-up PR.
This commit is contained in:
Stevche Radevski
2024-02-26 15:23:48 +01:00
committed by GitHub
parent 7ebe885ec9
commit b13c669528
10 changed files with 598 additions and 6 deletions

View File

@@ -0,0 +1,10 @@
import { StoreTypes } from "@medusajs/types"
export const createStoreFixture: StoreTypes.CreateStoreDTO = {
name: "Test store",
default_sales_channel_id: "test-sales-channel",
default_region_id: "test-region",
metadata: {
test: "test",
},
}

View File

@@ -1,6 +1,7 @@
import { Modules } from "@medusajs/modules-sdk"
import { IStoreModuleService } from "@medusajs/types"
import { moduleIntegrationTestRunner, SuiteOptions } from "medusa-test-utils"
import { createStoreFixture } from "../__fixtures__"
jest.setTimeout(100000)
@@ -11,9 +12,88 @@ moduleIntegrationTestRunner({
service,
}: SuiteOptions<IStoreModuleService>) => {
describe("Store Module Service", () => {
describe("noop", function () {
it("should run", function () {
expect(true).toBe(true)
describe("creating a store", () => {
it("should get created successfully", async function () {
const store = await service.create(createStoreFixture)
expect(store).toEqual(
expect.objectContaining({
name: "Test store",
default_sales_channel_id: "test-sales-channel",
default_region_id: "test-region",
metadata: {
test: "test",
},
})
)
})
})
describe("upserting a store", () => {
it("should get created if it does not exist", async function () {
const store = await service.upsert(createStoreFixture)
expect(store).toEqual(
expect.objectContaining({
name: "Test store",
default_sales_channel_id: "test-sales-channel",
default_region_id: "test-region",
metadata: {
test: "test",
},
})
)
})
it("should get created if it does not exist", async function () {
const createdStore = await service.upsert(createStoreFixture)
const upsertedStore = await service.upsert({ name: "Upserted store" })
expect(upsertedStore).toEqual(
expect.objectContaining({
name: "Upserted store",
})
)
expect(upsertedStore.id).not.toEqual(createdStore.id)
})
})
describe("updating a store", () => {
it("should update the name successfully", async function () {
const createdStore = await service.create(createStoreFixture)
const updatedStore = await service.update(createdStore.id, {
title: "Updated store",
})
expect(updatedStore.title).toEqual("Updated store")
})
})
describe("deleting a store", () => {
it("should successfully delete existing stores", async function () {
const createdStore = await service.create([
createStoreFixture,
createStoreFixture,
])
await service.delete([createdStore[0].id, createdStore[1].id])
const storeInDatabase = await service.list()
expect(storeInDatabase).toHaveLength(0)
})
})
describe("retrieving a store", () => {
it("should successfully return all existing stores", async function () {
await service.create([
createStoreFixture,
{ ...createStoreFixture, name: "Another store" },
])
const storesInDatabase = await service.list()
expect(storesInDatabase).toHaveLength(2)
expect(storesInDatabase.map((s) => s.name)).toEqual(
expect.arrayContaining(["Test store", "Another store"])
)
})
})
})

View File

@@ -0,0 +1,92 @@
{
"namespaces": [
"public"
],
"name": "public",
"tables": [
{
"columns": {
"id": {
"name": "id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"name": {
"name": "name",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"default_sales_channel_id": {
"name": "default_sales_channel_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"default_region_id": {
"name": "default_region_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"default_location_id": {
"name": "default_location_id",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "text"
},
"metadata": {
"name": "metadata",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
},
"created_at": {
"name": "created_at",
"type": "timestamptz",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"length": 6,
"default": "now()",
"mappedType": "datetime"
}
},
"name": "store",
"schema": "public",
"indexes": [
{
"keyName": "store_pkey",
"columnNames": [
"id"
],
"composite": false,
"primary": true,
"unique": true
}
],
"checks": [],
"foreignKeys": {}
}
]
}

View File

@@ -0,0 +1,9 @@
import { Migration } from "@mikro-orm/migrations"
export class InitialSetup20240226130829 extends Migration {
async up(): Promise<void> {
this.addSql(
'create table if not exists "store" ("id" text not null, "name" text not null, "default_sales_channel_id" text null, "default_region_id" text null, "default_location_id" text null, "metadata" jsonb null, "created_at" timestamptz not null default now(), constraint "store_pkey" primary key ("id"));'
)
}
}

View File

@@ -13,6 +13,21 @@ export default class Store {
@PrimaryKey({ columnType: "text" })
id: string
@Property({ columnType: "text" })
name: string
@Property({ columnType: "text", nullable: true })
default_sales_channel_id: string | null = null
@Property({ columnType: "text", nullable: true })
default_region_id: string | null = null
@Property({ columnType: "text", nullable: true })
default_location_id: string | null = null
@Property({ columnType: "jsonb", nullable: true })
metadata: Record<string, unknown> | null = null
@Property({
onCreate: () => new Date(),
columnType: "timestamptz",

View File

@@ -5,11 +5,21 @@ import {
ModulesSdkTypes,
IStoreModuleService,
StoreTypes,
Context,
} from "@medusajs/types"
import { ModulesSdkUtils } from "@medusajs/utils"
import {
InjectManager,
InjectTransactionManager,
MedusaContext,
ModulesSdkUtils,
isString,
promiseAll,
removeUndefined,
} from "@medusajs/utils"
import { Store } from "@models"
import { entityNameToLinkableKeysMap, joinerConfig } from "../joiner-config"
import { UpdateStoreInput } from "@types"
const generateMethodForModels = []
@@ -44,4 +54,132 @@ export default class StoreModuleService<TEntity extends Store = Store>
__joinerConfig(): ModuleJoinerConfig {
return joinerConfig
}
async create(
data: StoreTypes.CreateStoreDTO[],
sharedContext?: Context
): Promise<StoreTypes.StoreDTO[]>
async create(
data: StoreTypes.CreateStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO>
@InjectManager("baseRepository_")
async create(
data: StoreTypes.CreateStoreDTO | StoreTypes.CreateStoreDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
const input = Array.isArray(data) ? data : [data]
const result = await this.create_(input, sharedContext)
return await this.baseRepository_.serialize<StoreTypes.StoreDTO[]>(
Array.isArray(data) ? result : result[0]
)
}
@InjectTransactionManager("baseRepository_")
async create_(
data: StoreTypes.CreateStoreDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<Store[]> {
let normalizedInput = StoreModuleService.normalizeInput(data)
return await this.storeService_.create(normalizedInput, sharedContext)
}
async upsert(
data: StoreTypes.UpsertStoreDTO[],
sharedContext?: Context
): Promise<StoreTypes.StoreDTO[]>
async upsert(
data: StoreTypes.UpsertStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO>
@InjectTransactionManager("baseRepository_")
async upsert(
data: StoreTypes.UpsertStoreDTO | StoreTypes.UpsertStoreDTO[],
@MedusaContext() sharedContext: Context = {}
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
const input = Array.isArray(data) ? data : [data]
const forUpdate = input.filter(
(store): store is UpdateStoreInput => !!store.id
)
const forCreate = input.filter(
(store): store is StoreTypes.CreateStoreDTO => !store.id
)
const operations: Promise<Store[]>[] = []
if (forCreate.length) {
operations.push(this.create_(forCreate, sharedContext))
}
if (forUpdate.length) {
operations.push(this.update_(forUpdate, sharedContext))
}
const result = (await promiseAll(operations)).flat()
return await this.baseRepository_.serialize<
StoreTypes.StoreDTO[] | StoreTypes.StoreDTO
>(Array.isArray(data) ? result : result[0])
}
async update(
id: string,
data: StoreTypes.UpdateStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO>
async update(
selector: StoreTypes.FilterableStoreProps,
data: StoreTypes.UpdateStoreDTO,
sharedContext?: Context
): Promise<StoreTypes.StoreDTO[]>
@InjectManager("baseRepository_")
async update(
idOrSelector: string | StoreTypes.FilterableStoreProps,
data: StoreTypes.UpdateStoreDTO,
@MedusaContext() sharedContext: Context = {}
): Promise<StoreTypes.StoreDTO | StoreTypes.StoreDTO[]> {
let normalizedInput: UpdateStoreInput[] = []
if (isString(idOrSelector)) {
normalizedInput = [{ id: idOrSelector, ...data }]
} else {
const stores = await this.storeService_.list(
idOrSelector,
{},
sharedContext
)
normalizedInput = stores.map((store) => ({
id: store.id,
...data,
}))
}
const updateResult = await this.update_(normalizedInput, sharedContext)
const stores = await this.baseRepository_.serialize<
StoreTypes.StoreDTO[] | StoreTypes.StoreDTO
>(updateResult)
return isString(idOrSelector) ? stores[0] : stores
}
@InjectTransactionManager("baseRepository_")
protected async update_(
data: UpdateStoreInput[],
@MedusaContext() sharedContext: Context = {}
): Promise<Store[]> {
const normalizedInput = StoreModuleService.normalizeInput(data)
return await this.storeService_.update(normalizedInput, sharedContext)
}
private static normalizeInput<T extends StoreTypes.UpdateStoreDTO>(
stores: T[]
): T[] {
return stores.map((store) =>
removeUndefined({
...store,
name: store.name?.trim(),
})
)
}
}

View File

@@ -1,6 +1,9 @@
import { StoreTypes } from "@medusajs/types"
import { IEventBusModuleService, Logger } from "@medusajs/types"
export type InitializeModuleInjectableDependencies = {
logger?: Logger
eventBusService?: IEventBusModuleService
}
export type UpdateStoreInput = StoreTypes.UpdateStoreDTO & { id: string }

View File

@@ -1,3 +1,15 @@
import { BaseFilterable } from "../../dal"
export interface StoreDTO {
id: string
name: string
default_sales_channel_id?: string
default_region_id?: string
default_location_id?: string
metadata: Record<string, any> | null
}
export interface FilterableStoreProps
extends BaseFilterable<FilterableStoreProps> {
id?: string | string[]
name?: string | string[]
}

View File

@@ -1 +1,24 @@
export interface CreateStoreDTO {}
export interface CreateStoreDTO {
name: string
default_sales_channel_id?: string
default_region_id?: string
default_location_id?: string
metadata?: Record<string, any>
}
export interface UpsertStoreDTO {
id?: string
name?: string
default_sales_channel_id?: string
default_region_id?: string
default_location_id?: string
metadata?: Record<string, any>
}
export interface UpdateStoreDTO {
name?: string
default_sales_channel_id?: string
default_region_id?: string
default_location_id?: string
metadata?: Record<string, any>
}

View File

@@ -1,3 +1,213 @@
import { FindConfig } from "../common"
import { IModuleService } from "../modules-sdk"
import { Context } from "../shared-context"
import { FilterableStoreProps, StoreDTO } from "./common"
import { CreateStoreDTO, UpdateStoreDTO, UpsertStoreDTO } from "./mutations"
export interface IStoreModuleService extends IModuleService {}
/**
* The main service interface for the store module.
*/
export interface IStoreModuleService extends IModuleService {
/**
* This method creates stores.
*
* @param {CreateStoreDTO[]} data - The stores to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<StoreDTO[]>} The created stores.
*
* @example
* {example-code}
*/
create(data: CreateStoreDTO[], sharedContext?: Context): Promise<StoreDTO[]>
/**
* This method creates a store.
*
* @param {CreateStoreDTO} data - The store to be created.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<StoreDTO>} The created store.
*
* @example
* {example-code}
*/
create(data: CreateStoreDTO, sharedContext?: Context): Promise<StoreDTO>
/**
* This method updates existing stores, or creates new ones if they don't exist.
*
* @param {UpsertStoreDTO[]} data - The attributes to update or create in each store.
* @returns {Promise<StoreDTO[]>} The updated and created stores.
*
* @example
* {example-code}
*/
upsert(data: UpsertStoreDTO[], sharedContext?: Context): Promise<StoreDTO[]>
/**
* This method updates an existing store, or creates a new one if it doesn't exist.
*
* @param {UpsertStoreDTO} data - The attributes to update or create for the store.
* @returns {Promise<StoreDTO>} The updated or created store.
*
* @example
* {example-code}
*/
upsert(data: UpsertStoreDTO, sharedContext?: Context): Promise<StoreDTO>
/**
* This method updates an existing store.
*
* @param {string} id - The store's ID.
* @param {UpdateStoreDTO} data - The details to update in the store.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<StoreDTO>} The updated store.
*/
update(
id: string,
data: UpdateStoreDTO,
sharedContext?: Context
): Promise<StoreDTO>
/**
* This method updates existing stores.
*
* @param {FilterableStoreProps} selector - The filters to specify which stores should be updated.
* @param {UpdateStoreDTO} data - The details to update in the stores.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<StoreDTO[]>} The updated stores.
*
* @example
* {example-code}
*/
update(
selector: FilterableStoreProps,
data: UpdateStoreDTO,
sharedContext?: Context
): Promise<StoreDTO[]>
/**
* This method deletes stores by their IDs.
*
* @param {string[]} ids - The list of IDs of stores to delete.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves when the stores are deleted.
*
* @example
* {example-code}
*/
delete(ids: string[], sharedContext?: Context): Promise<void>
/**
* This method deletes a store by its ID.
*
* @param {string} id - The ID of the store.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<void>} Resolves when the store is deleted.
*
* @example
* {example-code}
*/
delete(id: string, sharedContext?: Context): Promise<void>
/**
* This method retrieves a store by its ID.
*
* @param {string} id - The ID of the retrieve.
* @param {FindConfig<StoreDTO>} config - The configurations determining how the store is retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a store.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<StoreDTO>} The retrieved store.
*
* @example
* A simple example that retrieves a {type name} by its ID:
*
* ```ts
* {example-code}
* ```
*
* To specify relations that should be retrieved:
*
* ```ts
* {example-code}
* ```
*
*
*/
retrieve(
id: string,
config?: FindConfig<StoreDTO>,
sharedContext?: Context
): Promise<StoreDTO>
/**
* This method retrieves a paginated list of stores based on optional filters and configuration.
*
* @param {FilterableStoreProps} filters - The filters to apply on the retrieved store.
* @param {FindConfig<StoreDTO>} config - The configurations determining how the store is retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a store.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<StoreDTO[]>} The list of stores.
*
* @example
* To retrieve a list of {type name} using their IDs:
*
* ```ts
* {example-code}
* ```
*
* To specify relations that should be retrieved within the {type name}:
*
* ```ts
* {example-code}
* ```
*
* By default, only the first `{default limit}` records are retrieved. You can control pagination by specifying the `skip` and `take` properties of the `config` parameter:
*
* ```ts
* {example-code}
* ```
*
*
*/
list(
filters?: FilterableStoreProps,
config?: FindConfig<StoreDTO>,
sharedContext?: Context
): Promise<StoreDTO[]>
/**
* This method retrieves a paginated list of stores along with the total count of available stores satisfying the provided filters.
*
* @param {FilterableStoreProps} filters - The filters to apply on the retrieved store.
* @param {FindConfig<StoreDTO>} config - The configurations determining how the store is retrieved. Its properties, such as `select` or `relations`, accept the
* attributes or relations associated with a store.
* @param {Context} sharedContext - A context used to share resources, such as transaction manager, between the application and the module.
* @returns {Promise<[StoreDTO[], number]>} The list of stores along with their total count.
*
* @example
* To retrieve a list of {type name} using their IDs:
*
* ```ts
* {example-code}
* ```
*
* To specify relations that should be retrieved within the {type name}:
*
* ```ts
* {example-code}
* ```
*
* By default, only the first `{default limit}` records are retrieved. You can control pagination by specifying the `skip` and `take` properties of the `config` parameter:
*
* ```ts
* {example-code}
* ```
*
*
*/
listAndCount(
filters?: FilterableStoreProps,
config?: FindConfig<StoreDTO>,
sharedContext?: Context
): Promise<[StoreDTO[], number]>
}