index.mdx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ---
  2. title: 'Data Model'
  3. sidebar_position: 0
  4. ---
  5. This section will explain the main parts of Vendure's data model and how they relate to one another. In Vendure these
  6. objects are referred to as **entities**, following the terminology of the [TypeORM](https://typeorm.io/entities) library which
  7. is used to manage the database.
  8. ## Catalog
  9. ### Products & ProductVariants
  10. Your catalog is composed of [`Products`](/reference/typescript-api/entities/product/) and [`ProductVariants`](/reference/typescript-api/entities/product-variant/).
  11. A `Product` always has _at least one_ `ProductVariant`. You can think of the product as a "container" which includes a name, description, and images that apply to all of
  12. its variants.
  13. Here's a visual example, in which we have a "Hoodie" product which is available in 3 sizes. Therefore, we have
  14. 3 variants of that product:
  15. ![Products and ProductVariants](./products-variants.webp)
  16. Multiple variants are made possible by adding one or more [`ProductOptionGroups`](/reference/typescript-api/entities/product-option-group) to
  17. the product. These option groups then define the available [`ProductOptions`](/reference/typescript-api/entities/product-option)
  18. If we were to add a new option group to the example above for "Color", with 2 options, "Black" and "White", then in total
  19. we would be able to define up to 6 variants:
  20. - Hoodie Small Black
  21. - Hoodie Small White
  22. - Hoodie Medium Black
  23. - Hoodie Medium White
  24. - Hoodie Large Black
  25. - Hoodie Large White
  26. :::info
  27. When a customer adds a product to their cart, they are adding a specific `ProductVariant` to their cart, not the `Product` itself.
  28. It is the `ProductVariant` that contains the SKU ("stock keeping unit", or product code) and price information.
  29. :::
  30. ### Facets
  31. [`Facets`](/reference/typescript-api/entities/facet/) are used to add structured labels to products and variants. A facet has
  32. one or more [`FacetValues`](/reference/typescript-api/entities/facet-value/). Facet values can be assigned to products or
  33. product variants.
  34. For example, a "Brand" facet could be used to label products with the brand name, with each facet value representing a different brand. You can
  35. also use facets to add other metadata to products and variants such as "New", "Sale", "Featured", etc.
  36. ![Facets and FacetValues](./facets.webp)
  37. These are the typical uses of facets in Vendure:
  38. - As the **basis of Collections**, in order to categorize your catalog (see next section).
  39. - To **filter products** in the storefront, also known as "faceted search". For example, a customer is on the "hoodies" collection
  40. page and wants to filter to only show Nike hoodies.
  41. - For **internal logic**, such as a promotion that applies to all variants with the "Summer Sale" facet value, or a shipping calculation
  42. that applies a surcharge to all products with the "Fragile" facet value. Such facets can be set to be private so that they
  43. are not exposed to the storefront.
  44. ### Collections
  45. [`Collections`](/reference/typescript-api/entities/collection/) are used to categorize and organize your catalog. A collection
  46. contains multiple product variants, and a product variant can belong to multiple collections. Collections can be nested to
  47. create a hierarchy of categories, which is typically used to create a menu structure in the storefront.
  48. ![Collections](./collections.webp)
  49. The specific product variants that belong to a collection are determined by the collection's [`CollectionFilters`](/reference/typescript-api/configuration/collection-filter/).
  50. A collection filter is a piece of logic which is used to determine whether a product variant should be included in the collection. By default, Vendure
  51. includes a number of collection filters:
  52. - **Filter by facet values**: Include all product variants which have a specific set of facet values.
  53. - **Filter by product variant name**: Include all product variants whose name matches a specific string.
  54. - **Manually select product variants**: Allows manual selection of individual product variants.
  55. - **Manually select products**: Allows manual selection of entire products, and then includes all variants of those products.
  56. ![Collection filters](./collection-filters.webp)
  57. It is also possible to create your own custom collection filters, which can be used to implement more complex logic. This is
  58. covered in the [custom collection filters](/TODO) guide.
  59. Collections are not _only_ used as the basis of storefront navigation. They are a general-purpose organization tool which can be used
  60. for many purposes, such as:
  61. - Creating a collection of "new arrivals" which is used on the homepage.
  62. - Creating a collection of "best sellers" which is used to display a list of popular products.
  63. - Creating a collection of "sale items" which is used to apply a discount to all products in the collection, via a promotion.
  64. ### Assets
  65. [`Assets`](/reference/typescript-api/entities/asset/) are used to store files such as images, videos, PDFs, etc. Assets can be
  66. assigned to **products**, **variants** and **collections** by default. By using [custom fields](/guides/concepts/custom-fields/) it is
  67. possible to assign assets to other entities. For example, for implementing customer profile images.
  68. ## Orders
  69. Orders are the central concept of Vendure's checkout process. An order is created when a customer adds a product to their cart, and
  70. is completed when the customer has paid for the order and the products have been shipped.
  71. :::note
  72. In Vendure, there is no distinction between a "cart" and an "order". The same entity is used for both. A "cart" is simply an order
  73. which is still "active" according to its current state.
  74. :::
  75. An [`Order`](/reference/typescript-api/entities/order/) is composed of one or more [`OrderLines`](/reference/typescript-api/entities/order-line/).
  76. Each order line represents a single product variant, and contains information such as the quantity, price, tax rate, etc.
  77. In turn, the order is associated with a [`Customer`](/reference/typescript-api/entities/customer/) and contains information such as
  78. the shipping address, billing address, shipping method, payment method, etc.
  79. ![Order](./order.webp)
  80. ## Customers
  81. A [`Customer`](/reference/typescript-api/entities/customer/) is a person who can buy from your shop. A customer can have one or more
  82. [`Addresses`](/reference/typescript-api/entities/address/), which are used for shipping and billing.
  83. If a customer has registered an account, they will have an associated [`User`](/reference/typescript-api/entities/user/). The user
  84. entity is used for authentication and authorization. **Guest checkouts** are also possible, in which case a customer will not have a user.
  85. ![Customer](./customer.webp)
  86. Customers can be organized into [`CustomerGroups`](/reference/typescript-api/entities/customer-group/). These groups can be used in
  87. logic relating to promotions, shipping rules, payment rules etc. For example, you could create a "VIP" customer group and then create
  88. a promotion which grants members of this group free shipping. Or a "B2B" group which is used in a custom tax calculator to
  89. apply a different tax rate to B2B customers.
  90. ## Administrators & Roles
  91. An [`Administrator`](/reference/typescript-api/entities/administrator/) is a user who has some degree of access to the
  92. functions of the Admin API and the Admin UI. Similar to customers, administrators are also associated with a user, which
  93. is responsible for managing authentication and authorization.
  94. Administrators are assigned one or more [`Roles`](/reference/typescript-api/entities/role/),
  95. which define specific [`Permissions`](/reference/typescript-api/common/permission/). Each query or mutation in the Admin API
  96. declares which permissions are required to execute it. If an administrator does not have the required permissions, the request
  97. will be rejected.
  98. ![Administrators & Roles](./admin-role.webp)
  99. In the example above, the administrator Sam Bailey has two roles assigned: "Order Manager" and "Catalog Manager". An administrator
  100. can have any number of roles assigned, and the permissions of all roles are combined to determine the permissions of the
  101. administrator. In this way, you can have fine-grained control over which administrators can perform which actions.
  102. There are 2 special roles which are created by default and cannot be changed:
  103. - **SuperAdmin**: This role has all permissions, and cannot be edited or deleted. It is assigned to the first administrator
  104. created when the server is started.
  105. - **Customer**: This role is assigned to all registered customers.
  106. ## Channels
  107. A [`Channel`](/reference/typescript-api/entities/channel/) represents a distinct sales channel, and has several uses in Vendure:
  108. - **Multi-tenancy**: Each channel can be configured with its own set of products, shipping methods, payment methods, etc. This
  109. allows you to run multiple shops from a single Vendure server.
  110. - **Multi-vendor**: Each channel can represent a distinct vendor or seller, which can be used to implement a marketplace.
  111. - **Region-specific stores**: Each channel can be configured with its own set of languages, currencies, tax rates, etc. This
  112. allows you to run multiple stores for different regions from a single Vendure server.
  113. - **Distinct sales channels**: Each channel can represent a sales channel of a single business, with one channel for the online
  114. store, one for selling via Amazon, one for selling via Facebook etc.
  115. There is _always_ the **default channel**, which cannot be deleted. This channel is the parent of all other channels, and also
  116. contains all entities in the entire application, no matter which channel they are associated with. This means that channel-aware
  117. entities are always associated with the default channel and _may_ additionally be associated with other channels.
  118. ![Channels](./channels.webp)
  119. Many entities are channel-aware, meaning that they can be associated with a multiple channels. The following entities are channel-aware:
  120. - [`Asset`](/reference/typescript-api/entities/asset/)
  121. - [`Collection`](/reference/typescript-api/entities/collection/)
  122. - [`Customer`](/reference/typescript-api/entities/customer-group/)
  123. - [`Facet`](/reference/typescript-api/entities/facet/)
  124. - [`FacetValue`](/reference/typescript-api/entities/facet-value/)
  125. - [`Order`](/reference/typescript-api/entities/order/)
  126. - [`PaymentMethod`](/reference/typescript-api/entities/payment-method/)
  127. - [`Product`](/reference/typescript-api/entities/product/)
  128. - [`ProductVariant`](/reference/typescript-api/entities/product-variant/)
  129. - [`Promotion`](/reference/typescript-api/entities/promotion/)
  130. - [`Role`](/reference/typescript-api/entities/role/)
  131. - [`ShippingMethod`](/reference/typescript-api/entities/shipping-method/)
  132. - [`StockLocation`](/reference/typescript-api/entities/stock-location/)
  133. Each channel is also assigned a single [`Seller`](/reference/typescript-api/entities/seller/). This entity is used to represent
  134. the vendor or seller of the products in the channel. This is useful for implementing a marketplace, where each channel represents
  135. a distinct vendor.