Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

How to get sub relations #37

Open
jcphlux opened this issue Jan 23, 2020 · 0 comments
Open

How to get sub relations #37

jcphlux opened this issue Jan 23, 2020 · 0 comments

Comments

@jcphlux
Copy link

jcphlux commented Jan 23, 2020

I am trying to get the an order with a list of orderParts and each order part ref the part it is derived from as the user need to be able to customize the part details for an order. when I run the below function getById I get the order with the list of orderParts but the part is not included on each orderPart.

image

Is there a way to get sub relations?

My db class

import { Injectable } from '@angular/core';
import Dexie from 'dexie';
import relationships from 'dexie-relationships';
import { OrderPart } from '../models/order-part.model';
import { Order } from '../models/order.model';

@Injectable()
export class AppDbService extends Dexie {

  orderParts: Dexie.Table<OrderPart, number>;
  orders: Dexie.Table<Order, number>;
  parts: Dexie.Table<Part, number>;

  constructor() {
    super('MyApp', { addons: [relationships] });

    var db = this;

    db.version(1).stores({
      orderParts: '&id, orderId -> orders.id, partId -> parts.id',
      orders: '&id',
      parts: '&id'
    });

    this.orderParts = this.table('orderParts');
    this.orders = this.table('orders');
    this.parts = this.table('parts');

    db.orderParts.mapToClass(OrderPart);
    db.orders.mapToClass(Order);
    db.parts.mapToClass(Part);
  }

  async getById(id: number): Promise<T> {
    return await this.orders.where('id').equals(id).with({ parts: 'orderParts' }).then(orders => {
      return orders[0];
    });
  }
}

the Order class

export class Order {
  constructor(id?: number) {
    this.id = id;
  }

  public id: number;
  public parts: OrderPart[] = [];
}

the OrderPart class

export class OrderPart {
  constructor(id?: number, partId?: number, quantity?: number, price?: number, orderId?: number) {
    this.id = id;
    this.partId = partId;
    this.quantity = quantity;
    this.price = price;
    this.orderId = orderId;;
  }

  public id: number;
  public partId: number;
  public quantity: number;
  public price: number;
  public orderId: number;
}

the Part class

export class Part {
  constructor(id?: number, partNumber?: string, description?: string, price?: number) {
    this.id = id;
    this.partNumber = partNumber;
    this.description = description;
    this.price = price;
  }

  public id: number;
  public partNumber: string;
  public description: string;
  public price: number;
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant