UNPKG

git-documentdb

Version:

Offline-first database that syncs with Git

850 lines 34 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 this source tree. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Collection = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const isomorphic_git_1 = require("isomorphic-git"); const ulid_1 = require("ulid"); const error_1 = require("./error"); const validator_1 = require("./validator"); const const_1 = require("./const"); const get_1 = require("./crud/get"); const history_1 = require("./crud/history"); const delete_1 = require("./crud/delete"); const find_1 = require("./crud/find"); const put_1 = require("./crud/put"); /** * Documents under a collectionPath are gathered together in a collection. * * @remarks * In a collection API, shortId (shortName) is used instead of _id (name). * * - shortId is a file path whose collectionPath and extension are omitted. (_id = collectionPath + shortId) * * - shortName is a file path whose collectionPath is omitted. (name = collectionPath + shortName) * * @example * ``` * const gitDDB = new GitDocumentDB({ db_name: 'db01' }); * * // Both put git_documentdb/db01/Nara/flower.json: { _id: 'Nara/flower', name: 'cherry blossoms' }. * gitDDB.put({ _id: 'Nara/flower', name: 'cherry blossoms' }); * gitDDB.collection('Nara').put({ _id: 'flower', name: 'cherry blossoms' }) * * // Notice that APIs return different _id values despite the same source file. * gitDDB.get({ _id: 'Nara/flower' }); // returns { _id: 'Nara/flower', name: 'cherry blossoms' }. * gitDDB.collection('Nara').get({ _id: 'flower' }); // returns { _id: 'flower', name: 'cherry blossoms' }. * ``` * @public */ class Collection { /** * Constructor * * @param collectionPathFromParent - A relative collectionPath from a parent collection. * @param parent - A parent collection of this collection. * * @throws {@link Err.InvalidCollectionPathCharacterError} * @throws {@link Err.InvalidCollectionPathLengthError} * * @public */ constructor(gitDDB, collectionPathFromParent, parent, options) { var _a; this._collectionPath = ''; this._gitDDB = gitDDB; this._collectionPath = validator_1.Validator.normalizeCollectionPath(collectionPathFromParent); this._gitDDB.validator.validateCollectionPath(this.collectionPath); if (parent === undefined) { this._parent = undefined; options !== null && options !== void 0 ? options : (options = { namePrefix: '', debounceTime: undefined, }); (_a = options.debounceTime) !== null && _a !== void 0 ? _a : (options.debounceTime = -1); } else { this._parent = parent; } this._options = { ...parent === null || parent === void 0 ? void 0 : parent.options, ...options }; if (this._options.idGenerator !== undefined) { this._monoID = this._options.idGenerator; } else { this._monoID = (0, ulid_1.monotonicFactory)(); } } /** * Get a clone of collection options * * @readonly * @public */ get options() { return { ...this._options }; } /** * Normalized path of a collection * * @remarks * collectionPath is '' or path strings that have a trailing slash and no heading slash. '/' is not allowed. Backslash \\ or yen ¥ is replaced with slash /. * @public */ get collectionPath() { return this._parent === undefined ? this._collectionPath : this._parent.collectionPath + this._collectionPath; } /** * Parent collection * * @remarks * Child collection inherits Parent's CollectionOptions. * * @public */ get parent() { return this._parent; } /*********************************************** * Public methods ***********************************************/ /** * Generate new _id as monotonic ULID * * @remarks * See https://github.com/ulid/javascript * * @returns 26 Base32 alphabets * * @public */ generateId(seedTime) { if (seedTime === undefined) { seedTime = Date.now(); } return this._options.namePrefix + this._monoID(seedTime); } /** * Get a collection * * @param collectionPath - relative path from this.collectionPath. Sub-directories are also permitted. e.g. 'pages', 'pages/works'. * * @remarks * - Notice that this function just read an existing directory. It does not make a new sub-directory. * * @returns A child collection of this collection * * @public */ collection(collectionPath, options) { return new Collection(this._gitDDB, collectionPath, this, options); } /** * Get collections directly under the specified dirPath. * * @param dirPath - dirPath is a relative path from collectionPath. Default is ''. * @returns Array of Collections which does not include ''. * * @public */ async getCollections(dirPath = '') { var _a; dirPath = validator_1.Validator.normalizeCollectionPath(this.collectionPath + dirPath); dirPath = dirPath.slice(0, -1); const commitOid = await (0, isomorphic_git_1.resolveRef)({ fs: fs_1.default, dir: this._gitDDB.workingDir, ref: 'main' }); const treeResult = await (0, isomorphic_git_1.readTree)({ fs: fs_1.default, dir: this._gitDDB.workingDir, oid: commitOid, filepath: dirPath, }).catch(() => undefined); const rootTree = (_a = treeResult === null || treeResult === void 0 ? void 0 : treeResult.tree) !== null && _a !== void 0 ? _a : []; const collections = []; for (const entry of rootTree) { const fullDocPath = dirPath !== '' ? `${dirPath}/${entry.path}` : entry.path; if (entry.type === 'tree') { if (fullDocPath !== const_1.GIT_DOCUMENTDB_METADATA_DIR) { collections.push(new Collection(this._gitDDB, fullDocPath)); } } } return collections; } // eslint-disable-next-line complexity put(shortIdOrDoc, jsonDocOrOptions, options) { var _a; let shortId; let _id; let jsonDoc; // Resolve overloads if (typeof shortIdOrDoc === 'string' || shortIdOrDoc === undefined || shortIdOrDoc === null) { if (!shortIdOrDoc) shortIdOrDoc = this.generateId(); shortId = shortIdOrDoc; _id = this.collectionPath + shortId; jsonDoc = jsonDocOrOptions; } else { if (!shortIdOrDoc._id) shortIdOrDoc._id = this.generateId(); shortId = shortIdOrDoc._id; _id = this.collectionPath + shortId; jsonDoc = shortIdOrDoc; options = jsonDocOrOptions; } // JSON let clone; try { clone = JSON.parse(JSON.stringify(jsonDoc)); } catch (err) { return Promise.reject(new error_1.Err.InvalidJsonObjectError(shortId)); } clone._id = _id; const { extension, data } = this._gitDDB.serializeFormat.serialize(clone); const shortName = shortId + extension; try { this._gitDDB.validator.validateId(shortId); this._gitDDB.validator.validateDocument(clone); } catch (err) { return Promise.reject(err); } options !== null && options !== void 0 ? options : (options = { debounceTime: undefined, }); (_a = options.debounceTime) !== null && _a !== void 0 ? _a : (options.debounceTime = this._options.debounceTime); return (0, put_1.putImpl)(this._gitDDB, this.collectionPath, shortId, shortName, data, options).then(res => { const putResult = { ...res, _id: shortId, name: shortName, type: 'json', }; return putResult; }); } insert(shortIdOrDoc, jsonDocOrOptions, options) { var _a, _b; var _c; // Resolve overloads if (typeof shortIdOrDoc === 'string' || shortIdOrDoc === undefined || shortIdOrDoc === null) { options !== null && options !== void 0 ? options : (options = { debounceTime: undefined, }); options.insertOrUpdate = 'insert'; (_a = options.debounceTime) !== null && _a !== void 0 ? _a : (options.debounceTime = this._options.debounceTime); } else { jsonDocOrOptions !== null && jsonDocOrOptions !== void 0 ? jsonDocOrOptions : (jsonDocOrOptions = { debounceTime: undefined, }); jsonDocOrOptions.insertOrUpdate = 'insert'; (_b = (_c = jsonDocOrOptions).debounceTime) !== null && _b !== void 0 ? _b : (_c.debounceTime = this._options.debounceTime); } return this.put(shortIdOrDoc, jsonDocOrOptions, options); } update(shortIdOrDoc, jsonDocOrOptions, options) { var _a, _b; var _c; // Resolve overloads if (typeof shortIdOrDoc === 'string' || shortIdOrDoc === undefined || shortIdOrDoc === null) { options !== null && options !== void 0 ? options : (options = { debounceTime: undefined, }); options.insertOrUpdate = 'update'; (_a = options.debounceTime) !== null && _a !== void 0 ? _a : (options.debounceTime = this._options.debounceTime); } else { jsonDocOrOptions !== null && jsonDocOrOptions !== void 0 ? jsonDocOrOptions : (jsonDocOrOptions = { debounceTime: undefined, }); jsonDocOrOptions.insertOrUpdate = 'update'; (_b = (_c = jsonDocOrOptions).debounceTime) !== null && _b !== void 0 ? _b : (_c.debounceTime = this._options.debounceTime); } return this.put(shortIdOrDoc, jsonDocOrOptions, options); } /** * Insert data if not exists. Otherwise, update it. * * @param shortName - shortName is a file path whose collectionPath is omitted. shortName of JsonDoc must ends with extension. * * @remarks * - The saved file path is `${GitDocumentDB#workingDir}/${Collection#collectionPath}/${shortName}${extension}`. * * - If shortName is undefined, it is automatically generated. * * - _id property of a JsonDoc is automatically set or overwritten by shortName parameter whose extension is omitted. * * - An update operation is not skipped even if no change occurred on a specified data. * * @throws {@link Err.InvalidJsonFileExtensionError} * @throws {@link Err.InvalidJsonObjectError} * * @privateRemarks # Errors from putImpl * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.TaskCancelError} * * @throws # Errors from validateDocument, validateId * @throws - {@link Err.InvalidIdCharacterError} * @throws - {@link Err.InvalidIdLengthError} * * @throws # Errors from putWorker * @throws - {@link Err.UndefinedDBError} * @throws - {@link Err.CannotCreateDirectoryError} * @throws - {@link Err.CannotWriteDataError} * * @public */ // eslint-disable-next-line complexity putFatDoc(shortName, doc, options) { var _a; let shortId; let data; let docType; // Resolve overloads if (typeof doc === 'string') { if (!shortName) shortName = this.generateId(); docType = 'text'; data = doc; } else if (doc instanceof Uint8Array) { if (!shortName) shortName = this.generateId(); docType = 'binary'; data = doc; } else if (typeof doc === 'object') { const extension = this._gitDDB.serializeFormat.extension(doc); if (!shortName) shortName = this.generateId() + extension; docType = 'json'; // JsonDoc if (!shortName.endsWith(extension)) { return Promise.reject(new error_1.Err.InvalidJsonFileExtensionError()); } shortId = shortName.replace(new RegExp(extension + '$'), ''); // Validate JSON let clone; try { clone = JSON.parse(JSON.stringify(doc)); } catch (err) { return Promise.reject(new error_1.Err.InvalidJsonObjectError(shortId)); } clone._id = this.collectionPath + shortId; data = this._gitDDB.serializeFormat.serialize(clone).data; try { this._gitDDB.validator.validateDocument(clone); } catch (err) { return Promise.reject(err); } } else { return Promise.reject(new error_1.Err.InvalidDocTypeError(typeof doc)); } try { this._gitDDB.validator.validateId(shortName); } catch (err) { return Promise.reject(err); } options !== null && options !== void 0 ? options : (options = { debounceTime: undefined, }); (_a = options.debounceTime) !== null && _a !== void 0 ? _a : (options.debounceTime = this._options.debounceTime); return (0, put_1.putImpl)(this._gitDDB, this.collectionPath, shortId, shortName, data, options).then(res => { if (docType === 'json') { const putResult = { ...res, type: 'json', _id: shortId, }; return putResult; } else if (docType === 'text') { const putResult = { ...res, type: 'text', }; return putResult; } else if (docType === 'binary') { const putResult = { ...res, type: 'binary', }; return putResult; } return Promise.reject(new error_1.Err.InvalidDocTypeError(typeof doc)); }); } /** * Insert a data * * @param shortName - shortName is a file path whose collectionPath is omitted. shortName of JsonDoc must ends with extension. * * @remarks * - Throws SameIdExistsError when data that has the same _id exists. It might be better to use put() instead of insert(). * * - The saved file path is `${GitDocumentDB#workingDir}/${Collection#collectionPath}/${shortName}${extension}`. * * - If shortName is undefined, it is automatically generated. * * - _id property of a JsonDoc is automatically set or overwritten by shortName parameter whose extension is omitted. * * @throws {@link Err.InvalidJsonObjectError} * * @privateRemarks # Errors from putImpl * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.TaskCancelError} * * @throws # Errors from validateDocument, validateId * @throws - {@link Err.InvalidIdCharacterError} * @throws - {@link Err.InvalidIdLengthError} * * @throws # Errors from putWorker * @throws - {@link Err.UndefinedDBError} * @throws - {@link Err.CannotCreateDirectoryError} * @throws - {@link Err.CannotWriteDataError} * * @throws - {@link Err.SameIdExistsError} * * @public */ insertFatDoc(shortName, doc, options) { var _a; // Resolve overloads options !== null && options !== void 0 ? options : (options = { debounceTime: undefined, }); options.insertOrUpdate = 'insert'; (_a = options.debounceTime) !== null && _a !== void 0 ? _a : (options.debounceTime = this._options.debounceTime); return this.putFatDoc(shortName, doc, options); } /** * Update a data * * @param shortName - shortName is a file path whose collectionPath is omitted. shortName of JsonDoc must ends with extension. * * @remarks * - Throws DocumentNotFoundError if a specified data does not exist. It might be better to use put() instead of update(). * * - The saved file path is `${GitDocumentDB#workingDir}/${Collection#collectionPath}/${shortName}${extension}`. * * - _id property of a JsonDoc is automatically set or overwritten by shortName parameter whose extension is omitted. * * - An update operation is not skipped even if no change occurred on a specified data. * * @throws {@link Err.InvalidJsonObjectError} * * @privateRemarks # Errors from putImpl * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.TaskCancelError} * * @throws # Errors from validateDocument, validateId * @throws - {@link Err.InvalidIdCharacterError} * @throws - {@link Err.InvalidIdLengthError} * * @throws # Errors from putWorker * @throws - {@link Err.UndefinedDBError} * @throws - {@link Err.CannotCreateDirectoryError} * @throws - {@link Err.CannotWriteDataError} * * @throws - {@link Err.DocumentNotFoundError} * * @public */ updateFatDoc(shortName, doc, options) { var _a; // Resolve overloads options !== null && options !== void 0 ? options : (options = { debounceTime: undefined, }); options.insertOrUpdate = 'update'; (_a = options.debounceTime) !== null && _a !== void 0 ? _a : (options.debounceTime = this._options.debounceTime); return this.putFatDoc(shortName, doc, options); } /** * Get a JSON document * * @param shortId - shortId is a file path whose collectionPath and extension are omitted. * * @returns * - undefined if a specified document does not exist. * * - JsonDoc may not have _id property when an app other than GitDocumentDB creates it. * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ async get(_id) { let shortName = _id + this._gitDDB.serializeFormat.firstExtension; const result = (await (0, get_1.getJsonDocFromWorkingDir)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat)); if (result === undefined && this._gitDDB.serializeFormat.secondExtension !== undefined) { shortName = _id + this._gitDDB.serializeFormat.secondExtension; return (0, get_1.getJsonDocFromWorkingDir)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat); } return result; } /** * Get a FatDoc data * * @param shortName - shortName is a file path whose collectionPath is omitted. * * @returns * - undefined if a specified data does not exist. * * - FatJsonDoc if the file extension is SerializeFormat.extension. Be careful that JsonDoc may not have _id property when an app other than GitDocumentDB creates it. * * - FatBinaryDoc if described in .gitattribtues, otherwise FatTextDoc. * * - getOptions.forceDocType always overwrite return type. * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ async getFatDoc(shortName, getOptions) { const result = (await (0, get_1.getImpl)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat, getOptions, { withMetadata: true, })); if (result === undefined && this._gitDDB.serializeFormat.secondExtension !== undefined) { return (0, get_1.getImpl)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat, getOptions, { withMetadata: true, }); } return result; } /** * Get a Doc which has specified oid * * @param fileOid - Object ID (SHA-1 hash) that represents a Git object. (See https://git-scm.com/docs/git-hash-object ) * * @remarks * - undefined if a specified oid does not exist. * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ getDocByOid(fileOid, docType = 'text') { return (0, get_1.getImpl)(this._gitDDB, '', this.collectionPath, this._gitDDB.serializeFormat, { forceDocType: docType }, { withMetadata: false, oid: fileOid, }); } /** * Get an old revision of a JSON document * * @param shortId - shortId is a file path whose collectionPath and extension are omitted. * @param revision - Specify a number to go back to old revision. Default is 0. * See {@link git-documentdb#Collection.getHistory} for the array of revisions. * @param historyOptions - The array of revisions is filtered by HistoryOptions.filter. * * @remarks * - undefined if a specified document does not exist or it is deleted. * * - If serializeFormat is front-matter, this function can't correctly distinguish files that has the same _id but different extension. Use getFatDocOldRevision() instead. e.g.) foo.md and foo.yml * * @example * ``` * collection.getOldRevision(_shortId, 0); // returns the latest document. * collection.getOldRevision(_shortId, 2); // returns a document two revisions older than the latest. * ``` * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ async getOldRevision(shortId, revision, historyOptions) { let shortName = shortId + this._gitDDB.serializeFormat.firstExtension; const result = (await (0, get_1.getImpl)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat, { forceDocType: 'json' }, { withMetadata: false, revision: revision, }, historyOptions)); if (result === undefined && this._gitDDB.serializeFormat !== undefined) { shortName = shortId + this._gitDDB.serializeFormat.secondExtension; return (0, get_1.getImpl)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat, { forceDocType: 'json' }, { withMetadata: false, revision: revision, }, historyOptions); } return result; } /** * Get an old revision of a FatDoc data * * @param shortName - shortName is a file path whose collectionPath is omitted. * @param revision - Specify a number to go back to old revision. Default is 0. * See {@link git-documentdb#Collection.getHistory} for the array of revisions. * @param historyOptions - The array of revisions is filtered by HistoryOptions.filter. * * @remarks * - undefined if a specified data does not exist or it is deleted. * * - JsonDoc if the file extension is SerializedFormat.extension. Be careful that JsonDoc may not have _id property when an app other than GitDocumentDB creates it. * * - FatBinaryDoc if described in .gitattribtues, otherwise FatTextDoc. * * - getOptions.forceDocType always overwrite return type. * * @example * ``` * collection.getFatDocOldRevision(shortName, 0); // returns the latest FatDoc. * collection.getFatDocOldRevision(shortName, 2); // returns a FatDoc two revisions older than the latest. * ``` * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ getFatDocOldRevision(shortName, revision, historyOptions, getOptions) { return (0, get_1.getImpl)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat, getOptions, { withMetadata: true, revision: revision, }, historyOptions); } /** * Get revision history of a JSON document * * @remarks * - By default, revisions are sorted by reverse chronological order. However, keep in mind that Git dates may not be consistent across repositories. * * - If serializeFormat is front-matter, this function can't work for .yml files. Use getFatDocHistory() instead. e.g.) foo.yml * * @param shortId - shortId is a file path whose collectionPath and extension is omitted. * @param historyOptions - The array of revisions is filtered by HistoryOptions.filter. * * @returns Array of JsonDoc or undefined if a specified document does not exist or it is deleted. * * @example * ``` * Commit-01 to 08 were committed in order. file_v1 and file_v2 are two revisions of a file. * * - Commit-08: Not exists * - Commit-07: deleted * - Commit-06: file_v2 * - Commit-05: deleted * - Commit-04: file_v2 * - Commit-03: file_v1 * - Commit-02: file_v1 * - Commit-01: Not exists * * Commit-02 newly inserted a file (file_v1). * Commit-03 did not change about the file. * Commit-04 updated the file from file_v1 to file_v2. * Commit-05 deleted the file. * Commit-06 inserted the deleted file (file_v2) again. * Commit-07 deleted the file again. * Commit-08 did not change about the file. * * Here, getHistory() will return [undefined, file_v2, undefined, file_v2, file_v1] as a history. * * NOTE: * - Consecutive same values (commit-02 and commit-03) are combined into one. * - getHistory() ignores commit-01 because it was committed before the first insert. * Thus, a history is not [undefined, undefined, file_v2, undefined, file_v2, file_v1, file_v1, undefined]. * ``` * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ getHistory(_id, historyOptions) { const shortName = _id + this._gitDDB.serializeFormat.firstExtension; return (0, history_1.getHistoryImpl)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat, historyOptions, { forceDocType: 'json' }, false); } /** * Get revision history of a FatDoc data * * @param shortName - shortName is a file path whose collectionPath is omitted. * * @remarks * See {@link git-documentdb#GitDocumentDB.getHistory} for detailed examples. * * @returns Array of FatDoc or undefined. * - undefined if a specified data does not exist or it is deleted. * * - Array of FatJsonDoc if isJsonDocCollection is true or the file extension is SerializeFormat.extension. Be careful that JsonDoc may not have _id property when an app other than GitDocumentDB creates it. * * - Array of FatBinaryDoc if described in .gitattribtues, otherwise array of FatTextDoc. * * - getOptions.forceDocType always overwrite return type. * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ getFatDocHistory(shortName, historyOptions, getOptions) { return (0, history_1.getHistoryImpl)(this._gitDDB, shortName, this.collectionPath, this._gitDDB.serializeFormat, historyOptions, getOptions, true); } async delete(shortIdOrDoc, options) { let shortId; if (typeof shortIdOrDoc === 'string') { shortId = shortIdOrDoc; } else if (shortIdOrDoc === null || shortIdOrDoc === void 0 ? void 0 : shortIdOrDoc._id) { shortId = shortIdOrDoc._id; } else { return Promise.reject(new error_1.Err.UndefinedDocumentIdError()); } let shortName = shortId + this._gitDDB.serializeFormat.firstExtension; // Check if file exists for deleting FrontMatter if (this._gitDDB.serializeFormat.secondExtension !== undefined) { const fullDocPath = this.collectionPath + shortName; const filePath = path_1.default.resolve(this._gitDDB.workingDir, fullDocPath); if (!fs_1.default.existsSync(filePath)) { shortName = shortId + this._gitDDB.serializeFormat.secondExtension; } } return await (0, delete_1.deleteImpl)(this._gitDDB, this.collectionPath, shortId, shortName, options).then(res => { const deleteResult = { ...res, _id: shortId, type: 'json', }; return deleteResult; }); } /** * Delete a data * * @param shortName - shortName is a file path whose collectionPath is omitted. * * @throws {@link Err.UndefinedDocumentIdError} * * @privateRemarks # Errors from deleteImpl * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.TaskCancelError} * * @throws # Errors from deleteWorker * @throws - {@link Err.UndefinedDBError} * @throws - {@link Err.DocumentNotFoundError} * @throws - {@link Err.CannotDeleteDataError} * * @public */ deleteFatDoc(shortName, options) { if (shortName === undefined) { return Promise.reject(new error_1.Err.UndefinedDocumentIdError()); } const docType = this._gitDDB.serializeFormat.hasObjectExtension(shortName) ? 'json' : 'text'; if (docType === 'text') { // TODO: select binary or text by .gitattribtues } const shortId = this._gitDDB.serializeFormat.removeExtension(shortName); return (0, delete_1.deleteImpl)(this._gitDDB, this.collectionPath, shortId, shortName, options).then(res => { // NOTE: Cannot detect JsonDoc whose file path does not end with SerializeFormat.extension if (docType === 'json') { const deleteResult = { ...res, type: 'json', _id: shortId, }; return deleteResult; } else if (docType === 'text') { const deleteResult = { ...res, type: 'text', }; return deleteResult; } else if (docType === 'binary') { const deleteResult = { ...res, type: 'binary', }; return deleteResult; } // Not occur return Promise.reject(new error_1.Err.InvalidDocTypeError(docType)); }); } /** * Get all the JSON documents * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ find(options) { var _a; options !== null && options !== void 0 ? options : (options = {}); (_a = options.forceDocType) !== null && _a !== void 0 ? _a : (options.forceDocType = 'json'); return (0, find_1.findImpl)(this._gitDDB, this.collectionPath, this._gitDDB.serializeFormat, true, false, options); } /** * Get all the FatDoc data * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.InvalidJsonObjectError} * * @public */ findFatDoc(options) { return (0, find_1.findImpl)(this._gitDDB, this.collectionPath, this._gitDDB.serializeFormat, false, true, options); } onSyncEvent(remoteURLorSync, event, callback) { let sync; if (typeof remoteURLorSync === 'string') { sync = this._gitDDB.getSync(remoteURLorSync); } else { sync = remoteURLorSync; } if (sync === undefined) { throw new error_1.Err.UndefinedSyncError(); } return sync.on(event, callback, this.collectionPath); } offSyncEvent(remoteURLorSync, event, callback) { let sync; if (typeof remoteURLorSync === 'string') { sync = this._gitDDB.getSync(remoteURLorSync); } else { sync = remoteURLorSync; } if (sync === undefined) { throw new error_1.Err.UndefinedSyncError(); } sync.off(event, callback); } } exports.Collection = Collection; //# sourceMappingURL=collection.js.map