Skip to content

Instantly share code, notes, and snippets.

@acdcjunior
Last active July 5, 2017 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acdcjunior/71f37445997e26528cfe2a7b79f22aae to your computer and use it in GitHub Desktop.
Save acdcjunior/71f37445997e26528cfe2a7b79f22aae to your computer and use it in GitHub Desktop.
Banco.js
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;
}());
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);
}
}
@acdcjunior
Copy link
Author

acdcjunior commented Jun 22, 2017

@acdcjunior
Copy link
Author

@acdcjunior
Copy link
Author

Divida Tecnica: linha 28 e 50 estao duplicadas

@acdcjunior
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment