access-db
Version:
[统一数据库的连接]Unified access to all kinds of databases
38 lines (37 loc) • 1.35 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 constants_1 = require("../constants/constants");
const findTrans_1 = __importDefault(require("../utils/findTrans"));
const fs = require('fs');
const POSITION_STEP = 9;
function fetchCount(table, params = {}) {
const filePath = (0, pathTo_1.pathTo)(table);
let oldBuf, num = 0;
if (!fs.existsSync(filePath)) {
return { data: num };
// throw new Error(FASTDB_FILE_ERROR)
}
oldBuf = fs.readFileSync(filePath);
let t_positon = POSITION_STEP, t_json, n = 0;
while (t_positon < oldBuf.length) {
let start = oldBuf.lastIndexOf(`{"id":`, t_positon);
let end = oldBuf.indexOf(`,{"id"`, t_positon);
if (end === -1) {
end = oldBuf.length - 1;
}
if (start > -1) {
t_json = JSON.parse(oldBuf.slice(start, end).toString());
}
let isOk = (0, findTrans_1.default)(params, 1, t_json, constants_1.PLATFORM_NAME.FASTDB);
if (isOk) {
num++;
}
t_positon = end + POSITION_STEP;
}
return { data: num };
}
exports.default = fetchCount;