muya 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.js +1 -1
- package/esm/create-state.js +1 -1
- package/esm/create.js +1 -1
- package/esm/scheduler.js +1 -1
- package/esm/select.js +1 -1
- package/esm/sqlite/__tests__/create-sqlite-state.test.js +1 -0
- package/esm/sqlite/__tests__/map-deque.test.js +1 -0
- package/esm/sqlite/__tests__/table.test.js +1 -0
- package/esm/sqlite/__tests__/use-sqlite-state.test.js +1 -0
- package/esm/sqlite/create-sqlite-state.js +1 -0
- package/esm/sqlite/table/backend.js +1 -0
- package/esm/sqlite/table/bun-backend.js +1 -0
- package/esm/sqlite/table/map-deque.js +1 -0
- package/esm/sqlite/table/table.js +10 -0
- package/esm/sqlite/table/table.types.js +0 -0
- package/esm/sqlite/table/where.js +1 -0
- package/esm/sqlite/use-sqlite-value.js +1 -0
- package/esm/utils/common.js +1 -1
- package/package.json +1 -1
- package/src/__tests__/scheduler.test.tsx +2 -2
- package/src/create-state.ts +3 -2
- package/src/create.ts +22 -24
- package/src/scheduler.ts +15 -7
- package/src/select.ts +15 -17
- package/src/sqlite/__tests__/create-sqlite-state.test.ts +81 -0
- package/src/sqlite/__tests__/map-deque.test.ts +61 -0
- package/src/sqlite/__tests__/table.test.ts +142 -0
- package/src/sqlite/__tests__/use-sqlite-state.test.ts +213 -0
- package/src/sqlite/create-sqlite-state.ts +256 -0
- package/src/sqlite/table/backend.ts +21 -0
- package/src/sqlite/table/bun-backend.ts +38 -0
- package/src/sqlite/table/map-deque.ts +29 -0
- package/src/sqlite/table/table.ts +200 -0
- package/src/sqlite/table/table.types.ts +55 -0
- package/src/sqlite/table/where.ts +267 -0
- package/src/sqlite/use-sqlite-value.ts +76 -0
- package/src/types.ts +1 -0
- package/src/utils/common.ts +6 -2
- package/types/create.d.ts +3 -3
- package/types/scheduler.d.ts +12 -3
- package/types/sqlite/create-sqlite-state.d.ts +22 -0
- package/types/sqlite/table/backend.d.ts +20 -0
- package/types/sqlite/table/bun-backend.d.ts +2 -0
- package/types/sqlite/table/map-deque.d.ts +5 -0
- package/types/sqlite/table/table.d.ts +3 -0
- package/types/sqlite/table/table.types.d.ts +52 -0
- package/types/sqlite/table/where.d.ts +32 -0
- package/types/sqlite/use-sqlite-value.d.ts +21 -0
- package/types/types.d.ts +1 -0
- package/types/utils/common.d.ts +2 -2
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export declare const IN_MEMORY_DB = ":memory:";
|
|
2
|
+
export interface QueryResult {
|
|
3
|
+
/** The number of rows affected by the query. */
|
|
4
|
+
rowsAffected: number;
|
|
5
|
+
/**
|
|
6
|
+
* The last inserted `id`.
|
|
7
|
+
*
|
|
8
|
+
* This value is not set for Postgres databases. If the
|
|
9
|
+
* last inserted id is required on Postgres, the `select` function
|
|
10
|
+
* must be used, with a `RETURNING` clause
|
|
11
|
+
* (`INSERT INTO todos (title) VALUES ($1) RETURNING id`).
|
|
12
|
+
*/
|
|
13
|
+
lastInsertId?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface Backend {
|
|
16
|
+
execute: (query: string, bindValues?: unknown[]) => Promise<QueryResult>;
|
|
17
|
+
select: <T>(query: string, bindValues?: unknown[]) => Promise<T>;
|
|
18
|
+
transaction: (callback: (tx: Backend) => Promise<void>) => Promise<void>;
|
|
19
|
+
path: string;
|
|
20
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Backend } from './backend';
|
|
2
|
+
import type { Where } from './where';
|
|
3
|
+
export type DocType = {
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
};
|
|
6
|
+
export type KeyTypeAvailable = 'string' | 'number';
|
|
7
|
+
export interface DbOptions<Document extends DocType> {
|
|
8
|
+
readonly sorBy?: keyof Document;
|
|
9
|
+
readonly order?: 'asc' | 'desc';
|
|
10
|
+
readonly tableName: string;
|
|
11
|
+
readonly indexes?: Array<keyof Document>;
|
|
12
|
+
readonly backend: Backend;
|
|
13
|
+
/**
|
|
14
|
+
* Optional key. If omitted, the table uses implicit SQLite ROWID as the key.
|
|
15
|
+
*/
|
|
16
|
+
readonly key?: keyof Document;
|
|
17
|
+
}
|
|
18
|
+
interface DbNotGeneric {
|
|
19
|
+
readonly backend: Backend;
|
|
20
|
+
}
|
|
21
|
+
export interface SearchOptions<Document extends DocType, Selected = Document> {
|
|
22
|
+
readonly sorBy?: keyof Document;
|
|
23
|
+
readonly order?: 'asc' | 'desc';
|
|
24
|
+
readonly limit?: number;
|
|
25
|
+
readonly offset?: number;
|
|
26
|
+
readonly where?: Where<Document>;
|
|
27
|
+
readonly stepSize?: number;
|
|
28
|
+
/**
|
|
29
|
+
* Naive projection. Prefer specialized queries for heavy fan-out graphs.
|
|
30
|
+
*/
|
|
31
|
+
readonly select?: (document: Document, meta: {
|
|
32
|
+
rowId: number;
|
|
33
|
+
}) => Selected;
|
|
34
|
+
}
|
|
35
|
+
export type Key = string | number;
|
|
36
|
+
export type MutationOp = 'insert' | 'update' | 'delete';
|
|
37
|
+
export interface MutationResult {
|
|
38
|
+
key: Key;
|
|
39
|
+
op: MutationOp;
|
|
40
|
+
}
|
|
41
|
+
export interface Table<Document extends DocType> extends DbNotGeneric {
|
|
42
|
+
readonly set: (document: Document, backendOverride?: Backend) => Promise<MutationResult>;
|
|
43
|
+
readonly batchSet: (documents: Document[]) => Promise<MutationResult[]>;
|
|
44
|
+
readonly get: <Selected = Document>(key: Key, selector?: (document: Document) => Selected) => Promise<Selected | undefined>;
|
|
45
|
+
readonly delete: (key: Key) => Promise<MutationResult | undefined>;
|
|
46
|
+
readonly search: <Selected = Document>(options?: SearchOptions<Document, Selected>) => AsyncIterableIterator<Selected>;
|
|
47
|
+
readonly count: (options?: {
|
|
48
|
+
where?: Where<Document>;
|
|
49
|
+
}) => Promise<number>;
|
|
50
|
+
readonly deleteBy: (where: Where<Document>) => Promise<MutationResult[]>;
|
|
51
|
+
}
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface Field {
|
|
2
|
+
readonly table: string;
|
|
3
|
+
readonly field: string;
|
|
4
|
+
}
|
|
5
|
+
interface Condition<Document extends Record<string, unknown>, K extends keyof Document = keyof Document> {
|
|
6
|
+
readonly is?: Document[K] | Array<Document[K]>;
|
|
7
|
+
readonly isNot?: Document[K] | Array<Document[K]>;
|
|
8
|
+
readonly gt?: Document[K] | Array<Document[K]>;
|
|
9
|
+
readonly gte?: Document[K] | Array<Document[K]>;
|
|
10
|
+
readonly lt?: Document[K] | Array<Document[K]>;
|
|
11
|
+
readonly lte?: Document[K] | Array<Document[K]>;
|
|
12
|
+
readonly in?: Document[K] | Array<Document[K]>;
|
|
13
|
+
readonly notIn?: Document[K] | Array<Document[K]>;
|
|
14
|
+
readonly like?: Document[K] | Array<Document[K]>;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* We extend `keyof Document` by the special literal "KEY".
|
|
18
|
+
* That means users can write `{ KEY: ... }` in addition to `{ someField: ... }`.
|
|
19
|
+
*
|
|
20
|
+
* - If K extends keyof Document, then primitive values must match Document[K].
|
|
21
|
+
* - If K === "KEY", then primitive values are treated as strings/Array<string>.
|
|
22
|
+
*/
|
|
23
|
+
export type Where<Document extends Record<string, unknown>> = {
|
|
24
|
+
[K in keyof Document | 'KEY']?: Condition<Document, K extends keyof Document ? K : keyof Document> | (K extends keyof Document ? Document[K] : string) | (K extends keyof Document ? Array<Document[K]> : string[]);
|
|
25
|
+
} | {
|
|
26
|
+
readonly AND?: Array<Where<Document>>;
|
|
27
|
+
readonly OR?: Array<Where<Document>>;
|
|
28
|
+
readonly NOT?: Where<Document>;
|
|
29
|
+
};
|
|
30
|
+
export declare function getWhere<Document extends Record<string, unknown>>(where: Where<Document>, tableAlias?: string): string;
|
|
31
|
+
export declare function getWhereQuery<Document extends Record<string, unknown>>(where: Where<Document>): string;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type DependencyList } from 'react';
|
|
2
|
+
import type { SyncTable } from './create-sqlite-state';
|
|
3
|
+
import type { DocType } from './table/table.types';
|
|
4
|
+
import type { Where } from './table/where';
|
|
5
|
+
export interface SqLiteActions {
|
|
6
|
+
readonly next: () => Promise<boolean>;
|
|
7
|
+
readonly reset: () => Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export interface UseSearchOptions<Document extends DocType, Selected = Document> {
|
|
10
|
+
readonly sorBy?: keyof Document;
|
|
11
|
+
readonly order?: 'asc' | 'desc';
|
|
12
|
+
readonly limit?: number;
|
|
13
|
+
readonly offset?: number;
|
|
14
|
+
readonly where?: Where<Document>;
|
|
15
|
+
readonly stepSize?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Naive projection. Prefer specialized queries for heavy fan-out graphs.
|
|
18
|
+
*/
|
|
19
|
+
readonly select?: (document: Document) => Selected;
|
|
20
|
+
}
|
|
21
|
+
export declare function useSqliteValue<Document extends DocType, Selected = Document>(state: SyncTable<Document>, options?: UseSearchOptions<Document, Selected>, deps?: DependencyList): [undefined extends Selected ? Document[] : Selected[], SqLiteActions];
|
package/types/types.d.ts
CHANGED
package/types/utils/common.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Cache, IsEqual } from '../types';
|
|
1
|
+
import type { Cache, IsEqual, State } from '../types';
|
|
2
2
|
export interface CancelablePromise<T> {
|
|
3
3
|
promise: Promise<T>;
|
|
4
4
|
controller?: AbortController;
|
|
@@ -17,4 +17,4 @@ export declare function canUpdate<T>(cache: Cache<T>, isEqual?: IsEqual<T>): boo
|
|
|
17
17
|
/**
|
|
18
18
|
* Handle async updates for `create` and `select`
|
|
19
19
|
*/
|
|
20
|
-
export declare function handleAsyncUpdate<T>(
|
|
20
|
+
export declare function handleAsyncUpdate<T>(state: State<T>, value: T): T;
|