Skip to main content

Overview

Inserts one or more values at the head of the list stored at key. If key does not exist, it is created as an empty list before performing the push operations.

Method Signature

lpush<TData>(key: string, ...elements: TData[]): Promise<number>

Parameters

key
string
required
The key of the list.
elements
TData[]
required
One or more elements to insert at the head of the list. Elements are inserted one after the other, so the first element in the arguments will be the first element of the list.

Return Value

length
number
The length of the list after the push operations.

Examples

Basic Usage

import { Redis } from '@upstash/redis';

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL,
  token: process.env.UPSTASH_REDIS_REST_TOKEN,
});

// Push a single element
const length1 = await redis.lpush('mylist', 'world');
console.log(length1); // 1

// Push multiple elements
const length2 = await redis.lpush('mylist', 'hello', 'hi');
console.log(length2); // 3
// List is now: ['hi', 'hello', 'world']

Working with Objects

interface Task {
  id: string;
  title: string;
  priority: number;
}

const task: Task = {
  id: '123',
  title: 'Complete documentation',
  priority: 1,
};

const length = await redis.lpush<Task>('tasks', task);
console.log(length); // 1

Building a Queue

// Add new items to the head of the queue
await redis.lpush('queue:jobs', 'job-1');
await redis.lpush('queue:jobs', 'job-2');
await redis.lpush('queue:jobs', 'job-3');

// Queue is now: ['job-3', 'job-2', 'job-1']

See Also

  • RPUSH - Insert elements at the tail of a list
  • LPOP - Remove and return the first element of a list
  • LRANGE - Get a range of elements from a list
  • LLEN - Get the length of a list