Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/language/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class DocumentLoadError extends Error {

export async function loadDocument(
fileName: string,
pluginModelFiles: string[] = [],
additionalModelFiles: string[] = [],
): Promise<
{ success: true; model: Model; warnings: string[] } | { success: false; errors: string[]; warnings: string[] }
> {
Expand Down Expand Up @@ -50,9 +50,9 @@ export async function loadDocument(
URI.file(path.resolve(path.join(_dirname, '../res', STD_LIB_MODULE_NAME))),
);

// load plugin model files
// load additional model files
const pluginDocs = await Promise.all(
pluginModelFiles.map((file) =>
additionalModelFiles.map((file) =>
services.shared.workspace.LangiumDocuments.getOrCreateDocument(URI.file(path.resolve(file))),
),
);
Expand Down
21 changes: 20 additions & 1 deletion packages/plugins/policy/src/policy-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
// --- Post mutation work ---

if (hasPostUpdatePolicies && result.rows.length > 0) {
// verify if before-update rows and post-update rows still id-match
if (beforeUpdateInfo) {
invariant(beforeUpdateInfo.rows.length === result.rows.length);
const idFields = QueryUtils.requireIdFields(this.client.$schema, mutationModel);
for (const postRow of result.rows) {
const beforeRow = beforeUpdateInfo.rows.find((r) => idFields.every((f) => r[f] === postRow[f]));
if (!beforeRow) {
throw new QueryError(
'Before-update and after-update rows do not match by id. If you have post-update policies on a model, updating id fields is not supported.',
);
}
}
}

// entities updated filter
const idConditions = this.buildIdConditions(mutationModel, result.rows);

Expand Down Expand Up @@ -234,10 +248,15 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
if (!beforeUpdateAccessFields || beforeUpdateAccessFields.length === 0) {
return undefined;
}

// combine update's where with policy filter
const policyFilter = this.buildPolicyFilter(model, model, 'update');
const combinedFilter = where ? conjunction(this.dialect, [where.where, policyFilter]) : policyFilter;

const query: SelectQueryNode = {
kind: 'SelectQueryNode',
from: FromNode.create([TableNode.create(model)]),
where,
where: WhereNode.create(combinedFilter),
selections: [...beforeUpdateAccessFields.map((f) => SelectionNode.create(ColumnNode.create(f)))],
};
const result = await proceed(query);
Expand Down
Loading