Skip to content

Commit

Permalink
feat(url-clicks): update schema and scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-jie committed Dec 21, 2020
1 parent 8e03cf8 commit 5173734
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
19 changes: 12 additions & 7 deletions src/server/models/url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Sequelize from 'sequelize'
import { UrlClicks } from './statistics/clicks'
import { UrlClicks, UrlClicksType } from './statistics/clicks'
import { ACTIVE, INACTIVE } from './types'
import {
SHORT_URL_REGEX,
Expand All @@ -24,7 +24,7 @@ interface UrlBaseType extends IdType {
}

export interface UrlType extends IdType, UrlBaseType, Sequelize.Model {
readonly clicks: number
readonly UrlClicks?: UrlClicksType
readonly createdAt: string
readonly updatedAt: string
readonly email: string
Expand Down Expand Up @@ -144,11 +144,6 @@ export const Url = <UrlTypeStatic>sequelize.define(
values: [ACTIVE, INACTIVE],
defaultValue: ACTIVE,
},
clicks: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0,
},
isFile: {
type: Sequelize.BOOLEAN,
allowNull: false,
Expand Down Expand Up @@ -231,6 +226,16 @@ export const Url = <UrlTypeStatic>sequelize.define(
fields: [Sequelize.literal(`(${urlSearchVector})`)],
},
],
scopes: {
getClicks: {
include: [
{
model: UrlClicks,
as: 'UrlClicks',
},
],
},
},
},
)

Expand Down
4 changes: 2 additions & 2 deletions src/server/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const User = <UserTypeStatic>sequelize.define(
return {
include: [
{
model: Url,
model: Url.scope('getClicks'),
as: 'Urls',
where: whereUrlConditions,
// use left outer join instead of default inner join
Expand All @@ -115,7 +115,7 @@ export const User = <UserTypeStatic>sequelize.define(
return {
include: [
{
model: Url,
model: Url.scope('getClicks'),
as: 'Urls',
where: { shortUrl },
},
Expand Down
5 changes: 3 additions & 2 deletions src/server/repositories/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UrlType } from '../models/url'
import { UrlClicksType } from '../models/statistics/clicks'

/**
* A type that represents Urls stored in the data store.
Expand All @@ -8,13 +9,13 @@ export type StorableUrl = Pick<
| 'shortUrl'
| 'longUrl'
| 'state'
| 'clicks'
| 'isFile'
| 'createdAt'
| 'updatedAt'
| 'description'
| 'contactEmail'
>
> &
Pick<UrlClicksType, 'clicks'>

/**
* A type that represents a file that can be stored in the data store.
Expand Down

0 comments on commit 5173734

Please sign in to comment.