Async remote database link

This commit is contained in:
2019-11-28 19:55:59 +01:00
parent 78b810b445
commit cb85217751
5 changed files with 57 additions and 19 deletions

View File

@@ -42,7 +42,6 @@
@click="hideMenu"
>
<div class="navbar-end">
<install-app />
<router-link
class="navbar-item"
:to="{ name: 'about' }"

View File

@@ -9,14 +9,11 @@ import './registerServiceWorker'
import filters from './utils/filters'
import './utils/icons'
import messages from './messages'
import '@pwabuilder/pwainstall'
Vue.use(VueI18n)
Vue.config.productionTip = false
Vue.config.ignoredElements = ['pwa-install']
for (const filter of Object.keys(filters)) {
Vue.filter(filter, filters[filter])
}

View File

@@ -1,10 +0,0 @@
import couchService from '@/services/CouchService'
class AdminService {
public async getAccountList(): Promise<any[]> {
const result = await couchService.remote.allDocs()
return result.rows
}
}
export default new AdminService()

View File

@@ -28,11 +28,18 @@ const remoteConfig = {
const LOCALE_DB: string = 'LOCALE_DB'
const REMOTE: string = 'https://juliencalixte.ddns.net/database'
const initRemote = () => {
return new Promise<PouchDB.Database>((resolve, reject) => {
try {
resolve(new PouchDb(`${REMOTE}/vaquant`, remoteConfig))
} catch (error) {
reject(error)
}
})
}
class CouchService {
public remote: PouchDB.Database = new PouchDb(
`${REMOTE}/vaquant`,
remoteConfig
)
public remote: PouchDB.Database | null = null
public db: PouchDB.Database | null = new PouchDb(LOCALE_DB)
public remoteUserDb: PouchDB.Database | null = null
public userDb: PouchDB.Database | null = null
@@ -53,6 +60,13 @@ class CouchService {
this.cancelSync()
})
}
this.initRemote()
}
public async initRemote() {
if (!this.remote) {
this.remote = await initRemote()
}
}
public async initLive(): Promise<boolean> {
@@ -68,11 +82,19 @@ class CouchService {
})
.on('change', (result: PouchDB.Replication.SyncResult<any>) => {
if (result.direction === 'pull') {
emit(SYNC, result.change.docs.map((doc: any) => doc._id))
emit(
SYNC,
result.change.docs.map((doc: any) => doc._id)
)
}
})
}
if (!this.remote) {
await this.initRemote()
return false
}
this.sync = this.db
.sync(this.remote, {
live: true,

View File

@@ -4,6 +4,12 @@ import IResponse from '@/models/IResponse'
class UserService {
public async signup(user: IUser, password: string): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try {
const response = await couchService.remote.signUp(user.userId, password, {
metadata: user
@@ -24,6 +30,12 @@ class UserService {
}
public async login(userId: string, password: string): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try {
const response = await couchService.remote.logIn(userId, password)
return {
@@ -45,6 +57,12 @@ class UserService {
}
public async logout(): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try {
const { ok } = await couchService.remote.logOut()
return {
@@ -59,6 +77,12 @@ class UserService {
}
public async deleteUser(userId: string): Promise<IResponse> {
if (!couchService.remote) {
return {
ok: false,
message: 'no remote'
}
}
try {
const { ok } = await couchService.remote.deleteUser(userId)
return {
@@ -72,6 +96,9 @@ class UserService {
}
public async getUser(current: IUser | null): Promise<IUser | null> {
if (!couchService.remote) {
return null
}
try {
const session = await couchService.remote.getSession()
if (session.ok && session.userCtx && session.userCtx.name) {
@@ -91,6 +118,9 @@ class UserService {
}
public async updateUser(user: IUser): Promise<boolean> {
if (!couchService.remote) {
return false
}
try {
const result = await couchService.remote.putUser(user.userId, {
metadata: user