2023-05-08 22:06:24 +00:00
|
|
|
import {AlternateActions, AlternateStorage} from 'app/common/AlternateActions';
|
2020-07-21 13:20:51 +00:00
|
|
|
import {DocData} from 'app/common/DocData';
|
|
|
|
import {TableData} from 'app/common/TableData';
|
|
|
|
import {IndexColumns} from 'app/server/lib/DocStorage';
|
|
|
|
|
2023-05-08 22:06:24 +00:00
|
|
|
export type {ProcessedAction} from 'app/common/AlternateActions';
|
|
|
|
export type OnDemandStorage = AlternateStorage;
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle converting UserActions to DocActions for onDemand tables.
|
|
|
|
*/
|
2023-05-08 22:06:24 +00:00
|
|
|
export class OnDemandActions extends AlternateActions {
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2021-12-07 11:21:16 +00:00
|
|
|
private _tablesMeta: TableData = this._docData.getMetaTable('_grist_Tables');
|
|
|
|
private _columnsMeta: TableData = this._docData.getMetaTable('_grist_Tables_column');
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2023-05-08 22:06:24 +00:00
|
|
|
constructor(_storage: OnDemandStorage, private _docData: DocData,
|
|
|
|
private _forceOnDemand: boolean = false) {
|
|
|
|
super(_storage);
|
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
// TODO: Ideally a faster data structure like an index by tableId would be used to decide whether
|
|
|
|
// the table is onDemand.
|
|
|
|
public isOnDemand(tableId: string): boolean {
|
2022-05-18 16:05:37 +00:00
|
|
|
if (this._forceOnDemand) { return true; }
|
2020-07-21 13:20:51 +00:00
|
|
|
const tableRef = this._tablesMeta.findRow('tableId', tableId);
|
|
|
|
// OnDemand tables must have a record in the _grist_Tables metadata table.
|
|
|
|
return tableRef ? Boolean(this._tablesMeta.getValue(tableRef, 'onDemand')) : false;
|
|
|
|
}
|
|
|
|
|
2023-05-08 22:06:24 +00:00
|
|
|
public usesAlternateStorage(tableId: string): boolean {
|
|
|
|
return this.isOnDemand(tableId);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the indexes we would like to have, given the current schema.
|
|
|
|
*/
|
|
|
|
public getDesiredIndexes(): IndexColumns[] {
|
|
|
|
const desiredIndexes: IndexColumns[] = [];
|
|
|
|
for (const c of this._columnsMeta.getRecords()) {
|
|
|
|
const t = this._tablesMeta.getRecord(c.parentId as number);
|
|
|
|
if (t && t.onDemand && c.type && (c.type as string).startsWith('Ref:')) {
|
|
|
|
desiredIndexes.push({tableId: t.tableId as string, colId: c.colId as string});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return desiredIndexes;
|
|
|
|
}
|
|
|
|
}
|