Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T32139 default negative ids for new models #112

Open
wants to merge 54 commits into
base: health_master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
6f050ba
Automatically convert files in data to the format understood by djang…
daanvdk Apr 2, 2021
bf6407a
New models will get a negative id by default instead of null, with up…
Jun 28, 2021
849c0cd
v0.28.3
Jun 28, 2021
744cb65
v0.28.4
Jun 28, 2021
dfff896
Merge branch 'isnew-for-negative-ids' into T32139-default-negative-id…
Jun 28, 2021
0fb001d
v0.29.0
Jun 28, 2021
7dda13e
Related models will still get id of null to indicate relation is empty
Jun 28, 2021
e27cce9
Add more tests showcasing clearing behaviour also after copying
Jun 28, 2021
6ad90ae
v0.28.4
Jul 2, 2021
d479d3a
Merge remote-tracking branch 'origin/isnew-for-negative-ids' into isn…
Jul 7, 2021
4c39b2f
Merge branch 'isnew-for-negative-ids' into T32139-default-negative-id…
Jul 7, 2021
87c1df7
v0.29.1
Jul 7, 2021
5f90823
Create new negative id on clear
Jul 7, 2021
14eed14
Fix review comments from Daan
Jul 26, 2021
c125b0e
Test in jsdom environment so that we can use Blob/FormData
daanvdk Jul 26, 2021
725c2cf
Add tests
daanvdk Jul 26, 2021
be18b90
Add documentation
Jul 26, 2021
ca5532f
fix docs
Jul 26, 2021
e58a9f5
Merge branch 'master' into files-in-request-body
Aug 23, 2021
66b1c14
add tests to check if files convert after model.save
Aug 23, 2021
fbe2e80
Improve test
Aug 24, 2021
a2ca2ac
Improve both tests
Aug 24, 2021
d7ff52e
Automatically convert files in data to the format understood by djang…
daanvdk Apr 2, 2021
8d5efa9
Test in jsdom environment so that we can use Blob/FormData
daanvdk Jul 26, 2021
b904268
Add tests
daanvdk Jul 26, 2021
b1fa3ff
Merge remote-tracking branch 'CY/files-in-request-body' into files-in…
Oct 25, 2021
10160ea
add tests to check if files convert after model.save
Aug 23, 2021
204beaf
Improve test
Aug 24, 2021
604c335
Improve both tests
Aug 24, 2021
a64c093
Fix frontend_lint and update node version
Oct 25, 2021
0c2f2ce
Merge remote-tracking branch 'CY/files-in-request-body' into files-in…
Oct 25, 2021
97def3e
Merge pull request #123 from Bilonan/files-in-request-body
abzainuddin Oct 25, 2021
36b0d2a
Merge pull request #103 from CodeYellowBV/files-in-request-body
abzainuddin Nov 26, 2021
3f916bb
v0.28.3
abzainuddin Nov 26, 2021
60f3f8b
v0.28.4
abzainuddin Nov 26, 2021
dcb8690
add check if relation is defined
Nov 29, 2021
87aa1f7
build
Nov 29, 2021
f7dc23f
Add comment for relation check
Nov 29, 2021
82ff876
Add test "save all with not defined relation error"
Nov 30, 2021
fe5418c
add snapshot
Nov 30, 2021
1e70ed4
Merge pull request #125 from Bilonan/T34782-relations-error
abzainuddin Nov 30, 2021
ee852d2
Fix styling of some comments, and added a migration guide for convert…
Feb 28, 2022
445474d
Merge branch 'health_master' into T32139-default-negative-ids-for-new…
Feb 28, 2022
7de2476
build es files
Feb 28, 2022
ccc3e1d
v0.29.2
Feb 28, 2022
34cedfc
v0.30.0
Feb 28, 2022
80a82f2
Merge remote-tracking branch 'origin-cy/master' into T32139-default-n…
Feb 28, 2022
d7897be
add failing test for inconsistent ordering
stefanmajoor Mar 3, 2022
b3855df
fix instable ordering in relations
stefanmajoor Mar 3, 2022
5a66e98
update build
stefanmajoor Mar 3, 2022
6e9a5fc
Improve running time of repository parsing
daanvdk Mar 4, 2022
cec7709
Merge pull request #129 from CodeYellowBV/fix_with_sorting_bug
daanvdk Mar 4, 2022
65ea570
Merge remote-tracking branch 'origin-cy/master' into T32139-default-n…
Mar 28, 2022
126db33
build es files
Mar 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
node-version: ['6']
node-version: ['14']

steps:
- name: Checkout code
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { Model } from 'mobx-spine';

// Define a class Animal, with 2 observed properties `id` and `name`.
class Animal extends Model {
@observable id = null; // Default value is null.
@observable id; // Default value is undefined, a new negative integer will automatically be generated for a new model.
@observable name = ''; // Default value is ''.
}
```
Expand All @@ -56,7 +56,7 @@ If we instantiate a new animal without arguments it will create an empty animal
// Create an empty instance of an Animal.
const lion = new Animal();

console.log(lion.id); // null
console.log(lion.id); // -1, a unique negative integer for this model
console.log(lion.name); // ''
```

Expand All @@ -67,6 +67,7 @@ You can also supply data when creating a new instance:
const cat = new Animal({ id: 1, name: 'Cat' });

console.log(cat.name); // Cat
console.log(cat.id); // 1
```

When data is supplied in the constructor, these can be reset by calling `clear`:
Expand All @@ -90,6 +91,13 @@ cat.name = '';
console.log(cat.undefinedProperty); // undefined
```

### New models
A new model is a model that exists in the store, but not on the backend. A new model either has a negative id, or the id is `null`. Checking if a model is new can be done with `model.isNew()` which returns a boolean `true` when the model is new.

By default, a new model will be initialized with a negative id, this way the model can be used as a related model. When a model is initialized with a negative id it will automatically generate a new negative id for the model on a clear. In some cases a `null` id might be preferred, in this case a model can be forced to get a `null` id by passing `{id: null}` in the constructor. In this case the id will also be reset to `null` on a clear. A model with a `null` id functions the same as a model with a negative id other than that the model cannot be used in a relation.

Some projects might still use the legacy method of checking for new models by checking if `!model.id`. This does not work with the default negative IDs. To migrate a project to the default negative IDs implementation you should search and replace the whole project for occurrences of `.id` and fix all lines that check if an id is set to use the `isNew()` property.

### Constructor: options

|key|default| | |
Expand Down Expand Up @@ -308,6 +316,9 @@ class animal = new Animal({ id: 2, name: 'Rova', breed: { id: 3, name: 'Main Coo
console.log(animal.breed.name); // Throws cannot read property name from undefined.
```

### Negative IDs for related models
A related model will always be initialized with a `null` id. When the id of the related model is set to `null` it indicates to django-binder that the field is empty.

### Pick fields

You can pick fields by either defining a static `pickFields` variable or a `pickFields` function. Keep in mind that `id` is mandatory, so it will always be included.
Expand Down
Loading