vue3-pouch 0.0.7 → 0.0.9
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/dist/index.d.ts +4 -2
- package/dist/index.js +17 -14
- package/package.json +1 -1
- package/src/composable/usePouchRef.ts +28 -34
- package/src/types/index.d.ts +4 -0
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
/// <reference types="pouchdb-replication" />
|
|
7
7
|
/// <reference types="pouchdb-find" />
|
|
8
8
|
import * as vue0 from "vue";
|
|
9
|
+
import { Ref } from "vue";
|
|
9
10
|
import * as _vue_shared0 from "@vue/shared";
|
|
10
11
|
|
|
11
12
|
//#region src/types/index.d.ts
|
|
@@ -16,10 +17,11 @@ type PouchDatabase<T extends {}> = PouchDB.Database<T>;
|
|
|
16
17
|
|
|
17
18
|
interface PouchFindParams<T extends {}> extends PouchDB.Find.FindRequest<T> {}
|
|
18
19
|
type PouchExistingDocument<T extends {}> = PouchDB.Core.ExistingDocument<T>;
|
|
20
|
+
type Config<T extends {}> = PouchFindParams<T> | "all" | string | Ref<PouchFindParams<T> | string>;
|
|
19
21
|
//#endregion
|
|
20
22
|
//#region src/composable/usePouchRef.d.ts
|
|
21
|
-
declare function usePouchRef<TContent extends TDatabaseType, TDatabaseType extends {} = {}, TIsSingle extends boolean = false, TDatabase extends PouchDatabase<TDatabaseType> = PouchDatabase<TDatabaseType>>(config:
|
|
22
|
-
content: vue0.DeepReadonly<vue0.UnwrapNestedRefs<[(TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]) | null] extends [vue0.Ref<any, any>] ? _vue_shared0.IfAny<vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]), vue0.Ref<vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]), vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[])>, vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[])> : vue0.Ref<vue0.UnwrapRef<TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]> | null, (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]) | vue0.UnwrapRef<TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]> | null>>>;
|
|
23
|
+
declare function usePouchRef<TContent extends TDatabaseType, TDatabaseType extends {} = {}, TIsSingle extends boolean = false, TDatabase extends PouchDatabase<TDatabaseType> = PouchDatabase<TDatabaseType>>(config: Config<TContent>, db: TDatabase): {
|
|
24
|
+
content: vue0.DeepReadonly<vue0.UnwrapNestedRefs<[(TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]) | null | undefined] extends [vue0.Ref<any, any>] ? _vue_shared0.IfAny<vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]), vue0.Ref<vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]), vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[])>, vue0.Ref<any, any> & (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[])> : vue0.Ref<vue0.UnwrapRef<TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]> | null | undefined, (TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]) | vue0.UnwrapRef<TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]> | null | undefined>>>;
|
|
23
25
|
};
|
|
24
26
|
//#endregion
|
|
25
27
|
export { usePouchRef };
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
|
-
import { onMounted, onUnmounted, readonly, ref } from "vue";
|
|
1
|
+
import { isRef, onMounted, onUnmounted, readonly, ref } from "vue";
|
|
2
2
|
|
|
3
3
|
//#region src/composable/usePouchRef.ts
|
|
4
4
|
function usePouchRef(config, db) {
|
|
5
|
-
const contentWrite = ref(
|
|
5
|
+
const contentWrite = ref(void 0);
|
|
6
6
|
const content = readonly(contentWrite);
|
|
7
7
|
let observer;
|
|
8
|
+
async function updateRef() {
|
|
9
|
+
try {
|
|
10
|
+
const goodConfig = isRef(config) ? config.value : config;
|
|
11
|
+
if (goodConfig === "all") contentWrite.value = (await db.allDocs({ include_docs: true })).rows.map((d) => {
|
|
12
|
+
return d.doc;
|
|
13
|
+
}) ?? null;
|
|
14
|
+
else if (typeof goodConfig === "string") contentWrite.value = await db.get(goodConfig) ?? null;
|
|
15
|
+
else if (goodConfig.limit === 1) contentWrite.value = (await db.find(goodConfig)).docs[0] ?? null;
|
|
16
|
+
else contentWrite.value = (await db.find(goodConfig)).docs ?? null;
|
|
17
|
+
} catch (e) {
|
|
18
|
+
throw e;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
8
21
|
onMounted(async () => {
|
|
9
|
-
|
|
10
|
-
return d.doc;
|
|
11
|
-
});
|
|
12
|
-
else if (typeof config === "string") contentWrite.value = await db.get(config);
|
|
13
|
-
else if (config.limit === 1) contentWrite.value = (await db.find(config)).docs[0];
|
|
14
|
-
else contentWrite.value = (await db.find(config)).docs;
|
|
22
|
+
await updateRef();
|
|
15
23
|
observer = db.changes({
|
|
16
24
|
since: "now",
|
|
17
25
|
live: true
|
|
18
26
|
}).on("change", async () => {
|
|
19
|
-
|
|
20
|
-
return d.doc;
|
|
21
|
-
});
|
|
22
|
-
else if (typeof config === "string") contentWrite.value = await db.get(config);
|
|
23
|
-
else if (config.limit === 1) contentWrite.value = (await db.find(config)).docs[0];
|
|
24
|
-
else contentWrite.value = (await db.find(config)).docs;
|
|
27
|
+
await updateRef();
|
|
25
28
|
});
|
|
26
29
|
});
|
|
27
30
|
onUnmounted(() => {
|
package/package.json
CHANGED
|
@@ -1,58 +1,52 @@
|
|
|
1
|
-
import { ref, onMounted, onUnmounted, readonly } from 'vue'
|
|
2
|
-
import type { PouchDatabase, PouchExistingDocument, PouchFindParams, PouchObserver } from '../types';
|
|
1
|
+
import { ref, onMounted, onUnmounted, readonly, isRef } from 'vue'
|
|
2
|
+
import type { Config, PouchDatabase, PouchExistingDocument, PouchFindParams, PouchObserver } from '../types';
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
export function usePouchRef<TContent extends TDatabaseType,
|
|
6
6
|
TDatabaseType extends {} = {},
|
|
7
7
|
TIsSingle extends boolean = false,
|
|
8
8
|
TDatabase extends PouchDatabase<TDatabaseType> = PouchDatabase<TDatabaseType>,
|
|
9
|
-
>(config:
|
|
10
|
-
const contentWrite = ref<(TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]) | null>(
|
|
9
|
+
>(config: Config<TContent>, db: TDatabase) {
|
|
10
|
+
const contentWrite = ref<(TIsSingle extends true ? PouchExistingDocument<TContent> : PouchExistingDocument<TContent>[]) | null | undefined>(undefined)
|
|
11
11
|
|
|
12
12
|
const content = readonly(contentWrite)
|
|
13
13
|
|
|
14
14
|
let observer: PouchObserver | undefined;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return d.doc!
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
else if (typeof config === 'string') {
|
|
24
|
-
contentWrite.value = await db.get(config)
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
if (config.limit === 1) {
|
|
28
|
-
contentWrite.value = (await db.find(config)).docs[0]
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
contentWrite.value = (await db.find(config)).docs
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
observer = db.changes({
|
|
35
|
-
since: 'now',
|
|
36
|
-
live: true
|
|
37
|
-
}).on('change', async () => {
|
|
38
|
-
if (config === "all") {
|
|
15
|
+
|
|
16
|
+
async function updateRef() {
|
|
17
|
+
try {
|
|
18
|
+
const goodConfig = isRef(config) ? config.value : config
|
|
19
|
+
if (goodConfig === "all") {
|
|
39
20
|
contentWrite.value = (await db.allDocs({
|
|
40
21
|
include_docs: true
|
|
41
22
|
})).rows.map((d) => {
|
|
42
23
|
return d.doc!
|
|
43
|
-
})
|
|
24
|
+
}) ?? null
|
|
44
25
|
}
|
|
45
|
-
else if (typeof
|
|
46
|
-
contentWrite.value = await db.get(
|
|
26
|
+
else if (typeof goodConfig === 'string') {
|
|
27
|
+
contentWrite.value = await db.get(goodConfig) ?? null
|
|
47
28
|
}
|
|
48
29
|
else {
|
|
49
|
-
if (
|
|
50
|
-
contentWrite.value = (await db.find(
|
|
30
|
+
if (goodConfig.limit === 1) {
|
|
31
|
+
contentWrite.value = (await db.find(goodConfig)).docs[0] ?? null
|
|
51
32
|
}
|
|
52
33
|
else {
|
|
53
|
-
contentWrite.value = (await db.find(
|
|
34
|
+
contentWrite.value = (await db.find(goodConfig)).docs ?? null
|
|
54
35
|
}
|
|
55
36
|
}
|
|
37
|
+
}
|
|
38
|
+
catch (e) {
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
onMounted(async () => {
|
|
43
|
+
|
|
44
|
+
await updateRef()
|
|
45
|
+
observer = db.changes({
|
|
46
|
+
since: 'now',
|
|
47
|
+
live: true
|
|
48
|
+
}).on('change', async () => {
|
|
49
|
+
await updateRef()
|
|
56
50
|
})
|
|
57
51
|
})
|
|
58
52
|
onUnmounted(() => {
|
package/src/types/index.d.ts
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
/// <reference types="pouchdb-replication" />
|
|
7
7
|
/// <reference types="pouchdb-find" />
|
|
8
8
|
|
|
9
|
+
import { Ref } from "vue"
|
|
10
|
+
|
|
9
11
|
|
|
10
12
|
export type PouchDatabase<T extends {}> = PouchDB.Database<T>
|
|
11
13
|
|
|
@@ -24,3 +26,5 @@ export type PouchExistingDocument<T extends {}> = PouchDB.Core.ExistingDocument<
|
|
|
24
26
|
export type PouchExistingDocumentArray<T extends {}> = Array<{
|
|
25
27
|
docs: PouchDB.Core.ExistingDocument<T>
|
|
26
28
|
}>
|
|
29
|
+
|
|
30
|
+
export type Config<T extends {}> = PouchFindParams<T> | "all" | string | Ref<PouchFindParams<T> | string>
|