summaryrefslogtreecommitdiff
path: root/packages/idb-bridge/src/backend-interface.ts
blob: c963b189605ea6f2c347f997127a5685a179c6de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
  TransactionMode,
  Value,
  BridgeIDBCursorDirection,
  Key,
  KeyPath,
  BridgeIDBDatabaseInfo,
} from "./util/types";
import BridgeIDBKeyRange from "./BridgeIDBKeyRange";

export interface ObjectStoreProperties {
  keyPath: KeyPath | null;
  autoIncrement: boolean;
  indexes: string[];
}

export interface IndexProperties {
  keyPath: KeyPath;
  multiEntry: boolean;
  unique: boolean;
}

export interface Schema {
  databaseName: string;
  databaseVersion: number;
  objectStores: { [name: string]: ObjectStoreProperties };
  indexes: { [name: string]: IndexProperties };
}

export interface DatabaseConnection {
  connectionCookie: string;
}

export interface DatabaseTransaction {
  transactionCookie: string;
}

export enum ResultLevel {
  Full,
  OnlyKeys,
  OnlyCount,
}

export interface RecordGetRequest {
  direction: BridgeIDBCursorDirection;
  objectStoreName: string;
  indexName: string | undefined;
  /**
   * The range of keys to return.
   * If indexName is defined, the range refers to the index keys.
   * Otherwise it refers to the object store keys.
   */
  range: BridgeIDBKeyRange | undefined;
  /**
   * Last cursor position in terms of the index key.
   * Can only be specified if indexName is defined and
   * lastObjectStorePosition is defined.
   * 
   * Must either be undefined or within range.
   */
  lastIndexPosition?: Key;
  /**
   * Last position in terms of the object store key.
   */
  lastObjectStorePosition?: Key;
  /**
   * If specified, the index key of the results must be
   * greater or equal to advanceIndexKey.
   * 
   * Only applicable if indexName is specified.
   */
  advanceIndexKey?: Key;
  /**
   * If specified, the primary key of the results must be greater
   * or equal to advancePrimaryKey.
   */
  advancePrimaryKey?: Key;
  /**
   * Maximum number of resuts to return.
   * If -1, return all available results
   */
  limit: number;
  resultLevel: ResultLevel;
}

export interface RecordGetResponse {
  values: Value[] | undefined;
  indexKeys: Key[] | undefined;
  primaryKeys: Key[] | undefined;
  count: number;
}

export interface RecordStoreRequest {
  objectStoreName: string;
  value: Value;
  key: Key | undefined;
  overwrite: boolean;
}

export interface Backend {
  getDatabases(): Promise<BridgeIDBDatabaseInfo[]>;

  connectDatabase(name: string): Promise<DatabaseConnection>;

  beginTransaction(
    conn: DatabaseConnection,
    objectStores: string[],
    mode: TransactionMode,
  ): Promise<DatabaseTransaction>;

  enterVersionChange(
    conn: DatabaseConnection,
    newVersion: number,
  ): Promise<DatabaseTransaction>;

  /**
   * Even though the standard interface for indexedDB doesn't require
   * the client to run deleteDatabase in a version transaction, there is
   * implicitly one running. 
   */
  deleteDatabase(btx: DatabaseTransaction, name: string): Promise<void>;

  close(db: DatabaseConnection): Promise<void>;

  getSchema(db: DatabaseConnection): Schema;

  renameIndex(btx: DatabaseTransaction, oldName: string, newName: string): void;

  deleteIndex(btx: DatabaseTransaction, indexName: string): void;

  rollback(btx: DatabaseTransaction): Promise<void>;

  commit(btx: DatabaseTransaction): Promise<void>;

  deleteObjectStore(btx: DatabaseTransaction, name: string): void;

  createObjectStore(
    btx: DatabaseTransaction,
    name: string,
    keyPath: string | string[] | null,
    autoIncrement: boolean,
  ): void;

  renameObjectStore(
    btx: DatabaseTransaction,
    oldName: string,
    newName: string,
  ): void;

  createIndex(
    btx: DatabaseTransaction,
    indexName: string,
    objectStoreName: string,
    keyPath: KeyPath,
    multiEntry: boolean,
    unique: boolean,
  ): void;

  deleteRecord(
    btx: DatabaseTransaction,
    objectStoreName: string,
    range: BridgeIDBKeyRange,
  ): Promise<void>;

  getRecords(
    btx: DatabaseTransaction,
    req: RecordGetRequest,
  ): Promise<RecordGetResponse>;

  storeRecord(
    btx: DatabaseTransaction,
    storeReq: RecordStoreRequest,
  ): Promise<void>;
}