-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.js
172 lines (168 loc) · 4.01 KB
/
posts.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const _kebabCase = require('lodash/kebabCase');
const assert = require('http-assert');
const dynazord = require('dynazord');
const isUUID = require('validator/lib/isUUID');
const { v4: uuid } = require('uuid');
const createTable = {
TableName: 'dynazord-example-posts',
BillingMode: 'PAY_PER_REQUEST',
KeySchema: [
{ AttributeName: 'id', KeyType: 'HASH' },
],
GlobalSecondaryIndexes: [
{
IndexName: 'blogPostsByTime',
KeySchema: [
{ AttributeName: 'blogID', KeyType: 'HASH' },
{ AttributeName: 'publishedAt', KeyType: 'RANGE' },
],
Projection: { ProjectionType: 'KEYS_ONLY' },
},
],
AttributeDefinitions: [
{ AttributeName: 'id', AttributeType: 'S' },
{ AttributeName: 'blogID', AttributeType: 'S' },
{ AttributeName: 'publishedAt', AttributeType: 'N' },
],
};
const posts = dynazord.createModel({
tableName: createTable.TableName,
keySchema: {
hash: 'id',
},
secondaryIndexes: {
blogPostsByTime: {
hash: 'blogID',
range: 'publishedAt',
},
},
properties: {
id: {
type: String,
required: true,
default: () => uuid(),
validate: {
notNull: true,
notEmpty: true,
isUUID: value => isUUID(value, 4),
},
},
blogID: {
type: String,
required: true,
enum: [ 'jdrydn.com', 'theverge.com' ],
},
title: {
type: String,
required: true,
validate: {
notNull: true,
notEmpty: true,
},
},
description: {
type: String,
},
slug: {
type: String,
required: true,
default: () => 'EXAMPLE-SLUG',
validate: {
notNull: true,
notEmpty: true,
},
},
content: {
type: Array,
required: true,
properties: {
type: Object,
required: true,
properties: {
html: {
type: String,
validate: {
notEmpty: true,
},
},
image: {
type: Buffer,
validate: {
notEmpty: true,
},
},
embed: {
type: Object,
// Can contain infinite properties
validate: {
notEmpty: true,
},
},
},
validate: {
notEmpty: true,
},
},
validate: {
notEmpty: true,
},
},
pageViews: {
type: Number,
validate: {
isUnsigned: true,
},
},
publishedAt: {
type: Date,
// Optionally set the underlying format to a Number to assist with sorting
format: Number,
validate: {
isBefore: '2099-12-31T23:59:59.00Z',
isAfter: '2000-01-01T00:00:00.00Z',
}
},
status: {
type: String,
enum: [ 'PUBLISHED', 'DRAFT', 'SCHEDULED', 'DELETED' ],
required: true,
},
},
hooks: {
beforeValidate(post, opts) {
if (post.title && (!post.slug || post.slug === 'EXAMPLE-SLUG')) {
post.slug = _kebabCase(post.title);
}
},
// beforeBulkCreate(entries) {
// entries.forEach((post, i) => {
// if (post.title && (!post.slug || post.slug === 'EXAMPLE-SLUG')) {
// entries[i].slug = _kebabCase(post.title);
// }
// });
// },
afterValidate: {
async isSlugUnique(post, opts) {
const { id, slug } = post;
if (slug) {
// Lookup if this slug has been used before
const existing = await this.scan({ slug });
// And if it exists on another post, throw an error
assert(!existing || existing.id !== id, 400, new Error('Expected slug to be unique'), { slug });
}
},
},
},
options: {
createdAtTimestamp: true,
updatedAtTimestamp: true,
},
});
posts.hooks.on('beforeBulkCreate', entries => {
entries.forEach((post, i) => {
if (post.title && (!post.slug || post.slug === 'EXAMPLE-SLUG')) {
entries[i].slug = _kebabCase(post.title);
}
});
});
module.exports = posts;