-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.fsl
115 lines (93 loc) · 2.43 KB
/
collections.fsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
collection Customer {
name: String
email: String
address: {
street: String,
city: String,
state: String,
postalCode: String,
country: String
}
compute cart: Order? = (customer => Order.byCustomerAndStatus(customer, 'cart').first())
// Use a computed field to get the set of Orders for a customer.
compute orders: Set<Order> = ( customer => Order.byCustomer(customer))
// Use a unique constraint to ensure no two customers have the same email.
unique [.email]
index byEmail {
terms [.email]
}
}
collection Product {
name: String
description: String
// Use an Integer to represent cents.
// This avoids floating-point precision issues.
price: Int
category: Ref<Category>
stock: Int
// Use a unique constraint to ensure no two products have the same name.
unique [.name]
check stockIsValid (product => product.stock >= 0)
check priceIsValid (product => product.price > 0)
index byCategory {
terms [.category]
}
index sortedByCategory {
values [.category]
}
index byName {
terms [.name]
}
index sortedByPriceLowToHigh {
values [.price, .name, .description, .stock]
}
}
collection Category {
name: String
description: String
compute products: Set<Product> = (category => Product.byCategory(category))
unique [.name]
index byName {
terms [.name]
}
}
collection Order {
customer: Ref<Customer>
status: "cart" | "processing" | "shipped" | "delivered"
createdAt: Time
compute items: Set<OrderItem> = (order => OrderItem.byOrder(order))
compute total: Number = (order => order.items.fold(0, (sum, orderItem) => {
if (orderItem.product != null) {
sum + orderItem.product!.price * orderItem.quantity
} else {
sum
}
}))
payment: { *: Any }
check oneOrderInCart (order => {
Order.byCustomerAndStatus(order.customer, "cart").count() <= 1
})
// Define an index to get all orders for a customer. Orders will be sorted by
// createdAt in descending order.
index byCustomer {
terms [.customer]
values [desc(.createdAt), .status]
}
index byCustomerAndStatus {
terms [.customer, .status]
}
}
collection OrderItem {
order: Ref<Order>
product: Ref<Product>
quantity: Int
unique [.order, .product]
check positiveQuantity (orderItem => orderItem.quantity > 0)
index byOrder {
terms [.order]
values [.product, .quantity]
}
index byOrderAndProduct {
terms [.order, .product]
}
}