Search code examples
inner-joinjaydata

inner join in JayData


I use JayData liberary and I want to know that it is possible to simulate inner join in JayData, like:

Customers inner join Orders on
Customers.CustomerID = Orders.CustomerID

and how it could be possible?


Solution

  • AdHoc joins in general are not yet supported by JayData in the current release. It's on the roadmap though.

    It is however possible to achieve similar behavior on a number of ways, depending on your needs and the underlying data provider. I assume you are using the OData provider.

    In this case you can use navigation properties to represent the relation and thus achieve an implicit join on queryies.

    Entity and context definition:

    $data.Entity.extend("Customer", {
     Id: { key: true, computed: true, type: 'int' },
     Name: { type:string },
     Orders: { type: $data.EntitySet, elementType: 'Order', inverseProperty: 'Customer' }
    });
    
    $data.Entity.extend("Order" {
     Id: { key: true, computed: true, type: 'int' },
     Customer: { type: Customer, inverseProperty: 'Orders' }
    });
    
    $data.EntityCotnext.extend("CustomerOrders", {
      Customers: { type: $data.EntitySet, elementType: Customer },
      Orders: { type: $Data.EntitySet, elementType: Order }
    });
    

    1) query orders by customer:

    context.Orders.filter( function(order) {
      return order.Customer.Name.startsWith('John');
    }).toArray(...)
    

    2) query customer by orders: this is new feature released in JayData 1.1 (make sure to update your files)

    ordersQuery = context.Orders.filter( function(order) { return order.Value > 200 });
    
    context.Customers.filter( function(customer) {
     return custom.Orders.some(ordersQuery);
    }).toArray(...);