@aiao/rxdb-adapter-sqlite 0.0.1
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/README.md +1 -0
- package/RxDBAdapterSqlite.d.ts +66 -0
- package/SqliteClient.d.ts +19 -0
- package/SqliteRepository.d.ts +23 -0
- package/SqliteRepositoryBase.d.ts +85 -0
- package/SqliteTreeRepository.d.ts +17 -0
- package/execute_statement_helper.d.ts +7 -0
- package/generate_entity_delete_sql.d.ts +9 -0
- package/generate_entity_find_tree_sql.d.ts +41 -0
- package/generate_entity_insert_sql.d.ts +9 -0
- package/generate_entity_soft_delete_sql.d.ts +9 -0
- package/generate_entity_update_sql.d.ts +9 -0
- package/generate_query_find_sql.d.ts +30 -0
- package/generate_table_create_sql.d.ts +8 -0
- package/generate_table_trigger_sql.d.ts +8 -0
- package/index.d.ts +5 -0
- package/index.js +1136 -0
- package/package.json +20 -0
- package/sqlite-load.utils.d.ts +39 -0
- package/sqlite.interface.d.ts +57 -0
- package/sqlite.utils.d.ts +107 -0
- package/types.d.ts +7 -0
- package/wa-sqlite.d.ts +611 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# rxdb-adapter-sqlite
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { EntityType, IRepository, IRxDBAdapter, RxDB, RxDBAdapterLocalBase, TransactionFun } from '@aiao/rxdb';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { ADAPTER_NAME, SqliteOptions, SqliteResult } from './sqlite.interface';
|
|
4
|
+
import { SQLiteCompatibleType } from './wa-sqlite';
|
|
5
|
+
/**
|
|
6
|
+
* SQLite Adapter
|
|
7
|
+
*/
|
|
8
|
+
export declare class RxDBAdapterSqlite extends RxDBAdapterLocalBase implements IRxDBAdapter {
|
|
9
|
+
#private;
|
|
10
|
+
readonly options: SqliteOptions;
|
|
11
|
+
name: string;
|
|
12
|
+
/**
|
|
13
|
+
* 数据库版本
|
|
14
|
+
*/
|
|
15
|
+
version$: Observable<string>;
|
|
16
|
+
constructor(rxdb: RxDB, options: SqliteOptions);
|
|
17
|
+
/**
|
|
18
|
+
* 连接 wab sqlite
|
|
19
|
+
*/
|
|
20
|
+
connect(): Observable<this>;
|
|
21
|
+
/**
|
|
22
|
+
* 断开连接
|
|
23
|
+
*/
|
|
24
|
+
disconnect(): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* 获取版本
|
|
27
|
+
*/
|
|
28
|
+
version(): Promise<string>;
|
|
29
|
+
/**
|
|
30
|
+
* 获取实体仓库
|
|
31
|
+
* @param entity 实体类
|
|
32
|
+
*/
|
|
33
|
+
getRepository<T extends EntityType, RT extends IRepository<T>>(entity: T): RT;
|
|
34
|
+
/**
|
|
35
|
+
* 判断表是否存在
|
|
36
|
+
* @param EntityType
|
|
37
|
+
*/
|
|
38
|
+
isTableExisted<T extends EntityType>(EntityType: T): Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* 判断表是否存在
|
|
41
|
+
* @param EntityType
|
|
42
|
+
*/
|
|
43
|
+
isTableExisted$<T extends EntityType>(EntityType: T): Observable<boolean>;
|
|
44
|
+
/**
|
|
45
|
+
* 创建 table
|
|
46
|
+
* @param EntityType
|
|
47
|
+
*/
|
|
48
|
+
createTable<T extends EntityType>(EntityType: T): Promise<boolean>;
|
|
49
|
+
/**
|
|
50
|
+
* 执行 sql
|
|
51
|
+
* @param sql
|
|
52
|
+
* @param bindings
|
|
53
|
+
*/
|
|
54
|
+
execute(sql: string, bindings?: SQLiteCompatibleType[]): Observable<SqliteResult>;
|
|
55
|
+
/**
|
|
56
|
+
* 处理事务
|
|
57
|
+
* @param callback
|
|
58
|
+
*/
|
|
59
|
+
transaction<T extends TransactionFun>(callback: T): ReturnType<T>;
|
|
60
|
+
transaction$<T extends TransactionFun>(callback: T): Observable<Awaited<ReturnType<T>>>;
|
|
61
|
+
}
|
|
62
|
+
declare module '@aiao/rxdb' {
|
|
63
|
+
interface RxDBAdapters {
|
|
64
|
+
[ADAPTER_NAME]: RxDBAdapterSqlite;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { LoadModuleOptions, SqliteResult } from './sqlite.interface';
|
|
2
|
+
import { SQLiteCompatibleType } from './wa-sqlite';
|
|
3
|
+
export interface ISqliteClient {
|
|
4
|
+
init(dbName: string, options: LoadModuleOptions): Promise<void>;
|
|
5
|
+
execute(sql: string, bindings?: SQLiteCompatibleType[]): Promise<SqliteResult>;
|
|
6
|
+
disconnect(): Promise<void>;
|
|
7
|
+
version(): Promise<string>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* sqlite 客户端
|
|
11
|
+
*/
|
|
12
|
+
export declare class SqliteClient implements ISqliteClient {
|
|
13
|
+
#private;
|
|
14
|
+
protected changeChanel?: BroadcastChannel | null;
|
|
15
|
+
init(dbName: string, options: LoadModuleOptions): Promise<void>;
|
|
16
|
+
version(): Promise<string>;
|
|
17
|
+
disconnect(): Promise<void>;
|
|
18
|
+
execute(sql: string, bindings?: SQLiteCompatibleType[]): Promise<SqliteResult>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { CountOptions, EntityType, FindAllOptions, FindByCursorOptions, FindOneOptions, FindOneOrFailOptions, FindOptions, GetOptions, IRepository, MutationType, RuleGroup, UUID } from '@aiao/rxdb';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { SqliteRepositoryBase } from './SqliteRepositoryBase';
|
|
4
|
+
/**
|
|
5
|
+
* 操作 entity 仓库
|
|
6
|
+
*/
|
|
7
|
+
export declare class SqliteRepository<T extends EntityType> extends SqliteRepositoryBase<T> implements IRepository<T> {
|
|
8
|
+
#private;
|
|
9
|
+
get(id: UUID, options?: GetOptions): Observable<InstanceType<T>>;
|
|
10
|
+
findOne(where: RuleGroup<InstanceType<T>>, options?: FindOneOptions): Observable<InstanceType<T> | undefined>;
|
|
11
|
+
findOneOrFail(where: RuleGroup<InstanceType<T>>, options?: FindOneOrFailOptions): Observable<InstanceType<T>>;
|
|
12
|
+
find(where: RuleGroup<InstanceType<T>>, options?: FindOptions): Observable<InstanceType<T>[]>;
|
|
13
|
+
findAll(where: RuleGroup<InstanceType<T>>, options?: FindAllOptions): Observable<InstanceType<T>[]>;
|
|
14
|
+
findByCursor(where: RuleGroup<InstanceType<T>>, options?: FindByCursorOptions): Observable<InstanceType<T>[]>;
|
|
15
|
+
count(where: RuleGroup<InstanceType<T>>, options?: CountOptions): Observable<number>;
|
|
16
|
+
create(entity: InstanceType<T>): Promise<InstanceType<T>>;
|
|
17
|
+
update(entity: InstanceType<T>, patch: Partial<InstanceType<T>>): Promise<InstanceType<T>>;
|
|
18
|
+
remove(entity: InstanceType<T>): Promise<InstanceType<T>>;
|
|
19
|
+
/**
|
|
20
|
+
* 根据 type 计算主动更新缓存策略
|
|
21
|
+
*/
|
|
22
|
+
merge_cache(type: MutationType, entities: InstanceType<T>[]): void;
|
|
23
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { EntityMetadata, EntityType, MutationType, RepositoryBase, UUID } from '@aiao/rxdb';
|
|
2
|
+
import { Observable, Subject } from 'rxjs';
|
|
3
|
+
import { SqlResult } from './generate_query_find_sql';
|
|
4
|
+
import { RxDBAdapterSqlite } from './RxDBAdapterSqlite';
|
|
5
|
+
import { SqliteSuccessResult } from './sqlite.interface';
|
|
6
|
+
import { ISqliteClient } from './SqliteClient';
|
|
7
|
+
/**
|
|
8
|
+
* 查询任务
|
|
9
|
+
*/
|
|
10
|
+
export interface QueryTask {
|
|
11
|
+
/**
|
|
12
|
+
* 查询语句
|
|
13
|
+
*/
|
|
14
|
+
sql: string;
|
|
15
|
+
/**
|
|
16
|
+
* 查询参数
|
|
17
|
+
*/
|
|
18
|
+
params: any[];
|
|
19
|
+
/**
|
|
20
|
+
* 观察者数量
|
|
21
|
+
*/
|
|
22
|
+
observerCount: number;
|
|
23
|
+
/**
|
|
24
|
+
* 用于强制刷新查询语句
|
|
25
|
+
*/
|
|
26
|
+
refresh$: Observable<number>;
|
|
27
|
+
/**
|
|
28
|
+
* 查询结果
|
|
29
|
+
*/
|
|
30
|
+
result$: Observable<SqliteSuccessResult>;
|
|
31
|
+
/**
|
|
32
|
+
* 查询结果的实体 id
|
|
33
|
+
*/
|
|
34
|
+
resultEntityIds?: UUID[];
|
|
35
|
+
resultEntities?: InstanceType<any>[];
|
|
36
|
+
/**
|
|
37
|
+
* 查询被销毁
|
|
38
|
+
*/
|
|
39
|
+
destroy$: Observable<void>;
|
|
40
|
+
/**
|
|
41
|
+
* 强制刷新
|
|
42
|
+
*/
|
|
43
|
+
refresh: () => void;
|
|
44
|
+
/**
|
|
45
|
+
* 更新数据
|
|
46
|
+
*/
|
|
47
|
+
next: (value: SqliteSuccessResult) => void;
|
|
48
|
+
/**
|
|
49
|
+
* 抛出错误
|
|
50
|
+
*/
|
|
51
|
+
error: (value: Error) => void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 操作 entity 仓库
|
|
55
|
+
*/
|
|
56
|
+
export declare class SqliteRepositoryBase<T extends EntityType> extends RepositoryBase<T> {
|
|
57
|
+
#private;
|
|
58
|
+
protected adapter: RxDBAdapterSqlite;
|
|
59
|
+
protected client$: Observable<ISqliteClient>;
|
|
60
|
+
protected metadata: EntityMetadata;
|
|
61
|
+
protected need_run_task: Subject<void>;
|
|
62
|
+
protected run_task$: Observable<void>;
|
|
63
|
+
protected cache_query_task_map: Map<string, QueryTask>;
|
|
64
|
+
protected cache_query_tasks: QueryTask[];
|
|
65
|
+
protected transaction_entities: {
|
|
66
|
+
type: MutationType;
|
|
67
|
+
entities: InstanceType<T>[];
|
|
68
|
+
}[];
|
|
69
|
+
protected transaction_pending: boolean;
|
|
70
|
+
constructor(adapter: RxDBAdapterSqlite, client$: Observable<ISqliteClient>, EntityType: T);
|
|
71
|
+
/**
|
|
72
|
+
* 根据 type 计算主动更新缓存策略
|
|
73
|
+
*/
|
|
74
|
+
protected merge_cache(type: MutationType, entities: InstanceType<T>[]): void;
|
|
75
|
+
/**
|
|
76
|
+
* 添加缓存
|
|
77
|
+
* @param sqliteSuccessResult
|
|
78
|
+
* @param forcedUpdate 强制刷新,在数据有的情况下也会更新数据,在修改数据的情况下需要
|
|
79
|
+
*/
|
|
80
|
+
protected add_query_cache(sqliteSuccessResult: SqliteSuccessResult, forcedUpdate?: boolean): InstanceType<T>[];
|
|
81
|
+
/**
|
|
82
|
+
* 创建查询任务
|
|
83
|
+
*/
|
|
84
|
+
protected create_query_task(sqlResult: SqlResult): QueryTask;
|
|
85
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { EntityType, FindTreeOptions, ITreeRepository, MutationType, RuleGroup } from '@aiao/rxdb';
|
|
2
|
+
import { Observable } from 'rxjs';
|
|
3
|
+
import { SqliteRepository } from './SqliteRepository';
|
|
4
|
+
/**
|
|
5
|
+
* tree entity 仓库
|
|
6
|
+
*/
|
|
7
|
+
export declare class SqliteTreeRepository<T extends EntityType> extends SqliteRepository<T> implements ITreeRepository<T> {
|
|
8
|
+
#private;
|
|
9
|
+
findDescendants(entity: InstanceType<T>, where: RuleGroup<T>, options?: FindTreeOptions): Observable<InstanceType<T>[]>;
|
|
10
|
+
countDescendants(entity: InstanceType<T>, where: RuleGroup<T>, options?: FindTreeOptions): Observable<number>;
|
|
11
|
+
findAncestors(entity: InstanceType<T>, where: RuleGroup<T>, options?: FindTreeOptions): Observable<InstanceType<T>[]>;
|
|
12
|
+
countAncestors(entity: InstanceType<T>, where: RuleGroup<T>, options?: FindTreeOptions): Observable<number>;
|
|
13
|
+
/**
|
|
14
|
+
* 根据 type 计算主动更新缓存策略
|
|
15
|
+
*/
|
|
16
|
+
merge_cache(type: MutationType, entities: InstanceType<T>[]): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { SQLiteAPI, SQLiteCompatibleType } from './wa-sqlite';
|
|
2
|
+
export type TransactionCallback = (sqlite: SQLiteAPI, db: number) => Promise<any>;
|
|
3
|
+
/**
|
|
4
|
+
* 执行单行 sql
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: (sqlite3: SQLiteAPI, db: number, sql: string, bindings?: SQLiteCompatibleType[]) => Promise<import('./sqlite.interface').SqliteSuccessResult>;
|
|
7
|
+
export default _default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EntityMetadata, IEntity, IQueryContext } from '@aiao/rxdb';
|
|
2
|
+
/**
|
|
3
|
+
* Entity remove SQL
|
|
4
|
+
*/
|
|
5
|
+
declare const _default: (metadata: EntityMetadata, entity: IEntity, context?: IQueryContext) => {
|
|
6
|
+
sql: string;
|
|
7
|
+
params: `${string}-${string}-${string}-${string}-${string}`[];
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { EntityMetadata, FindTreeOptions, RuleGroup, UUID } from '@aiao/rxdb';
|
|
2
|
+
interface TreeOptions extends FindTreeOptions {
|
|
3
|
+
count?: boolean;
|
|
4
|
+
descendants: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* 生成树查询
|
|
8
|
+
*/
|
|
9
|
+
export declare const generate_tree_base: (metadata: EntityMetadata, entityId: UUID, ruleGroup: RuleGroup<any>, options: TreeOptions) => {
|
|
10
|
+
sql: string;
|
|
11
|
+
params: `${string}-${string}-${string}-${string}-${string}`[];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* 查询子孙节点
|
|
15
|
+
*/
|
|
16
|
+
export declare const generate_entity_find_descendants_sql: (metadata: EntityMetadata, entityId: UUID, where: RuleGroup<any>, options?: FindTreeOptions) => {
|
|
17
|
+
sql: string;
|
|
18
|
+
params: `${string}-${string}-${string}-${string}-${string}`[];
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* 查询祖先节点
|
|
22
|
+
*/
|
|
23
|
+
export declare const generate_entity_find_ancestors_sql: (metadata: EntityMetadata, entityId: UUID, where: RuleGroup<any>, options?: FindTreeOptions) => {
|
|
24
|
+
sql: string;
|
|
25
|
+
params: `${string}-${string}-${string}-${string}-${string}`[];
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 查询子孙节点数量
|
|
29
|
+
*/
|
|
30
|
+
export declare const generate_entity_count_descendants_sql: (metadata: EntityMetadata, entityId: UUID, where: RuleGroup<any>, options?: FindTreeOptions) => {
|
|
31
|
+
sql: string;
|
|
32
|
+
params: `${string}-${string}-${string}-${string}-${string}`[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* 查询祖先节点数量
|
|
36
|
+
*/
|
|
37
|
+
export declare const generate_entity_count_ancestors_sql: (metadata: EntityMetadata, entityId: UUID, where: RuleGroup<any>, options?: FindTreeOptions) => {
|
|
38
|
+
sql: string;
|
|
39
|
+
params: `${string}-${string}-${string}-${string}-${string}`[];
|
|
40
|
+
};
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EntityMetadata, IEntity, IQueryContext } from '@aiao/rxdb';
|
|
2
|
+
/**
|
|
3
|
+
* Entity Create SQL
|
|
4
|
+
*/
|
|
5
|
+
declare const _default: (metadata: EntityMetadata, entity: IEntity, context?: IQueryContext) => {
|
|
6
|
+
sql: string;
|
|
7
|
+
params: unknown[];
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EntityMetadata, IEntity, IQueryContext } from '@aiao/rxdb';
|
|
2
|
+
/**
|
|
3
|
+
* 计算更新 entity 的 SQL
|
|
4
|
+
*/
|
|
5
|
+
declare const _default: (metadata: EntityMetadata, entity: IEntity, patch: Partial<IEntity>, context?: IQueryContext) => {
|
|
6
|
+
sql: string;
|
|
7
|
+
params: unknown[];
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { CountOptions, EntityMetadata, FindAllOptions, FindOneOrFailOptions, FindOptions, RuleGroup } from '@aiao/rxdb';
|
|
2
|
+
import { RxDBAdapterSqlite } from './RxDBAdapterSqlite';
|
|
3
|
+
import { SQLiteCompatibleType } from './wa-sqlite';
|
|
4
|
+
export interface SqlResult {
|
|
5
|
+
sql: string;
|
|
6
|
+
params: SQLiteCompatibleType[];
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 生成 ruleGroup sql 查询条件
|
|
10
|
+
* @param ruleGroup
|
|
11
|
+
* @param fieldAliasMap
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export declare const buildRuleGroup: <RG extends RuleGroup<any, any, any>>(ruleGroup: RG, fieldAliasMap?: Map<string, string>) => string;
|
|
15
|
+
/**
|
|
16
|
+
* 生成 find 查询
|
|
17
|
+
*/
|
|
18
|
+
export declare const generate_query_find_sql: (adapter: RxDBAdapterSqlite, metadata: EntityMetadata, ruleGroup: RuleGroup<any>, options?: FindOptions) => SqlResult;
|
|
19
|
+
/**
|
|
20
|
+
* 生成 findAll 查询
|
|
21
|
+
*/
|
|
22
|
+
export declare const generate_query_find_all_sql: (adapter: RxDBAdapterSqlite, metadata: EntityMetadata, ruleGroup: RuleGroup<any>, options?: FindAllOptions) => SqlResult;
|
|
23
|
+
/**
|
|
24
|
+
* 生成 count 查询
|
|
25
|
+
*/
|
|
26
|
+
export declare const generate_query_count_sql: (adapter: RxDBAdapterSqlite, metadata: EntityMetadata, ruleGroup: RuleGroup<any>, options?: CountOptions) => SqlResult;
|
|
27
|
+
/**
|
|
28
|
+
* 生成 findOne 查询
|
|
29
|
+
*/
|
|
30
|
+
export declare const generate_query_find_one_sql: (adapter: RxDBAdapterSqlite, metadata: EntityMetadata, ruleGroup: RuleGroup<any>, options?: FindOneOrFailOptions) => SqlResult;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { RxDBAdapterSqlite } from './RxDBAdapterSqlite';
|
|
3
|
+
/**
|
|
4
|
+
* 计算创建表的 sql
|
|
5
|
+
* @param metadata 实体元数据
|
|
6
|
+
*/
|
|
7
|
+
declare const _default: (adapter: RxDBAdapterSqlite, metadata: EntityMetadata) => string;
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { EntityMetadata } from '@aiao/rxdb';
|
|
2
|
+
import { RxDBAdapterSqlite } from './RxDBAdapterSqlite';
|
|
3
|
+
/**
|
|
4
|
+
* 计算表的触发器
|
|
5
|
+
* @param metadata 实体元数据
|
|
6
|
+
*/
|
|
7
|
+
declare const _default: (adapter: RxDBAdapterSqlite, entityMetadata: EntityMetadata) => string;
|
|
8
|
+
export default _default;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { RxDBAdapterSqlite } from './RxDBAdapterSqlite';
|
|
2
|
+
export { WA_SQLITE_VFS_LIST, sqliteLoad } from './sqlite-load.utils';
|
|
3
|
+
export type { SqliteOptions } from './sqlite.interface';
|
|
4
|
+
export { RxdbAdapterSqliteError } from './sqlite.utils';
|
|
5
|
+
export { SqliteClient } from './SqliteClient';
|