-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): add Order and Trade models
This commit creates new database models for orders and trades. The `Order` model is used to persist all data related to a specific order, whereas the `Trade` model will be used for persisting data about completed trades. Some order-specific properties from `SwapDeal` have moved to the `Order` model. Rather than repeat that data for each `SwapDeal` instance that swaps the same order, which results in data duplication and creates the possibility for inconsistencies, swaps for the same order now point to the same `Order` instance. This also attempts to add all known associations between models for easy join queries via the Sequelize API going forward. Closes #621.
- Loading branch information
Showing
10 changed files
with
168 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Sequelize from 'sequelize'; | ||
import { db } from '../../types'; | ||
|
||
export default (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) => { | ||
const attributes: db.SequelizeAttributes<db.OrderAttributes> = { | ||
id: { type: DataTypes.STRING, primaryKey: true, allowNull: false }, | ||
nodeId: { type: DataTypes.INTEGER }, | ||
localId: { type: DataTypes.STRING }, | ||
initialQuantity: { type: DataTypes.DECIMAL(8), allowNull: true }, | ||
pairId: { type: DataTypes.STRING, allowNull: false }, | ||
price: { type: DataTypes.DECIMAL(8), allowNull: false }, | ||
isBuy: { type: DataTypes.BOOLEAN, allowNull: false }, | ||
createdAt: { type: DataTypes.BIGINT, allowNull: false }, | ||
}; | ||
|
||
const options: Sequelize.DefineOptions<db.OrderInstance> = { | ||
tableName: 'orders', | ||
timestamps: false, | ||
}; | ||
|
||
const Order = sequelize.define<db.OrderInstance, db.OrderAttributes>('Order', attributes, options); | ||
|
||
Order.associate = (models: Sequelize.Models) => { | ||
models.Order.belongsTo(models.Node, { | ||
foreignKey: 'nodeId', | ||
constraints: true, | ||
}); | ||
models.Order.belongsTo(models.Pair, { | ||
foreignKey: 'pairId', | ||
constraints: false, | ||
}); | ||
models.Order.hasMany(models.Trade, { | ||
as: 'makerTrades', | ||
foreignKey: 'makerOrderId', | ||
constraints: true, | ||
}); | ||
models.Order.hasMany(models.Trade, { | ||
as: 'takerTrades', | ||
foreignKey: 'takerOrderId', | ||
constraints: true, | ||
}); | ||
models.Order.hasMany(models.SwapDeal, { | ||
foreignKey: 'orderId', | ||
constraints: true, | ||
}); | ||
}; | ||
|
||
return Order; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Sequelize from 'sequelize'; | ||
import { db } from '../../types'; | ||
|
||
export default (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) => { | ||
const attributes: db.SequelizeAttributes<db.TradeAttributes> = { | ||
makerOrderId: { type: DataTypes.STRING, primaryKey: true }, | ||
takerOrderId: { type: DataTypes.STRING, primaryKey: true }, | ||
quantity: { type: DataTypes.DECIMAL(8), allowNull: false }, | ||
}; | ||
|
||
const options: Sequelize.DefineOptions<db.TradeInstance> = { | ||
tableName: 'trades', | ||
timestamps: true, | ||
updatedAt: false, | ||
}; | ||
|
||
const Trade = sequelize.define<db.TradeInstance, db.TradeAttributes>('Trade', attributes, options); | ||
|
||
Trade.associate = (models: Sequelize.Models) => { | ||
models.Trade.belongsTo(models.Order, { | ||
as: 'makerOrder', | ||
foreignKey: 'makerOrderId', | ||
constraints: true, | ||
}); | ||
models.Trade.belongsTo(models.Order, { | ||
as: 'takerOrder', | ||
foreignKey: 'takerOrderId', | ||
constraints: true, | ||
}); | ||
}; | ||
|
||
return Trade; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters