Query

constructor(options: QueryOptions)

Create a new query instance.

const q = new Query({
    redis: new Redis(...),
    encoderDecoder: // optional, defaults to JSON.stringify/JSON.parse
});

.createCollection(name: string)

Create a new collection by giving it a name and document type.

const users = q.createCollection<User>("users");

Collection

.set(id: string, data: T): Promise<void>

Insert a new document to the collection. This will throw an error if the document already exists.

await users.set(userId, user)

.get(id: string): Promise<Document<T> | null>

Get a document by id.

const document = await users.get(userId)
// {
//     id: userId,
//     ts: 000, // the timestamp when created or last updated
//     data: {} // the data you have stored
// }

.delete(id: string): Promise<void>

Delete a document by id.

await users.delete(userId)

.update(id: string, data: T): Promise<void>

Update a document by id. This will throw an error if the document does not exist.

user.name = "New Name"
await users.update(userId, user)

.createIndex(options: CreateIndexOptions<T>): Index<T>

Create a new index on the collection. The terms field will be strongly typed depending on the type you have passed in when creating the collection.

const usersByOrganization = users.createIndex({
    name: "users_by_organization",
    terms: ["organization"],
});

Index

.match(query): Promise<Document<T>[]>

Search for matches in the index. The query argument is strongly typed depending on the terms you have passed in when creating the index.

const upstashEmployees = await usersByOrganization.match({ 
    organization: "Upstash" 
  });

  /**
   * [
   *     {
   *         id: "documentId",
   *         ts: 000, // the timestamp when created or last updated
   *         data: {
   *             id: "chronark",
   *             name: "Andreas Thomas",
   *             organization: "Upstash",
   *             email: "andreas@upstash.com"
   *         }
   *     }
   * ]
   */

.reIndex(): Promise<void>

Rebuild the index. This might be necessary if you are adding an index after documents are already in the collection. reIndex will go through the entire collection and rebuild the index, depending on the size of the collection this might take a while.

.delete(): Promise<void>

Delete the index and all its data. The items in the collection will not be affected.