forked from hanchon-live/ethermint-faucet-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.ts
82 lines (80 loc) · 1.72 KB
/
database.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Sequelize, DataTypes, Op } from "sequelize";
export const sequelize = new Sequelize(
process.env.POSTGRES_DB || "faucet",
process.env.POSTGRES_USER || "postgres",
process.env.POSTGRES_PASSWORD || "test",
{
host: process.env.POSTGRES_HOST || "localhost",
port: (process.env.POSTGRES_PORT || 5432) as any,
dialect: "postgres",
dialectOptions: {
ssl: process.env.POSTGRES_SSL == "true",
},
}
);
export const User = sequelize.define("user", {
sub: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
nickname: {
type: DataTypes.STRING,
allowNull: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
picture: {
type: DataTypes.STRING,
allowNull: false,
},
});
export const Transaction = sequelize.define("transaction", {
address: {
type: DataTypes.STRING,
allowNull: false,
},
amount: {
type: DataTypes.INTEGER,
allowNull: false,
},
transactionHash: {
type: DataTypes.STRING,
allowNull: true,
},
});
export const BlockedAddress = sequelize.define("blockedAddress", {
address: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
});
export const latestTransactionSince = async (user: any, date: any) => {
return await Transaction.findOne({
where: {
userId: user.id,
createdAt: {
[Op.gt]: date,
},
transactionHash: {
[Op.not]: null,
},
},
order: [["createdAt", "DESC"]],
});
};
Transaction.belongsTo(User, {
foreignKey: { allowNull: false },
onDelete: "CASCADE",
});
User.hasMany(Transaction, {
foreignKey: { allowNull: false },
onDelete: "CASCADE",
});