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.



More posts like this

Edgedb Exclusive Constraint

2023-04-12 | #edgedb #edgedb-constraints

The term exclusive constraint in EdgeDB refers to a type of constraint that ensures a specific property is unique or within a certain range. For example, you can create an exclusive constraint in a User object that ensures the email property is unique, thus ensuring that each user is only saved once. In the following example, we are defining an exclusive constraint that ensures uniqueness in the email property: type User { required property id -> uuid; required property name -> str; exclusive constraint unique_email on (.

Continue reading 


Edgedb Constraints

2023-04-11 | #edgedb #edgedb-constraints

Edgedb is a relational database management system (RDBMS) that provides various constraint mechanisms to securely store and manage your data while preserving the integrity of the data recorded in your database. Constraints are a set of rules used to maintain the integrity of the data stored in your database. There are five different types of constraint: 1. Primary Key Constraints: Primary key constraints ensure that each row is uniquely identified. This constraint specifies that there can only be one primary key column in a table, and each value in this column can only be used once.

Continue reading 


Edgedb Object Type

2023-04-11 | #edgedb #edgedb-types

EdgeDB can be defined as an object-oriented database management system. Data is defined as object types, and these types are defined with attributes and methods. In this article, a detailed explanation will be provided about the object type in EdgeDB. The object type is one of the object types in EdgeDB. An EdgeDB object can be thought of as a class, and these objects have attributes and methods. The object type defines the characteristics of an object type by keeping these attributes and methods together.

Continue reading 