2018-11-13 02:45:08 +02:00
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
|
|
|
const { Database } = require('lib/database.js');
|
|
|
|
|
|
|
|
class ResourceLocalState extends BaseModel {
|
|
|
|
static tableName() {
|
|
|
|
return 'resource_local_states';
|
|
|
|
}
|
|
|
|
|
|
|
|
static modelType() {
|
|
|
|
return BaseModel.TYPE_RESOURCE_LOCAL_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async byResourceId(resourceId) {
|
|
|
|
if (!resourceId) throw new Error('Resource ID not provided'); // Sanity check
|
|
|
|
|
|
|
|
const result = await this.modelSelectOne('SELECT * FROM resource_local_states WHERE resource_id = ?', [resourceId]);
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2018-11-13 02:45:08 +02:00
|
|
|
if (!result) {
|
|
|
|
const defaultRow = this.db().createDefaultRow(this.tableName());
|
|
|
|
delete defaultRow.id;
|
|
|
|
defaultRow.resource_id = resourceId;
|
|
|
|
return defaultRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async save(o) {
|
2019-07-29 15:43:53 +02:00
|
|
|
const queries = [{ sql: 'DELETE FROM resource_local_states WHERE resource_id = ?', params: [o.resource_id] }, Database.insertQuery(this.tableName(), o)];
|
2018-11-13 02:45:08 +02:00
|
|
|
|
|
|
|
return this.db().transactionExecBatch(queries);
|
|
|
|
}
|
|
|
|
|
|
|
|
static batchDelete(ids, options = null) {
|
|
|
|
options = options ? Object.assign({}, options) : {};
|
|
|
|
options.idFieldName = 'resource_id';
|
|
|
|
return super.batchDelete(ids, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = ResourceLocalState;
|