Last active
July 5, 2017 22:59
-
-
Save acdcjunior/71f37445997e26528cfe2a7b79f22aae to your computer and use it in GitHub Desktop.
Banco.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Banco = (function () { | |
function Banco(nomeBanco) { | |
if (nomeBanco === void 0) { nomeBanco = 'testes'; } | |
this.checkEhString(nomeBanco); | |
this.db = new PouchDB(nomeBanco); | |
} | |
Banco.prototype.checkEhString = function (id) { | |
if (typeof id !== 'string') { | |
throw new Error('Argumento deve ser uma string e nao um objeto! Recebido: ' | |
+ JSON.stringify(id)); | |
} | |
}; | |
Banco.prototype.tratarErros = function (err) { | |
console.error('ERRO!', err); | |
}; | |
Banco.prototype.save = function (objeto, callbackSucesso, callbackErro) { | |
if (callbackSucesso === void 0) { callbackSucesso = function () { }; } | |
if (callbackErro === void 0) { callbackErro = this.tratarErros; } | |
if (objeto._id === undefined) { | |
this.db.post(objeto).then(callbackSucesso).catch(callbackErro); | |
} | |
else { | |
this.db.put(objeto).then(callbackSucesso).catch(callbackErro); | |
} | |
}; | |
Banco.prototype.findAll = function (callbackSucesso, callbackErro) { | |
if (callbackErro === void 0) { callbackErro = this.tratarErros; } | |
this.db.allDocs({ include_docs: true }) | |
.then(function (rs) { return callbackSucesso(rs.rows.map(function (row) { return row.doc; })); }) | |
.catch(callbackErro); | |
}; | |
Banco.prototype.findById = function (id, callbackSucesso, callbackErro) { | |
if (callbackErro === void 0) { callbackErro = this.tratarErros; } | |
this.checkEhString(id); | |
this.db.get(id).then(callbackSucesso).catch(callbackErro); | |
}; | |
Banco.prototype.removeById = function (id, callbackSucesso, callbackErro) { | |
var _this = this; | |
if (callbackSucesso === void 0) { callbackSucesso = function () { }; } | |
if (callbackErro === void 0) { callbackErro = this.tratarErros; } | |
this.checkEhString(id); | |
this.findById(id, function (doc) { | |
_this.db.remove(doc._id, doc._rev).then(callbackSucesso).catch(callbackErro); | |
}, callbackErro); | |
}; | |
Banco.prototype.removeAll = function (callbackSucesso, callbackErro) { | |
var _this = this; | |
if (callbackSucesso === void 0) { callbackSucesso = function () { }; } | |
if (callbackErro === void 0) { callbackErro = this.tratarErros; } | |
this.db.allDocs().then(function (result) { | |
return Promise.all(result.rows.map(function (row) { | |
return _this.db.remove(row.id, row.value.rev); | |
})); | |
}).then(callbackSucesso).catch(callbackErro); | |
}; | |
Banco.prototype.findByPropriedade = function (propriedade, valor, callbackSucesso, callbackErro) { | |
if (callbackSucesso === void 0) { callbackSucesso = function () { }; } | |
if (callbackErro === void 0) { callbackErro = this.tratarErros; } | |
this.db.query(function (doc, emit) { emit(doc[propriedade]); }, { key: valor, include_docs: true }) | |
.then(function (rs) { return callbackSucesso(rs.rows.map(function (row) { return row.doc; })); }) | |
.catch(callbackErro); | |
}; | |
return Banco; | |
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare var PouchDB; | |
class Banco { | |
private db: any; | |
constructor(nomeBanco: string = 'testes') { | |
this.checkEhString(nomeBanco); | |
this.db = new PouchDB(nomeBanco); | |
} | |
private checkEhString(id) { | |
if (typeof id !== 'string') { | |
throw new Error('Argumento deve ser uma string e nao um objeto! Recebido: ' | |
+ JSON.stringify(id)); | |
} | |
} | |
private tratarErros(err) { | |
console.error('ERRO!', err); | |
} | |
public save(objeto, callbackSucesso = () => { }, callbackErro = this.tratarErros) { | |
if (objeto._id === undefined) { | |
this.db.post(objeto).then(callbackSucesso).catch(callbackErro); | |
} else { | |
this.db.put(objeto).then(callbackSucesso).catch(callbackErro); | |
} | |
} | |
public findAll(callbackSucesso, callbackErro = this.tratarErros) { | |
this.db.allDocs({ include_docs: true }) | |
.then((rs) => callbackSucesso(rs.rows.map((row) => row.doc))) | |
.catch(callbackErro); | |
} | |
public findById(id, callbackSucesso, callbackErro = this.tratarErros) { | |
this.checkEhString(id); | |
this.db.get(id).then(callbackSucesso).catch(callbackErro); | |
} | |
public removeById(id, callbackSucesso = () => { }, callbackErro = this.tratarErros) { | |
this.checkEhString(id); | |
this.findById(id, (doc) => { | |
this.db.remove(doc._id, doc._rev).then(callbackSucesso).catch(callbackErro); | |
}, callbackErro); | |
} | |
public removeAll(callbackSucesso = () => { }, callbackErro = this.tratarErros) { | |
this.db.allDocs().then((result) => { | |
return Promise.all(result.rows.map((row) => { | |
return this.db.remove(row.id, row.value.rev); | |
})); | |
}).then(callbackSucesso).catch(callbackErro); | |
} | |
public findByPropriedade(propriedade: string, valor: any, callbackSucesso:(any)=>void = () => { }, callbackErro = this.tratarErros) { | |
this.db.query((doc, emit) => { emit(doc[propriedade]); }, { key: valor, include_docs: true }) | |
.then((rs) => callbackSucesso(rs.rows.map((row) => row.doc))) | |
.catch(callbackErro); | |
} | |
} |
Divida Tecnica: linha 28 e 50 estao duplicadas
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.typescriptlang.org/play/#src=declare%20var%20P....