Last active
July 24, 2016 13:27
-
-
Save CarlLee/6b7b028d810bf648f603a509efed1ac6 to your computer and use it in GitHub Desktop.
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
var Sequelize = require('sequelize'); | |
var DataTypes = Sequelize.DataTypes; | |
var sequelize = new Sequelize( | |
// ignore configs | |
); | |
// Represents user account information | |
var User = sequelize.define("user", { | |
username : { type : DataTypes.STRING }, | |
password : { type : DataTypes.STRING }, | |
balance : { type : DataTypes.INTEGER } | |
}); | |
// Create user friend relations | |
User.belongsToMany(User, { | |
through : "user_friends", | |
foreignKey: "friend_id", | |
as : "Friends" | |
}); | |
// Represent a request to borrow or lend money, may be denied | |
var LendingContract = sequelize.define("lending_contract", { | |
// The amount of money to be lent or borrowed | |
amount : { type : DataTypes.DECIMAL(10, 2), allowNull : false }, | |
// Monthly interest rate of this contract, | |
monthly_interest_rate : { type : DataTypes.FLOAT, allowNull : false, defaultValue: 0 }, | |
// The amount of days within which the lender is supposed to be repaid | |
duration_days : { type : DataTypes.INTEGER }, | |
// Represents who initiated this contract | |
author : { | |
type: DataTypes.INTEGER, | |
references : { | |
model : User, | |
key : "id" | |
}, | |
allowNull : false | |
}, | |
// Represents who will accept or deny this contract | |
target : { | |
type: DataTypes.INTEGER, | |
references : { | |
model : User, | |
key : "id" | |
}, | |
allowNull : false | |
}, | |
// Represents who the lender is | |
lender : { | |
type: DataTypes.INTEGER, | |
references : { | |
model : User, | |
key : "id" | |
}, | |
allowNull : false | |
}, | |
// Represents who the lendee is, lender & lendee column can be replaced by a simple boolean column "is_lending". | |
// But in that case, both author & target can be lender or lendee, that can be very confusing when writing business logic. | |
// This approach is clearer to read and easier to program. | |
lendee : { | |
type: DataTypes.INTEGER, | |
references : { | |
model : User, | |
key : "id" | |
}, | |
allowNull : false | |
}, | |
// The state of this contract | |
state : { | |
type : DataTypes.ENUM, | |
values : ["PENDING", "ACCEPTED", "DENIED", "REPAID"] | |
} | |
}); | |
// This records the actual bank transaction from user to user for the purpose of auditing. | |
// If contract_id is null, this is a simple transaction without being required to be repaid. | |
var Transaction = sequelize.define("transaction", { | |
amount : { type : DataTypes.DECIMAL(10, 2), allowNull : false }, | |
from_user_id : { | |
type: DataTypes.INTEGER, | |
references : { | |
model : User, | |
key : "id" | |
}, | |
allowNull : false | |
}, | |
to_user_id : { | |
type: DataTypes.INTEGER, | |
references : { | |
model : User, | |
key : "id" | |
}, | |
allowNull : false | |
}, | |
contract_id : { | |
type: DataTypes.INTEGER, | |
references : { | |
model : LendingContract, | |
key : "id" | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment