UNPKG

git-documentdb

Version:

Offline-first database that syncs with Git

149 lines 5.77 kB
"use strict"; /** * 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.deleteWorker = exports.deleteImpl = void 0; const path_1 = __importDefault(require("path")); const fs_extra_1 = __importDefault(require("fs-extra")); const isomorphic_git_1 = __importDefault(require("isomorphic-git")); const const_1 = require("../const"); const error_1 = require("../error"); const utils_1 = require("../utils"); /** * Implementation of delete() * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.RepositoryNotOpenError} * @throws {@link Err.TaskCancelError} * * @throws # Errors from deleteWorker * @throws - {@link Err.UndefinedDBError} * @throws - {@link Err.DocumentNotFoundError} * @throws - {@link Err.CannotDeleteDataError} * * @internal */ function deleteImpl(gitDDB, collectionPath, shortId, shortName, options) { var _a, _b; if (gitDDB.isClosing) { return Promise.reject(new error_1.Err.DatabaseClosingError()); } if (!gitDDB.isOpened) { return Promise.reject(new error_1.Err.RepositoryNotOpenError()); } options !== null && options !== void 0 ? options : (options = { commitMessage: undefined, taskId: undefined, enqueueCallback: undefined, }); const fullDocPath = collectionPath + shortName; const commitMessage = (_a = options.commitMessage) !== null && _a !== void 0 ? _a : `delete: ${fullDocPath}(<%file_oid%>)`; const taskId = (_b = options.taskId) !== null && _b !== void 0 ? _b : gitDDB.taskQueue.newTaskId(); // delete() must be serial. return new Promise((resolve, reject) => { gitDDB.taskQueue.pushToTaskQueue({ label: 'delete', taskId: taskId, collectionPath, shortId, shortName, func: (beforeResolve, beforeReject) => deleteWorker(gitDDB, collectionPath, shortName, commitMessage) .then(result => { beforeResolve(); resolve(result); }) .catch((err) => { beforeReject(); reject(err); }), cancel: () => { reject(new error_1.Err.TaskCancelError(taskId)); }, enqueueCallback: options === null || options === void 0 ? void 0 : options.enqueueCallback, }); }); } exports.deleteImpl = deleteImpl; /** * Remove and commit a file * * @throws {@link Err.UndefinedDBError} * @throws {@link Err.DocumentNotFoundError} * @throws {@link Err.CannotDeleteDataError} */ async function deleteWorker(gitDDB, collectionPath, shortName, commitMessage) { if (gitDDB === undefined) { throw new error_1.Err.UndefinedDBError(); } const fullDocPath = collectionPath + shortName; if (collectionPath === undefined || shortName === undefined || fullDocPath === '') { throw new error_1.Err.DocumentNotFoundError(); } let commit; const filePath = path_1.default.resolve(gitDDB.workingDir, fullDocPath); const headCommit = await isomorphic_git_1.default .resolveRef({ fs: fs_extra_1.default, dir: gitDDB.workingDir, ref: 'HEAD' }) .catch(() => undefined); if (headCommit === undefined) throw new error_1.Err.DocumentNotFoundError(); const { oid } = await isomorphic_git_1.default .readBlob({ fs: fs_extra_1.default, dir: gitDDB.workingDir, oid: headCommit, filepath: fullDocPath, }) .catch(() => { throw new error_1.Err.DocumentNotFoundError(); }); const fileOid = oid; await isomorphic_git_1.default.remove({ fs: fs_extra_1.default, dir: gitDDB.workingDir, filepath: fullDocPath }); commitMessage = commitMessage.replace(/<%file_oid%>/, fileOid.substr(0, const_1.SHORT_SHA_LENGTH)); try { // Default ref is HEAD const commitOid = await isomorphic_git_1.default.commit({ fs: fs_extra_1.default, dir: gitDDB.workingDir, author: gitDDB.author, committer: gitDDB.committer, message: commitMessage, }); const readCommitResult = await isomorphic_git_1.default.readCommit({ fs: fs_extra_1.default, dir: gitDDB.workingDir, oid: commitOid, }); commit = (0, utils_1.normalizeCommit)(readCommitResult); await fs_extra_1.default.remove(filePath); // remove parent directory recursively if empty const dirname = path_1.default.dirname(fullDocPath); const dirs = dirname.split(/[/\\¥]/); for (let i = 0; i < dirs.length; i++) { const dirpath = i === 0 ? path_1.default.resolve(gitDDB.workingDir, ...dirs) : path_1.default.resolve(gitDDB.workingDir, ...dirs.slice(0, -i)); // eslint-disable-next-line no-await-in-loop await fs_extra_1.default.rmdir(dirpath).catch(e => { /* not empty */ }); } } catch (err) { return Promise.reject(new error_1.Err.CannotDeleteDataError(err.message)); } return { fileOid, commit, name: shortName, }; } exports.deleteWorker = deleteWorker; //# sourceMappingURL=delete.js.map