access-db
Version:
[统一数据库的连接]Unified access to all kinds of databases
49 lines (48 loc) • 1.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
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 updateTrans_1 = __importDefault(require("../utils/updateTrans"));
const constants_1 = require("../constants/constants");
const fs = require('fs');
function fetchUpdate(table, id, params) {
const filePath = (0, pathTo_1.pathTo)(table);
let oldBuf, result, tempJson, start = -1, end = -1;
if (!fs.existsSync(filePath)) {
result = {};
return { data: result };
// throw new Error(FASTDB_FILE_ERROR)
}
if (!id) {
throw new Error(error_1.FASTDB_GET_ID_ERROR);
}
if (params.id) {
throw new Error(error_1.FASTDB_UPDATE_ID_ERROR);
}
oldBuf = fs.readFileSync(filePath);
start = (0, utils_1.isNumber)(id) ? oldBuf.indexOf(`{"id":${id}`) : oldBuf.indexOf(`{"id":"${id}"`);
if (start === -1) {
// 没有
result = {};
}
else {
end = oldBuf.indexOf(`,{"id"`, start + 8);
if (end === -1) {
// 没有
end = oldBuf.length - 1;
}
tempJson = JSON.parse(oldBuf.slice(start, end).toString());
result = (0, updateTrans_1.default)(params, tempJson, constants_1.PLATFORM_NAME.FASTDB);
let nowBuf = Buffer.from(JSON.stringify(result));
let leftBuf = oldBuf.slice(0, start);
let rightBuf = oldBuf.slice(end);
let resBuf = Buffer.concat([leftBuf, nowBuf, rightBuf]);
fs.writeFileSync(filePath, resBuf);
}
return { data: result };
}
exports.default = fetchUpdate;