@medusajs/product 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/dist/index.d.ts +8 -0
  2. package/dist/index.js +24 -0
  3. package/dist/initialize/index.d.ts +4 -0
  4. package/dist/initialize/index.js +11 -0
  5. package/dist/loaders/connection.d.ts +4 -0
  6. package/dist/loaders/connection.js +56 -0
  7. package/dist/loaders/container.d.ts +4 -0
  8. package/dist/loaders/container.js +76 -0
  9. package/dist/loaders/index.d.ts +2 -0
  10. package/dist/loaders/index.js +18 -0
  11. package/dist/migrations/Migration20230609132805.d.ts +4 -0
  12. package/dist/migrations/Migration20230609132805.js +42 -0
  13. package/dist/models/index.d.ts +6 -0
  14. package/dist/models/index.js +18 -0
  15. package/dist/models/product-category.d.ts +20 -0
  16. package/dist/models/product-category.js +123 -0
  17. package/dist/models/product-collection.d.ts +9 -0
  18. package/dist/models/product-collection.js +55 -0
  19. package/dist/models/product-option-value.d.ts +12 -0
  20. package/dist/models/product-option-value.js +59 -0
  21. package/dist/models/product-option.d.ts +14 -0
  22. package/dist/models/product-option.js +68 -0
  23. package/dist/models/product-tag.d.ts +11 -0
  24. package/dist/models/product-tag.js +55 -0
  25. package/dist/models/product-type.d.ts +8 -0
  26. package/dist/models/product-type.js +44 -0
  27. package/dist/models/product-variant.d.ts +34 -0
  28. package/dist/models/product-variant.js +162 -0
  29. package/dist/models/product.d.ts +43 -0
  30. package/dist/models/product.js +177 -0
  31. package/dist/module-definition.d.ts +2 -0
  32. package/dist/module-definition.js +44 -0
  33. package/dist/repositories/index.d.ts +5 -0
  34. package/dist/repositories/index.js +13 -0
  35. package/dist/repositories/product-category.d.ts +16 -0
  36. package/dist/repositories/product-category.js +86 -0
  37. package/dist/repositories/product-collection.d.ts +15 -0
  38. package/dist/repositories/product-collection.js +46 -0
  39. package/dist/repositories/product-tag.d.ts +15 -0
  40. package/dist/repositories/product-tag.js +46 -0
  41. package/dist/repositories/product-variant.d.ts +15 -0
  42. package/dist/repositories/product-variant.js +46 -0
  43. package/dist/repositories/product.d.ts +21 -0
  44. package/dist/repositories/product.js +72 -0
  45. package/dist/scripts/bin/run-migration-down.d.ts +2 -0
  46. package/dist/scripts/bin/run-migration-down.js +31 -0
  47. package/dist/scripts/bin/run-migration-up.d.ts +2 -0
  48. package/dist/scripts/bin/run-migration-up.js +31 -0
  49. package/dist/scripts/bin/run-seed.d.ts +2 -0
  50. package/dist/scripts/bin/run-seed.js +37 -0
  51. package/dist/scripts/index.d.ts +2 -0
  52. package/dist/scripts/index.js +18 -0
  53. package/dist/scripts/migration-down.d.ts +10 -0
  54. package/dist/scripts/migration-down.js +51 -0
  55. package/dist/scripts/migration-up.d.ts +10 -0
  56. package/dist/scripts/migration-up.js +51 -0
  57. package/dist/scripts/seed.d.ts +5 -0
  58. package/dist/scripts/seed.js +87 -0
  59. package/dist/services/index.d.ts +6 -0
  60. package/dist/services/index.js +18 -0
  61. package/dist/services/product-category.d.ts +11 -0
  62. package/dist/services/product-category.js +18 -0
  63. package/dist/services/product-collection.d.ts +11 -0
  64. package/dist/services/product-collection.js +17 -0
  65. package/dist/services/product-module-service.d.ts +25 -0
  66. package/dist/services/product-module-service.js +36 -0
  67. package/dist/services/product-tag.d.ts +11 -0
  68. package/dist/services/product-tag.js +16 -0
  69. package/dist/services/product-variant.d.ts +11 -0
  70. package/dist/services/product-variant.js +13 -0
  71. package/dist/services/product.d.ts +15 -0
  72. package/dist/services/product.js +43 -0
  73. package/dist/types/index.d.ts +17 -0
  74. package/dist/types/index.js +2 -0
  75. package/dist/utils/create-connection.d.ts +3 -0
  76. package/dist/utils/create-connection.js +35 -0
  77. package/dist/utils/index.d.ts +3 -0
  78. package/dist/utils/index.js +19 -0
  79. package/dist/utils/load-database-config.d.ts +7 -0
  80. package/dist/utils/load-database-config.js +59 -0
  81. package/dist/utils/query/index.d.ts +6 -0
  82. package/dist/utils/query/index.js +35 -0
  83. package/package.json +62 -0
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.runMigrations = void 0;
27
+ const utils_1 = require("../utils");
28
+ const ProductModels = __importStar(require("../models"));
29
+ /**
30
+ * This script is only valid for mikro orm managers. If a user provide a custom manager
31
+ * he is in charge of running the migrations.
32
+ * @param options
33
+ * @param logger
34
+ * @param moduleDeclaration
35
+ */
36
+ async function runMigrations({ options, logger, } = {}) {
37
+ logger ?? (logger = console);
38
+ const dbData = (0, utils_1.loadDatabaseConfig)(options);
39
+ const entities = Object.values(ProductModels);
40
+ const orm = await (0, utils_1.createConnection)(dbData, entities);
41
+ try {
42
+ const migrator = orm.getMigrator();
43
+ await migrator.up();
44
+ logger?.info("Product module migration executed");
45
+ }
46
+ catch (error) {
47
+ logger?.error(`Product module migration failed to run - Error: ${error}`);
48
+ }
49
+ await orm.close();
50
+ }
51
+ exports.runMigrations = runMigrations;
@@ -0,0 +1,5 @@
1
+ import { LoaderOptions } from "@medusajs/types";
2
+ import { ProductServiceInitializeCustomDataLayerOptions, ProductServiceInitializeOptions } from "../types";
3
+ export declare function run({ options, logger, path, }: Partial<Pick<LoaderOptions<ProductServiceInitializeOptions | ProductServiceInitializeCustomDataLayerOptions>, "options" | "logger">> & {
4
+ path: string;
5
+ }): Promise<void>;
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.run = void 0;
27
+ const utils_1 = require("../utils");
28
+ const ProductModels = __importStar(require("../models"));
29
+ const _models_1 = require("../models");
30
+ const os_1 = require("os");
31
+ const path_1 = require("path");
32
+ async function run({ options, logger, path, }) {
33
+ logger?.info(`Loading seed data from ${path}...`);
34
+ const { productCategoriesData, productsData, variantsData } = await Promise.resolve().then(() => __importStar(require((0, path_1.resolve)(process.cwd(), path)))).catch((e) => {
35
+ logger?.error(`Failed to load seed data from ${path}. Please, provide a relative path and check that you export the following productCategoriesData, productsData, variantsData.${os_1.EOL}${e}`);
36
+ throw e;
37
+ });
38
+ logger ?? (logger = console);
39
+ const dbData = (0, utils_1.loadDatabaseConfig)(options);
40
+ const entities = Object.values(ProductModels);
41
+ const orm = await (0, utils_1.createConnection)(dbData, entities);
42
+ const manager = orm.em.fork();
43
+ try {
44
+ logger?.info("Inserting product categories, products and variants...");
45
+ await createProductCategories(manager, productCategoriesData);
46
+ await createProducts(manager, productsData);
47
+ await createProductVariants(manager, variantsData);
48
+ }
49
+ catch (e) {
50
+ logger?.error(`Failed to insert the seed data in the PostgreSQL database ${dbData.clientUrl}.${os_1.EOL}${e}`);
51
+ }
52
+ await orm.close(true);
53
+ }
54
+ exports.run = run;
55
+ async function createProductCategories(manager, categoriesData) {
56
+ const categories = [];
57
+ for (let categoryData of categoriesData) {
58
+ let categoryDataClone = { ...categoryData };
59
+ let parentCategory = null;
60
+ const parentCategoryId = categoryDataClone.parent_category_id;
61
+ delete categoryDataClone.parent_category_id;
62
+ if (parentCategoryId) {
63
+ parentCategory = await manager.findOne(_models_1.ProductCategory, parentCategoryId);
64
+ }
65
+ const category = await manager.create(_models_1.ProductCategory, {
66
+ ...categoryDataClone,
67
+ parent_category: parentCategory,
68
+ });
69
+ categories.push(category);
70
+ }
71
+ await manager.persistAndFlush(categories);
72
+ return categories;
73
+ }
74
+ async function createProducts(manager, data) {
75
+ const products = data.map((productData) => {
76
+ return manager.create(_models_1.Product, productData);
77
+ });
78
+ await manager.persistAndFlush(products);
79
+ return products;
80
+ }
81
+ async function createProductVariants(manager, data) {
82
+ const variants = data.map((variantsData) => {
83
+ return manager.create(_models_1.ProductVariant, variantsData);
84
+ });
85
+ await manager.persistAndFlush(variants);
86
+ return variants;
87
+ }
@@ -0,0 +1,6 @@
1
+ export { default as ProductModuleService } from "./product-module-service";
2
+ export { default as ProductService } from "./product";
3
+ export { default as ProductTagService } from "./product-tag";
4
+ export { default as ProductVariantService } from "./product-variant";
5
+ export { default as ProductCollectionService } from "./product-collection";
6
+ export { default as ProductCategoryService } from "./product-category";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ProductCategoryService = exports.ProductCollectionService = exports.ProductVariantService = exports.ProductTagService = exports.ProductService = exports.ProductModuleService = void 0;
7
+ var product_module_service_1 = require("./product-module-service");
8
+ Object.defineProperty(exports, "ProductModuleService", { enumerable: true, get: function () { return __importDefault(product_module_service_1).default; } });
9
+ var product_1 = require("./product");
10
+ Object.defineProperty(exports, "ProductService", { enumerable: true, get: function () { return __importDefault(product_1).default; } });
11
+ var product_tag_1 = require("./product-tag");
12
+ Object.defineProperty(exports, "ProductTagService", { enumerable: true, get: function () { return __importDefault(product_tag_1).default; } });
13
+ var product_variant_1 = require("./product-variant");
14
+ Object.defineProperty(exports, "ProductVariantService", { enumerable: true, get: function () { return __importDefault(product_variant_1).default; } });
15
+ var product_collection_1 = require("./product-collection");
16
+ Object.defineProperty(exports, "ProductCollectionService", { enumerable: true, get: function () { return __importDefault(product_collection_1).default; } });
17
+ var product_category_1 = require("./product-category");
18
+ Object.defineProperty(exports, "ProductCategoryService", { enumerable: true, get: function () { return __importDefault(product_category_1).default; } });
@@ -0,0 +1,11 @@
1
+ import { ProductCategory } from "../models";
2
+ import { DAL, FindConfig, ProductTypes, SharedContext } from "@medusajs/types";
3
+ declare type InjectedDependencies = {
4
+ productCategoryRepository: DAL.RepositoryService;
5
+ };
6
+ export default class ProductCategoryService<TEntity = ProductCategory> {
7
+ protected readonly productCategoryRepository_: DAL.RepositoryService;
8
+ constructor({ productCategoryRepository }: InjectedDependencies);
9
+ list(filters?: ProductTypes.FilterableProductCategoryProps, config?: FindConfig<ProductTypes.ProductCategoryDTO>, sharedContext?: SharedContext): Promise<TEntity[]>;
10
+ }
11
+ export {};
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("../utils");
4
+ class ProductCategoryService {
5
+ constructor({ productCategoryRepository }) {
6
+ this.productCategoryRepository_ = productCategoryRepository;
7
+ }
8
+ async list(filters = {}, config = {}, sharedContext) {
9
+ const transformOptions = {
10
+ includeDescendantsTree: filters?.include_descendants_tree || false
11
+ };
12
+ delete filters.include_descendants_tree;
13
+ const queryOptions = (0, utils_1.buildQuery)(filters, config);
14
+ queryOptions.where ?? (queryOptions.where = {});
15
+ return await this.productCategoryRepository_.find(queryOptions, transformOptions);
16
+ }
17
+ }
18
+ exports.default = ProductCategoryService;
@@ -0,0 +1,11 @@
1
+ import { ProductCollection } from "../models";
2
+ import { DAL, FindConfig, ProductTypes, SharedContext } from "@medusajs/types";
3
+ declare type InjectedDependencies = {
4
+ productCollectionRepository: DAL.RepositoryService;
5
+ };
6
+ export default class ProductCollectionService<TEntity = ProductCollection> {
7
+ protected readonly productCollectionRepository_: DAL.RepositoryService<TEntity>;
8
+ constructor({ productCollectionRepository }: InjectedDependencies);
9
+ list(filters?: ProductTypes.FilterableProductCollectionProps, config?: FindConfig<ProductTypes.ProductCollectionDTO>, sharedContext?: SharedContext): Promise<TEntity[]>;
10
+ }
11
+ export {};
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("../utils");
4
+ class ProductCollectionService {
5
+ constructor({ productCollectionRepository }) {
6
+ this.productCollectionRepository_ = productCollectionRepository;
7
+ }
8
+ async list(filters = {}, config = {}, sharedContext) {
9
+ const queryOptions = (0, utils_1.buildQuery)(filters, config);
10
+ queryOptions.where ?? (queryOptions.where = {});
11
+ if (filters.title) {
12
+ queryOptions.where["title"] = { $like: filters.title };
13
+ }
14
+ return await this.productCollectionRepository_.find(queryOptions);
15
+ }
16
+ }
17
+ exports.default = ProductCollectionService;
@@ -0,0 +1,25 @@
1
+ import { ProductCategoryService, ProductCollectionService, ProductService, ProductTagService, ProductVariantService } from ".";
2
+ import { Product, ProductCategory, ProductCollection, ProductTag, ProductVariant } from "../models";
3
+ import { FindConfig, ProductTypes, SharedContext } from "@medusajs/types";
4
+ declare type InjectedDependencies = {
5
+ productService: ProductService<any>;
6
+ productVariantService: ProductVariantService<any>;
7
+ productTagService: ProductTagService<any>;
8
+ productCategoryService: ProductCategoryService<any>;
9
+ productCollectionService: ProductCollectionService<any>;
10
+ };
11
+ export default class ProductModuleService<TProduct = Product, TProductVariant = ProductVariant, TProductTag = ProductTag, TProductCollection = ProductCollection, TProductCategory = ProductCategory> implements ProductTypes.IProductModuleService<TProduct, TProductVariant, TProductTag, TProductCollection, TProductCategory> {
12
+ protected readonly productService_: ProductService<TProduct>;
13
+ protected readonly productVariantService: ProductVariantService<TProductVariant>;
14
+ protected readonly productCategoryService: ProductCategoryService<TProductCategory>;
15
+ protected readonly productTagService: ProductTagService<TProductTag>;
16
+ protected readonly productCollectionService: ProductCollectionService<TProductCollection>;
17
+ constructor({ productService, productVariantService, productTagService, productCategoryService, productCollectionService, }: InjectedDependencies);
18
+ list(filters?: ProductTypes.FilterableProductProps, config?: FindConfig<ProductTypes.ProductDTO>, sharedContext?: SharedContext): Promise<ProductTypes.ProductDTO[]>;
19
+ listAndCount(filters?: ProductTypes.FilterableProductProps, config?: FindConfig<ProductTypes.ProductDTO>, sharedContext?: SharedContext): Promise<[ProductTypes.ProductDTO[], number]>;
20
+ listVariants(filters?: ProductTypes.FilterableProductVariantProps, config?: FindConfig<ProductTypes.ProductVariantDTO>, sharedContext?: SharedContext): Promise<ProductTypes.ProductVariantDTO[]>;
21
+ listTags(filters?: ProductTypes.FilterableProductTagProps, config?: FindConfig<ProductTypes.ProductTagDTO>, sharedContext?: SharedContext): Promise<ProductTypes.ProductTagDTO[]>;
22
+ listCollections(filters?: ProductTypes.FilterableProductCollectionProps, config?: FindConfig<ProductTypes.ProductCollectionDTO>, sharedContext?: SharedContext): Promise<ProductTypes.ProductCollectionDTO[]>;
23
+ listCategories(filters?: ProductTypes.FilterableProductCategoryProps, config?: FindConfig<ProductTypes.ProductCategoryDTO>, sharedContext?: SharedContext): Promise<ProductTypes.ProductCategoryDTO[]>;
24
+ }
25
+ export {};
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ class ProductModuleService {
4
+ constructor({ productService, productVariantService, productTagService, productCategoryService, productCollectionService, }) {
5
+ this.productService_ = productService;
6
+ this.productVariantService = productVariantService;
7
+ this.productTagService = productTagService;
8
+ this.productCategoryService = productCategoryService;
9
+ this.productCollectionService = productCollectionService;
10
+ }
11
+ async list(filters = {}, config = {}, sharedContext) {
12
+ const products = await this.productService_.list(filters, config, sharedContext);
13
+ return JSON.parse(JSON.stringify(products));
14
+ }
15
+ async listAndCount(filters = {}, config = {}, sharedContext) {
16
+ const [products, count] = await this.productService_.listAndCount(filters, config, sharedContext);
17
+ return [JSON.parse(JSON.stringify(products)), count];
18
+ }
19
+ async listVariants(filters = {}, config = {}, sharedContext) {
20
+ const variants = await this.productVariantService.list(filters, config, sharedContext);
21
+ return JSON.parse(JSON.stringify(variants));
22
+ }
23
+ async listTags(filters = {}, config = {}, sharedContext) {
24
+ const tags = await this.productTagService.list(filters, config, sharedContext);
25
+ return JSON.parse(JSON.stringify(tags));
26
+ }
27
+ async listCollections(filters = {}, config = {}, sharedContext) {
28
+ const collections = await this.productCollectionService.list(filters, config, sharedContext);
29
+ return JSON.parse(JSON.stringify(collections));
30
+ }
31
+ async listCategories(filters = {}, config = {}, sharedContext) {
32
+ const categories = await this.productCategoryService.list(filters, config, sharedContext);
33
+ return JSON.parse(JSON.stringify(categories));
34
+ }
35
+ }
36
+ exports.default = ProductModuleService;
@@ -0,0 +1,11 @@
1
+ import { ProductTag } from "../models";
2
+ import { DAL, FindConfig, ProductTypes, SharedContext } from "@medusajs/types";
3
+ declare type InjectedDependencies = {
4
+ productTagRepository: DAL.RepositoryService;
5
+ };
6
+ export default class ProductTagService<TEntity = ProductTag> {
7
+ protected readonly productTagRepository_: DAL.RepositoryService<TEntity>;
8
+ constructor({ productTagRepository }: InjectedDependencies);
9
+ list(filters?: ProductTypes.FilterableProductTagProps, config?: FindConfig<ProductTypes.ProductTagDTO>, sharedContext?: SharedContext): Promise<TEntity[]>;
10
+ }
11
+ export {};
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("../utils");
4
+ class ProductTagService {
5
+ constructor({ productTagRepository }) {
6
+ this.productTagRepository_ = productTagRepository;
7
+ }
8
+ async list(filters = {}, config = {}, sharedContext) {
9
+ const queryOptions = (0, utils_1.buildQuery)(filters, config);
10
+ if (filters.value) {
11
+ queryOptions.where["value"] = { $ilike: filters.value };
12
+ }
13
+ return await this.productTagRepository_.find(queryOptions);
14
+ }
15
+ }
16
+ exports.default = ProductTagService;
@@ -0,0 +1,11 @@
1
+ import { ProductVariant } from "../models";
2
+ import { DAL, FindConfig, ProductTypes, SharedContext } from "@medusajs/types";
3
+ declare type InjectedDependencies = {
4
+ productVariantRepository: DAL.RepositoryService;
5
+ };
6
+ export default class ProductVariantService<TEntity = ProductVariant> {
7
+ protected readonly productVariantRepository_: DAL.RepositoryService<TEntity>;
8
+ constructor({ productVariantRepository }: InjectedDependencies);
9
+ list(filters?: ProductTypes.FilterableProductVariantProps, config?: FindConfig<ProductTypes.ProductVariantDTO>, sharedContext?: SharedContext): Promise<TEntity[]>;
10
+ }
11
+ export {};
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("../utils");
4
+ class ProductVariantService {
5
+ constructor({ productVariantRepository }) {
6
+ this.productVariantRepository_ = productVariantRepository;
7
+ }
8
+ async list(filters = {}, config = {}, sharedContext) {
9
+ const queryOptions = (0, utils_1.buildQuery)(filters, config);
10
+ return await this.productVariantRepository_.find(queryOptions);
11
+ }
12
+ }
13
+ exports.default = ProductVariantService;
@@ -0,0 +1,15 @@
1
+ import { ProductTagService, ProductVariantService } from ".";
2
+ import { Product } from "../models";
3
+ import { DAL, FindConfig, ProductTypes, SharedContext } from "@medusajs/types";
4
+ declare type InjectedDependencies = {
5
+ productRepository: DAL.RepositoryService;
6
+ productVariantService: ProductVariantService;
7
+ productTagService: ProductTagService;
8
+ };
9
+ export default class ProductService<TEntity = Product> {
10
+ protected readonly productRepository_: DAL.RepositoryService<TEntity>;
11
+ constructor({ productRepository }: InjectedDependencies);
12
+ list(filters?: ProductTypes.FilterableProductProps, config?: FindConfig<ProductTypes.ProductDTO>, sharedContext?: SharedContext): Promise<TEntity[]>;
13
+ listAndCount(filters?: ProductTypes.FilterableProductProps, config?: FindConfig<ProductTypes.ProductDTO>, sharedContext?: SharedContext): Promise<[TEntity[], number]>;
14
+ }
15
+ export {};
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("../utils");
4
+ class ProductService {
5
+ constructor({ productRepository }) {
6
+ this.productRepository_ = productRepository;
7
+ }
8
+ async list(filters = {}, config = {}, sharedContext) {
9
+ if (filters.category_ids) {
10
+ if (Array.isArray(filters.category_ids)) {
11
+ filters.categories = {
12
+ id: { $in: filters.category_ids },
13
+ };
14
+ }
15
+ else {
16
+ filters.categories = {
17
+ id: filters.category_ids,
18
+ };
19
+ }
20
+ delete filters.category_ids;
21
+ }
22
+ const queryOptions = (0, utils_1.buildQuery)(filters, config);
23
+ return await this.productRepository_.find(queryOptions);
24
+ }
25
+ async listAndCount(filters = {}, config = {}, sharedContext) {
26
+ if (filters.category_ids) {
27
+ if (Array.isArray(filters.category_ids)) {
28
+ filters.categories = {
29
+ id: { $in: filters.category_ids },
30
+ };
31
+ }
32
+ else {
33
+ filters.categories = {
34
+ id: filters.category_ids,
35
+ };
36
+ }
37
+ delete filters.category_ids;
38
+ }
39
+ const queryOptions = (0, utils_1.buildQuery)(filters, config);
40
+ return await this.productRepository_.findAndCount(queryOptions);
41
+ }
42
+ }
43
+ exports.default = ProductService;
@@ -0,0 +1,17 @@
1
+ import { Constructor, DAL, IEventBusService } from "@medusajs/types";
2
+ export declare type ProductServiceInitializeOptions = {
3
+ database: {
4
+ clientUrl: string;
5
+ schema?: string;
6
+ driverOptions?: Record<string, unknown>;
7
+ };
8
+ };
9
+ export declare type ProductServiceInitializeCustomDataLayerOptions = {
10
+ manager?: any;
11
+ repositories?: {
12
+ [key: string]: Constructor<DAL.RepositoryService>;
13
+ };
14
+ };
15
+ export declare type InitializeModuleInjectableDependencies = {
16
+ eventBusService?: IEventBusService;
17
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ import { PostgreSqlDriver } from "@mikro-orm/postgresql";
2
+ import { ProductServiceInitializeOptions } from "../types";
3
+ export declare function createConnection(database: ProductServiceInitializeOptions["database"], entities: any[]): Promise<import("@mikro-orm/core").MikroORM<PostgreSqlDriver>>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createConnection = void 0;
4
+ const path_1 = require("path");
5
+ const postgresql_1 = require("@mikro-orm/postgresql");
6
+ const migrations_1 = require("@mikro-orm/migrations");
7
+ async function createConnection(database, entities) {
8
+ const orm = await postgresql_1.MikroORM.init({
9
+ discovery: { disableDynamicFileAccess: true },
10
+ entities,
11
+ debug: process.env.NODE_ENV === "development",
12
+ baseDir: process.cwd(),
13
+ clientUrl: database.clientUrl,
14
+ schema: database.schema ?? "public",
15
+ driverOptions: database.driverOptions ?? {
16
+ connection: { ssl: true },
17
+ },
18
+ tsNode: process.env.APP_ENV === "development",
19
+ type: "postgresql",
20
+ migrations: {
21
+ path: (0, path_1.resolve)(__dirname, "../../dist/migrations"),
22
+ pathTs: (0, path_1.resolve)(__dirname, "../../src/migrations"),
23
+ glob: "!(*.d).{js,ts}",
24
+ disableForeignKeys: false,
25
+ silent: false,
26
+ dropTables: false,
27
+ transactional: true,
28
+ allOrNothing: true,
29
+ safe: true,
30
+ generator: migrations_1.TSMigrationGenerator,
31
+ },
32
+ });
33
+ return orm;
34
+ }
35
+ exports.createConnection = createConnection;
@@ -0,0 +1,3 @@
1
+ export * from "./query";
2
+ export * from "./create-connection";
3
+ export * from "./load-database-config";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./query"), exports);
18
+ __exportStar(require("./create-connection"), exports);
19
+ __exportStar(require("./load-database-config"), exports);
@@ -0,0 +1,7 @@
1
+ import { ProductServiceInitializeCustomDataLayerOptions, ProductServiceInitializeOptions } from "../types";
2
+ /**
3
+ * Load the config for the database connection. The options can be retrieved
4
+ * through PRODUCT_* (e.g PRODUCT_POSTGRES_URL) or * (e.g POSTGRES_URL) environment variables or the options object.
5
+ * @param options
6
+ */
7
+ export declare function loadDatabaseConfig(options?: ProductServiceInitializeOptions | ProductServiceInitializeCustomDataLayerOptions): ProductServiceInitializeOptions["database"];
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadDatabaseConfig = void 0;
4
+ const utils_1 = require("@medusajs/utils");
5
+ function getEnv(key) {
6
+ const value = process.env[`PRODUCT_${key}`] ?? process.env[`${key}`];
7
+ return value ?? "";
8
+ }
9
+ function isProductServiceInitializeOptions(obj) {
10
+ return !!obj?.database;
11
+ }
12
+ function getDefaultDriverOptions(clientUrl) {
13
+ const localOptions = {
14
+ connection: {
15
+ ssl: false,
16
+ },
17
+ };
18
+ const remoteOptions = {
19
+ connection: {
20
+ ssl: {
21
+ rejectUnauthorized: false,
22
+ },
23
+ },
24
+ };
25
+ if (clientUrl) {
26
+ return clientUrl.match(/localhost/i) ? localOptions : remoteOptions;
27
+ }
28
+ return process.env.NODE_ENV?.match(/prod/i)
29
+ ? remoteOptions
30
+ : process.env.NODE_ENV?.match(/dev/i)
31
+ ? localOptions
32
+ : {};
33
+ }
34
+ /**
35
+ * Load the config for the database connection. The options can be retrieved
36
+ * through PRODUCT_* (e.g PRODUCT_POSTGRES_URL) or * (e.g POSTGRES_URL) environment variables or the options object.
37
+ * @param options
38
+ */
39
+ function loadDatabaseConfig(options) {
40
+ const clientUrl = getEnv("POSTGRES_URL");
41
+ const database = {
42
+ clientUrl: getEnv("POSTGRES_URL"),
43
+ schema: getEnv("POSTGRES_SCHEMA") ?? "public",
44
+ driverOptions: JSON.parse(getEnv("POSTGRES_DRIVER_OPTIONS") ||
45
+ JSON.stringify(getDefaultDriverOptions(clientUrl))),
46
+ };
47
+ if (isProductServiceInitializeOptions(options)) {
48
+ database.clientUrl = options.database.clientUrl ?? database.clientUrl;
49
+ database.schema = options.database.schema ?? database.schema;
50
+ database.driverOptions =
51
+ options.database.driverOptions ??
52
+ getDefaultDriverOptions(database.clientUrl);
53
+ }
54
+ if (!database.clientUrl) {
55
+ throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_ARGUMENT, "No database clientUrl provided. Please provide the clientUrl through the PRODUCT_POSTGRES_URL or POSTGRES_URL environment variable or the options object in the initialize function.");
56
+ }
57
+ return database;
58
+ }
59
+ exports.loadDatabaseConfig = loadDatabaseConfig;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Move to a new build query utils
3
+ */
4
+ import { DAL, FindConfig } from "@medusajs/types";
5
+ export declare function deduplicateIfNecessary<T = any>(collection: T | T[]): T | T[];
6
+ export declare function buildQuery<T = any, TDto = any>(filters?: Record<string, any>, config?: FindConfig<TDto>): DAL.FindOptions<T>;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildQuery = exports.deduplicateIfNecessary = void 0;
4
+ const utils_1 = require("@medusajs/utils");
5
+ function deduplicateIfNecessary(collection) {
6
+ return Array.isArray(collection) ? [...new Set(collection)] : collection;
7
+ }
8
+ exports.deduplicateIfNecessary = deduplicateIfNecessary;
9
+ function buildQuery(filters = {}, config = {}) {
10
+ const where = {};
11
+ buildWhere(filters, where);
12
+ const findOptions = {
13
+ populate: config.relations ?? [],
14
+ fields: config.select,
15
+ limit: config.take,
16
+ offset: config.skip,
17
+ };
18
+ return { where, options: findOptions };
19
+ }
20
+ exports.buildQuery = buildQuery;
21
+ function buildWhere(filters = {}, where = {}) {
22
+ for (let [prop, value] of Object.entries(filters)) {
23
+ if (Array.isArray(value)) {
24
+ value = deduplicateIfNecessary(value);
25
+ where[prop] = ["$in", "$nin"].includes(prop) ? value : { $in: value };
26
+ continue;
27
+ }
28
+ if ((0, utils_1.isObject)(value)) {
29
+ where[prop] = {};
30
+ buildWhere(value, where[prop]);
31
+ continue;
32
+ }
33
+ where[prop] = value;
34
+ }
35
+ }