IndexedDB - 存储大量数据的正确方法

时间:2017-10-13 12:08:52

标签: indexeddb dexie

我必须实现一个使用LocalDB来管理离线订单创建的Web应用程序。

在这一刻,我正在使用Dexie为Angular2 / Typescript实现LocalDB。

我要做的是实现一个数据库,以便能够管理:

  • 客户(+1000条记录)
  • 产品(+2000记录)
  • 产品
  • 按客户定义的价格+(100.000条记录)
  • 订单(少量数据)

等...

它必须管理大量数据,因为产品,客户和价格很多或是记录......

我的问题是:

如果我必须使用常见的关系数据库(如SQL SERVER或MySql)来实现此数据库, 当我撰写临时订单时,我存储了idMaterial和数量......

但我在IndexedDB中必须做些什么?

最好使用idMaterial / Qty存储临时命令,例如mySql / SqlServer,并检索信息 通过INNER JOIN进行产品(需要时)或更好地将产品/价格的所有信息存储在本地表中 为了避免INNER JOIN查询?

我可以在连接超过2000+产品的表和每个客户定价的100,000个产品之间运行INNER JOIN吗?

感谢支持!

1 个答案:

答案 0 :(得分:1)

IndexedDB的架构很像SQL数据库,因为它有表,行和事务,即使它们的名称不同(Table = ObjectStore,row = Object)。

使用Dexie,使用外键进行这种典型的连接非常容易。 IndexedDB不检查外键的约束,但您可以执行类似于SQL连接的查询。

有一个dexie的插件,dexie-relationships,可以帮助进行连接查询。

import Dexie from 'dexie'
import relationships from 'dexie-relationships'

class OrdersDB extends Dexie {
  customers: Dexie.Table<Customer, string>;
  products: Dexie.Table<Producs, string>;
  pricesPerCustomer: Dexie.Table<PricePerCustomer, string>;
  orders: Dexie.Table<Order, string>;

  constructor() {
    super ("OrdersDB", {addons: [relationships]});
    this.version(1).stores({
      customers: 'id, name',
      products: 'id, name',
      pricesPerCustomer: `
        id,
        customerId -> customers.id,
        productId -> products.id,
        [customerId+productId]`, // Optimizes compound query (see below)
      orders: `
        id,
        customerId -> customers.id,
        productId -> products.id`
    });
  }
}

interface Customer {
  id: string;
  name: string;
  orders?: Order[]; // db.customers.with({orders: 'orders'})
  prices?: PricesPerCustomer[]; // with({prices: 'pricesPerCustomer'})
}

interface Product {
  id: string;
  name: string;
  prices?: PricesPerCustomer[]; // with({prices: 'pricesPerCustomer'})
}

interface PricePerCustomer {
  id: string;
  price: number;
  currency: string;
  customerId: string;
  customer?: Customer; // with({customer: 'customerId'})
  productId: string;
  product?: Product; // with({product: 'productId'})
}  

interface Order {
  id: string;
  customerId: string;
  customer?: Customer; // with({customer: 'customerId'})
  productId: string;
  product?: Product; // with({product: 'productId'})
  quantity: number;
  price?: number; // When returned from getOrders() below.
  currency?: string; // --"--
}


const db = new OrdersDB();

/* Returns array of Customer with the "orders" and "prices" arrays attached.
*/
async function getCustomersBeginningWithA() {
  return await db.customers.where('name').startsWithIgnoreCase('a')
    .with({orders: 'orders', prices: 'pricesPerCustomer'});
}

/* Returns the price for a certain customer and product using
   a compound query (Must use Dexie 2.0 for this). The query is
   optimized if having a compound index ['customerId+productId']
   declared in the database schema (as done above).
*/
async function getPrice (customerId: string, productId: string) {
  return await db.pricesPerCustomer.get({
    customerId: customerId,
    productId: productId
  });
}

async function getOrders (customerId: string) {
  // Load orders for given customer with product property set.
  const orders = await db.orders.where({customerId: customerId})
    .with({product: 'productId'});

  // Load prices for this each customer/product
  const prices = await Promise.all(orders.map(order =>
    getPrice(customerId, order.id)));

  // Return orders with price and currency properties set:
  return orders.map((order, idx) => {
    const pricePerCustomer = prices[idx];
    return {
      ...order,
      price: pricePerCustomer.price,
      currency: pricePerCustomer.currency
    };
  });
}

请注意,我已将每个主键声明为字符串,因此您必须手动创建每个键。本来可以在模式声明中使用自动生成的数字(使用“++ id,...”而不是“id,...”)。如果是,请将表声明为Dexie.Table&lt; Customer,number&gt;而不是Dexie.Table&lt; Customer,string&gt;。