diff options
Diffstat (limited to 'vnext/server/db')
-rw-r--r-- | vnext/server/db/Users.js | 43 | ||||
-rw-r--r-- | vnext/server/db/__testdata__/testdata.sql | 1 | ||||
-rw-r--r-- | vnext/server/db/index.js | 10 |
3 files changed, 54 insertions, 0 deletions
diff --git a/vnext/server/db/Users.js b/vnext/server/db/Users.js new file mode 100644 index 00000000..445b0ff0 --- /dev/null +++ b/vnext/server/db/Users.js @@ -0,0 +1,43 @@ +import { DataTypes, Op } from 'sequelize' +import db from './index' + +const User = db.define('user', { + id: { + type: DataTypes.INTEGER, + primaryKey: true + }, + nick: DataTypes.STRING, + passw: DataTypes.STRING, + banned: DataTypes.INTEGER, + last_seen: DataTypes.DATE +}, { + timestamps: false +}) + +export const getMonthlyActiveUsers = async () => { + const seenDate = new Date() + seenDate.setMonth(seenDate.getMonth() - 1) + return await User.count({ + where: { + banned: { + [Op.eq]: 0 + }, + last_seen: { + [Op.gt]: seenDate + } + } + }) +} + +export const getUserByName = async (name = '') => { + return await User.findOne({ + where: { + nick: { + [Op.eq]: name + }, + banned: { + [Op.eq]: 0 + } + } + }) +} diff --git a/vnext/server/db/__testdata__/testdata.sql b/vnext/server/db/__testdata__/testdata.sql new file mode 100644 index 00000000..f2876732 --- /dev/null +++ b/vnext/server/db/__testdata__/testdata.sql @@ -0,0 +1 @@ +INSERT INTO users(nick,passw,last_seen) VALUES('ugnich', '12345', NULL), ('freefd', '12345', DATETIME('now')) diff --git a/vnext/server/db/index.js b/vnext/server/db/index.js new file mode 100644 index 00000000..9c9e564b --- /dev/null +++ b/vnext/server/db/index.js @@ -0,0 +1,10 @@ +import debug from 'debug' +var log = debug('db') + +import { Sequelize } from 'sequelize' + +const db = new Sequelize(process.env.DATABASE_URL, { + logging: (...msg) => log(msg) +}) + +export default db |