Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/services/SearchSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,23 @@ class SearchSync extends BasicService {
}
}

async _getDocsToSync(model, from = new Date(null)) {
return await await model.find({
async _getDocsToSync({ model, from = new Date(null), maxDocs = 200, sequenceKey }) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Очень странная запись new Date(null), если нужна начала эпохи то лучше написать new Date(0) потому что new Date(null) как раз вернет начало эпохи.
Просто когда видишь в коде new Date(null) то кажется что будет вообще Invalid Date.

Я понимаю, что это было до этого PR, но если можно то лучше сейчас поправить.

const query = {
updatedAt: { $gte: from },
});
};
if (sequenceKey) {
query._id = { $gt: sequenceKey };
}
const docs = await model.find(query).limit(maxDocs);
const result = {
docs,
};

if (docs.length === maxDocs) {
result.sequenceKey = docs[docs.length - 1]._id;
}

return result;
}

async _getAllIndexes(model, offset = 0) {
Expand All @@ -203,14 +216,25 @@ class SearchSync extends BasicService {
return allDocs;
}

async _syncModel(model, from) {
const dataToSync = await this._getDocsToSync(model, from);
async _syncModel(model, from, sequenceKey) {
const { docs: dataToSync, sequenceKey: newSequenceKey } = await this._getDocsToSync({
model,
from,
sequenceKey,
});

const syncPromises = [];
if (dataToSync.length > 0) {
for (const data of dataToSync) {
await this._syncDoc(model, data);
syncPromises.push(this._syncDoc(model, data));
}
}

await Promise.all(syncPromises);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Всю эту конструкцию с Promise.all можно записать в более компактном и как я считаю понятном виде (без лишних переменных):

if (dataToSync.length > 0) {
	await Promise.all(dataToSync.map(data => this._syncDoc(model, data)));
}

Кода меньше, нет лишних push и массива.


if (newSequenceKey) {
await this._syncModel(model, from, newSequenceKey);
}
}

async _syncDeleted(model) {
Expand Down