Skip to content

Instantly share code, notes, and snippets.

@johnsi15
Created April 25, 2019 02:43
Show Gist options
  • Save johnsi15/7cb022710ada1ac6317e44afcf33b90d to your computer and use it in GitHub Desktop.
Save johnsi15/7cb022710ada1ac6317e44afcf33b90d to your computer and use it in GitHub Desktop.
Realizando inserciones, consultas compuestas, límites y ordenamiento
class PostDAO {
constructor () {
this.db = firebase.firestore()
const settings = { timestampsInSnapshots: true }
this.db.settings(settings)
}
add (post, id) {
this.db.collection('posts').doc(id).set({
titulo: post.titulo,
descripcion: post.descripcion,
autor: post.autor,
fecha: firebase.firestore.FieldValue.serverTimestamp()
})
console.log('Se crea post')
}
batch () {
const batch = this.db.batch()
const ref1 = this.db.collection('posts').doc('123456789')
batch.set(ref1, { titulo: '123456789' })
const ref2 = this.db.collection('posts').doc('987654321')
batch.set(ref2, { titulo: '987654321' })
const ref3 = this.db.collection('posts').doc('456789')
batch.set(ref3, { titulo: '456789' })
batch
.commit()
.then(() => {
console.log('Batch correcto')
})
.catch(error => console.error(error))
}
addWithKey (post) {
this.db
.collection('posts')
.add({
titulo: post.titulo,
descripcion: post.descripcion,
autor: post.autor,
fecha: firebase.firestore.FieldValue.serverTimestamp()
})
.then(docRef => {
console.log(`UID is => ${docRef.id}`)
})
}
addWithMerge (imagenLink, id) {
console.log(`Post => ${id}, se agrega imangenlink`)
this.db.collection('posts').doc(id).set(
{
imagenLink: imagenLink
},
{ merge: true }
)
}
update (imagenLink, id) {
let refUser = this.db.collection('posts').doc(id)
console.log(`Post => ${id}, se actualiza imangenlink`)
refUser.update({
imagenLink: imagenLink
})
}
updateObject (id) {
console.log(`Post => ${id}, se agrega post.categoria`)
let refUser = this.db.collection('posts').doc(id)
refUser.update({
'post.categoria': '1'
})
}
deleteFields (id) {
console.log(`Post => ${id}, se elimina imagenLink`)
this.db.collection('posts').doc(id).update({
imagenLink: firebase.firestore.FieldValue.delete()
})
}
delete (id) {
console.log(`Post => ${id}, se elimina`)
this.db.collection('posts').doc(id).delete()
}
querySingle (id) {
let ref = this.db.collection('posts').doc(id)
ref.get().then(respDoc => {
console.log(`querySingle postID ${id} => ${respDoc.data().titulo}`)
})
}
queryByTitulo (titulo) {
this.db
.collection('posts')
.where('titulo', '==', titulo)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(
`queryByTitulo postTitulo ${titulo}=> ${doc.data().titulo}`
)
})
})
}
allPosts () {
this.db
.collection('posts')
.orderBy('titulo', 'asc')
.limit(3)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(`allPosts con Limit 2 => ${doc.data().titulo}`)
})
})
}
queryPostsByTituloAndAutor (titulo, autor) {
this.db
.collection('posts')
.where('titulo', '==', titulo)
.where('autor', '==', autor)
.get()
.then(querySnapshot => {
querySnapshot.forEach(doc => {
console.log(
`queryPostsByTituloAndAutor Titulo:${titulo}, autor:${autor} => ${doc.data().titulo}`
)
})
})
}
}
$(() => {
const userDAO = new PostDAO()
$('#btnAdd').click(() => {
userDAO.add(
{
titulo: 'Test1',
descripcion: 'Test1 Desc',
autor: 'juan@gmail.com'
},
'14638228'
)
})
$('#btnBatch').click(() => {
userDAO.batch()
})
$('#btnAddKey').click(() => {
userDAO.addWithKey({
titulo: 'Test1',
descripcion: 'Test1 Desc',
autor: 'juan@gmail.com'
})
})
$('#btnAddWithMerge').click(() => {
userDAO.addWithMerge('ImageLink', '14638228')
})
$('#btnupdate').click(() => {
userDAO.update('ImageLinkUpdate', '14638228')
})
$('#btnUpdateObject').click(() => {
userDAO.updateObject('14638228')
})
$('#btnDeleteFields').click(() => {
userDAO.deleteFields('14638228')
})
$('#btnDelete').click(() => {
userDAO.delete('14638228')
})
$('#btnQuerySingle').click(() => {
userDAO.querySingle('14638228')
})
$('#queryByTitulo').click(() => {
userDAO.queryByTitulo('Test1')
})
$('#queryAllPosts').click(() => {
userDAO.allPosts()
})
$('#queryPostsByTituloAndAutor').click(() => {
userDAO.queryPostsByTituloAndAutor('Test1', 'juan@gmail.com')
})
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Test Firestore</title>
<script src="https://www.gstatic.com/firebasejs/5.3.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/5.0.0/firebase-firestore.js"></script>
<script>
// Initialize Firebase
var config = {
};
firebase.initializeApp(config);
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="firestore.js"></script>
<body>
<div>
<button id="btnAdd">Add</button>
<button id="btnBatch">Batch</button>
<button id="btnAddKey">AddKey</button>
<button id="btnAddWithMerge">Add Merge</button>
<button id="btnupdate">Update</button>
<button id="btnUpdateObject">Update Object</button>
<button id="btnDeleteFields">Delete Fields</button>
<button id="btnQuerySingle">Query Single</button>
<button id="queryByTitulo">Query By Titulo</button>
<button id="queryAllPosts">All Posts</button>
<button id="queryPostsByTituloAndAutor">Query Titulo Autor</button>
<button id="btnDelete">Delete</button>
</div>
</body>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment