Showing
5 changed files
with
67 additions
and
29 deletions
| ... | @@ -6,7 +6,8 @@ export declare class Repository { | ... | @@ -6,7 +6,8 @@ export declare class Repository { |
| 6 | protected device: Device; | 6 | protected device: Device; |
| 7 | protected _db: any; | 7 | protected _db: any; |
| 8 | protected _params: any; | 8 | protected _params: any; |
| 9 | - readonly db: any; | 9 | + db: any; |
| 10 | + params: any; | ||
| 10 | constructor(rest: Rest, device: Device); | 11 | constructor(rest: Rest, device: Device); |
| 11 | findById(id: string): Promise<any>; | 12 | findById(id: string): Promise<any>; |
| 12 | findByIds(ids: Array<string>): Promise<Array<any>>; | 13 | findByIds(ids: Array<string>): Promise<Array<any>>; | ... | ... |
| ... | @@ -5,7 +5,12 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, | ... | @@ -5,7 +5,12 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, |
| 5 | 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; | 5 | 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; |
| 6 | return c > 3 && r && Object.defineProperty(target, key, r), r; | 6 | return c > 3 && r && Object.defineProperty(target, key, r), r; |
| 7 | }; | 7 | }; |
| 8 | +var __metadata = (this && this.__metadata) || function (k, v) { | ||
| 9 | + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
| 10 | +}; | ||
| 8 | var core_1 = require("@angular/core"); | 11 | var core_1 = require("@angular/core"); |
| 12 | +var rest_1 = require("./rest"); | ||
| 13 | +var device_1 = require("./device"); | ||
| 9 | var Repository = (function () { | 14 | var Repository = (function () { |
| 10 | function Repository(rest, device) { | 15 | function Repository(rest, device) { |
| 11 | this.rest = rest; | 16 | this.rest = rest; |
| ... | @@ -15,16 +20,29 @@ var Repository = (function () { | ... | @@ -15,16 +20,29 @@ var Repository = (function () { |
| 15 | get: function () { | 20 | get: function () { |
| 16 | return this._db; | 21 | return this._db; |
| 17 | }, | 22 | }, |
| 23 | + set: function (db) { | ||
| 24 | + this._db = db; | ||
| 25 | + }, | ||
| 26 | + enumerable: true, | ||
| 27 | + configurable: true | ||
| 28 | + }); | ||
| 29 | + Object.defineProperty(Repository.prototype, "params", { | ||
| 30 | + get: function () { | ||
| 31 | + return this._params; | ||
| 32 | + }, | ||
| 33 | + set: function (params) { | ||
| 34 | + this._params = params; | ||
| 35 | + }, | ||
| 18 | enumerable: true, | 36 | enumerable: true, |
| 19 | configurable: true | 37 | configurable: true |
| 20 | }); | 38 | }); |
| 21 | Repository.prototype.findById = function (id) { | 39 | Repository.prototype.findById = function (id) { |
| 22 | - return this._db.get(id); | 40 | + return this.db.get(id); |
| 23 | }; | 41 | }; |
| 24 | Repository.prototype.findByIds = function (ids) { | 42 | Repository.prototype.findByIds = function (ids) { |
| 25 | var _this = this; | 43 | var _this = this; |
| 26 | return new Promise(function (resolve, reject) { | 44 | return new Promise(function (resolve, reject) { |
| 27 | - _this._db | 45 | + _this.db |
| 28 | .allDocs({ | 46 | .allDocs({ |
| 29 | include_docs: true, | 47 | include_docs: true, |
| 30 | keys: ids | 48 | keys: ids |
| ... | @@ -44,7 +62,7 @@ var Repository = (function () { | ... | @@ -44,7 +62,7 @@ var Repository = (function () { |
| 44 | options['key'] = type; | 62 | options['key'] = type; |
| 45 | } | 63 | } |
| 46 | return new Promise(function (resolve, reject) { | 64 | return new Promise(function (resolve, reject) { |
| 47 | - _this._db | 65 | + _this.db |
| 48 | .query('index_type/type', options) | 66 | .query('index_type/type', options) |
| 49 | .then(function (res) { | 67 | .then(function (res) { |
| 50 | resolve(_this.prepareDocs(res)); | 68 | resolve(_this.prepareDocs(res)); |
| ... | @@ -69,7 +87,7 @@ var Repository = (function () { | ... | @@ -69,7 +87,7 @@ var Repository = (function () { |
| 69 | } | 87 | } |
| 70 | }).then(function () { | 88 | }).then(function () { |
| 71 | var changes = false; | 89 | var changes = false; |
| 72 | - _this._db.replicate.from(_this._params.url) | 90 | + _this.db.replicate.from(_this.params.url) |
| 73 | .once('change', function (info) { changes = true; }) | 91 | .once('change', function (info) { changes = true; }) |
| 74 | .once('complete', function () { resolve(changes); }) | 92 | .once('complete', function () { resolve(changes); }) |
| 75 | .once('error', function (error) { reject('replication error'); }); | 93 | .once('error', function (error) { reject('replication error'); }); |
| ... | @@ -80,22 +98,22 @@ var Repository = (function () { | ... | @@ -80,22 +98,22 @@ var Repository = (function () { |
| 80 | Repository.prototype.prepare = function (url) { | 98 | Repository.prototype.prepare = function (url) { |
| 81 | var _this = this; | 99 | var _this = this; |
| 82 | return new Promise(function (resolve, reject) { | 100 | return new Promise(function (resolve, reject) { |
| 83 | - _this._params = _this.parseUrl(url); | 101 | + _this.params = _this.parseUrl(url); |
| 84 | - if (!_this._params) { | 102 | + if (!_this.params) { |
| 85 | reject(); | 103 | reject(); |
| 86 | } | 104 | } |
| 87 | - if (_this._db && _this._db.name == _this._params.db_name) { | 105 | + if (_this.db && _this.db.name == _this.params.db_name) { |
| 88 | resolve(); | 106 | resolve(); |
| 89 | } | 107 | } |
| 90 | - else if (!_this._db) { | 108 | + else if (!_this.db) { |
| 91 | - _this.init(_this._params.db_name).then(function () { | 109 | + _this.init(_this.params.db_name).then(function () { |
| 92 | resolve(); | 110 | resolve(); |
| 93 | }); | 111 | }); |
| 94 | } | 112 | } |
| 95 | else { | 113 | else { |
| 96 | - _this._db.destroy() | 114 | + _this.db.destroy() |
| 97 | .then(function () { | 115 | .then(function () { |
| 98 | - return _this.init(_this._params.db_name); | 116 | + return _this.init(_this.params.db_name); |
| 99 | }).then(function () { | 117 | }).then(function () { |
| 100 | resolve(); | 118 | resolve(); |
| 101 | }); | 119 | }); |
| ... | @@ -104,9 +122,9 @@ var Repository = (function () { | ... | @@ -104,9 +122,9 @@ var Repository = (function () { |
| 104 | }; | 122 | }; |
| 105 | Repository.prototype.init = function (db_name) { | 123 | Repository.prototype.init = function (db_name) { |
| 106 | if (window['PouchDB']) { | 124 | if (window['PouchDB']) { |
| 107 | - this._db = new window['PouchDB'](db_name); | 125 | + this.db = new window['PouchDB'](db_name); |
| 108 | } | 126 | } |
| 109 | - return this._db.putIfNotExists("_design/index_type", { | 127 | + return this.db.putIfNotExists("_design/index_type", { |
| 110 | views: { | 128 | views: { |
| 111 | type: { | 129 | type: { |
| 112 | map: (function (doc) { | 130 | map: (function (doc) { |
| ... | @@ -146,6 +164,8 @@ var Repository = (function () { | ... | @@ -146,6 +164,8 @@ var Repository = (function () { |
| 146 | return Repository; | 164 | return Repository; |
| 147 | }()); | 165 | }()); |
| 148 | Repository = __decorate([ | 166 | Repository = __decorate([ |
| 149 | - core_1.Injectable() | 167 | + core_1.Injectable(), |
| 168 | + __metadata("design:paramtypes", [rest_1.Rest, | ||
| 169 | + device_1.Device]) | ||
| 150 | ], Repository); | 170 | ], Repository); |
| 151 | exports.Repository = Repository; | 171 | exports.Repository = Repository; | ... | ... |
| ... | @@ -5,6 +5,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, | ... | @@ -5,6 +5,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, |
| 5 | 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; | 5 | 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; |
| 6 | return c > 3 && r && Object.defineProperty(target, key, r), r; | 6 | return c > 3 && r && Object.defineProperty(target, key, r), r; |
| 7 | }; | 7 | }; |
| 8 | +var __metadata = (this && this.__metadata) || function (k, v) { | ||
| 9 | + if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
| 10 | +}; | ||
| 8 | var core_1 = require("@angular/core"); | 11 | var core_1 = require("@angular/core"); |
| 9 | var http_1 = require("@angular/http"); | 12 | var http_1 = require("@angular/http"); |
| 10 | exports.ERROR_CODE_UNAUTHORIZED = "unauthorized"; | 13 | exports.ERROR_CODE_UNAUTHORIZED = "unauthorized"; |
| ... | @@ -89,7 +92,8 @@ var Rest = Rest_1 = (function () { | ... | @@ -89,7 +92,8 @@ var Rest = Rest_1 = (function () { |
| 89 | }()); | 92 | }()); |
| 90 | Rest.serviceUrl = "http://someurl.com"; | 93 | Rest.serviceUrl = "http://someurl.com"; |
| 91 | Rest = Rest_1 = __decorate([ | 94 | Rest = Rest_1 = __decorate([ |
| 92 | - core_1.Injectable() | 95 | + core_1.Injectable(), |
| 96 | + __metadata("design:paramtypes", [http_1.Http]) | ||
| 93 | ], Rest); | 97 | ], Rest); |
| 94 | exports.Rest = Rest; | 98 | exports.Rest = Rest; |
| 95 | var Rest_1; | 99 | var Rest_1; | ... | ... |
| ... | @@ -15,18 +15,30 @@ export class Repository { | ... | @@ -15,18 +15,30 @@ export class Repository { |
| 15 | return this._db; | 15 | return this._db; |
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | + set db(db) { | ||
| 19 | + this._db = db; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + get params() { | ||
| 23 | + return this._params; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + set params(params) { | ||
| 27 | + this._params = params; | ||
| 28 | + } | ||
| 29 | + | ||
| 18 | constructor( | 30 | constructor( |
| 19 | protected rest:Rest , | 31 | protected rest:Rest , |
| 20 | protected device:Device | 32 | protected device:Device |
| 21 | ) {} | 33 | ) {} |
| 22 | 34 | ||
| 23 | findById(id:string) : Promise<any> { | 35 | findById(id:string) : Promise<any> { |
| 24 | - return this._db.get(id); | 36 | + return this.db.get(id); |
| 25 | } | 37 | } |
| 26 | 38 | ||
| 27 | findByIds(ids:Array<string>) : Promise<Array<any>> { | 39 | findByIds(ids:Array<string>) : Promise<Array<any>> { |
| 28 | return new Promise<Array<any>>((resolve, reject) => { | 40 | return new Promise<Array<any>>((resolve, reject) => { |
| 29 | - this._db | 41 | + this.db |
| 30 | .allDocs({ | 42 | .allDocs({ |
| 31 | include_docs: true , | 43 | include_docs: true , |
| 32 | keys : ids | 44 | keys : ids |
| ... | @@ -48,7 +60,7 @@ export class Repository { | ... | @@ -48,7 +60,7 @@ export class Repository { |
| 48 | } | 60 | } |
| 49 | 61 | ||
| 50 | return new Promise<Array<any>>((resolve, reject) => { | 62 | return new Promise<Array<any>>((resolve, reject) => { |
| 51 | - this._db | 63 | + this.db |
| 52 | .query('index_type/type', options) | 64 | .query('index_type/type', options) |
| 53 | .then((res) => { | 65 | .then((res) => { |
| 54 | resolve(this.prepareDocs(res)); | 66 | resolve(this.prepareDocs(res)); |
| ... | @@ -71,7 +83,7 @@ export class Repository { | ... | @@ -71,7 +83,7 @@ export class Repository { |
| 71 | } | 83 | } |
| 72 | }).then(() => { | 84 | }).then(() => { |
| 73 | let changes = false; | 85 | let changes = false; |
| 74 | - this._db.replicate.from(this._params.url) | 86 | + this.db.replicate.from(this.params.url) |
| 75 | .once('change', (info) => { changes = true; }) | 87 | .once('change', (info) => { changes = true; }) |
| 76 | .once('complete', () => { resolve(changes); }) | 88 | .once('complete', () => { resolve(changes); }) |
| 77 | .once('error', (error) => { reject('replication error'); }); | 89 | .once('error', (error) => { reject('replication error'); }); |
| ... | @@ -82,22 +94,22 @@ export class Repository { | ... | @@ -82,22 +94,22 @@ export class Repository { |
| 82 | 94 | ||
| 83 | prepare(url:string) : Promise<any> { | 95 | prepare(url:string) : Promise<any> { |
| 84 | return new Promise<any>((resolve,reject) => { | 96 | return new Promise<any>((resolve,reject) => { |
| 85 | - this._params = this.parseUrl(url); | 97 | + this.params = this.parseUrl(url); |
| 86 | 98 | ||
| 87 | - if (!this._params) { | 99 | + if (!this.params) { |
| 88 | reject(); | 100 | reject(); |
| 89 | } | 101 | } |
| 90 | 102 | ||
| 91 | - if (this._db && this._db.name == this._params.db_name) { | 103 | + if (this.db && this.db.name == this.params.db_name) { |
| 92 | resolve(); | 104 | resolve(); |
| 93 | - } else if(!this._db) { | 105 | + } else if(!this.db) { |
| 94 | - this.init(this._params.db_name).then(() => { | 106 | + this.init(this.params.db_name).then(() => { |
| 95 | resolve(); | 107 | resolve(); |
| 96 | }); | 108 | }); |
| 97 | } else { | 109 | } else { |
| 98 | - this._db.destroy() | 110 | + this.db.destroy() |
| 99 | .then(() => { | 111 | .then(() => { |
| 100 | - return this.init(this._params.db_name); | 112 | + return this.init(this.params.db_name); |
| 101 | }).then(() => { | 113 | }).then(() => { |
| 102 | resolve(); | 114 | resolve(); |
| 103 | }); | 115 | }); |
| ... | @@ -108,9 +120,9 @@ export class Repository { | ... | @@ -108,9 +120,9 @@ export class Repository { |
| 108 | 120 | ||
| 109 | init(db_name:string) : Promise<any> { | 121 | init(db_name:string) : Promise<any> { |
| 110 | if (window['PouchDB']) { | 122 | if (window['PouchDB']) { |
| 111 | - this._db = new window['PouchDB'](db_name); | 123 | + this.db = new window['PouchDB'](db_name); |
| 112 | } | 124 | } |
| 113 | - return this._db.putIfNotExists("_design/index_type", { | 125 | + return this.db.putIfNotExists("_design/index_type", { |
| 114 | views : { | 126 | views : { |
| 115 | type : { | 127 | type : { |
| 116 | map : (function(doc) { | 128 | map : (function(doc) { | ... | ... |
| ... | @@ -3,6 +3,7 @@ | ... | @@ -3,6 +3,7 @@ |
| 3 | "allowSyntheticDefaultImports": true, | 3 | "allowSyntheticDefaultImports": true, |
| 4 | "declaration": true, | 4 | "declaration": true, |
| 5 | "experimentalDecorators": true, | 5 | "experimentalDecorators": true, |
| 6 | + "emitDecoratorMetadata": true, | ||
| 6 | "lib": ["dom", "es2015"], | 7 | "lib": ["dom", "es2015"], |
| 7 | "noImplicitAny": false, | 8 | "noImplicitAny": false, |
| 8 | "outDir": "./dist/", | 9 | "outDir": "./dist/", | ... | ... |
-
Please register or login to post a comment