2022-12-28 13:27:14 +01:00
|
|
|
export const SettingsSchema = {
|
|
|
|
|
name: 'Settings',
|
|
|
|
|
primaryKey: 'id',
|
|
|
|
|
properties: {
|
|
|
|
|
id: 'int',
|
|
|
|
|
key: 'string',
|
|
|
|
|
value: 'string',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-29 16:22:58 +02:00
|
|
|
class SettingModel {
|
|
|
|
|
constructor(realm) {
|
2023-08-22 10:32:40 +02:00
|
|
|
this.schema = 'Settings';
|
|
|
|
|
this.realm = realm;
|
2022-09-29 16:22:58 +02:00
|
|
|
}
|
2023-08-22 10:32:40 +02:00
|
|
|
|
2022-09-29 16:22:58 +02:00
|
|
|
add(data) {
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
if (data.id === undefined) {
|
2022-10-04 17:48:38 +02:00
|
|
|
let id = this.realm.objects(this.schema).max('id');
|
2022-09-29 16:22:58 +02:00
|
|
|
data.id = id === undefined ? 1 : id + 1;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
this.realm.create(this.schema, data, 'never');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log('db setting add', e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 17:48:38 +02:00
|
|
|
upsert(data) {
|
|
|
|
|
let tmp = this.get(data.key);
|
|
|
|
|
if (tmp === false || tmp === undefined) {
|
|
|
|
|
this.add(data);
|
|
|
|
|
} else {
|
|
|
|
|
if (data.id === undefined) {
|
|
|
|
|
data.id = tmp.id;
|
|
|
|
|
}
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
this.realm.create(this.schema, data, 'modified');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 18:03:52 +02:00
|
|
|
update(data) {
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
this.realm.create(this.schema, data, 'modified');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 17:48:38 +02:00
|
|
|
get(key) {
|
|
|
|
|
let tmp = this.realm.objects(this.schema);
|
|
|
|
|
if (tmp.length === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
tmp = tmp.filtered('key = "' + key + '"')[0];
|
|
|
|
|
}
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 18:27:54 +02:00
|
|
|
delete(key) {
|
|
|
|
|
const tmp = this.get(key);
|
|
|
|
|
if (typeof tmp === 'object') {
|
|
|
|
|
this.realm.write(() => {
|
|
|
|
|
this.realm.delete(tmp);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-29 16:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
2023-08-22 10:32:40 +02:00
|
|
|
export default SettingModel;
|