LocalStorage Adapter

LocalStorage Adapter provides persistent browser storage for URPC entities using the localStorage API. Ideal for lightweight client-side applications that need simple data persistence.

Basic Setup

import { URPC } from "@unilab/urpc";
import { LocalStorageAdapter } from "@unilab/urpc-adapters";
import { UserEntity } from "./entities/user";

const MyPlugin = {
  entities: [UserEntity],
};

URPC.init({
  plugins: [MyPlugin],
  entityConfigs: {
    user: {
      defaultSource: "localstorage",
    },
  },
  globalAdapters: [
    {
      source: "localstorage",
      factory: () => new LocalStorageAdapter(),
    },
  ],
});

Creating Data

import { repo } from "@unilab/urpc";

const newUser = await repo({
  entity: UserEntity,
  source: "localstorage",
}).create({
  data: {
    id: "user123",
    name: "John Doe",
    email: "[email protected]",
    avatar: "https://example.com/avatar.jpg",
  },
});
console.log("✅ User created:", newUser);

Creating Multiple Records

const newUsers = await repo({
  entity: UserEntity,
  source: "localstorage",
}).createMany({
  data: [
    {
      id: "user1",
      name: "Alice",
      email: "[email protected]",
    },
    {
      id: "user2", 
      name: "Bob",
      email: "[email protected]",
    },
  ],
});
console.log("✅ Users created:", newUsers);

Finding Data

const user = await repo({
  entity: UserEntity,
  source: "localstorage",
}).findOne({
  where: { id: "user123" },
});

if (user) {
  console.log("✅ User found:", user);
  // Call entity methods
  user.greet("Welcome to LocalStorage!");
} else {
  console.log("❌ User not found");
}

Updating Data

const updatedUser = await repo({
  entity: UserEntity,
  source: "localstorage",
}).update({
  where: { id: "user123" },
  data: {
    name: "Jane Smith",
    email: "[email protected]",
  },
});
console.log("✅ User updated:", updatedUser);

Updating Multiple Records

const updatedUsers = await repo({
  entity: UserEntity,
  source: "localstorage",
}).updateMany({
  where: { status: "inactive" },
  data: {
    status: "active",
    updatedAt: new Date(),
  },
});
console.log(`✅ Updated ${updatedUsers.length} users`);

Deleting Data

const deleted = await repo({
  entity: UserEntity,
  source: "localstorage",
}).delete({
  where: { id: "user123" },
});

if (deleted) {
  console.log("✅ User deleted successfully");
} else {
  console.log("❌ User not found for deletion");
}

Upsert Operations

const user = await repo({
  entity: UserEntity,
  source: "localstorage",
}).upsert({
  where: { email: "[email protected]" },
  update: { name: "John Updated" },
  create: {
    id: "new_user",
    name: "John Doe",
    email: "[email protected]",
  },
});
console.log("✅ User upserted:", user);

Listing All Data

const users = await repo({
  entity: UserEntity,
  source: "localstorage",
}).findMany();

console.log(`Found ${users.length} users:`, users);