Skip to content

Commit

Permalink
Merge pull request #1 from lupas/master
Browse files Browse the repository at this point in the history
updating
  • Loading branch information
Reocin authored Jan 1, 2019
2 parents 8a79fd5 + ae5a91e commit 8437d1a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 20 deletions.
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Make sure you have Firebase installed in your project and Firestore initiated.

```json
"dependencies": {
"firebase": "^5.5.9"
"firebase": "^5.7.2"
}
```

Expand All @@ -32,6 +32,8 @@ import { unwrapFirestoreDoc } from 'firewings'
import { addToFirestore } from 'firewings'
//or
import { setToFirestore } from 'firewings'
//or
import { changeDocId } from 'firewings'
```

# Included Functions
Expand Down Expand Up @@ -153,6 +155,30 @@ await ref.doc(id).set(clone)
await setToFirestore(ref.doc(id), data)
```

## changeDocId()

This function changes the id of an existing document to a new id. It does this by creating a new document wwith the new key, and then deleting the old document.

> WARNING: Only do this, if you are sure what you are doing. The old document will be deleted, so any references might be invalid. Also make sure you have no onDelete() actions that you don't want to get triggered.
**TRADITIONAL WAY**:

```js
// First get the document
const doc = await queryFirestore(docRef)
// Then save it under the new id
const newRef = docRef.parent.doc(newKey)
const newDoc = await setToFirestore(newRef, doc)
// Then delete the old document and return the new document
await docRef.delete()
```

**WITH FIREWINGS**:

```js
await changeDocId(ref, 'newId')
```

### Disclaimer

These are just some quick functions I personally used in different Firebase projects to save some repeating lines of code. Since I used that in every project, I decided to build a node module out of it to easily manage it.
48 changes: 39 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.unwrapFirestoreDoc = exports.queryFirestore = exports.addToFirestore = exports.setToFirestore = void 0;
exports.changeDocId = exports.unwrapFirestoreDoc = exports.queryFirestore = exports.addToFirestore = exports.setToFirestore = void 0;

/***************************************************/
/********************************************************************************************/

/** Takes a query and a payload ********************/
/** Takes a query and a payload *************************************************************/

/** Removes the properties id and path from the copy of a object and set() it to firebase */

/**************************************************/
/********************************************************************************************/
const setToFirestore = async function setToFirestore(ref, payload) {
let clone = Object.assign({}, payload);
if (clone.id) delete clone.id;
if (clone.path) delete clone.path;

try {
await ref.set(clone);
clone.id = ref.id;
clone.path = ref.path;
return clone;
} catch (e) {
return Promise.reject(e);
}
Expand All @@ -35,11 +38,13 @@ const setToFirestore = async function setToFirestore(ref, payload) {
exports.setToFirestore = setToFirestore;

const addToFirestore = async function addToFirestore(ref, payload) {
let clone = Object.assign({}, payload);

try {
const snapshot = await ref.add(payload);
payload.id = snapshot.id;
payload.path = snapshot.path;
return payload;
const docRef = await ref.add(clone);
clone.id = docRef.id;
clone.path = docRef.path;
return clone;
} catch (e) {
return Promise.reject(e);
}
Expand Down Expand Up @@ -107,5 +112,30 @@ const unwrapFirestoreDoc = function unwrapFirestoreDoc(snapshot) {
return item;
}
};
/***************************************************************************************/

/** Gets a document, copies it to a document with the new id and deletes the old one****/

/** WARNING: Do this at your own risk, only do this if you are sure what you are doing */

/***************************************************************************************/


exports.unwrapFirestoreDoc = unwrapFirestoreDoc;

const changeDocId = async function changeDocId(docRef, newKey) {
try {
// First get the document
const doc = await queryFirestore(docRef); // Then save it under the new id

const newRef = docRef.parent.doc(newKey);
const newDoc = await setToFirestore(newRef, doc); // Then delete the old document and return the new document

await docRef.delete();
return newDoc;
} catch (e) {
return Promise.reject(e);
}
};

exports.unwrapFirestoreDoc = unwrapFirestoreDoc;
exports.changeDocId = changeDocId;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firewings",
"version": "0.0.10",
"version": "0.2.0",
"description": "Give Firebase wings! - Useful helper-functions for Firebase's JS SDK.",
"main": "lib/index.js",
"author": "Pascal Luther",
Expand All @@ -11,10 +11,11 @@
"helper-functions"
],
"scripts": {
"compile": "./node_modules/.bin/babel src --out-dir lib"
"compile": "./node_modules/.bin/babel src --out-dir lib",
"publishToNpm": "npm run compile && npm publish"
},
"peerDependencies": {
"firebase": "^5.5.9"
"firebase": "^5.7.2"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
Expand Down
38 changes: 31 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/***************************************************/
/** Takes a query and a payload ********************/
/********************************************************************************************/
/** Takes a query and a payload *************************************************************/
/** Removes the properties id and path from the copy of a object and set() it to firebase */
/**************************************************/
/********************************************************************************************/

export const setToFirestore = async function(ref, payload) {
let clone = Object.assign({}, payload)
Expand All @@ -10,6 +10,9 @@ export const setToFirestore = async function(ref, payload) {

try {
await ref.set(clone)
clone.id = ref.id
clone.path = ref.path
return clone
} catch (e) {
return Promise.reject(e)
}
Expand All @@ -21,11 +24,12 @@ export const setToFirestore = async function(ref, payload) {
/**************************************************/

export const addToFirestore = async function(ref, payload) {
let clone = Object.assign({}, payload)
try {
const snapshot = await ref.add(payload)
payload.id = snapshot.id
payload.path = snapshot.path
return payload
const docRef = await ref.add(clone)
clone.id = docRef.id
clone.path = docRef.path
return clone
} catch (e) {
return Promise.reject(e)
}
Expand Down Expand Up @@ -77,3 +81,23 @@ export const unwrapFirestoreDoc = function(snapshot) {
return item
}
}

/***************************************************************************************/
/** Gets a document, copies it to a document with the new id and deletes the old one****/
/** WARNING: Do this at your own risk, only do this if you are sure what you are doing */
/***************************************************************************************/

export const changeDocId = async function(docRef, newKey) {
try {
// First get the document
const doc = await queryFirestore(docRef)
// Then save it under the new id
const newRef = docRef.parent.doc(newKey)
const newDoc = await setToFirestore(newRef, doc)
// Then delete the old document and return the new document
await docRef.delete()
return newDoc
} catch (e) {
return Promise.reject(e)
}
}

0 comments on commit 8437d1a

Please sign in to comment.