UNPKG

reduct-js

Version:

ReductStore Client SDK for Javascript/NodeJS/Typescript

225 lines (224 loc) 8.5 kB
import { BucketSettings } from "./messages/BucketSettings"; import { BucketInfo } from "./messages/BucketInfo"; import { EntryInfo } from "./messages/EntryInfo"; import { LabelMap, ReadableRecord, WritableRecord } from "./Record"; import { Batch } from "./Batch"; import { RecordBatch } from "./RecordBatch"; import { QueryOptions } from "./messages/QueryEntry"; import { HttpClient } from "./http/HttpClient"; /** * Options for writing records */ export interface WriteOptions { ts?: bigint; labels?: LabelMap; contentType?: string; } /** * Represents a bucket in ReductStore */ export declare class Bucket { private name; private readonly httpClient; /** * Create a bucket. Use Client.creatBucket or Client.getBucket instead it * @constructor * @param name * @param httpClient * @see {Client} */ constructor(name: string, httpClient: HttpClient); /** * Get bucket settings * @async * @return {Promise<BucketSettings>} */ getSettings(): Promise<BucketSettings>; /** * Set bucket settings * @async * @param settings {BucketSettings} new settings (you can set a part of settings) */ setSettings(settings: BucketSettings): Promise<void>; /** * Get information about a bucket * @async * @return {Promise<BucketInfo>} */ getInfo(): Promise<BucketInfo>; /** * Get entry list * @async * @return {Promise<EntryInfo>} */ getEntryList(): Promise<EntryInfo[]>; /** * Remove bucket * @async * @return {Promise<void>} */ remove(): Promise<void>; /** * Remove an entry * @async * @param entry {string} name of the entry * @return {Promise<void>} */ removeEntry(entry: string): Promise<void>; /** * Remove a record * @param entry {string} name of the entry * @param ts {BigInt} timestamp of record in microseconds */ removeRecord(entry: string, ts: bigint): Promise<void>; /** * Remove a batch of records * @param entry {string} name of the entry * @param tsList {BigInt[]} list of timestamps of records in microseconds */ beginRemoveBatch(entry: string): Promise<Batch>; /** * Remove records by query * @param entry {string | string[]} name of the entry or entries * @param start {BigInt} start point of the time period, if undefined, the query starts from the first record * @param stop {BigInt} stop point of the time period. If undefined, the query stops at the last record * @param options {QueryOptions} options for query. You can use only include, exclude, eachS, eachN other options are ignored */ removeQuery(entry: string | string[], start?: bigint, stop?: bigint, options?: QueryOptions): Promise<number>; /** * Start writing a record into an entry * @param entry name of the entry * @param options {BigInt | WriteOptions} timestamp in microseconds for the record or options. It is current time if undefined. * @return Promise<WritableRecord> * @example * const record = await bucket.beginWrite("entry", { * ts: 12345667n * labels: {label1: "value1", label2: "value2"} * contentType: "text/plain" * ); * await record.write("Hello!"); */ beginWrite(entry: string, options?: bigint | WriteOptions): Promise<WritableRecord>; /** * Update labels of an existing record * * If a label has empty string value, it will be removed. * * @param entry {string} name of the entry * @param ts {BigInt} timestamp of record in microseconds * @param labels {LabelMap} labels to update */ update(entry: string, ts: bigint, labels: LabelMap): Promise<void>; /** * Start reading a record from an entry * @param entry name of the entry * @param ts {BigInt} timestamp of record in microseconds. Get the latest one, if undefined * @param head {boolean} return only head of the record * @return Promise<ReadableRecord> */ beginRead(entry: string, ts?: bigint, head?: boolean): Promise<ReadableRecord>; /** * Rename an entry * @param entry entry name to rename * @param newEntry new entry name */ renameEntry(entry: string, newEntry: string): Promise<void>; /** * Rename a bucket * @param newName new name of the bucket */ rename(newName: string): Promise<void>; /** * Query records for a time interval as generator * @param entry {string | string[]} name of the entry or entries * @param start {BigInt} start point of the time period * @param stop {BigInt} stop point of the time period * @param options {QueryOptions} options options for query * @example * for await (const record in bucket.query("entry-1", start, stop)) { * console.log(record.ts, record.size); * console.log(record.labels); * const content = await record.read(); * // or use pipe * const fileStream = fs.createWriteStream(`ts_${record.size}.txt`); * record.pipe(fileStream); * } */ query(entry: string | string[], start?: bigint, stop?: bigint, options?: QueryOptions): AsyncGenerator<ReadableRecord>; getName(): string; private readRecord; /** * Create a new batch for writing records to the database. * @param entry */ beginWriteBatch(entry: string): Promise<Batch>; /** * Create a new batch for writing records to multiple entries. * @example * const batch = await bucket.beginWriteRecordBatch(); * batch.add("entry-1", 1000n, "data"); * batch.add("entry-2", 2000n, "data"); * await batch.send(); */ beginWriteRecordBatch(): RecordBatch; /** * Create a new batch for updating records in the database. * @param entry */ beginUpdateBatch(entry: string): Promise<Batch>; /** * Create a new batch for updating records across multiple entries. * @example * const batch = bucket.beginUpdateRecordBatch(); * batch.addOnlyLabels("entry-1", 1000n, { label1: "value1", label2: "" }); * batch.addOnlyLabels("entry-2", 2000n, { label1: "value2" }); * await batch.send(); */ beginUpdateRecordBatch(): RecordBatch; /** * Create a new batch for removing records across multiple entries. */ beginRemoveRecordBatch(): RecordBatch; /** * Create a query link for downloading records * @param entry name of the entry or entries * @param start start point of the time period for the query * @param stop stop point of the time period for the query * @param query options for the query * @param record selector for the record to download (required): * - `number`: legacy record index (works only before ReductStore v1.19; removed from v1.19 API because broken, removed in SDK v1.21) * - `{ entry, timestamp }`: explicit record identity for ReductStore v1.19+ * @param expireAt expiration time of the link. Default is 24 hours from now * @param fileName name of the file to download. Default is `${entry}_<selector>.bin` or `${bucket}_<selector>.bin` for multi-entry * @param baseUrl base url for link generation. If not set, the server's base url will be used */ createQueryLink(entry: string | string[], start?: bigint, stop?: bigint, query?: QueryOptions, record?: number | { entry: string; timestamp: bigint; }, expireAt?: Date, fileName?: string, baseUrl?: string): Promise<string>; /** * Write attachments to an entry. * * Attachments are stored as JSON records in `${entry}/$meta` with `key` label. * * @param entry name of the source entry * @param attachments map of attachment key to JSON-serializable content */ writeAttachments(entry: string, attachments: Record<string, unknown>): Promise<void>; /** * Read attachments from an entry. * * @param entry name of the source entry * @return map of attachment key to decoded JSON value */ readAttachments(entry: string): Promise<Record<string, unknown>>; /** * Remove attachments from an entry. * * If `attachmentKeys` is omitted, remove all attachments. * * @param entry name of the source entry * @param attachmentKeys list of keys to remove */ removeAttachments(entry: string, attachmentKeys?: string[]): Promise<void>; }