Skip to content

Commit

Permalink
Merge pull request #35 from mbroersen/develop
Browse files Browse the repository at this point in the history
Add model first then add relations
  • Loading branch information
mbroersen authored Nov 11, 2020
2 parents cb0c7ba + 2a68d9e commit f048ef6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dist/jeloquent.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"module": "dist/jeloquent.js",
"unpkg": "dist/jeloquent.js",
"main": "dist/jeloquent.js",
"version": "2.4.4",
"version": "2.4.5",
"description": "micro framework / memory orm model store",
"scripts": {
"build": "webpack --config webpack.config.js",
Expand Down
24 changes: 21 additions & 3 deletions src/Store/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class Model {
const model = this.getInstance();
model.fill(modelData);
window.Store.database().insert(this.className(), model);
model.fillRelations(modelData);
data[i] = model;

}
return data;
}
Expand Down Expand Up @@ -119,16 +121,32 @@ class Model {


fill (data) {
// insert through relations after model insert;
for (let i = 0; i < this.numberOfFields; i++) {
const fieldName = this.originalFields[i].$name;
if (data[fieldName] !== undefined) {
this[`_${fieldName}`] = data[fieldName];
if (!(this.originalFields[i] instanceof Relation)) {
const fieldName = this.originalFields[i].$name;
if (data[fieldName] !== undefined) {
this[`_${fieldName}`] = data[fieldName];
}
}
}

this.setPrimaryKey();
}

fillRelations(data) {
// insert through relations after model insert;
for (let i = 0; i < this.numberOfFields; i++) {
if ((this.originalFields[i] instanceof Relation)) {
const fieldName = this.originalFields[i].$name;
if (data[fieldName] !== undefined) {
this[`_${fieldName}`] = data[fieldName];
}
}
}
}


setPrimaryKey() {
if (this.primaryFields.length === 1) {
this.primaryKeyValue = this[this.primaryFields[0].$name] ?? null;
Expand Down
5 changes: 1 addition & 4 deletions tests/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,13 @@ test('Insert relations via model', () => {
team: {id: 1, name: 'team relation 1'},
user_address: {id: 22, city: 'Hoorn', steet: 'waagplein', house_number: 1, user_id: 1},
avatar: {avatar_type: 'User', avatar_id: 1, img_url: 'team.png'},
});

User.insert({
comments: [
{id: 9, title: 'titel', text: 'hoi', user_id: 1},
{id: 19, title: 'a titel', text: 'hoi 2', user_id: 1},
{id: 29, title: 'titel b', text: 'hoi 2', user_id: 1},
{id: 39, title: '9 titel', text: 'hoi 2', user_id: 1},
]
})
});

expect(User.all().length).toStrictEqual(1);
expect(Team.find(1)).toBeInstanceOf(Team);
Expand Down

0 comments on commit f048ef6

Please sign in to comment.