2016-06-07 64 views
3

關係我已經像下面CurrencyExchangeRate數據庫表:自引用多到許多與Objection.js

CREATE TABLE Currency (id INT, code VARCHAR(3), name TEXT);  
CREATE TABLE ExchangeRate (baseCurrencyId INT, counterCurrencyId INT, rate FLOAT); 

INSERT INTO Currency (id, code, name) VALUES 
    (1, 'USD', 'US Dollars'), 
    (2, 'AUD', 'Australian Dollars'); 
INSERT INTO ExchangeRate (baseCurrencyId, counterCurrencyId, rate) VALUES 
    (1, 2, 1.342), 
    (2, 1, 0.745); 

給定一個baseCurrency Currency.id和的逆流Currency.code,我想找到相應的交換rate counterCurrency name

建模這種關係最有效的方法是什麼? (我使用Objection.js V0.4.0)

回答

5

我加了引號表和列名,以使它們區分大小寫:

CREATE TABLE "Currency" (
    id INT, code VARCHAR(3), name TEXT 
);  
CREATE TABLE "ExchangeRate" (
    "baseCurrencyId" INT, "counterCurrencyId" INT, rate FLOAT 
); 

INSERT INTO "Currency" (id, code, name) VALUES 
    (1, 'USD', 'US Dollars'), 
    (2, 'AUD', 'Australian Dollars'); 
INSERT INTO "ExchangeRate" ("baseCurrencyId", "counterCurrencyId", rate) VALUES 
    (1, 2, 1.342), 
    (2, 1, 0.745); 

您可以模擬爲多對多的關係,其中存儲在額外的參數加入表:

const knex = require('knex')({ 
    client: 'pg', 
    connection: 'postgres:///objection_test' 
}); 
const { Model } = require('objection'); 
const Promise = require('bluebird'); 

class Currency extends Model { 

    static get tableName() { return 'Currency'; } 

    static get relationMappings() { 
    return { 
     currencyWithRate: { 
     relation: Model.ManyToManyRelation, 
     modelClass: Currency, 
     join: { 
      from: 'Currency.id', 
      through: { 
      from: 'ExchangeRate.baseCurrencyId', 
      to: 'ExchangeRate.counterCurrencyId', 
      extra: ['rate'] 
      }, 
      to: 'Currency.id' 
     } 
     } 
    }; 
    } 
} 

const boundModel = Currency.bindKnex(knex); 

boundModel.query().then(currencies => { 
    // get related currencies for each 
    return Promise.all(currencies.map(cur => { 
    return Promise.join(cur, cur.$relatedQuery('currencyWithRate'), 
     (cur, exchangeRateCurrency) => { 
     return { 
      currency: cur, 
      exchangeRateCurrency: exchangeRateCurrency 
     }; 
     }); 
    })); 
}).then(result => { 
    console.dir(result, { depth: null}); 
}).finally(() => { 
    return knex.destroy(); 
}); 

讓我們假設上面的代碼是test.js:

Mikaels-MacBook-Pro-2: mikaelle$ node test.js 
[ { currency: 
    AnonymousModelSubclass { 
     id: 1, 
     code: 'USD', 
     name: 'US Dollars', 
     currencyWithRate: [ AnonymousModelSubclass { id: 2, code: 'AUD', name: 'Australian Dollars', rate: 1.342 } ] }, 
    exchangeRateCurrency: [ AnonymousModelSubclass { id: 2, code: 'AUD', name: 'Australian Dollars', rate: 1.342 } ] }, 
    { currency: 
    AnonymousModelSubclass { 
     id: 2, 
     code: 'AUD', 
     name: 'Australian Dollars', 
     currencyWithRate: [ AnonymousModelSubclass { id: 1, code: 'USD', name: 'US Dollars', rate: 0.745 } ] }, 
    exchangeRateCurrency: [ AnonymousModelSubclass { id: 1, code: 'USD', name: 'US Dollars', rate: 0.745 } ] } ] 
Mikaels-MacBook-Pro-2: mikaelle$ 

看起來像$relatedQuery()也會向請求關係的Model查詢對象。

+0

謝謝,相當詳細的例子。我將不得不嘗試一下,看看它是否可以解決我的問題:) – conny