UNPKG

access-db

Version:

[统一数据库的连接]Unified access to all kinds of databases

34 lines (33 loc) 1.05 kB
"use strict"; 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 fetchGet(table, id) { const filePath = (0, pathTo_1.pathTo)(table); let oldBuf, result; 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); } oldBuf = fs.readFileSync(filePath); let start = (0, utils_1.isNumber)(id) ? oldBuf.indexOf(`{"id":${id}`) : oldBuf.indexOf(`{"id":"${id}"`); if (start === -1) { // 没有 result = {}; } else { let end = oldBuf.indexOf(`,{"id"`, start + 8); if (end === -1) { end = oldBuf.length - 1; } result = JSON.parse(oldBuf.slice(start, end).toString()); } return { data: result }; } exports.default = fetchGet;