Edgedb Computed Links
[tr] Türkçe Oku 2023-04-12
Computed Links is a structure used to define relationships between two objects. These links are created by using a defined query or function to calculate the relationship between associated objects.
Computed Links is a structure used to define relationships between two objects. These connections are created by using a defined query or function to calculate the relationship between the associated objects. This structure is very useful for ensuring consistency of the data held in the database. In addition to establishing a tight connection between data, Computed Links also ensure that the links are automatically updated when the data stored in the database is modified or updated.
Additionally, it can also be used to facilitate querying and reporting operations. For example, by calculating the relationship between two objects through a Computed Link
, the querying process can be made more efficient.
This structure can also be used in large and complex data models thanks to Edgedb’s high-performance and efficient database engine.
For example, for an e-commerce website, an order can have multiple products and each product can have different stock levels, prices, and other attributes. In this case, a Computed Link can be used to establish a connection between orders and products.
Table A could be a table named Order
and it could include the order number, date, customer information, and other attributes of orders. Table B could be a table named Product
and it could include the name, stock level, price, and other attributes of products.
To achieve this, you can use a Computed Link to connect each order to multiple products. For example, you can define a Computed Link using the order_id
field and the product_id
field. This link will ensure that each order in the orders table is connected to the relevant product in the products table.
The following example shows how to create this Computed Link structure in the Edgedb language:
type Order {
required property order_id -> str;
required property created_at -> datetime;
required link customers -> Customer;
multi link products := (
WITH
order := (SELECT Order FILTER .order_id = <str>$self.order_id),
order_product := (SELECT OrderProduct FILTER .order_id = order.order_id)
SELECT Product FILTER .product_id = order_product.product_id
);
}
type Product {
required property product_id -> str;
required property name -> str;
required property price -> decimal;
required property stock_level -> int16;
}
type OrderProduct {
required link order -> Order;
required link product -> Product;
}
In this example, two separate tables named Order
and Product
are defined. In the Order
table, a Computed Link named “products” is defined, which enables each order in the Order table to be linked to the corresponding product in the Product
table.
In Computed Link structure, multiple relationships are specified using multi link
. In this example, the expression “multi link products” allows each order to be connected to multiple products.
The query inside the WITH
clause of the Computed Link is used to determine the related objects. In this example, a variable named order
is created to identify the products of each order, and then another variable named order_product
is created using this variable. This query is used to filter OrderProduct
objects that contain the products of the specified order.
Computed Link is used in conjunction with the SELECT
statement to determine the related objects. In this example, the Computed Link is used with the SELECT Product
statement to select the related products.
A Computed Link has been used between the Order
and Product
tables to enable each order to be linked to multiple products. This connection is very useful to ensure data consistency, facilitate query operations, and make the data model more flexible.
Let’s write a query example using the tables and Computed Link in the example above. The query will list the order number and the names and prices of the products related to that order.
SELECT
s.order_id,
ARRAY_AGG(u.name) AS product_name,
ARRAY_AGG(u.price) AS product_price
FROM
Order s
LEFT JOIN OrderProduct su ON s.order_id = su.order.order_id
LEFT JOIN Product u ON su.product.product_id = u.product_id
GROUP BY
s.order_id;
This query uses the Order and OrderProduct tables, joining them together and utilizing the Computed Link to determine the products for each order. As a result, the query lists the order number along with the names and prices of the products associated with that order
By using the LEFT JOIN
statement, results can be displayed even if some orders do not have any products yet. The GROUP BY
statement ensures that only one row is created for each order when the products for each order are combined.