chore: update backend demo

This commit is contained in:
Arthur 2023-02-24 10:52:04 +08:00 committed by Qaplagzy
parent c079f70b09
commit 6c48b15bc0

View File

@ -6,6 +6,7 @@ const _ = require("lodash");
* @param {any[]} ops op list * @param {any[]} ops op list
*/ */
async function applyOp(collection, ops) { async function applyOp(collection, ops) {
const operations = [];
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
for (const op of ops) { for (const op of ops) {
const { path, id } = op; const { path, id } = op;
@ -19,51 +20,66 @@ async function applyOp(collection, ops) {
if (op.value.direction === "rightbottom") { if (op.value.direction === "rightbottom") {
insertPos += 1; insertPos += 1;
} }
await collection.updateOne( operations.push({
filter, updateOne: {
{ filter,
$inc: { update: {
[`celldata.$[e].${field}`]: op.value.count, $inc: {
[`celldata.$[e].${field}`]: op.value.count,
},
}, },
arrayFilters: [{ [`e.${field}`]: { $gte: insertPos } }],
}, },
{ arrayFilters: [{ [`e.${field}`]: { $gte: insertPos } }] } });
);
} else if (op.op === "deleteRowCol") { } else if (op.op === "deleteRowCol") {
/** /**
* special op: deleteRowCol * special op: deleteRowCol
*/ */
const field = op.value.type === "row" ? "r" : "c"; const field = op.value.type === "row" ? "r" : "c";
// delete cells operations.push(
await collection.updateOne(filter, { // delete cells
$pull: { {
celldata: { updateOne: {
[field]: { filter,
$gte: op.value.start, update: {
$lte: op.value.end, $pull: {
celldata: {
[field]: {
$gte: op.value.start,
$lte: op.value.end,
},
},
},
}, },
}, },
}, },
}); // decr indexes
// decr indexes
await collection.updateOne(
filter,
{ {
$inc: { updateOne: {
[`celldata.$[e].${field}`]: -(op.value.end - op.value.start + 1), filter,
update: {
$inc: {
[`celldata.$[e].${field}`]: -(
op.value.end -
op.value.start +
1
),
},
},
arrayFilters: [{ [`e.${field}`]: { $gte: op.value.start } }],
}, },
}, }
{ arrayFilters: [{ [`e.${field}`]: { $gte: op.value.start } }] }
); );
} else if (op.op === "addSheet") { } else if (op.op === "addSheet") {
/** /**
* special op: addSheet * special op: addSheet
*/ */
collection.insertOne(op.value); operations.push({ insertOne: { document: op.value } });
} else if (op.op === "deleteSheet") { } else if (op.op === "deleteSheet") {
/** /**
* special op: deleteSheet * special op: deleteSheet
*/ */
collection.deleteOne(filter); operations.push({ deleteOne: { filter } });
} else if ( } else if (
path.length >= 3 && path.length >= 3 &&
path[0] === "data" && path[0] === "data" &&
@ -89,49 +105,71 @@ async function applyOp(collection, ops) {
}, },
}; };
if (path.length === 3) { if (path.length === 3) {
const cellExists = await collection.findOne({ const cellExists = await collection.countDocuments(
...filter, {
celldata: { ...filter,
$elemMatch: { celldata: {
r, $elemMatch: {
c,
},
},
});
if (cellExists) {
await collection.updateOne(filter, updater, options);
} else {
await collection.updateOne(filter, {
$addToSet: {
celldata: {
r, r,
c, c,
v: op.value, },
},
},
{ limit: 1 }
);
if (cellExists) {
operations.push({
updateOne: { filter, update: updater, ...options },
});
} else {
operations.push({
updateOne: {
filter,
update: {
$addToSet: {
celldata: {
r,
c,
v: op.value,
},
},
}, },
}, },
}); });
} }
} else { } else {
await collection.updateOne(filter, updater, options); operations.push({
updateOne: { filter, update: updater, ...options },
});
} }
} else if (path.length === 2 && path[0] === "data" && _.isNumber(path[1])) { } else if (path.length === 2 && path[0] === "data" && _.isNumber(path[1])) {
// entire row operation // entire row operation
console.error("row assigning not supported"); console.error("row assigning not supported");
} else if (path.length === 0 && op.op === "add") { } else if (path.length === 0 && op.op === "add") {
// add new sheet // add new sheet
await collection.insertOne(op.value); operations.push({ insertOne: { document: op.value } });
} else if (path[0] !== "data") { } else if (path[0] !== "data") {
// other config update // other config update
if (op.op === "remove") { if (op.op === "remove") {
await collection.updateOne(filter, { operations.push({
$unset: { updateOne: {
[op.path.join(".")]: "", filter,
update: {
$unset: {
[op.path.join(".")]: "",
},
},
}, },
}); });
} else { } else {
await collection.updateOne(filter, { operations.push({
$set: { updateOne: {
[op.path.join(".")]: op.value, filter,
update: {
$set: {
[op.path.join(".")]: op.value,
},
},
}, },
}); });
} }
@ -139,6 +177,7 @@ async function applyOp(collection, ops) {
console.error("unprocessable op", op); console.error("unprocessable op", op);
} }
} }
collection.bulkWrite(operations);
} }
module.exports = { module.exports = {