createTable()

Creates a new table where entities can be inserted/updated/deleted/retrieved.

interface User {
  id: string;
  name: string;
  age?: number;
}

const userTable = createTable<User>(db, "users")();
Parameter Description
db The database created by createDB() .
name Name of your table. For future usage.
Option Description
primary The primary key of the entity. See Primary Keys
indexes Fields of the entity on which an index should be created. See Indexes

Valid Entities

The interface you pass to createTable() defines what properties are available on your entity. Valid property types are primitives (string, boolean, number, null, undefined, BigInt), Dates, arrays, and complex objects. You cannot use symbols or functions as a type.

interface ValidEntity {
  name: string;
  isDeleted: boolean;
  age: number;
  surname: string | null;
  likesPizza: boolean | undefined;
  ageInMs: BigInt;
  createdAt: Date;
  nicknames: string[];
  some: {
    nested: {
      object: boolean;
    };
  };
}

At runtime, you can check that you can use an object in a table with isValidEntity.

Primary keys

BlinkDB automatically assumes that the primary key of a table is id , if not otherwise supplied:

// `uuid` should be the primary id
interface Task {
  uuid: string;
  name?: string;
  description?: string;
}

const userTable = createTable<Task>(db, "tasks")({
  primary: "uuid",
});

Indexes

Indexes are a valuable performance improvement for databases by reducing the amount of items scanned per query. To use them effectively, specify properties here that are often used in filters when you call first() / one() / many() / watch() - This will increase speed for queries drastically, with only a small performance hit on writes.

At startup, BlinkDB will automatically create a primary key index for you.

interface User {
  id: string;
  // Email will often be used in filters, better create an index
  email: string;
  // No need for an index on Age - it won't be specified much in filters
  age: number;
}

const userTable = createTable<Task>(db, "tasks")({
  primary: "uuid",
  indexes: ["email"],
});
blinkDB © 2023