access-db
Version:
[统一数据库的连接]Unified access to all kinds of databases
42 lines (41 loc) • 1.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const pathTo_1 = require("../utils/pathTo");
const error_1 = require("../constants/error");
const utils_1 = require("../utils/utils");
const fs = require('fs');
function fetchDel(table, id) {
const filePath = (0, pathTo_1.pathTo)(table);
let oldBuf, result, start = -1, end = -1;
if (!fs.existsSync(filePath)) {
result = 0;
return { data: result };
// throw new Error(FASTDB_FILE_ERROR)
}
if (!id) {
throw new Error(error_1.FASTDB_GET_ID_ERROR);
}
oldBuf = fs.readFileSync(filePath);
start = (0, utils_1.isNumber)(id) ? oldBuf.indexOf(`{"id":${id}`) : oldBuf.indexOf(`{"id":"${id}"`);
if (start === -1) {
// 没有
result = 0;
}
else {
end = oldBuf.indexOf(`,{"id"`, start + 8);
if (end === -1) {
// 没有
end = oldBuf.length - 1;
}
else {
end = start === 1 ? end + 1 : end;
}
let leftBuf = oldBuf.slice(0, start === 1 ? start : start - 1);
let rightBuf = oldBuf.slice(end);
let resBuf = Buffer.concat([leftBuf, rightBuf]);
fs.writeFileSync(filePath, resBuf);
result = 1;
}
return { data: result };
}
exports.default = fetchDel;