Emil Mammadov
Emil Mammadov

Reputation: 468

How can I convert this Firebase database to SQL database?

I am using Firebase for my project. Firebase database looks like that:

{
  myObjects:{
    1:{
         index: '1',
         body: 'foo1'
    },
    2:{
         index: '1',
         body: 'foo2'
    },
    3:{
         index: '2',
         body: 'foo3' 
    },
  },

  objectIndex: 1
}

As above, I have myObjects object and objectIndex variable. I was retrieving myObjects which index is same as objectIndex variable. objectIndex variable increments every 3 days and when it reaches 50 it turns into 0. So it is dynamic and I couldn't store it on the table.

Now I want to convert my Firebase database to MySQL.

MySQL will look like this:

|----|------|-------|
| id | body | index |
|----|------|-------|
| 1  | foo1 |   1   |
|----|------|-------|
| 2  | foo2 |   1   |
|----|------|-------|
| 3  | foo3 |   2   |
|----|------|-------|

Where I can store objectIndex variable? I can update my table structure according to your suggestions.

Thanks in advance.

Upvotes: 0

Views: 2076

Answers (1)

rolivencia
rolivencia

Reputation: 163

You can have a 1-row and 1-column "objectIndex" table where the value can be updated by using an SQL Cron-Job.

You can then build a query with a cartesian product that returns your data as follows:

|----|------|-------|-------------|
| id | body | index | objectIndex |
|----|------|-------|-------------|
| 1  | foo1 |   1   |     25      |
|----|------|-------|-------------|
| 2  | foo2 |   1   |     25      |
|----|------|-------|-------------|
| 3  | foo3 |   2   |     25      |
|----|------|-------|-------------|

It is redundant but gets the job done. The code to retrieve these values can be written as follows:

SELECT id, body, index, objectIndex
FROM objectTable, objectIndexTable

Upvotes: 2

Related Questions