hadron-document
Version:
Hadron Document
239 lines • 8.08 kB
TypeScript
import type { Element, ElementEventsType } from './element';
import { ElementList } from './element';
import EventEmitter from 'eventemitter3';
import type { KeyInclusionOptions, ObjectGeneratorOptions } from './object-generator';
import type { BSONArray, BSONObject, BSONValue } from './utils';
import type { HadronEJSONOptions } from './utils';
import { DocumentEvents, type DocumentEventsType } from './document-events';
export declare const DEFAULT_VISIBLE_ELEMENTS = 25;
/**
* Represents a document.
*/
export declare class Document extends EventEmitter<DocumentEventsType | ElementEventsType> {
uuid: string;
doc: BSONObject;
cloned: boolean;
isUpdatable: boolean;
elements: ElementList;
type: 'Document';
currentType: 'Document';
size: number | null;
expanded: boolean;
maxVisibleElementsCount: number;
editing: boolean;
markedForDeletion: boolean;
modifiedEJSONString: string | null;
/**
* Send cancel event.
*/
cancel(): void;
/**
* Create the new document from the provided object.
*
* @param {Object} doc - The document.
* @param {boolean} cloned - If it is a cloned document.
*/
constructor(doc: BSONObject, cloned?: boolean);
apply(doc: BSONObject | Document): void;
preserveTypes(other: Document): void;
/**
* Adjust field types in this document based on the collection's existing
* schema to prevent unintended type narrowing on insert. For example, if
* a field is typed as Double in the schema but the value 5.0 was parsed
* as Int32, this will cast it back to Double.
*
* @param schemaFields - A map of dotted field paths to objects containing
* a `type` property with their observed schema type(s). Uses
* mongodb-schema naming ('Double', 'Long', etc.). Only fields present
* in this map are considered for type adjustment.
*/
preserveTypesFromSchema(schemaFields: Readonly<Record<string, {
type: string | string[];
}>>): void;
/**
* Generate the javascript object for this document.
*
* @returns {Object} The javascript object.
*/
generateObject(options?: ObjectGeneratorOptions): BSONObject;
/**
* Generate the javascript object with the original elements in this document.
*
* @returns {Object} The original javascript object.
*/
generateOriginalObject(options?: ObjectGeneratorOptions): BSONObject;
/**
* Generate the `query` and `updateDoc` to be used in an update operation
* where the update only succeeds when the changed document's elements have
* not been changed in the background.
*
* `query` and `updateDoc` may use $getField and $setField if field names
* contain either `.` or start with `$`. These operators are only available
* on MongoDB 5.0+. (Note that field names starting with `$` are also only
* allowed in MongoDB 5.0+.)
*
* @param keyInclusionOptions Specify which fields to include in the
* originalFields list.
*
* @returns {Object} An object containing the `query` and `updateDoc` to be
* used in an update operation.
*/
generateUpdateUnlessChangedInBackgroundQuery(opts?: Readonly<KeyInclusionOptions>): {
query: BSONObject;
updateDoc: {
$set?: BSONObject;
$unset?: BSONObject;
} | BSONArray;
};
/**
* Get an element by its key.
*
* @param {String} key
*
* @returns {Element} The element.
*/
get(key: string): Element | undefined;
/**
* Get an element by a series of segment names.
*
* @param {Array} path - The series of fieldnames. Cannot be empty.
*
* @returns {Element} The element.
*/
getChild(path: (string | number)[]): Element | undefined;
/**
* Get the _id value for the document.
*
* @returns {Object} The id.
*/
getId(): BSONValue;
/**
* Generate the query javascript object reflecting the elements that
* are specified by the keys listed in `keys`. The values of this object are
* the original values, this can be used when querying for an update based
* on multiple criteria.
*
* @param keyInclusionOptions Specify which fields to include in the
* originalFields list.
*
* @returns {Object} The javascript object.
*/
getQueryForOriginalKeysAndValuesForSpecifiedKeys(opts?: Readonly<KeyInclusionOptions>): BSONObject;
/**
* Get the _id value as a string. Required if _id is not always an ObjectId.
*
* @returns {String} The string id.
*/
getStringId(): null | string;
/**
* Insert a placeholder element at the end of the document.
*
* @returns {Element} The placeholder element.
*/
insertPlaceholder(): Element;
/**
* Add a new element to this document.
*
* @param {String} key - The element key.
* @param {Object} value - The value.
*
* @returns {Element} The new element.
*/
insertBeginning(key: string | number, value: BSONValue): Element;
/**
* Add a new element to this document.
*
* @param {String} key - The element key.
* @param {Object} value - The value.
*
* @returns {Element} The new element.
*/
insertEnd(key: string | number, value: BSONValue): Element;
/**
* Insert an element after the provided element.
*
* @param {Element} element - The element to insert after.
* @param {String} key - The key.
* @param {Object} value - The value.
*
* @returns {Element} The new element.
*/
insertAfter(element: Element, key: string | number, value: BSONValue): Element | undefined;
/**
* A document always exists, is never added.
*
* @returns Always false.
*/
isAdded(): boolean;
/**
* Determine if the element is modified at all.
*
* @returns {Boolean} If the element is modified.
*/
isModified(): boolean;
/**
* A document is never removed
*
* @returns {false} Always false.
*/
isRemoved(): boolean;
/**
* The document object is always the root object.
*
* @returns {true} Always true.
*/
isRoot(): this is Document;
/**
* Generates a sequence of elements.
*
* @returns {Array} The elements.
*/
_generateElements(): ElementList;
/**
* @deprecated Use DocumentEvents import instead
*/
static get Events(): typeof DocumentEvents;
/**
* Parse a new Document from extended JSON input.
*/
static FromEJSON(input: string): Document;
/**
* Parse multiple Document from extended JSON input.
* If the input consists of only a single document without
* `[array]` brackets, return an array consisting of only
* that document.
*/
static FromEJSONArray(input: string): Document[];
/**
* Convert this Document instance into a human-readable EJSON string.
*/
toEJSON(source?: 'original' | 'current', options?: HadronEJSONOptions): string;
/**
* Expands a document by expanding all of its fields
*/
expand(): void;
/**
* Collapses a document by collapsing all of its fields
*/
collapse(): void;
getVisibleElements(): Element[];
setMaxVisibleElementsCount(newCount: number): void;
getTotalVisibleElementsCount(): number;
findUUIDs(): {
subtype3Count: number;
subtype4Count: number;
};
startEditing(elementId?: string, field?: 'key' | 'value' | 'type'): void;
finishEditing(): void;
onUpdateStart(): void;
onUpdateSuccess(doc: Record<string, unknown>): void;
onUpdateBlocked(): void;
onUpdateError(error: Error): void;
markForDeletion(): void;
finishDeletion(): void;
onRemoveStart(): void;
onRemoveSuccess(): void;
onRemoveError(error: Error): void;
setModifiedEJSONString(ejson: string | null): void;
}
//# sourceMappingURL=document.d.ts.map