|
@@ -35,53 +35,48 @@ async function run() {
|
|
|
for (let i = 0; i < eventDocs.length; i++) {
|
|
for (let i = 0; i < eventDocs.length; i++) {
|
|
|
let eventDoc = eventDocs[i];
|
|
let eventDoc = eventDocs[i];
|
|
|
console.log(eventDoc);
|
|
console.log(eventDoc);
|
|
|
- if (eventDoc.tb == 'identitycounters') continue; // skip
|
|
|
|
|
// 只同步特定的几张表: arts, daily-arts, users, albums, translates
|
|
// 只同步特定的几张表: arts, daily-arts, users, albums, translates
|
|
|
- if (eventDoc.tb != 'arts'
|
|
|
|
|
- && eventDoc.tb != 'daily-arts'
|
|
|
|
|
- && eventDoc.tb != 'users'
|
|
|
|
|
- && eventDoc.tb != 'albums'
|
|
|
|
|
- && eventDoc.tb != 'videostories'
|
|
|
|
|
- && eventDoc.tb != 'translates') {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (!eventDoc.db) eventDoc.db = remoteDbs[0];
|
|
|
|
|
- // 对应的表
|
|
|
|
|
- remotetb = remoteDB.collection(eventDoc.tb);
|
|
|
|
|
- localtb = localDB.collection(eventDoc.tb);
|
|
|
|
|
- if (!remotetb || !localtb) {
|
|
|
|
|
- console.warn('unkown sync database: ' + eventDoc.db + ', will skip');
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (eventDoc.op == 'remove') {
|
|
|
|
|
- await localtb.deleteOne({ _id: eventDoc.rid });
|
|
|
|
|
- console.log("sync remove : " + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
- } else if (eventDoc.op == 'save') {
|
|
|
|
|
- remotedoc = await remotetb.findOne({ _id: eventDoc.rid });
|
|
|
|
|
- localdoc = await localtb.findOne({ _id: eventDoc.rid });
|
|
|
|
|
- if (!remotedoc) {
|
|
|
|
|
- console.log("remote doc not found, may be deleted, skip: " + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
- } else if (!localdoc) {
|
|
|
|
|
- console.log("sync add :" + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
- try {
|
|
|
|
|
- await localtb.insertOne(remotedoc);
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- console.error(e);
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- console.log("sync update :" + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
- if (eventDoc.tb == 'arts') { // art 表需保留新的title,desc,copy 这几个字段,避免覆盖
|
|
|
|
|
- remotedoc.title = localdoc.title;
|
|
|
|
|
- remotedoc.desc = localdoc.desc;
|
|
|
|
|
- remotedoc.seoTitle = localdoc.seoTitle;
|
|
|
|
|
- remotedoc.seoDescription = localdoc.seoDescription;
|
|
|
|
|
- } else if (eventDoc.tb == 'albums' || eventDoc.tb == 'videostories') { // albums 和 videostories 表需保留新增的seoTitle,seoDescription字段,避免覆盖
|
|
|
|
|
- remotedoc.seoTitle = localdoc.seoTitle;
|
|
|
|
|
- remotedoc.seoDescription = localdoc.seoDescription;
|
|
|
|
|
|
|
+ if (eventDoc.tb == 'arts'
|
|
|
|
|
+ || eventDoc.tb != 'daily-arts'
|
|
|
|
|
+ || eventDoc.tb != 'users'
|
|
|
|
|
+ || eventDoc.tb != 'albums'
|
|
|
|
|
+ || eventDoc.tb != 'videostories'
|
|
|
|
|
+ || eventDoc.tb != 'translates') {
|
|
|
|
|
+
|
|
|
|
|
+ if (!eventDoc.db) eventDoc.db = remoteDbs[0];
|
|
|
|
|
+ // 对应的表
|
|
|
|
|
+ remotetb = remoteDB.collection(eventDoc.tb);
|
|
|
|
|
+ localtb = localDB.collection(eventDoc.tb);
|
|
|
|
|
+ if (remotetb && localtb) {
|
|
|
|
|
+ if (eventDoc.op == 'remove') {
|
|
|
|
|
+ await localtb.deleteOne({ _id: eventDoc.rid });
|
|
|
|
|
+ console.log("sync remove : " + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
+ } else if (eventDoc.op == 'save') {
|
|
|
|
|
+ remotedoc = await remotetb.findOne({ _id: eventDoc.rid });
|
|
|
|
|
+ localdoc = await localtb.findOne({ _id: eventDoc.rid });
|
|
|
|
|
+ if (!remotedoc) {
|
|
|
|
|
+ console.log("remote doc not found, may be deleted, skip: " + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
+ } else if (!localdoc) {
|
|
|
|
|
+ console.log("sync add :" + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
+ try {
|
|
|
|
|
+ await localtb.insertOne(remotedoc);
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.error(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ console.log("sync update :" + eventDoc.tb + " " + eventDoc.rid);
|
|
|
|
|
+ if (eventDoc.tb == 'arts') { // art 表需保留新的title,desc,copy 这几个字段,避免覆盖
|
|
|
|
|
+ remotedoc.title = localdoc.title;
|
|
|
|
|
+ remotedoc.desc = localdoc.desc;
|
|
|
|
|
+ remotedoc.seoTitle = localdoc.seoTitle;
|
|
|
|
|
+ remotedoc.seoDescription = localdoc.seoDescription;
|
|
|
|
|
+ } else if (eventDoc.tb == 'albums' || eventDoc.tb == 'videostories') { // albums 和 videostories 表需保留新增的seoTitle,seoDescription字段,避免覆盖
|
|
|
|
|
+ remotedoc.seoTitle = localdoc.seoTitle;
|
|
|
|
|
+ remotedoc.seoDescription = localdoc.seoDescription;
|
|
|
|
|
+ }
|
|
|
|
|
+ await localtb.replaceOne({ _id: eventDoc.rid }, remotedoc);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- await localtb.replaceOne({ _id: eventDoc.rid }, remotedoc);
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|