2021-01-22 19:41:11 +02:00
|
|
|
import BaseModel from '../BaseModel';
|
|
|
|
import { ResourceLocalStateEntity } from '../services/database/types';
|
2021-01-29 20:45:11 +02:00
|
|
|
import Database from '../database';
|
2018-11-13 02:45:08 +02:00
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
export default class ResourceLocalState extends BaseModel {
|
2023-03-06 16:22:01 +02:00
|
|
|
public static tableName() {
|
2018-11-13 02:45:08 +02:00
|
|
|
return 'resource_local_states';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static modelType() {
|
2018-11-13 02:45:08 +02:00
|
|
|
return BaseModel.TYPE_RESOURCE_LOCAL_STATE;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static async byResourceId(resourceId: string) {
|
2018-11-13 02:45:08 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-16 18:42:42 +02:00
|
|
|
public static saveQueries(o: ResourceLocalStateEntity) {
|
|
|
|
return [{ 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
|
|
|
|
2023-07-16 18:42:42 +02:00
|
|
|
public static async save(o: ResourceLocalStateEntity) {
|
|
|
|
return this.db().transactionExecBatch(this.saveQueries(o));
|
2018-11-13 02:45:08 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static batchDelete(ids: string[], options: any = null) {
|
2023-06-01 13:02:36 +02:00
|
|
|
options = options ? { ...options } : {};
|
2018-11-13 02:45:08 +02:00
|
|
|
options.idFieldName = 'resource_id';
|
|
|
|
return super.batchDelete(ids, options);
|
|
|
|
}
|
|
|
|
}
|