Skip to content

Commit

Permalink
[DOCS-3761] Remove FQL typer workarounds (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodewig authored Dec 4, 2024
1 parent dc73056 commit 283076d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ Customer documents and related API responses:
+ // Use a computed field to calculate the customer's cumulative purchase total.
+ // The field sums purchase `total` values from the customer's linked Order documents.
+ compute totalPurchaseAmt: Number = (customer => customer.orders.fold(0, (sum, order) => {
+ let order: Any = order
+ sum + order.total
+ }))
...
Expand Down
6 changes: 3 additions & 3 deletions __tests__/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const seedTestData = async () => {
});
productCreates.push(
faunaClient.query<Product>(fql`
let p: Any = Product.byName(${product.name}).first()
let p = Product.byName(${product.name}).first()
if (p != null) {
p!.update({ stock: ${product.stock} })
} else {
Expand Down Expand Up @@ -139,13 +139,13 @@ export const seedTestData = async () => {
await faunaClient.query<Order>(fql`
let order = Order.byCustomer(Customer.byId(${customer.id})).firstWhere(o => o.status == ${status})
if (order == null) {
let newOrder: Any = Order.create({
let newOrder = Order.create({
createdAt: Time.now(),
status: ${status},
customer: Customer.byId(${customer.id}),
payment: {}
})
let product: Any = Product.byName("Drone").first()!
let product = Product.byName("Drone").first()!
OrderItem.create({ order: newOrder, product: product, quantity: 1 })
newOrder
} else {
Expand Down
3 changes: 1 addition & 2 deletions schema/collections.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ collection Order {

compute items: Set<OrderItem> = (order => OrderItem.byOrder(order))
compute total: Number = (order => order.items.fold(0, (sum, orderItem) => {
let orderItem: Any = orderItem
if (orderItem.product != null) {
sum + orderItem.product.price * orderItem.quantity
sum + orderItem.product!.price * orderItem.quantity
} else {
sum
}
Expand Down
6 changes: 3 additions & 3 deletions schema/functions.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function getOrCreateCart(id) {

function checkout(orderId, status, payment) {
// Find the order by id, using the ! operator to assert that the order exists.
let order: Any = Order.byId(orderId)!
let order = Order.byId(orderId)!

// Check that we are setting the order to the processing status. If not, we should
// not be calling this function.
Expand Down Expand Up @@ -98,15 +98,15 @@ function checkout(orderId, status, payment) {

// Check that the order items are still in stock.
order!.items.forEach((item) => {
let product: Any = item.product
let product = item.product
if (product.stock < item.quantity) {
abort("One of the selected products does not have the requested quantity in stock.")
}
})

// Decrement the stock of each product in the order.
order!.items.forEach((item) => {
let product: Any = item.product
let product = item.product
product.update({ stock: product.stock - item.quantity })
})

Expand Down
6 changes: 3 additions & 3 deletions src/routes/customers/customers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ router.get(
// Get the Customer document by id, using the ! operator to assert that the document exists.
// If the document does not exist, Fauna will throw a document_not_found error.
// Use projection to only return the fields you need.
fql`let customer: Any = Customer.byId(${id})!
fql`let customer = Customer.byId(${id})!
// Return projected fields for the response.
${customerResponse}
`
Expand Down Expand Up @@ -95,7 +95,7 @@ router.post(
const { data: customer } = await faunaClient.query<DocumentT<Customer>>(
// Create a new Customer document with the provided fields.
// Use projection to only return the fields you need.
fql`let customer: Any = Customer.create(${{ name, email, address }})
fql`let customer = Customer.create(${{ name, email, address }})
// Return projected fields for the response.
${customerResponse}
`
Expand Down Expand Up @@ -154,7 +154,7 @@ router.patch(
// Get the Customer document by id, using the ! operator to assert that the document exists.
// If the document does not exist, Fauna will throw a document_not_found error.
// Use projection to only return the fields you need.
fql`let customer: Any = Customer.byId(${id})!.update(${{ name, email, address }})
fql`let customer = Customer.byId(${id})!.update(${{ name, email, address }})
// Return projected fields for the response.
${customerResponse}
`
Expand Down
13 changes: 6 additions & 7 deletions src/routes/orders/orders.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ router.get(
const { data: order } = await faunaClient.query<DocumentT<Order>>(
// Get the Order document by id, using the ! operator to assert that the document exists.
// If the document does not exist, Fauna will throw a document_not_found error.
fql`let order: Any = Order.byId(${id})!
fql`let order = Order.byId(${id})!
// Return the order as an OrderResponse object.
${orderResponse}
`
Expand Down Expand Up @@ -121,7 +121,7 @@ router.patch(
// error. We then use the validateOrderStatusTransition UDF to ensure that the order status transition
// is valid. If the transition is not valid, the UDF will throw an abort error.
const query = fql`
let order: Any = Order.byId(${id})!
let order = Order.byId(${id})!
// Validate the order status transition if a status is provided.
if (${status !== undefined}) {
validateOrderStatusTransition(order!.status, ${status})
Expand All @@ -148,7 +148,7 @@ router.patch(
// of each product in the order. This ensures that the product stock is updated in the same transaction
// as the order status.
status === "processing"
? fql`let order: Any = checkout(${id}, ${status}, ${payment})
? fql`let order = checkout(${id}, ${status}, ${payment})
${orderResponse}
`
: query
Expand Down Expand Up @@ -206,9 +206,8 @@ router.get(
// use the Order.byCustomer index to retrieve all orders for that customer and map over
// the results to return only the fields we care about.
const query = fql`
let customer: Any = Customer.byId(${customerId})!
let customer = Customer.byId(${customerId})!
Order.byCustomer(customer).pageSize(${pageSizeNumber}).map((order) => {
let order: Any = order
// Return the order as an OrderResponse object.
${orderResponse}
})
Expand Down Expand Up @@ -269,7 +268,7 @@ router.post(
const { data: cart } = await faunaClient.query<DocumentT<Order>>(
// Call our getOrCreateCart UDF to get the customer's cart. The function
// definition can be found 'server/schema/functions.fsl'.
fql`let order: Any = getOrCreateCart(${id})
fql`let order = getOrCreateCart(${id})
// Return the cart as an OrderResponse object.
${orderResponse}
`
Expand Down Expand Up @@ -369,7 +368,7 @@ router.get(
const { data: cart } = await faunaClient.query<DocumentT<Order>>(
// Get the customer's cart by id, using the ! operator to assert that the document exists.
// If the document does not exist, Fauna will throw a document_not_found error.
fql`let order: Any = Customer.byId(${id})!.cart
fql`let order = Customer.byId(${id})!.cart
// Return the order as an OrderResponse object.
${orderResponse}
`
Expand Down
13 changes: 8 additions & 5 deletions src/routes/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ router.get(
// new object with the desired fields.
.map(product => {
let product: Any = product
let category: Any = product.category
{
id: product.id,
name: product.name,
price: product.price,
description: product.description,
stock: product.stock,
category: { id: category.id, name: category.name, description: category.description },
category: {
id: product.category.id,
name: product.category.name,
description: product.category.description
}
}
})
`;
Expand Down Expand Up @@ -143,7 +146,7 @@ router.post(
if (category == null) abort("Category does not exist.")
// Create the product with the given values.
let args = { name: ${name}, price: ${price}, stock: ${stock}, description: ${description}, category: category }
let product: Any = Product.create(args)
let product = Product.create(args)
// Use projection to only return the fields you need.
product {
id,
Expand Down Expand Up @@ -219,7 +222,7 @@ router.patch(
fql`
// Get the product by id, using the ! operator to assert that the product exists.
// If it does not exist Fauna will throw a document_not_found error.
let product: Any = Product.byId(${id})!
let product = Product.byId(${id})!
// Get the category by name. We can use .first() here because we know that the category
// name is unique.
let category = Category.byName(${category ?? ""}).first()
Expand Down Expand Up @@ -316,7 +319,7 @@ router.get(
// which require document reads.
// Learn more about covered queries here: https://docs.fauna.com/fauna/current/learn/data_model/indexes#covered-queries
const query = fql`
let products: Any = Product.sortedByPriceLowToHigh({ from: ${Number(
let products = Product.sortedByPriceLowToHigh({ from: ${Number(
minPrice
)}, to: ${Number(maxPrice)}})
.pageSize(${Number(pageSize)})
Expand Down

0 comments on commit 283076d

Please sign in to comment.