Browse Source

system repo functions

pull/1/head
Tim Glasgow 2 years ago
parent
commit
2f7698af51
  1. 64
      squarenotsquare/realm/repos/SystemRepo.js

64
squarenotsquare/realm/repos/SystemRepo.js

@ -0,0 +1,64 @@
import {System} from '../entities/System';
import uuid from 'uuid';
export default class SystemRepo {
realmDB = null;
constructor(db) {
this.realmDB = db;
}
getAllSystemValues = () => {
let system = this.realmDB.objects(System.name);
let systemValues = {};
system.forEach((row) => {
systemValues[row.key] = row.value;
});
return systemValues;
};
createSystemValue = (key, value) => {
let sysID = null;
let sysKey = key;
let sysValue = value;
let existing = this.getSystemKeyValue(key);
if (existing.id === null) {
sysID = uuid.v4();
} else {
sysID = existing.id;
}
this.realmDB.write(() => {
this.realmDB.create(
System.name,
{
id: sysID,
key: sysKey,
value: sysValue,
},
true,
);
});
};
deleteSystemValue = (key) => {
let existingKey = this.getSystemKeyValue(key);
if (!existingKey.id === null) {
this.realmDB.write(() => {
this.realmDB.delete(existingKey);
});
}
};
getSystemKeyValue = (key) => {
let row = this.realmDB.objects(System.name).filtered('key = $0', key);
if (row.length === 1) {
return {id: row[0].id, value: row[0].value};
} else if (row.length > 1) {
return 'Multiple system keys found for ' + key;
} else {
return {id: null, value: null};
}
};
}
Loading…
Cancel
Save