PHP 8.2.30
Preview: flat.doc.md Size: 2.74 KB
//proc/thread-self/root/home/byroehnu/.trash/node_modules11/adminjs/src/utils/flat/flat.doc.md

## The AdminJS Data model

Before you dive into the details of the flat helpers, let me briefly introduce the way how the
database data is stored in AdminJS.

### The Simple Case

On the backend all the information from DataBase tables/collections goes to {@BaseRecord#params}
object. Usually, it is a `{key: value}` store where `key` is a table name, and value could be
either a string, a number, or an instance of Date() etc.

On the Frontend - these values go to the corresponding {@link RecordJSON.params} property.

This is an example `params` property in the "simple case":

```javascript
params: {
  name: 'John',
  surname: 'Doe',
  age: 28,
}
```

### The Real World Case

In the real world:
* databases have nested JSONB properties or Mixed Schemas
* developers would like to send more complicated data from the Fronted to the backend as arrays.

To achieve that we "flatten" all the data before saving them to `params` property.

So in the real world params could look like:

```javascript
params: {
  name: 'John',
  surname: 'Doe',
  age: 28,
  // nested objects
  'auth.facebook': 'some login token',
  'auth.twitter': 'twitter login token',
  // arrays
  'interests.0': 'running',
  'interests.1': 'jogging',
}
```
and all the data can be even nested on the deeper levels.

### Why we did that?

An alternative solution would be to store an entire raw object and don't care. But there are 2
reasons why we picked this one.

**1. storing selected data in the database**

If you send to the ORM unflatten save request like this: 

```javascript
Model.save({{ auth: { facebook: 'newFacebookToken' } }})
```

it will override an entire `auth` property, so from the example above, we will lose `auth.twitter`.

In the second (flatten) case:

```javascript
Model.save({ `auth.facebook`: 'newFacebookToken' }})
```

ORM should keep the value of the `auth.twitter`

> The above is true for Mongoose adapter which is the most advanced regarding handing mixed values

**2. Sending data between the Frontend and the Backend in {@link https://developer.mozilla.org/en-US/docs/Web/API/FormData FormData} format**

AdminJS allows you to upload Files from the Frontend to the Backend. The most optimal way of
doing that is by using {@link https://developer.mozilla.org/en-US/docs/Web/API/FormData FormData}.
But, this requires that values for all the fields are send in `[key: string]: string` form.
And this, as you might guess, fits perfectly to our flatten `params` logic.

### Consequences

Flattening in AdminJS has its consequences everywhere where you use

- {@link BaseRecord} and
- {@link RecordJSON}, 

because instead of raw object you have it's flatten version.

Also, (as mentioned) the `payload` send to the backed is also flattened.

There you should use helpers gathered in {@link flat}

Directory Contents

Dirs: 0 × Files: 26

Name Size Perms Modified Actions
44 B lrw-r--r-- 2026-02-28 00:32:02
Edit Download
448 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
1.42 KB lrw-r--r-- 2026-02-28 00:32:10
Edit Download
906 B lrw-r--r-- 2026-02-28 00:32:10
Edit Download
1.24 KB lrw-r--r-- 2026-02-28 00:32:14
Edit Download
2.74 KB lrw-r--r-- 2026-02-28 00:31:56
Edit Download
965 B lrw-r--r-- 2026-02-28 00:32:14
Edit Download
587 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
2.84 KB lrw-r--r-- 2026-02-28 00:32:20
Edit Download
2.51 KB lrw-r--r-- 2026-02-28 00:32:20
Edit Download
65 B lrw-r--r-- 2026-02-28 00:32:28
Edit Download
2.01 KB lrw-r--r-- 2026-02-28 00:32:34
Edit Download
686 B lrw-r--r-- 2026-02-28 00:32:36
Edit Download
38 B lrw-r--r-- 2026-02-28 00:32:40
Edit Download
730 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
976 B lrw-r--r-- 2026-02-28 00:32:40
Edit Download
890 B lrw-r--r-- 2026-02-28 00:32:40
Edit Download
1.21 KB lrw-r--r-- 2026-02-28 00:31:56
Edit Download
2.47 KB lrw-r--r-- 2026-02-28 00:32:40
Edit Download
1.41 KB lrw-r--r-- 2026-02-28 00:32:40
Edit Download
504 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
1.49 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download
1.11 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download
639 B lrw-r--r-- 2026-02-28 00:31:56
Edit Download
4.70 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download
2.03 KB lrw-r--r-- 2026-02-28 00:32:44
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).