# Managing database data

Now that you've added your Firebase database as the data source, you can start managing its data. Here, you'll learn how to perform *CRUD operations* with [Firebase Realtime DB](#firebase-realtime-db-operations) and [Firestore](#firestore-operations).

## Firebase Realtime DB operations

### Read data

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code:

```javascript
const userId = firebase.auth().currentUser.uid;
return firebase
  .database()
  .ref('/users/' + userId)
  .once('value')
  .then(snapshot => snapshot.val());
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FOSWR0rTa41zHUoCOL2qh%2FCleanShot%202025-04-15%20at%2016.04.12%402x-min.png?alt=media&#x26;token=0a50b643-90f3-4189-bcfe-a70499ba5353" alt=""><figcaption></figcaption></figure>

2. Click **Execute action** and check the *Result* tab - the data should be there.

### Add a record

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code:

```javascript
const listRef = firebase.database().ref("list/");

listRef.set({
   record1: {
      field1: newValue1,
      field2: newValue2,
   },
});
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FG3xLHM5LB7zqgbAgxWpM%2FCleanShot%202025-04-15%20at%2016.07.40%402x-min.png?alt=media&#x26;token=fd70390b-72a9-470f-b6b6-6599c574df5e" alt=""><figcaption></figcaption></figure>

2. Click **Execute action** and check the *Result* tab - the new record should be created.

### Update a record

Updates can be executed using *two methods* - `set()` and `update()`. Below you'll find an example of both methods.

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code depending on the selected method:

* `set()`

```javascript
const listRef = firebase.database().ref("list/record");

listRef.set({
      field1: newValue1,
      field2: newValue2,
});
```

{% hint style="warning" %}
Please note that `set()` can overwrite your data, while `update()` only updates the required fields.
{% endhint %}

* `update()`

```javascript
const recordRef = firebase.database().ref("list/record");

recordRef.update({
   field1: newValue1,
});
```

2. Click **Execute action** and check the *Result* tab - the requested records should be updated with the new values.

### Delete a record

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code:

```javascript
const recordRef = firebase.database().ref("list/record");
recordRef.remove();
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FFgCiXNnaj6G7KsxKcHmd%2FCleanShot%202025-04-15%20at%2016.16.57%402x-min.png?alt=media&#x26;token=93a025d0-1b2f-498b-8d93-ed14779dc939" alt=""><figcaption></figcaption></figure>

2. Click **Execute action** and check the *Result* tab - the specific record should be removed.

All other operations with the Realtime Database data like *Filter, Order, etc.* can be done via the Firestore API. You can find the API reference [here](https://firebase.google.com/docs/database/web/lists-of-data?authuser=1).

***

You can also check out more useful articles on how to further manage your data in UI Bakery:point\_down:

{% content-ref url="../../../build-from-scratch/getting-started/transform-data-with-javascript/mapping-and-transforming-data" %}
[mapping-and-transforming-data](https://docs.uibakery.io/build-from-scratch/getting-started/transform-data-with-javascript/mapping-and-transforming-data)
{% endcontent-ref %}

{% content-ref url="../../../build-from-scratch/getting-started/bind-data-to-ui" %}
[bind-data-to-ui](https://docs.uibakery.io/build-from-scratch/getting-started/bind-data-to-ui)
{% endcontent-ref %}

## Firestore operations

### List data

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code to load your Firestore collection:

```javascript
const collection = await firebase.firestore().collection("collectionName").get();
const result = [];
collection.forEach(doc => {
    const data = doc.data();
    result.push({ id: doc.id, ...data });
});
return result;
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FrRdzqYartQzvldSZGz5R%2FCleanShot%202025-04-15%20at%2016.24.53%402x-min.png?alt=media&#x26;token=a4a10441-d5b6-4279-83ba-6c8a93f97485" alt=""><figcaption></figcaption></figure>

2. Click **Execute action** and check the *Result* tab - your collection data should be displayed.

### Read a document

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code to fetch data about a specific document:

```javascript
const doc = await firebase.firestore().collection('collectionName').doc('docID').get();
return doc.data();
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2FhIWvIeShSCLjrObLFmrr%2FCleanShot%202025-04-15%20at%2016.28.21%402x-min.png?alt=media&#x26;token=d67d7bde-da6d-4535-9d83-2437f8146c5a" alt=""><figcaption></figcaption></figure>

2. Click **Execute action** and check the *Result* tab - document data should be displayed.

### Create a new record

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code:

```javascript
const newUser = { name: "Nik" };
const doc = await firebase.firestore().collection("collectionName").add(newUser);
return doc.id;
```

<figure><img src="https://837703843-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUX6zPRMFFK0yrTghj7cY%2Fuploads%2F8QWnBEfClT4XTq2LnqRU%2FCleanShot%202025-04-15%20at%2016.30.46%402x-min.png?alt=media&#x26;token=d6e0cd25-abaa-4f7e-a062-ec1a601793cd" alt=""><figcaption></figcaption></figure>

2. Click **Execute action** and check the *Result* tab - the new record should be there.

### Delete a record

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code:

```javascript
await firebase.firestore().collection('collectionName').doc('docID').delete(); 
```

2. Click **Execute action** and check the *Result* tab - the record should be deleted.

### **Update a record**

Updates can be executed using *two methods* - `set()` and `update()`. The difference between them is that `set()` will overwrite the whole document while `update()`will update only specific fields that need to be updated. \
Below you'll find an example of both methods.

1. [Create a new action](https://docs.uibakery.io/concepts/actions/action-basics#creating-actions) of the **JavaScript Code** type and add the following code depending on the selected method:

* `set()`

```javascript
const doc = await firebase.firestore().collection('collectionName').doc('docID').set({
    docField: newFieldValue
});
```

{% hint style="info" %}
If you don't want to overwrite your data, you can add `merge: true` to your code so that only the required fields will be updated.
{% endhint %}

* `update()`

```javascript
const doc = await firebase.firestore().collection('collectionName').doc('docID').update({
    docField: newFieldValue
});
```

2. Click **Execute action** and check the *Result* tab - the requested records should be updated with the new values.

All other operations with the Firestore collections like *Filter, Order, etc.* can be done via the Firestore API. You can find the API reference [here](https://firebase.google.com/docs/firestore/query-data/get-data).

***

You can also check out more useful articles on how to further manage your data in UI Bakery:point\_down:

{% content-ref url="../../../build-from-scratch/getting-started/transform-data-with-javascript/mapping-and-transforming-data" %}
[mapping-and-transforming-data](https://docs.uibakery.io/build-from-scratch/getting-started/transform-data-with-javascript/mapping-and-transforming-data)
{% endcontent-ref %}

{% content-ref url="../../../build-from-scratch/getting-started/bind-data-to-ui" %}
[bind-data-to-ui](https://docs.uibakery.io/build-from-scratch/getting-started/bind-data-to-ui)
{% endcontent-ref %}
