Name: schemas
Owner: Reaction Commerce
Description: The collection schemas API for Reaction Commerce
Created: 2018-04-04 18:30:03.0
Updated: 2018-04-28 11:28:31.0
Pushed: 2018-04-09 14:03:18.0
Homepage: null
Size: 72
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Reaction uses MongoDB, which is a schemaless database. This allows maximum flexibility, particularly important when quickly reacting to the design challenges that uniquely different customizations require in the course of an ecommerce operation.
However, we don't want to just get completely crazy, so we define a schema that is attached to the previously schemaless collection. These schemas apply basic content and structure validation, also very necessary in ecommerce.
As we apply each additional layer of structure, it's good to remember that there are usually server methods to bypass these layers as well.
Schemas are implemented using the simpl-schema NPM package.
Reaction.Collections are defined in the common code of lib/collections
, where the SimpleSchemas defined in lib/collections/schemas
are attached. View all schemas in the API Docs.
Custom schemas can also be defined in individual packages, by creating a lib/collections/schemas
folder inside your package.
All schemas should be registered using the registerSchema()
method that Reaction uses internally to register all of our core Schemas.
rt SimpleSchema from "simpl-schema";
rt { registerSchema } from "@reactioncommerce/schemas";
t MyNewSchema = new SimpleSchema({
d: {
type: SimpleSchema.Integer,
label: "ID"
me: {
type: String,
label: "Name of thing"
scription: {
type: String,
label: "Description of thing",
optional: true
sterSchema("MyNewSchema", MyNewSchema);
All registered schemas can be retrieved by importing Schemas
.
rt Schemas from "@reactioncommerce/schemas";
View all schemas in the API Docs.
| Reaction.Schemas | * | | —————————————————————————————————- | —————– | | Email | Address | | Accounts | CartItem | | CartItem | CartItems | | Cart | DiscountType | | DiscountRules | Discounts | | Layout | OrderItem | | OrderTransaction | Order | | Permissions | Workflow | | PackageConfig | CorePackageConfig | | PaymentMethod | Invoice | | Payment | VariantMedia | | ProductPosition | ProductVariant | | Product | ShippingMethod | | ShipmentQuote | ShipmentItem | | ShippingParcel | Shipment | | ShippingProvider | Shipping | | CustomEmailSettings | Metafield | | Currency | Locale | | Shop | Tag | | TaxRates | Taxes | | Templates | Translation |
Reaction provides autoValue
helpers in /lib/collections/schemas/helpers.js
.
xample Schema with autoValue
rt SimpleSchema from "simpl-schema";
rt { createdAtAutoValue, shopIdAutoValue } from "/lib/collections/schemas/helpers";
rt const SchemaExample = new SimpleSchema({
opId: {
type: String,
index: 1,
autoValue: shopIdAutoValue,
label: "Example shopId"
eatedAt: {
type: Date,
autoValue: createdAtAutoValue
| Helper method | Return value |
| ———————— | ————————————————————————————- |
| createdAtAutoValue
| Date representing now, if it's an insert |
| updatedAtAutoValue
| Date representing now |
| shopIdAutoValue
| Current shopId |
| shopIdAutoValueForCart
| Current shopId for a cart |
| schemaIdAutoValue
| A Random.id()
only if not already set on the server, or if inserting on the client. |
| shopDefaultCountry
| The country value from the default shop |
rt { SimpleSchema } from "meteor/aldeed:simple-schema";
rt const Workflow = new SimpleSchema({
tatus": {
type: String,
defaultValue: "new"
orkflow": {
type: Array,
optional: true
orkflow.$": String
This example extends the Schemas.PackageConfig
and adds new properties to the schema.
rt SimpleSchema from "simpl-schema";
rt { PackageConfig } from "/lib/collections/schemas/registry";
rt const PaypalPackageConfig = PackageConfig.clone().extend({
Remove blackbox: true from settings obj
ettings": {
type: Object,
optional: true,
blackbox: false,
defaultValue: {}
ettings.express_enabled": {
type: Boolean,
defaultValue: true
ettings.merchantId": {
type: String,
label: "Merchant ID",
optional: true
In the previous example, we cloned an existing schema to use it as the starting point for a new schema. In other cases, you might want to skip the clone and actually mutate the original schema. This allows you to adjust the schema in a way that will be respected by all code that uses it, without that code even needing to realize that you've made adjustments.
rt SimpleSchema from "simpl-schema";
rt { CartItem } from "/lib/collections/schemas";
Item.extend({
ySpecialCartProp": {
type: String,
optional: true
If you don't want to mutate the schema attached to a collection, you can instead attach a different schema of your own making.
rt { Cart } from "/lib/collections";
mport or create myCustomSchema
.attachSchema(myCustomSchema, { replace: true });
In order to make sure that you are never circumventing any schema replacement that other plugins might have done, you should always pull a schema off of its collection rather than importing it directly.
rt { Cart } from "/lib/collections/schemas";
.validate(someObj);
This is bad because if some plugin did Cart.attachSchema(myCustomSchema, { replace: true })
, you would still be using the default schema rather than the custom one it attached.
rt { Cart } from "/lib/collections";
.simpleSchema(someObj).validate(someObj);
This is correct because (a) it pulls the schema off the collection using Cart.simpleSchema(someObj)
, and (b) it passes the object to simpleSchema
function to get the correct schema back if multiple have been attached. Refer to Multiple Schemas section.
Multiple Schema functionality allows us to use different schemas for different documents within the same collection.
To work with multi-schema you need to specify the selector. You can do this by several ways:
llection.simpleSchema(object);
And if object doesn't:
llection.simpleSchema(object, { selector: { field: 'value' } });
In /lib/collections/schemas/products.js
, we attach two different schemas to the same Products
collection.
The multiple schemas are attached to the collection with a selector option.
tion.Collections.Products.attachSchema(Reaction.Schemas.Product,
selector: { type: "simple" } });
tion.Collections.Products.attachSchema(Reaction.Schemas.ProductVariant,
selector: { type: "variant" } });
However, now whenever we update a document in the Products
collection, we need to tell it which schema to use.
Applies a schema where price
is a Number:
tion.Collections.Products.update("SMr4rhDFnYvFMtDTX", {
et: {
price: 10
lector: {
type: "variant"
Applies a schema where price
is an Object:
tion.Collections.Products.update("BCTMZ6HTxFSppJESk", {
et: {
price: {
range: "1.00 - 12.99",
min: 1.00,
max: 12.99
}
lector: {
type: "simple"
It's important to note that collections do not enforce structure, so nothing will stop you from updating a product with a "type:simple"
, using the "type:variant"
schema.
Updates where the selector is not provided must have the selector in the update statement.
Provide selector in query:
tion.Collections.Products.update(
title: "This is a product", type: "simple"
{
$set: { description: "This is a modified product" }
Provide selector in update statement:
tion.Collections.Products.update(
title: "Product One" },
$set: {
description: "This is a modified product",
type: 'simple' // selector in <update>
Provide selector as an option
tion.Collections.Products.update(
title: "Product One", type: "simple" },
$set: {
description: 'This is a modified product three.'
},
selector: { type: "simple" } }
Provide the schema selector in the insert object:
tion.Collections.Products.insert({ title: "This is a product", type: "simple"});
Provide the schema selector as options
tion.Collections.Products.insert({ title: "This is a product" }, { selector: { type: "simple" } });