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" @click="hideMenu"
> >
<div class="navbar-end"> <div class="navbar-end">
<install-app />
<router-link <router-link
class="navbar-item" class="navbar-item"
:to="{ name: 'about' }" :to="{ name: 'about' }"

View File

@@ -9,14 +9,11 @@ import './registerServiceWorker'
import filters from './utils/filters' import filters from './utils/filters'
import './utils/icons' import './utils/icons'
import messages from './messages' import messages from './messages'
import '@pwabuilder/pwainstall'
Vue.use(VueI18n) Vue.use(VueI18n)
Vue.config.productionTip = false Vue.config.productionTip = false
Vue.config.ignoredElements = ['pwa-install']
for (const filter of Object.keys(filters)) { for (const filter of Object.keys(filters)) {
Vue.filter(filter, filters[filter]) 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 LOCALE_DB: string = 'LOCALE_DB'
const REMOTE: string = 'https://juliencalixte.ddns.net/database' 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 { class CouchService {
public remote: PouchDB.Database = new PouchDb( public remote: PouchDB.Database | null = null
`${REMOTE}/vaquant`,
remoteConfig
)
public db: PouchDB.Database | null = new PouchDb(LOCALE_DB) public db: PouchDB.Database | null = new PouchDb(LOCALE_DB)
public remoteUserDb: PouchDB.Database | null = null public remoteUserDb: PouchDB.Database | null = null
public userDb: PouchDB.Database | null = null public userDb: PouchDB.Database | null = null
@@ -53,6 +60,13 @@ class CouchService {
this.cancelSync() this.cancelSync()
}) })
} }
this.initRemote()
}
public async initRemote() {
if (!this.remote) {
this.remote = await initRemote()
}
} }
public async initLive(): Promise<boolean> { public async initLive(): Promise<boolean> {
@@ -68,11 +82,19 @@ class CouchService {
}) })
.on('change', (result: PouchDB.Replication.SyncResult<any>) => { .on('change', (result: PouchDB.Replication.SyncResult<any>) => {
if (result.direction === 'pull') { 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 this.sync = this.db
.sync(this.remote, { .sync(this.remote, {
live: true, live: true,

View File

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