Base

Base Class

The Base class provides methods to interact with a Deta Base. If you have not yet instantiated the Deta Object, you must do so before you can use the Base Class.

function Base(baseName: string, host?: string);
ParameterTypeDescription
baseNamestringThe name of the Base.
host?stringThe host of the Base.

ℹ️ Deta Bases are automatically created for you when you start using them.

Example:

const { Deta } = require('deta'); // import Deta
const deta = Deta();
// This how to connect to or create a database.
const db = deta.Base('simple_db');

Put

put is the fastest way to store an item in a Base. If an item already exists under the given key, it will be replaced. In the case you do not provide a key, Base will automatically generate a 12-character string as a key.

async function put(
data: DetaType,
key?: string,
options?: PutOptions,
): Promise<PutResponse>;
ParameterTypeDescription
dataDetaTypeThe data to be stored.
key?stringThe key to store the data under. Will be auto generated if not provided.
options?PutOptionsOptional parameters.

Returns a promise which resolves to a PutResponse containing the item. If the operation did not complete successfully, throws an Error.

ℹ️ Numbers in Base have some limits.

Example:

await base.put({ value: "Hello world!" }, "my-key");

Get

Retrieves an item by its key.

async function get(key: string): Promise<GetResponse>;
ParameterTypeDescription
keystringThe key of the item to retrieve.

Returns a promise which resolves to a GetResponse. If the item is found, the response will contain the item. If not found, the response will be null.

Example:

const item = await base.get("my-key");

Delete

Deletes an item by its key.

async function delete(key: string): Promise<DeleteResponse>;

Deletes an item by its key.

ParameterTypeDescription
keystringThe key of the item to delete.

Returns a promise which resolves to a DeleteResponse. The response will always be null, even if the key does not exist.

Example:

await base.delete("my-key");

Insert

Inserts a single item, but is different from put in that it will throw an error of the key already exists in the Base.

async function insert(
data: DetaType,
key?: string,
options?: InsertOptions,
): Promise<InsertResponse>;

ℹ️ insert is roughly 2 times slower than put.

ParameterTypeDescription
dataDetaTypeThe data to be stored.
key?stringThe key to store the data under. Will be auto generated if not provided.
options?InsertOptionsOptional parameters.

Returns a promise which resolves to an InsertResponse containing the item. If the operation did not complete successfully, or the key already exists, throws an Error.

Example:

await base.insert({ value: "Hello world!" }, "my-key");

Put Many

Puts up to 25 items at once with a single call.

async function putMany(
items: DetaType[],
options?: PutManyOptions,
): Promise<PutManyResponse>;
ParameterTypeDescription
itemsDetaType[]The list of items to be stored.
options?PutManyOptionsOptional parameters.

Returns a promise which resolves to a PutManyResponse containing the items. If the operation did not complete successfully, or items contains more than 25 items, throws an Error.

Example:

await base.putMany(["First item", "Second item", "Third item"]);

Update

Updates an existing item.

async function update(
updates: ObjectType,
key: string,
options?: UpdateOptions,
): Promise<UpdateResponse>;
ParameterTypeDescription
updatesObjectTypeAn object describing the updates on the item.
keystringThe key of the item to be updated.
options?UpdateOptionsOptional parameters.

Example:

await base.update(
{ value: "Hello updated world!" },
"my-key",
);

Update operations

Setting values is practiced through normal key-value pairs. The operation changes the values of the attributes provided in the object / dictionary if the attribute already exists. If not, it adds the attribute to the item with the corresponding value.

Other operations are implemented through methods in the Util class / struct, accessible through the util attribute of a Base instance.

Util

The util attribute of a Base instance is an instance of the Util class. It provides utility methods for use in update operations.

Increment

Increments the value of an attribute. The provided value can be any positive or negative integer. The attribute’s value must be a number. The default value is 1.

function increment(value?: number);

Append

Appends to a list. The value can be a primitive type or a array / list.

function append(value: BasicType | ArrayType);

Prepend

Prepends to a list. The value can be a primitive type or an array / list.

function prepend(value: BasicType | ArrayType);

Trim

Removes an attribute from the item.

function trim();

Fetch

Retrieves a list of items matching a query. It will retrieve everything if no query is provided, up to a limit of 1 MB or 1000 items.

A query is composed of a single query object or a list of query objects. In the case of a list, the indvidual queries are OR’ed.

ℹ️ Up to 1 MB of data is retrieved before filtering with the query. Thus, in some cases you might get an empty list of items and a last key in the response. To apply the query through all the items in your Base, you have to call fetch until last is empty.

async function fetch(
query?: CompositeType,
options?: FetchOptions,
): Promise<FetchResponse>;
ParameterTypeDescription
query?CompositeTypeThe query to filter the items by.
options?FetchOptionsOptional parameters.

Returns promise which resolves a FetchResponse.

Example:

const response = await base.fetch({ "value?contains": "world" });

FetchResponse

The FetchResponse class describes the response of the fetch method.

AttributeTypeDescription
countintThe number of items in the response.
laststr | NoneThe last key seen in the fetch response. If last is not None further items are to be retreived
itemslistThe list of items retreived.