Stefan Huber

recent changes

......@@ -6,7 +6,8 @@ export declare class Repository {
protected device: Device;
protected _db: any;
protected _params: any;
readonly db: any;
db: any;
params: any;
constructor(rest: Rest, device: Device);
findById(id: string): Promise<any>;
findByIds(ids: Array<string>): Promise<Array<any>>;
......
......@@ -5,7 +5,12 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var rest_1 = require("./rest");
var device_1 = require("./device");
var Repository = (function () {
function Repository(rest, device) {
this.rest = rest;
......@@ -15,16 +20,29 @@ var Repository = (function () {
get: function () {
return this._db;
},
set: function (db) {
this._db = db;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Repository.prototype, "params", {
get: function () {
return this._params;
},
set: function (params) {
this._params = params;
},
enumerable: true,
configurable: true
});
Repository.prototype.findById = function (id) {
return this._db.get(id);
return this.db.get(id);
};
Repository.prototype.findByIds = function (ids) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._db
_this.db
.allDocs({
include_docs: true,
keys: ids
......@@ -44,7 +62,7 @@ var Repository = (function () {
options['key'] = type;
}
return new Promise(function (resolve, reject) {
_this._db
_this.db
.query('index_type/type', options)
.then(function (res) {
resolve(_this.prepareDocs(res));
......@@ -69,7 +87,7 @@ var Repository = (function () {
}
}).then(function () {
var changes = false;
_this._db.replicate.from(_this._params.url)
_this.db.replicate.from(_this.params.url)
.once('change', function (info) { changes = true; })
.once('complete', function () { resolve(changes); })
.once('error', function (error) { reject('replication error'); });
......@@ -80,22 +98,22 @@ var Repository = (function () {
Repository.prototype.prepare = function (url) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._params = _this.parseUrl(url);
if (!_this._params) {
_this.params = _this.parseUrl(url);
if (!_this.params) {
reject();
}
if (_this._db && _this._db.name == _this._params.db_name) {
if (_this.db && _this.db.name == _this.params.db_name) {
resolve();
}
else if (!_this._db) {
_this.init(_this._params.db_name).then(function () {
else if (!_this.db) {
_this.init(_this.params.db_name).then(function () {
resolve();
});
}
else {
_this._db.destroy()
_this.db.destroy()
.then(function () {
return _this.init(_this._params.db_name);
return _this.init(_this.params.db_name);
}).then(function () {
resolve();
});
......@@ -104,9 +122,9 @@ var Repository = (function () {
};
Repository.prototype.init = function (db_name) {
if (window['PouchDB']) {
this._db = new window['PouchDB'](db_name);
this.db = new window['PouchDB'](db_name);
}
return this._db.putIfNotExists("_design/index_type", {
return this.db.putIfNotExists("_design/index_type", {
views: {
type: {
map: (function (doc) {
......@@ -146,6 +164,8 @@ var Repository = (function () {
return Repository;
}());
Repository = __decorate([
core_1.Injectable()
core_1.Injectable(),
__metadata("design:paramtypes", [rest_1.Rest,
device_1.Device])
], Repository);
exports.Repository = Repository;
......
......@@ -5,6 +5,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var http_1 = require("@angular/http");
exports.ERROR_CODE_UNAUTHORIZED = "unauthorized";
......@@ -89,7 +92,8 @@ var Rest = Rest_1 = (function () {
}());
Rest.serviceUrl = "http://someurl.com";
Rest = Rest_1 = __decorate([
core_1.Injectable()
core_1.Injectable(),
__metadata("design:paramtypes", [http_1.Http])
], Rest);
exports.Rest = Rest;
var Rest_1;
......
......@@ -15,18 +15,30 @@ export class Repository {
return this._db;
}
set db(db) {
this._db = db;
}
get params() {
return this._params;
}
set params(params) {
this._params = params;
}
constructor(
protected rest:Rest ,
protected device:Device
) {}
findById(id:string) : Promise<any> {
return this._db.get(id);
return this.db.get(id);
}
findByIds(ids:Array<string>) : Promise<Array<any>> {
return new Promise<Array<any>>((resolve, reject) => {
this._db
this.db
.allDocs({
include_docs: true ,
keys : ids
......@@ -48,7 +60,7 @@ export class Repository {
}
return new Promise<Array<any>>((resolve, reject) => {
this._db
this.db
.query('index_type/type', options)
.then((res) => {
resolve(this.prepareDocs(res));
......@@ -71,7 +83,7 @@ export class Repository {
}
}).then(() => {
let changes = false;
this._db.replicate.from(this._params.url)
this.db.replicate.from(this.params.url)
.once('change', (info) => { changes = true; })
.once('complete', () => { resolve(changes); })
.once('error', (error) => { reject('replication error'); });
......@@ -82,22 +94,22 @@ export class Repository {
prepare(url:string) : Promise<any> {
return new Promise<any>((resolve,reject) => {
this._params = this.parseUrl(url);
this.params = this.parseUrl(url);
if (!this._params) {
if (!this.params) {
reject();
}
if (this._db && this._db.name == this._params.db_name) {
if (this.db && this.db.name == this.params.db_name) {
resolve();
} else if(!this._db) {
this.init(this._params.db_name).then(() => {
} else if(!this.db) {
this.init(this.params.db_name).then(() => {
resolve();
});
} else {
this._db.destroy()
this.db.destroy()
.then(() => {
return this.init(this._params.db_name);
return this.init(this.params.db_name);
}).then(() => {
resolve();
});
......@@ -108,9 +120,9 @@ export class Repository {
init(db_name:string) : Promise<any> {
if (window['PouchDB']) {
this._db = new window['PouchDB'](db_name);
this.db = new window['PouchDB'](db_name);
}
return this._db.putIfNotExists("_design/index_type", {
return this.db.putIfNotExists("_design/index_type", {
views : {
type : {
map : (function(doc) {
......
......@@ -3,6 +3,7 @@
"allowSyntheticDefaultImports": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": ["dom", "es2015"],
"noImplicitAny": false,
"outDir": "./dist/",
......