Skip to content

Commit

Permalink
Merge pull request #36 from mbroersen/develop
Browse files Browse the repository at this point in the history
Adds isDirty and updating of foreign keys on save
  • Loading branch information
mbroersen authored Nov 13, 2020
2 parents f048ef6 + 171d0de commit 2d882e4
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 33 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.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"module": "dist/jeloquent.js",
"unpkg": "dist/jeloquent.js",
"main": "dist/jeloquent.js",
"version": "2.4.5",
"version": "2.5.0",
"description": "micro framework / memory orm model store",
"scripts": {
"build": "webpack --config webpack.config.js",
"watch": "webpack --config webpack.config.js --watch",
"test": "jest",
"lint": "eslint src/*/** --fix-dry-run"
},
Expand Down
3 changes: 2 additions & 1 deletion src/Jeloquent.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Database, Store, Table, Connection} from './Store/Store.js';
import {Model, Field, Relation, BelongsTo, HasOne, HasOneThrough, HasMany, HasManyThrough, MorphOne, MorphTo} from './Store/Model.js';
import {Model, Field, Relation, BelongsTo, HasOne, HasOneThrough, HasMany, HasManyThrough, MorphOne, MorphTo, ForeignKey} from './Store/Model.js';
import {ConnectionAdapter, ConnectionAdapterFactory} from "./Store/Connection/ConnectionAdapterFactory.js";
import QueueMessage from "./Store/Connection/Queue/QueueMessage";

Expand All @@ -13,6 +13,7 @@ export {
Table,
Model,
Field,
ForeignKey,
Relation,
BelongsTo,
HasOne,
Expand Down
17 changes: 9 additions & 8 deletions src/Store/Database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Table from "./Table.js";
import HasManyThrough from "./Model/Relation/HasManyThrough.js";

export default class Database {

Expand All @@ -11,17 +10,16 @@ export default class Database {
* @type {{<Table>}}
*/
this.tables = {};
this.hasManyThroughRelations = [];

models.forEach((model) => {
const table = new Table(model);
this.tables[table.name] = table;

this.hasManyThroughRelations.push(...model.getInstance().originalFields.filter(field => field instanceof HasManyThrough));
});
}

this.hasManyThroughRelations.forEach((hasManyThrough) => {
this.addIndex(hasManyThrough.model.className(), hasManyThrough.indexName);
setIndexes() {
Object.values(this.tables).forEach((table) => {
table.setupIndexes();
});
}

Expand All @@ -42,7 +40,11 @@ export default class Database {
}

addIndex(table, name) {
this.tables[table]?.addIndex(name);
this.tables[table].addIndex(name);
}

addToIndex(table, indexName, lookUpKey, id) {
return this.tables[table].addToIndex(indexName, lookUpKey, id)
}

removeFromIndex(table, indexName, lookUpKey, id) {
Expand Down Expand Up @@ -108,7 +110,6 @@ export default class Database {
}

return null;

}


Expand Down
81 changes: 78 additions & 3 deletions src/Store/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import MorphTo from "./Model/Field/MorphTo.js";

import Field from "./Model/Field.js";
import Relation from "./Model/Relation.js";
import ForeignKey from "./Model/Field/ForeignKey.js";

class Model {

Expand Down Expand Up @@ -98,6 +99,21 @@ class Model {
return window.Store.database().ids(this.className());
}

tableSetup(table) {
for (let i = 0; i < this.numberOfFields; i++) {
if (this.originalFields[i] instanceof ForeignKey) {
this.originalFields[i].tableSetup(table);
}
}

for (let i = 0; i < this.numberOfFields; i++) {
if (this.originalFields[i] instanceof HasManyThrough) {
this.originalFields[i].tableSetup(table);
}
}
}


save() {
const className = this.constructor.className();
const currentDatabase = window.Store.database();
Expand All @@ -112,16 +128,41 @@ class Model {
currentDatabase.delete(className, this._tmpId);
}

this.dirtyFields.forEach((field) => {
window.Store.database().removeFromIndex(className, field.$name, field.previousValue, this.primaryKeyValue);
this.fill(field.toJson());
window.Store.database().addToIndex(className, field.$name, field.value, this.primaryKeyValue);
});

if (tableIds.includes(this.primaryKey+'')) {
currentDatabase.update(className, this);
return;
}
currentDatabase.insert(className, this);
}

addNewIndex(name) {
window.Store.database().addIndex(this.constructor.className(), name);
}


removeFromIndex(foreignKeyField) {
const className = this.constructor.className();
const currentDatabase = window.Store.database();

currentDatabase.removeFromIndex(className, foreignKeyField.$name, foreignKeyField.previousValue, this.primaryKey);
}


addToIndex(foreignKeyField) {
const className = this.constructor.className();
const currentDatabase = window.Store.database();
currentDatabase.addToIndex(className, foreignKeyField.$name, foreignKeyField.fieldValue, this.primaryKey);
}


fill (data) {
// insert through relations after model insert;
this.fillPrimaryKey(data);
for (let i = 0; i < this.numberOfFields; i++) {
if (!(this.originalFields[i] instanceof Relation)) {
const fieldName = this.originalFields[i].$name;
Expand All @@ -130,10 +171,21 @@ class Model {
}
}
}
}

fillPrimaryKey(data) {
for (let i = 0; i < this.numberOfFields; i++) {
if (this.originalFields[i].isPrimary === true) {
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++) {
Expand Down Expand Up @@ -173,13 +225,35 @@ class Model {
return this.originalFields.filter(field => field.isPrimary).map(field => field.$name);
}

isDirty(fieldName) {
if (fieldName) {
return this.dirtyFieldNames.includes(fieldName);
}
return this.dirtyFields.length > 0;
}

get dirtyFields() {
return this.originalFields.filter(field => field.isDirty);
}

get dirtyFieldNames() {
return this.dirtyFields.map(field => field.$name);
}

resetDirty() {
this.originalFields.filter((field) => !(field instanceof Relation)).forEach(field => {
field.resetDirty();
})
}

addRelationFields(fields) {
const fieldList = [...fields];
fields.forEach((field, i) => {
if (field instanceof Relation) {
fieldList.splice(i, 0, ...field.getRelationalFields());
}
});

this.numberOfFields = fieldList.length;
return fieldList;
}
Expand All @@ -194,8 +268,8 @@ class Model {
Object.defineProperty(this,
`indexedFields`, {
get: () => {
return this.originalFields.filter((field) => field instanceof BelongsTo).reduce((array, relation) => {
array.push(relation.foreignKey);
return this.originalFields.filter((field) => field instanceof ForeignKey).reduce((array, relation) => {
array.push(relation.$name);
return array;
}, []);
},
Expand Down Expand Up @@ -246,4 +320,5 @@ export {
HasManyThrough,
MorphOne,
MorphTo,
ForeignKey,
};
20 changes: 19 additions & 1 deletion src/Store/Model/Field.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ export default class Field {
return this.setName().setParentProperties();
}

tableSetup() {
//todo setup table;
}

setParentProperties() {
Object.defineProperty(this.$parent,
this.$name, {
get: () => {
return this.value;
},
set: (value) => {
this.previousValue = JSON.parse(JSON.stringify(this.value));
this.previousValue = JSON.parse(JSON.stringify(this.value ?? value));
this.value = value;
}
}
Expand All @@ -41,11 +45,25 @@ export default class Field {
`_${this.$name}`,
{
set: (value) => {
this.previousValue = JSON.parse(JSON.stringify(this.value));
this.fieldValue = value;
}
});
}

get isDirty() {
return this.fieldValue != this.previousValue;
}

resetDirty() {
this.previousValue = JSON.parse(JSON.stringify(this.fieldValue));
}

toJson() {
const object = {};
object[this.$name] = this.fieldValue;
return JSON.parse(JSON.stringify(object));
}

get value() {
return this.fieldValue;
Expand Down
43 changes: 43 additions & 0 deletions src/Store/Model/Field/ForeignKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Field from "../Field";


//should return User or Team
export default class ForeignKey extends Field {

constructor(name) {
super(name);
}

tableSetup(table) {
table.addIndex(this.$name);
}

get value() {
return this.fieldValue;
}

set value(value) {
this.fieldValue = value;

// todo fix entity store update
// const objectValue = {}
//
// window.Store.database().update(
// this.$parent.constructor.name,
// {...value}
// );
}

setFillPropertyOnParent() {
// todo updating relation should update indexes;
Object.defineProperty(this.$parent,
`_${this.$name}`,
{
set: (value) => {
this.$parent.removeFromIndex(this);
this.fieldValue = value;
this.$parent.addToIndex(this);
}
});
}
}
8 changes: 7 additions & 1 deletion src/Store/Model/Relation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Field from "./Field.js";
import ForeignKey from "./Field/ForeignKey";

export default class Relation extends Field {

Expand All @@ -9,8 +10,13 @@ export default class Relation extends Field {
this.foreignKey = foreignKey;
}

tableSetup(table) {
table.addIndex(this.foreignKey);
}


getRelationalFields() {
return [new Field(this.foreignKey)];
return [new ForeignKey(this.foreignKey)];
}

setFillPropertyOnParent() {
Expand Down
8 changes: 8 additions & 0 deletions src/Store/Model/Relation/HasManyThrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default class HasManyThrough extends Relation {
//this.parent.comments
constructor(model, throughModel, foreignKey, localKey) {
super(model, foreignKey);
this.model = model;
this.throughModel = throughModel;
this.localKey = localKey ?? 'id';
}
Expand All @@ -21,6 +22,13 @@ export default class HasManyThrough extends Relation {
return this;
}

tableSetup() {
//todo fix this;
//table

this.model.addIndex(this.indexName);
}

getRelationalFields() {
return [];
}
Expand Down
1 change: 1 addition & 0 deletions src/Store/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Store {

use(storeName) {
this.useDatabase = storeName;
this.databases[this.useDatabase ?? 'default']?.setIndexes();
}

add(database) {
Expand Down
Loading

0 comments on commit 2d882e4

Please sign in to comment.