We've previously mentioned that crud cleans the database on structure changes. But that isn't the only feature crud provides to improve DX.
When using POST
to add data, crud will check if a change to the structure occured. To allow you to play with your data, crud will not check the structure if you do a PUT
on an item.
So if we first POST
an item to /products
POST 127.0.0.1:3004/build/products
{
"name": "Keyboard",
"color": "Silver",
"material": ["Metal", "Plastic"]
}
And then change that item:
PUT 127.0.0.1:3004/build/products
{
"name": "Keyboard",
"color": "Grey",
"_id": "6392357a6a845062bbc40814"
}
No change detection will be run.
This can be useful if we want to debug or test a specific scenario without clearing our database. For instance, maybe we want to check how our frontend reacts when the array of material
is null
, instead of just []
.
If you know of a field that will be relevant later but you don't have the data for it yet, you can specify it as null
in your POST
. This will tell crud not to wipe the database if the field later appears with actual data inside.
So if we run both of these one after another:
POST 127.0.0.1:3004/build/products
{
"name": "Keyboard",
"color": "Silver",
"material": null
}
POST 127.0.0.1:3004/build/products
{
"name": "Table",
"color": "Brown",
"material": ["Wood", "Metal"]
}
A GET
will give us both:
GET 127.0.0.1:3004/build/products
[
{
"name": "Keyboard",
"color": "Silver",
"material": null,
"_id": "6392357a6a845062bbc30183"
},
{
"name": "Table",
"color": "Brown",
"material": ["Wood", "Metal"],
"_id": "6392357a6a845062bbc41924"
},
]
To minimize headaches GET
on undefined or unknown endpoints always return []
. So if we have never done a POST
on /tables
, the following will just return []
:
GET 127.0.0.1:3004/build/tables
[]
This is done so that you don't encounter 404
errors if you choose to start out by building an overview component (e.g. summary of available tables) first. That way you can build the component that does the POST
and defines the structure of your items (e.g. interactive table configurator), later.
Last edited: 2022.12.14; crud:1.1.0