Skip to content

Commit

Permalink
Revert "[DOCS-3761] Remove FQL typer workarounds (#63)" (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrodewig authored Dec 11, 2024
1 parent 283076d commit cc13e92
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ 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 = Product.byName(${product.name}).first()
let p: Any = 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 = Order.create({
let newOrder: Any = Order.create({
createdAt: Time.now(),
status: ${status},
customer: Customer.byId(${customer.id}),
payment: {}
})
let product = Product.byName("Drone").first()!
let product: Any = Product.byName("Drone").first()!
OrderItem.create({ order: newOrder, product: product, quantity: 1 })
newOrder
} else {
Expand Down
3 changes: 2 additions & 1 deletion schema/collections.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ 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 = Order.byId(orderId)!
let order: Any = 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 = item.product
let product: Any = 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 = item.product
let product: Any = 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 = Customer.byId(${id})!
fql`let customer: Any = 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 = Customer.create(${{ name, email, address }})
fql`let customer: Any = 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 = Customer.byId(${id})!.update(${{ name, email, address }})
fql`let customer: Any = Customer.byId(${id})!.update(${{ name, email, address }})
// Return projected fields for the response.
${customerResponse}
`
Expand Down
13 changes: 7 additions & 6 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 = Order.byId(${id})!
fql`let order: Any = 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 = Order.byId(${id})!
let order: Any = 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 = checkout(${id}, ${status}, ${payment})
? fql`let order: Any = checkout(${id}, ${status}, ${payment})
${orderResponse}
`
: query
Expand Down Expand Up @@ -206,8 +206,9 @@ 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 = Customer.byId(${customerId})!
let customer: Any = 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 @@ -268,7 +269,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 = getOrCreateCart(${id})
fql`let order: Any = getOrCreateCart(${id})
// Return the cart as an OrderResponse object.
${orderResponse}
`
Expand Down Expand Up @@ -368,7 +369,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 = Customer.byId(${id})!.cart
fql`let order: Any = Customer.byId(${id})!.cart
// Return the order as an OrderResponse object.
${orderResponse}
`
Expand Down
13 changes: 5 additions & 8 deletions src/routes/products/products.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,14 @@ 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: product.category.id,
name: product.category.name,
description: product.category.description
}
category: { id: category.id, name: category.name, description: category.description },
}
})
`;
Expand Down Expand Up @@ -146,7 +143,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 = Product.create(args)
let product: Any = Product.create(args)
// Use projection to only return the fields you need.
product {
id,
Expand Down Expand Up @@ -222,7 +219,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 = Product.byId(${id})!
let product: Any = 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 @@ -319,7 +316,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 = Product.sortedByPriceLowToHigh({ from: ${Number(
let products: Any = Product.sortedByPriceLowToHigh({ from: ${Number(
minPrice
)}, to: ${Number(maxPrice)}})
.pageSize(${Number(pageSize)})
Expand Down

0 comments on commit cc13e92

Please sign in to comment.