Skip to content

Commit 686c856

Browse files
committed
Updates to Sanity Source
#32
1 parent 1d253c7 commit 686c856

File tree

2 files changed

+67
-20
lines changed

2 files changed

+67
-20
lines changed

packages/content/lib/content-sources/content-result.js

+46-17
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ class ContentResultDataFile {
99
* @type {*}
1010
*/
1111
content = '';
12-
12+
1313
/**
14-
*
15-
* @param {string} localPath
16-
* @param {string|JSON|Object|Array} content
14+
*
15+
* @param {string} localPath
16+
* @param {string|JSON|Object|Array} content
1717
*/
1818
constructor(localPath, content) {
1919
this.localPath = localPath;
2020
this.content = content;
2121
}
22-
22+
2323
/**
2424
* Returns the raw content if it's already a string,
2525
* otherwise returns the result of JSON.stringify(this.content).
@@ -48,7 +48,7 @@ class ContentResult {
4848
* @type {Array<string>}
4949
*/
5050
mediaUrls = [];
51-
51+
5252
/**
5353
* @param {Array<ContentResultDataFile>} dataFiles All the data files and their contents that should be saved
5454
* @param {Array<string>} mediaUrls All the media files that should be saved
@@ -57,31 +57,60 @@ class ContentResult {
5757
this.dataFiles = dataFiles;
5858
this.mediaUrls = mediaUrls;
5959
}
60-
60+
6161
/**
62-
*
63-
* @param {string} localPath
64-
* @param {*} content
62+
*
63+
* @param {string} localPath
64+
* @param {*} content
6565
*/
6666
addDataFile(localPath, content) {
6767
this.dataFiles.push(new ContentResultDataFile(localPath, content));
6868
}
69-
69+
7070
/**
71-
*
72-
* @param {string} url
71+
*
72+
* @param {string} url
7373
*/
7474
addMediaUrl(url) {
7575
this.mediaUrls.push(url);
7676
}
77-
77+
7878
/**
79-
*
80-
* @param {Iterable} files
79+
*
80+
* @param {Iterable} files
8181
*/
8282
addMediaUrls(files) {
8383
this.mediaUrls.push(...files);
8484
}
85+
86+
/**
87+
* @param {ContentResult) otheContentResult
88+
*/
89+
combine(otheContentResult) {
90+
this.addMediaUrls(otheContentResult.mediaUrls);
91+
92+
otheContentResult.dataFiles.forEach(item => {
93+
this.dataFiles.push(item);
94+
});
95+
}
96+
97+
/**
98+
*
99+
* @param {string} id
100+
*/
101+
collate(id) {
102+
103+
// Collect all data into 1 object.
104+
let collatedData = this.dataFiles.reduce((previousValue, currentValue) => {
105+
return [...previousValue, ...currentValue.content];
106+
}, []);
107+
108+
// Remove old datafiles.
109+
this.dataFiles = [];
110+
111+
const fileName = `${id}.json`;
112+
this.addDataFile(fileName, collatedData);
113+
}
85114
}
86115

87-
export default ContentResult;
116+
export default ContentResult;

packages/content/lib/content-sources/sanity-source.js

+21-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class SanityOptions extends SourceOptions {
3030
textConverters = [],
3131
limit = 100,
3232
maxNumPages = -1,
33+
combinePaginatedFiles = false,
3334
pageNumZeroPad = 0,
3435
...rest
3536
} = {}) {
@@ -96,6 +97,12 @@ export class SanityOptions extends SourceOptions {
9697
*/
9798
this.maxNumPages = maxNumPages;
9899

100+
/**
101+
* To combine paginated files into 1 file.
102+
* @type {boolean}
103+
*/
104+
this.combinePaginatedFiles = combinePaginatedFiles;
105+
99106
/**
100107
* How many zeros to pad each json filename index with. Default is 0
101108
* @type {number}
@@ -134,13 +141,15 @@ class SanitySource extends ContentSource {
134141
* @returns {Promise<ContentResult>}
135142
*/
136143
async fetchContent() {
137-
const result = new ContentResult();
144+
// const result = new ContentResult();
145+
// console.log(this.config);
138146

139147
let queryPromises = [];
140148
let customQueryPromises = [];
141149

142150
for (const id of this.config.queries) {
143151
let query = '*[_type == "' + id + '" ]';
152+
const result = new ContentResult();
144153

145154
queryPromises.push(await this._fetchPages(id, query, result, {
146155
start: 0,
@@ -149,14 +158,18 @@ class SanitySource extends ContentSource {
149158
}
150159

151160
for (const cqd of this.config.customQueries) {
161+
const result = new ContentResult();
152162
customQueryPromises.push(await this._fetchPages(cqd.id, cqd.query, result, {
153163
start: 0,
154164
limit: this.config.limit
155165
}));
156166
}
157167

158168
return Promise.all([...queryPromises, ...customQueryPromises]).then((values) => {
159-
return result;
169+
const masterResult = new ContentResult();
170+
values.forEach(el => masterResult.combine(el));
171+
172+
return masterResult;
160173
}).catch((error) => {
161174
this.logger.error(`Sync failed: ${error ? error.message || '' : ''}`);
162175
return error;
@@ -181,14 +194,19 @@ class SanitySource extends ContentSource {
181194
) {
182195

183196
const pageNum = params.start / params.limit;
184-
const q = query + '[' + params.start + '..' + (params.start + params.limit) + ']';
197+
const q = query + '[' + params.start + '..' + (params.start + params.limit - 1) + ']';
185198
const p = {};
186199

187200
this.logger.debug(`Fetching page ${pageNum} of ${id}`);
188201

189202
return this.client.fetch(q, p).then((content) => {
190203

191204
if (!content || !content.length) {
205+
// If we are combining files, we do that here.
206+
if (this.config.combinePaginatedFiles) {
207+
result.collate(id);
208+
}
209+
192210
// Empty result or no more pages left
193211
return Promise.resolve(result);
194212
}

0 commit comments

Comments
 (0)