@appsemble/node-utils
Version:
NodeJS utilities used by Appsemble internally.
52 lines • 2.04 kB
JavaScript
import { throwKoaError } from '../../koa.js';
import { logger } from '../../logger.js';
export function generateResourceQuery(ctx, { parseQuery }, resourceDefinition) {
try {
return parseQuery({
// @ts-expect-error 2322 null is not assignable to type (strictNullChecks)
$filter: ctx.queryParams.$filter,
// @ts-expect-error 2322 null is not assignable to type (strictNullChecks)
$orderby: ctx.queryParams.$orderby,
// @ts-expect-error 2322 null is not assignable to type (strictNullChecks)
resourceDefinition,
});
}
catch (error) {
if (error instanceof Error) {
throwKoaError(ctx, 400, 'Unable to process this query', { syntaxError: error.message });
}
logger.error(error);
throwKoaError(ctx, 400, 'Unable to process this query');
}
}
export async function deleteResourcesRecursively(type, app, options, context) {
const { deleteAppResource, getAppResources } = options;
const referencingResources = Object.entries(app.definition.resources ?? {}).filter(([, resourceDefinition]) => Object.values(resourceDefinition.references ?? {}).find((resourceReference) => resourceReference.resource === type));
for (const [referencingResourceType] of referencingResources) {
await deleteResourcesRecursively(referencingResourceType, app, options, context);
}
const resourcesToDeleteFindOptions = {
attributes: ['id'],
where: {
type,
AppId: app.id,
or: [{ seed: true }, { ephemeral: true }],
},
};
const resourcesToDelete = await getAppResources({
app,
findOptions: resourcesToDeleteFindOptions,
type,
context,
});
for (const resourceToDelete of resourcesToDelete) {
await deleteAppResource({
app,
context,
options,
id: resourceToDelete.id,
type,
});
}
}
//# sourceMappingURL=resources.js.map