Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 11x 11x 13x 13x 13x 1x 1x 1x 1x 1x 1x 13x 1x 1x 1x 1x 1x 1x 13x 2x 2x 13x 2x 2x 13x 1x 1x 13x 1x 1x 1x 13x 2x 2x 13x 13x | import {
createTable,
type FilterFn,
type InferRec,
type ReactiveTable,
type Rec,
type Row,
type RowListState,
type RowState,
} from '@anchorlib/storage/db';
import { onCleanup } from 'solid-js';
export interface TableRef<T extends Rec, R extends Row<T> = Row<T>> {
/**
* Get a row by its id.
* @param id - The id of the row to get.
* @returns The state of the requested row.
*/
get(id: string): RowState<R>;
/**
* Add a new row to the table.
* @param payload - The data to add as a new row.
* @returns The state of the newly added row.
*/
add(payload: T): RowState<R>;
/**
* Remove a row by its id.
* @param id - The id of the row to remove.
* @returns The state of the removed row.
*/
remove(id: string): RowState<R>;
/**
* List rows in the table with optional filtering, limiting, and ordering.
* @param filter - A filter function or IDBKeyRange to filter rows.
* @param limit - Maximum number of rows to return.
* @param direction - Direction to iterate through the rows.
* @returns The state of the row list.
*/
list(filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection): RowListState<R>;
/**
* List rows by a specific index with optional filtering, limiting, and ordering.
* @param name - The index name to use for listing.
* @param filter - A filter function or IDBKeyRange to filter rows.
* @param limit - Maximum number of rows to return.
* @param direction - Direction to iterate through the rows.
* @returns The state of the row list.
*/
listByIndex(
name: keyof R,
filter?: IDBKeyRange | FilterFn<R>,
limit?: number,
direction?: IDBCursorDirection
): RowListState<R>;
/**
* Seed the table with initial data.
* @param seeds - Array of initial row data.
* @returns The table reference instance for chaining.
*/
seed<T extends R[]>(seeds: T): this;
/**
* Get the underlying reactive table instance.
* @returns The reactive table instance.
*/
table(): ReactiveTable<T>;
}
export type InferRef<T> = T extends TableRef<Rec, infer R> ? R : never;
export type InferListRef<T> = T extends TableRef<Rec, infer R> ? R[] : never;
/**
* @deprecated Use `createTable()` instead.
* Creates a TableRef from an existing ReactiveTable instance.
* @param table - The existing ReactiveTable instance to wrap.
* @returns A TableRef wrapping the provided table.
*/
export function createTableRef<T extends ReactiveTable<Rec>>(table: T): TableRef<InferRec<T>>;
/**
* @deprecated Use `createTable()` instead.
* Creates a TableRef by creating a new ReactiveTable with the specified parameters.
* @param name - The name of the table to create.
* @param version - The version of the table schema (default: 1).
* @param indexes - Array of property names to create indexes for.
* @param remIndexes - Array of property names to remove indexes for.
* @param dbName - The name of the database to use (defaults to tableName).
* @returns A TableRef wrapping the newly created table.
*/
export function createTableRef<T extends Rec, R extends Row<T> = Row<T>>(
name: string,
version?: number,
indexes?: (keyof R)[],
remIndexes?: (keyof R)[],
dbName?: string
): TableRef<T, R>;
/**
* @deprecated Use `createTable()` instead.
* Implementation of createTableRef that handles both overloads.
* @param tableName - Either the name of the table to create or an existing ReactiveTable instance.
* @param version - The version of the table schema (default: 1).
* @param indexes - Array of property names to create indexes for.
* @param remIndexes - Array of property names to remove indexes for.
* @param dbName - The name of the database to use (defaults to tableName).
* @returns A TableRef wrapping either the existing or newly created table.
*/
export function createTableRef<T extends Rec, R extends Row<T> = Row<T>>(
tableName: string | ReactiveTable<T>,
version = 1,
indexes?: (keyof R)[],
remIndexes?: (keyof R)[],
dbName = tableName as string
): TableRef<T, R> {
if (typeof tableName === 'string') {
tableName = createTable<T, R>(tableName, version, indexes, remIndexes, dbName);
}
const tableRef = tableName as ReactiveTable<T, R>;
return {
get(id: string) {
const state = tableRef.get(id);
onCleanup(() => {
tableRef.leave(id);
});
return state;
},
add(payload: T) {
const state = tableRef.add(payload);
onCleanup(() => {
tableRef.leave(state.data.id);
});
return state;
},
list(filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {
return tableRef.list(filter, limit, direction);
},
listByIndex(name: keyof R, filter?: IDBKeyRange | FilterFn<R>, limit?: number, direction?: IDBCursorDirection) {
return tableRef.listByIndex(name, filter, limit, direction);
},
remove(id: string) {
return tableRef.remove(id);
},
seed(seeds: R[]) {
tableRef.seed(seeds);
return this;
},
table() {
return tableRef;
},
};
}
|