Exemplo de entidade composta
Esta página mostra uma entidade composta, Orders.details, armazenada no MongoDB em um stage e no PostgreSQL em outro, e como a mesma consulta retorna o mesmo resultado em ambos.
Um ponto no nome de uma entidade define uma entidade composta: Orders.details é uma entidade composta de Orders.
Se a fonte for NoSQL, os detalhes são armazenados dentro do pedido, em uma única coleção; se a fonte for relacional, eles são armazenados em tabelas separadas. No domínio, eles são tratados da mesma forma, qualquer que seja o tipo de fonte, de modo que o modelo não depende da tecnologia de persistência.
Entidade composta
Neste exemplo, Orders.details é uma entidade composta de Orders. Sua relação order, com target: details, cria a relação composta details em Orders.
Parte do esquema onde a entidade composta é definida:
domain:
entities:
...
- name: Orders
primaryKey: [id]
indexes:
- name: orderDate
fields: [orderDate]
properties:
- name: id
type: integer
required: true
autoIncrement: true
- name: customerId
required: true
length: 5
- name: orderDate
type: dateTime
- name: Orders.details
primaryKey: [orderId, productId]
properties:
- name: orderId
required: true
type: integer
- name: productId
required: true
type: integer
- name: unitPrice
type: decimal
- name: quantity
type: decimal
relations:
- name: order
from: orderId
entity: Orders
to: id
target: details
...
Esquema completo:
domain:
entities:
- name: Address
abstract: true
indexes:
- name: postalCode
fields: ["postalCode"]
- name: region
fields: ["region", "country"]
- name: city
fields: ["city"]
properties:
- name: address
- name: city
- name: region
- name: postalCode
length: 20
- name: country
- name: Categories
primaryKey: ["id"]
uniqueKey: ["name"]
properties:
- name: id
type: integer
required: true
autoIncrement: true
- name: name
required: true
- name: Customers
extends: Address
primaryKey: ["id"]
indexes:
- name: name
fields: ["name"]
properties:
- name: id
length: 5
required: true
- name: name
required: true
- name: Products
primaryKey: ["id"]
uniqueKey: ["name", "supplierId"]
properties:
- name: id
type: integer
required: true
autoIncrement: true
- name: name
required: true
- name: categoryId
type: integer
- name: quantity
- name: price
type: decimal
default: 0
relations:
- name: category
from: categoryId
entity: Categories
to: id
target: products
- name: Orders
primaryKey: ["id"]
indexes:
- name: orderDate
fields: ["orderDate"]
properties:
- name: id
type: integer
required: true
autoIncrement: true
- name: customerId
required: true
length: 5
- name: orderDate
type: dateTime
relations:
- name: customer
from: customerId
entity: Customers
to: id
target: orders
- name: Orders.details
primaryKey: ["orderId", "productId"]
properties:
- name: orderId
required: true
type: integer
- name: productId
required: true
type: integer
- name: unitPrice
type: decimal
- name: quantity
type: decimal
relations:
- name: order
from: orderId
entity: Orders
to: id
target: details
- name: product
from: productId
entity: Products
to: id
target: orderDetails
infrastructure:
views:
- name: default
mappings:
- name: default
entities:
- name: Address
abstract: true
properties:
- name: address
mapping: Address
- name: city
mapping: City
- name: region
mapping: Region
- name: postalCode
mapping: PostalCode
- name: country
mapping: Country
- name: Categories
mapping: Categories
properties:
- name: id
mapping: CategoryID
- name: name
mapping: CategoryName
- name: Customers
extends: Address
mapping: Customers
properties:
- name: id
mapping: CustomerID
- name: name
mapping: CompanyName
- name: Products
mapping: Products
properties:
- name: id
mapping: ProductID
- name: name
mapping: ProductName
- name: categoryId
mapping: CategoryID
- name: quantity
mapping: QuantityPerUnit
- name: price
mapping: UnitPrice
- name: Orders
mapping: Orders
properties:
- name: id
mapping: OrderID
- name: customerId
mapping: CustomerID
- name: orderDate
mapping: OrderDate
- name: Orders.details
mapping: Order Details
properties:
- name: orderId
mapping: OrderID
- name: productId
mapping: ProductID
- name: unitPrice
mapping: UnitPrice
- name: quantity
mapping: Quantity
- name: mongoDb
extends: default
entities:
- name: Orders
sequence: SQ_ORDERS
properties:
- name: id
mapping: _id
sources:
- name: Catalog
dialect: MySQL
mapping: default
connection: ${CNN_MYSQL}
- name: Crm
dialect: PostgreSQL
mapping: default
connection: ${CNN_POSTGRES}
- name: Ordering
dialect: MongoDB
mapping: mongoDb
connection: ${CNN_MONGODB}
stages:
- name: default
sources:
- name: Catalog
condition: entity.in(["Categories","Products"])
- name: Crm
condition: entity.in(["Address","Customers"])
- name: Ordering
condition: entity.in(["Orders","Orders.details"])
Variáveis de ambiente:
CNN_MYSQL={"host":"localhost","port":3306,"user":"test","password":"test","database":"test"}
CNN_POSTGRES={"host":"localhost","port":5433,"user":"test","password":"test","database":"test"}
CNN_MONGODB={"url":"mongodb://test:test@localhost:27017","database":"test"}
Laboratório:
Entidade composta em fontes diferentes
Este exemplo mostra como uma entidade composta é armazenada em fontes diferentes.
No stage default, Orders e Orders.details são armazenadas no MongoDB; no stage insights, são armazenadas no PostgreSQL.
Parte do esquema onde os stages são configurados:
...
stages:
- name: default
sources:
- name: Catalog
condition: entity.in(["Categories","Products"])
- name: Crm
condition: entity.in(["Address","Customers"])
- name: Ordering
condition: entity.in(["Orders","Orders.details"])
- name: insights
sources:
- name: Insights
...
Esquema completo:
domain:
enums:
entities:
- name: Address
abstract: true
indexes:
- name: postalCode
fields: ["postalCode"]
- name: region
fields: ["region", "country"]
- name: city
fields: ["city"]
properties:
- name: address
- name: city
- name: region
- name: postalCode
length: 20
- name: country
- name: Categories
primaryKey: ["id"]
uniqueKey: ["name"]
properties:
- name: id
type: integer
required: true
autoIncrement: true
- name: name
required: true
- name: Customers
extends: Address
primaryKey: ["id"]
indexes:
- name: name
fields: ["name"]
properties:
- name: id
length: 5
required: true
- name: name
required: true
- name: Products
primaryKey: ["id"]
uniqueKey: ["name", "supplierId"]
properties:
- name: id
type: integer
required: true
autoIncrement: true
- name: name
required: true
- name: categoryId
type: integer
- name: quantity
- name: price
type: decimal
default: 0
relations:
- name: category
from: categoryId
entity: Categories
to: id
target: products
- name: Orders
primaryKey: ["id"]
indexes:
- name: orderDate
fields: ["orderDate"]
properties:
- name: id
type: integer
required: true
autoIncrement: true
- name: customerId
required: true
length: 5
- name: orderDate
type: dateTime
relations:
- name: customer
from: customerId
entity: Customers
to: id
target: orders
- name: Orders.details
primaryKey: ["orderId", "productId"]
properties:
- name: orderId
required: true
type: integer
- name: productId
required: true
type: integer
- name: unitPrice
type: decimal
- name: quantity
type: decimal
relations:
- name: order
from: orderId
entity: Orders
to: id
target: details
- name: product
from: productId
entity: Products
to: id
target: orderDetails
infrastructure:
views:
- name: default
mappings:
- name: default
entities:
- name: Address
abstract: true
properties:
- name: address
mapping: Address
- name: city
mapping: City
- name: region
mapping: Region
- name: postalCode
mapping: PostalCode
- name: country
mapping: Country
- name: Categories
mapping: Categories
properties:
- name: id
mapping: CategoryID
- name: name
mapping: CategoryName
- name: Customers
extends: Address
mapping: Customers
properties:
- name: id
mapping: CustomerID
- name: name
mapping: CompanyName
- name: Products
mapping: Products
properties:
- name: id
mapping: ProductID
- name: name
mapping: ProductName
- name: categoryId
mapping: CategoryID
- name: quantity
mapping: QuantityPerUnit
- name: price
mapping: UnitPrice
- name: Orders
mapping: Orders
properties:
- name: id
mapping: OrderID
- name: customerId
mapping: CustomerID
- name: orderDate
mapping: OrderDate
- name: Orders.details
mapping: Order Details
properties:
- name: orderId
mapping: OrderID
- name: productId
mapping: ProductID
- name: unitPrice
mapping: UnitPrice
- name: quantity
mapping: Quantity
- name: mongoDb
extends: default
entities:
- name: Orders
sequence: SQ_ORDERS
properties:
- name: id
mapping: _id
sources:
- name: Catalog
dialect: MySQL
mapping: default
connection: ${CNN_MYSQL}
- name: Crm
dialect: PostgreSQL
mapping: default
connection: ${CNN_POSTGRES}
- name: Ordering
dialect: MongoDB
mapping: mongoDb
connection: ${CNN_MONGODB}
- name: Insights
dialect: PostgreSQL
mapping: default
connection: ${CNN_INSIGHTS}
stages:
- name: default
sources:
- name: Catalog
condition: entity.in(["Categories","Products"])
- name: Crm
condition: entity.in(["Address","Customers"])
- name: Ordering
condition: entity.in(["Orders","Orders.details"])
- name: insights
sources:
- name: Insights
Pedidos no MongoDB
No MongoDB, os detalhes são armazenados dentro de cada documento da coleção Orders:

Pedidos no PostgreSQL
No PostgreSQL, os pedidos são armazenados na tabela Orders:

e os detalhes na tabela Order Details:

Consultas
A mesma consulta, executada em cada stage, retorna os mesmos pedidos com seus detalhes (apenas o formato das datas depende do mecanismo de banco de dados).
Consulta no stage default
lambdaorm execute -e ".env" -s default -q "Orders.filter(p => p.customerId == customerId).include(p => [p.customer.map(p => p.name), p.details.include(p => p.product.include(p => p.category.map(p => p.name)).map(p => p.name)).map(p => [p.quantity, p.unitPrice])]).sort(p=> p.id).page(1,1)" -d "{\"customerId\": \"HANAR\"}"
Resultado:
[
{
"id": 3,
"customerId": "HANAR",
"employeeId": 4,
"orderDate": "1996-07-08T00:00:00.000+02:00",
"requiredDate": "1996-08-05",
"shippedDate": "1996-07-12",
"shipViaId": 2,
"freight": 65.83,
"name": "Hanari Carnes",
"address": "Rua do Pao, 67",
"city": "Rio de Janeiro",
"region": "RJ",
"postalCode": "05454-876",
"country": "Brazil",
"details": [
{
"quantity": 10,
"unitPrice": 7.7,
"product": {
"name": "Jack's New England Clam Chowder",
"category": {
"name": "Seafood"
}
}
},
{
"quantity": 35,
"unitPrice": 42.4,
"product": {
"name": "Manjimup Dried Apples",
"category": {
"name": "Produce"
}
}
},
{
"quantity": 15,
"unitPrice": 16.8,
"product": {
"name": "Louisiana Fiery Hot Pepper Sauce",
"category": {
"name": "Condiments"
}
}
}
],
"customer": {
"name": "Hanari Carnes"
}
}
]
Mesma consulta no stage insights
lambdaorm execute -e ".env" -s insights -q "Orders.filter(p => p.customerId == customerId).include(p => [p.customer.map(p => p.name), p.details.include(p => p.product.include(p => p.category.map(p => p.name)).map(p => p.name)).map(p => [p.quantity, p.unitPrice])]).sort(p=> p.id).page(1,1)" -d "{\"customerId\": \"HANAR\"}"
Resultado:
[
{
"id": 3,
"customerId": "HANAR",
"employeeId": 4,
"orderDate": "1996-07-07T22:00:00.000Z",
"requiredDate": "1996-08-04T22:00:00.000Z",
"shippedDate": "1996-07-11T22:00:00.000Z",
"shipViaId": 2,
"freight": 65.83,
"name": "Hanari Carnes",
"address": "Rua do Pao, 67",
"city": "Rio de Janeiro",
"region": "RJ",
"postalCode": "05454-876",
"country": "Brazil",
"customer": {
"name": "Hanari Carnes"
},
"details": [
{
"quantity": 10,
"unitPrice": 7.7,
"product": {
"name": "Jack's New England Clam Chowder",
"category": {
"name": "Seafood"
}
}
},
{
"quantity": 35,
"unitPrice": 42.4,
"product": {
"name": "Manjimup Dried Apples",
"category": {
"name": "Produce"
}
}
},
{
"quantity": 15,
"unitPrice": 16.8,
"product": {
"name": "Louisiana Fiery Hot Pepper Sauce",
"category": {
"name": "Condiments"
}
}
}
]
}
]