git-documentdb
Version:
Offline-first database that syncs with Git
198 lines • 8.25 kB
JavaScript
;
/**
* GitDocumentDB
* Copyright (c) Hidekazu Kubota
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of gitDDB source tree.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImpl = exports.getBinaryDocFromWorkingDir = exports.getTextDocFromWorkingDir = exports.getJsonDocFromWorkingDir = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const error_1 = require("../error");
const blob_1 = require("./blob");
const history_1 = require("./history");
/**
* Read json file from working directory.
* This is x10 faster than readBlob() from loose object,
* x100 faster than readBlob() from packed object.
*
* @throws {@link Err.DatabaseClosingError}
* @throws {@link Err.RepositoryNotOpenError}
* @throws {@link Err.InvalidJsonObjectError}
*/
async function getJsonDocFromWorkingDir(gitDDB, shortName, collectionPath, serializeFormat) {
if (gitDDB.isClosing) {
throw new error_1.Err.DatabaseClosingError();
}
if (!gitDDB.isOpened) {
return Promise.reject(new error_1.Err.RepositoryNotOpenError());
}
const fullDocPath = collectionPath + shortName;
const shortId = serializeFormat.removeExtension(shortName);
if (serializeFormat.format === 'json') {
const jsonDoc = await fs_extra_1.default.readJSON(gitDDB.workingDir + '/' + fullDocPath).catch(err => {
if (err instanceof SyntaxError) {
throw new error_1.Err.InvalidJsonObjectError(shortId);
}
else
return undefined;
});
if (jsonDoc === undefined)
return undefined;
if (jsonDoc._id !== undefined) {
// Overwrite _id property by shortId (_id without collectionPath) if JsonDoc is created by GitDocumentedDB (_id !== undefined).
jsonDoc._id = shortId;
}
return jsonDoc;
}
const extMatch = fullDocPath.match(/.+(\..+?)$/);
let extension = '';
if (extMatch) {
extension = extMatch[1];
}
const text = await fs_extra_1.default
.readFile(gitDDB.workingDir + '/' + fullDocPath, 'utf-8')
.catch(() => {
return undefined;
});
if (text === undefined)
return undefined;
const jsonDoc = (0, blob_1.textToJsonDoc)(text, serializeFormat, extension, shortId);
if (jsonDoc._id !== undefined) {
// Overwrite _id property by shortId (_id without collectionPath) if JsonDoc is created by GitDocumentedDB (_id !== undefined).
jsonDoc._id = shortId;
}
return jsonDoc;
}
exports.getJsonDocFromWorkingDir = getJsonDocFromWorkingDir;
/**
* Read text file from working directory.
* This is x10 faster than readBlob() from loose object,
* x100 faster than readBlob() from packed object.
*
* @throws {@link Err.DatabaseClosingError}
* @throws {@link Err.RepositoryNotOpenError}
* @throws {@link Err.InvalidJsonObjectError}
*/
async function getTextDocFromWorkingDir(gitDDB, shortName, collectionPath, serializeFormat) {
if (gitDDB.isClosing) {
throw new error_1.Err.DatabaseClosingError();
}
if (!gitDDB.isOpened) {
return Promise.reject(new error_1.Err.RepositoryNotOpenError());
}
const fullDocPath = collectionPath + shortName;
const textDoc = await fs_extra_1.default
.readFile(gitDDB.workingDir + '/' + fullDocPath, 'utf-8')
.catch(() => {
return undefined;
});
if (textDoc === undefined)
return undefined;
return textDoc;
}
exports.getTextDocFromWorkingDir = getTextDocFromWorkingDir;
/**
* Read binary file from working directory.
* This is x10 faster than readBlob() from loose object,
* x100 faster than readBlob() from packed object.
*
* @throws {@link Err.DatabaseClosingError}
* @throws {@link Err.RepositoryNotOpenError}
* @throws {@link Err.InvalidJsonObjectError}
*/
async function getBinaryDocFromWorkingDir(gitDDB, shortName, collectionPath, serializeFormat) {
if (gitDDB.isClosing) {
throw new error_1.Err.DatabaseClosingError();
}
if (!gitDDB.isOpened) {
return Promise.reject(new error_1.Err.RepositoryNotOpenError());
}
const fullDocPath = collectionPath + shortName;
const binaryDoc = await fs_extra_1.default.readFile(gitDDB.workingDir + '/' + fullDocPath).catch(() => {
return undefined;
});
if (binaryDoc === undefined)
return undefined;
return binaryDoc;
}
exports.getBinaryDocFromWorkingDir = getBinaryDocFromWorkingDir;
/**
* Common implementation of get-like commands
*
* @throws {@link Err.DatabaseClosingError}
* @throws {@link Err.RepositoryNotOpenError}
* @throws {@link Err.InvalidJsonObjectError}
*/
// eslint-disable-next-line complexity
async function getImpl(gitDDB, shortName, collectionPath, serializeFormat, options, internalOptions, historyOptions) {
var _a, _b, _c, _d;
if (gitDDB.isClosing) {
throw new error_1.Err.DatabaseClosingError();
}
if (!gitDDB.isOpened) {
return Promise.reject(new error_1.Err.RepositoryNotOpenError());
}
options !== null && options !== void 0 ? options : (options = {
forceDocType: undefined,
});
internalOptions !== null && internalOptions !== void 0 ? internalOptions : (internalOptions = {
withMetadata: undefined,
revision: undefined,
oid: undefined,
});
(_a = internalOptions.withMetadata) !== null && _a !== void 0 ? _a : (internalOptions.withMetadata = false);
(_b = internalOptions.revision) !== null && _b !== void 0 ? _b : (internalOptions.revision = 0);
(_c = internalOptions.oid) !== null && _c !== void 0 ? _c : (internalOptions.oid = '');
const fullDocPath = collectionPath + shortName;
// Do not use validateId for get()
// Just return undefined if not exists.
// gitDDB.validator.validateId(fullDocPath);
let readBlobResult;
if (internalOptions.oid !== '') {
readBlobResult = await (0, blob_1.readBlobByOid)(gitDDB.workingDir, internalOptions.oid);
// Do not return FatDoc because _id is not specified.
// eslint-disable-next-line require-atomic-updates
internalOptions.withMetadata = false;
}
else if (historyOptions === undefined &&
(!internalOptions.revision || internalOptions.revision === 0)) {
readBlobResult = await (0, blob_1.readLatestBlob)(gitDDB.workingDir, fullDocPath);
}
else if (internalOptions.revision >= 0) {
readBlobResult = await (0, history_1.readOldBlob)(gitDDB.workingDir, fullDocPath, internalOptions.revision, historyOptions);
}
else {
return undefined;
}
if (readBlobResult === undefined)
return undefined;
const docType = (_d = options.forceDocType) !== null && _d !== void 0 ? _d : (serializeFormat.hasObjectExtension(fullDocPath) ? 'json' : 'text');
if (docType === 'text') {
// TODO: select binary or text by .gitattribtues
}
if (docType === 'json') {
const extMatch = fullDocPath.match(/.+(\..+?)$/);
let extension = '';
if (extMatch) {
extension = extMatch[1];
}
if (internalOptions.oid !== '') {
return (0, blob_1.blobToJsonDocWithoutOverwrittenId)(readBlobResult, serializeFormat, extension);
}
const shortId = serializeFormat.removeExtension(shortName);
return (0, blob_1.blobToJsonDoc)(shortId, readBlobResult, internalOptions.withMetadata, serializeFormat, extension);
}
else if (docType === 'text') {
return (0, blob_1.blobToText)(shortName, readBlobResult, internalOptions.withMetadata);
}
else if (docType === 'binary') {
return (0, blob_1.blobToBinary)(shortName, readBlobResult, internalOptions.withMetadata);
}
}
exports.getImpl = getImpl;
//# sourceMappingURL=get.js.map