codenotary / immudb

immudb - immutable database based on zero trust, SQL/Key-Value/Document model, tamperproof, data change history
https://immudb.io
Other
8.62k stars 343 forks source link

Help with practical example #713

Closed hasanAjsf closed 3 years ago

hasanAjsf commented 3 years ago

I want to connect with 2 structs (let's say tables) together, as:

// OrderHeader -
type OrderHeader struct {
    ID            string  // autonumber, Primary ID mapped with `Order` in OrderLines  below
    Ref          string
        Supplier  string
        Amount      int // Sum of `Value` from OrderLines for all those lines where Order = ID
}

// OrderLines -
type OrderLines struct {
    ID         int // Primary ID mapped with other tables/structs not presented here
    Order   string  // same as ID in OrderHeader above
        Item      string
        Qty        int
        Price     int
       Value     int // Multiplication of Qty * Price
}

Example:

order1 := OrderHeader {
      ID: // autoincrement, as this is first order, it is 0
       Ref: "Order 1",
       Supplier: "Amazon",
       Amount: 0,
}

line1 := {
    Order: 0,
        Item: "Go book 1",
        Qty: 1,
        Price: 5,
}

line2 := {
    Order: 0,
        Item: "Go book 2",
        Qty: 1,
        Price: 7,
}

// Then I need:
// 1. Calculate calue for all lines, so that line1.Value = 1*5 =5, and line2.Value = 1*7 = 7
// 2. Update the Amount in order1 to be the sum of all the values of all orders connected with order 0, i.e.
// order1.Amount = line1.Value + line2.Value = 5 + 7 = 12 

At a late stage, the Qty of line2 is required to be updated to 2, so the system required to recalculate the following, based on the same logic above:

  1. Value of line2
  2. Amount of order1

Appreciate your support. thanks

jeroiraz commented 3 years ago

Hi @hajsf, using a key-value store for this use-case will require a mapping from application level data model to key-value data model. The simplest mapping would be involve using identifiers for orders e.g. keys of the form "orderHeader_ID" and the value being the entire serialised object. Same for order lines "orderLine_OrderID_orderLineID".

Then you can retrieve a specific orderHeader by its id by using "orderHeader123" as key for instance. Or to scan over a range of orders using "orderHeader" as prefix.

Also possible to retrieve order lines for a specific order header by using prefix "orderLine_123". Or even retrieve a specific order line by having the order header and order line identifiers.

This way at the application level you can recalculate the Amount of any order and submit the new entire object to immudb.

I'd suggest to start with this coarse-grained mapping to get familiarised. But it's possible to use fine-grained mapping by which each attribute of an object is mapped to its own key-value pair.

We're working on providing SQL capabilities to immudb in the near future, using a simple relational data model will be easier for this type of use cases.

Thanks for asking

hasanAjsf commented 3 years ago

Travis alot