-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoodie.service.ts
94 lines (72 loc) · 2.29 KB
/
hoodie.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Injectable } from '@angular/core';
/*
import * as Hoodie from '@hoodie/client';
import * as PouchDB from 'pouchdb-browser';
*/
@Injectable({
providedIn: 'root'
})
export class HoodieService {
hoodie: any;
constructor() {
// HACK: Get a handle to the hoodie client
const _window: any = (<any>window);
const hoodie: any = _window.hoodie;
// this.hoodie = new Hoodie({ url: location.origin, PouchDB: PouchDB.default });
this.hoodie = hoodie;
/*
const credentials = {username: 'user', password: 'passw'};
this.hoodie.account.signUp(credentials)
.finally(() => this.hoodie.account.signIn(credentials)
.then((val) => {
console.log('signed in as ', val);
this.hoodie.store.add({xx: 10, yy: new Date()});
})
.catch((error) => console.log('error: ', error)));
this.hoodie.store.on('change', () => this.hoodie.store.findAll()
.then((items) => console.log('items: ', items)));
*/
}
// account ...
accountSignUp(options: { username: String; password: String; }): Promise<any> {
return this.hoodie.account.signUp(options);
}
accountSignIn(options: { username: String; password: String; }): Promise<any> {
return this.hoodie.account.signIn(options);
}
accountSignOut(): Promise<any> {
return this.hoodie.account.signOut();
}
accountOn(eventName: String, handler) {
return this.hoodie.account.on(eventName, handler);
}
accountGet(path: String|String[], options: Object): Promise<any> {
return this.hoodie.account.get(path, options);
}
// store ...
storeOn(eventName: String, handler): any {
return this.hoodie.store.on(eventName, handler);
}
storeUpdateOrAdd(id: String, property: Object) {
return this.hoodie.store.updateOrAdd(id, property);
}
storeFindAll(): Promise<any> {
return this.hoodie.store.findAll();
}
storeFind(id: String): Promise<any> {
return this.hoodie.store.find(id);
}
storeAdd(doc: Object): Promise<any> {
return this.hoodie.store.add(doc);
}
storeUpdate(id_or_doc: String|Object, doc?: Object): any {
if (doc) {
return this.hoodie.store.update(id_or_doc, doc);
} else {
return this.hoodie.store.update(id_or_doc);
}
}
storeRemove(filter: Object): any {
return this.hoodie.store.remove(filter);
}
}