Skip to content

Commit

Permalink
Add PouchDB base service (#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
midsorbet authored and paulbert committed Jun 28, 2018
1 parent 79e49e1 commit e5ad8b8
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docker/planet/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ COPY package.json ./
COPY docker/planet/scripts/ ./
COPY . .

RUN apk add --update \
python \
build-base
RUN npm i --silent
RUN ./compile_planet.sh

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"material-icons": "^0.1.0",
"mime": "^2.3.1",
"ngx-img": "^10.15.0",
"pouchdb": "^7.0.0",
"pouchdb-find": "^7.0.0",
"rxjs": "^6.2.0",
"zone.js": "~0.8.26"
},
Expand All @@ -53,6 +55,8 @@
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"@types/pouchdb": "^6.3.2",
"@types/pouchdb-find": "^6.3.3",
"codelyzer": "~3.2.0",
"htmlhint-ng2": "0.0.13",
"jasmine-core": "~2.6.2",
Expand Down
2 changes: 2 additions & 0 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { FeedbackService } from './feedback/feedback.service';
import { ResourcesService } from './resources/resources.service';
import { SubmissionsService } from './submissions/submissions.service';
import { CoursesService } from './courses/courses.service';
import { SHARED_SERVICES } from './shared/database';
import { SyncService } from './shared/sync.service';
import { PlanetDialogsModule } from './shared/dialogs/planet-dialogs.module';
import { PlanetLanguageModule } from './shared/planet-language.module';
Expand Down Expand Up @@ -54,6 +55,7 @@ import { PlanetLanguageModule } from './shared/planet-language.module';
ResourcesService,
SubmissionsService,
CoursesService,
...SHARED_SERVICES,
SyncService
],
bootstrap: [ AppComponent ]
Expand Down
4 changes: 4 additions & 0 deletions src/app/shared/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PouchService } from './pouch.service';

export { PouchService } from './pouch.service';
export const SHARED_SERVICES = [ PouchService ];
68 changes: 68 additions & 0 deletions src/app/shared/database/pouch.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Injectable } from '@angular/core';
import PouchDB from 'pouchdb';
import PouchDBFind from 'pouchdb-find';
import { throwError, from } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { environment } from '../../../environments/environment';

PouchDB.plugin(PouchDBFind);

@Injectable()
export class PouchService {
private baseUrl = environment.couchAddress;
private localDBs;
private databases = [];

constructor() {
this.databases.forEach(db => {
const pouchDB = new PouchDB(`local-${db}`);

// indexes the field for faster lookup
pouchDB.createIndex({
index: {
fields: [ 'kind', 'createdAt' ]
}
});
this.localDBs[db] = pouchDB;
});
}

// @TODO: handle edge cases like offline, duplicate, duplications
// handle repliction errors or make use of navigator online?
replicateFromRemoteDBs() {
this.databases.forEach(db => {
this.localDBs[db].replicate.from(this.baseUrl + db);
});
}

replicateToRemoteDBs() {
this.databases.forEach(db => {
this.localDBs[db].replicate.to(this.baseUrl + db, {
filter(doc) {
return doc.pouchIndex === db;
}
});
});
}

replicateFromRemoteDB(db) {
return this.replicate(this.localDBs[db].replicate.from(this.baseUrl + db));
}

replicateToRemoteDB(db) {
return this.replicate(this.localDBs[db].replicate.to(this.baseUrl + db));
}

replicate(replicateFn) {
return from(replicateFn).pipe(catchError(this.handleError));
}

getLocalPouchDB(db) {
return this.localDBs[db];
}

private handleError(err) {
console.error('An error occurred in PouchDB', err);
return throwError(err.message || err);
}
}
4 changes: 4 additions & 0 deletions src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ import 'zone.js/dist/zone'; // Included with Angular CLI.
* Need to import at least one locale-data with intl.
*/
// import 'intl/locale-data/jsonp/en';

(window as any).global = window;
(window as any).process = {};
(window as any).process.nextTick = setTimeout;
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
Expand Down

0 comments on commit e5ad8b8

Please sign in to comment.