-
Notifications
You must be signed in to change notification settings - Fork 1
/
FeedModel.mjs
148 lines (133 loc) · 3.92 KB
/
FeedModel.mjs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import Sequelize from 'sequelize'
const Op = Sequelize.Op
export const id = 'id'
export const name = 'name'
export const url = 'url'
export const title = 'title'
export const description = 'description'
export const lengths = {
[name]: 20,
[url]: 512,
[title]: 255,
[description]: 1024,
}
const FeedModelBuilder = (sequelize) => {
const isPostgres = sequelize.dialect.sequelize.options.dialect === 'postgres'
const FeedModel = sequelize.define(
'feeds_feedentry',
{
[id]: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true,
},
[name]: {
type: Sequelize.STRING(lengths[name]),
allowNull: false,
},
[url]: {
type: Sequelize.STRING(lengths[url]),
allowNull: false,
},
[title]: {
type: Sequelize.STRING(lengths[title]),
allowNull: false,
},
[description]: {
type: Sequelize.TEXT,
allowNull: true,
},
},
{
freezeTableName: true,
}
)
const findByName = ({ name, limit }) =>
FeedModel.findAll({
limit: Math.min(limit || 25, 500),
where: {
name,
},
order: [
['updatedAt', isPostgres ? 'DESC NULLS LAST' : 'DESC'],
['createdAt', 'DESC'],
],
})
const insert = ({ name, url, title, description }) =>
FeedModel.findOne({
where: { name, url },
}).then((result) =>
FeedModel.upsert({
id: result?.id || undefined,
name,
url,
title,
description,
})
)
const remove = ({ name, url }) =>
FeedModel.findOne({
where: { name, url },
}).then((m) => m?.destroy())
const list = () =>
FeedModel.findAll({
group: ['name'],
attributes: ['name', [sequelize.fn('COUNT', 'name'), 'count']],
order: [['name', 'ASC']],
})
const count = ({ name }) =>
FeedModel.findAll({
where: { name },
attributes: [[sequelize.fn('COUNT', 'name'), 'count']],
})
const search = ({ query, limit }) =>
FeedModel.findAll({
limit: Math.min(limit || 100, 500),
where: {
[Op.or]: [
{
url: {
[Op.like]: `%${query}%`,
},
},
{
title: {
[Op.like]: `%${query}%`,
},
},
{
description: {
[Op.like]: `%${query}%`,
},
},
],
},
order: [
['updatedAt', 'DESC'],
['createdAt', 'DESC'],
],
})
const suggest = ({ query }) =>
FeedModel.findAll({
limit: 10,
group: ['name'],
attributes: ['name', [sequelize.fn('COUNT', 'name'), 'count']],
where: {
name: {
[Op.like]: `%${query}%`,
},
},
order: [['name', 'ASC']],
})
const dump = () =>
FeedModel.findAll({
attributes: ['name', 'url', 'title', 'description', 'createdAt', 'updatedAt'],
order: [
['name', 'ASC'],
['updatedAt', 'DESC'],
['createdAt', 'DESC'],
],
})
return { FeedModel, findByName, insert, remove, list, count, search, suggest, dump }
}
export default FeedModelBuilder