Skip to content

Commit

Permalink
feat(url-clicks): add url clicks table
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-jie committed Dec 6, 2020
1 parent 8ed6c13 commit fc8c0a8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/server/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { User } from './user'
import { DailyClicks } from './statistics/daily'
import { WeekdayClicks } from './statistics/weekday'
import { Devices } from './statistics/devices'
import { UrlClicks } from './statistics/clicks'
import { syncFunctions } from './functions'

// One user can create many urls but each url can only be mapped to one user.
Expand All @@ -18,9 +19,11 @@ UrlHistory.belongsTo(User, { foreignKey: { allowNull: false } })
Url.hasMany(DailyClicks, { foreignKey: 'shortUrl', as: 'DailyClicks' })
Url.hasMany(WeekdayClicks, { foreignKey: 'shortUrl', as: 'WeekdayClicks' })
Url.hasOne(Devices, { foreignKey: 'shortUrl', as: 'DeviceClicks' })
Url.hasOne(UrlClicks, { foreignKey: 'shortUrl', as: 'UrlClicks' })
DailyClicks.belongsTo(Url, { foreignKey: 'shortUrl' })
WeekdayClicks.belongsTo(Url, { foreignKey: 'shortUrl' })
Devices.belongsTo(Url, { foreignKey: 'shortUrl' })
UrlClicks.belongsTo(Url, { foreignKey: 'shortUrl' })

/**
* Initialise the database table.
Expand Down
29 changes: 29 additions & 0 deletions src/server/models/statistics/clicks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Sequelize from 'sequelize'

import { sequelize } from '../../util/sequelize'
import { IdType } from '../../../types/server/models'

export interface UrlClicksType extends IdType, Sequelize.Model {
readonly shortUrl: string
readonly clicks: number
readonly createdAt: string
readonly updatedAt: string
}

type UrlClicksTypeStatic = typeof Sequelize.Model & {
new (values?: object, options?: Sequelize.BuildOptions): UrlClicksType
}

export const UrlClicks = <UrlClicksTypeStatic>sequelize.define('url_clicks', {
shortUrl: {
type: Sequelize.STRING,
primaryKey: true,
validate: {
is: /^[a-z0-9-]+$/,
},
},
clicks: {
type: Sequelize.INTEGER,
defaultValue: 0,
},
})

0 comments on commit fc8c0a8

Please sign in to comment.