♻️ (data) try catch pouchdb init
This commit is contained in:
@@ -14,14 +14,23 @@ interface GetAllParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class Data {
|
class Data {
|
||||||
private locale = new PouchDb('lite-note', {
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
adapter: 'indexeddb'
|
private readonly locale: PouchDB.Database<{}> | null = null
|
||||||
})
|
|
||||||
|
constructor() {
|
||||||
|
try {
|
||||||
|
this.locale = new PouchDb('lite-note', {
|
||||||
|
adapter: 'indexeddb'
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('data error', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async add<DT extends DataType>(model: Model<DT>): Promise<boolean> {
|
public async add<DT extends DataType>(model: Model<DT>): Promise<boolean> {
|
||||||
try {
|
try {
|
||||||
const result = await this.locale.put(model)
|
const result = await this.locale?.put(model)
|
||||||
return result.ok
|
return result?.ok ?? false
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(error)
|
console.warn(error)
|
||||||
|
|
||||||
@@ -34,12 +43,12 @@ class Data {
|
|||||||
if (model._id) {
|
if (model._id) {
|
||||||
const oldModel = await this.get(model._id)
|
const oldModel = await this.get(model._id)
|
||||||
if (oldModel) {
|
if (oldModel) {
|
||||||
const result = await this.locale.put({ ...oldModel, ...model })
|
const result = await this.locale?.put({ ...oldModel, ...model })
|
||||||
return result.ok
|
return result?.ok ?? false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const result = await this.locale.put(model)
|
const result = await this.locale?.put(model)
|
||||||
return result.ok
|
return result?.ok ?? false
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(error)
|
console.warn(error)
|
||||||
|
|
||||||
@@ -53,11 +62,11 @@ class Data {
|
|||||||
if (!doc) {
|
if (!doc) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
const { ok } = await this.locale.put({
|
const result = await this.locale?.put({
|
||||||
...doc,
|
...doc,
|
||||||
_deleted: true
|
_deleted: true
|
||||||
})
|
})
|
||||||
return ok
|
return result?.ok ?? false
|
||||||
} catch {
|
} catch {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -67,7 +76,7 @@ class Data {
|
|||||||
id: string
|
id: string
|
||||||
): Promise<T | null> {
|
): Promise<T | null> {
|
||||||
try {
|
try {
|
||||||
return ((await this.locale.get(id)) as T) || null
|
return ((await this.locale?.get(id)) as T) || null
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
@@ -79,6 +88,10 @@ class Data {
|
|||||||
includeAttachments = false,
|
includeAttachments = false,
|
||||||
keys = []
|
keys = []
|
||||||
}: GetAllParams): Promise<T[]> {
|
}: GetAllParams): Promise<T[]> {
|
||||||
|
if (!this.locale) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
if (keys.length) {
|
if (keys.length) {
|
||||||
const response = await this.locale.allDocs({
|
const response = await this.locale.allDocs({
|
||||||
include_docs: includeDocs,
|
include_docs: includeDocs,
|
||||||
|
|||||||
Reference in New Issue
Block a user