@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
65 lines (63 loc) • 2.03 kB
JavaScript
import { linkedDatasource, resolvedShape } from "./datasource-types.js";
//#region src/codegen/datasource-schema.ts
/**
* The types a `contains` filter accepts, so a linked picker knows whether it can search its label.
* Kept in step with `TextFieldType` in ../datasource/fields.ts, which is not exported.
*/
const SEARCHABLE_TYPES = new Set([
"SINGLE_LINE_TEXT",
"LONG_TEXT",
"URL",
"EMAIL",
"PHONE",
"ADDRESS",
"RECORD_ID",
"LOCATION",
"TIME",
"BUTTON"
]);
/**
* One datasource's metadata as JSON, for tools that generate code from it.
*
* A linked field carries its target already resolved: the registration id and the field a person
* recognises a row by. Readers never map table ids themselves. `datasources` is keyed by
* registration id.
*/
function renderDatasourceSchema(datasource, datasources) {
const schema = {
id: datasource.id,
name: datasource.name,
primaryFieldId: datasource.primaryFieldId,
fields: datasource.fields.map((field) => schemaField(field, datasources))
};
return `${JSON.stringify(schema, null, 2)}\n`;
}
function schemaField(field, datasources) {
const base = {
id: field.id,
name: field.name,
type: field.type,
allowMultipleEntries: field.allowMultipleEntries,
required: field.required,
readonly: field.readonly,
createOnly: field.createOnly,
aiOnly: field.aiOnly === true,
defaultValue: field.defaultValue,
options: field.options
};
if (field.type !== "LINKED_RECORD") return base;
const linkedTableId = field.options?.["linkedTableId"];
const linked = typeof linkedTableId === "string" ? linkedDatasource(linkedTableId, datasources) : void 0;
if (linked === void 0 || linked.primaryFieldId === "") return base;
const primary = linked.fields.find((entry) => entry.id === linked.primaryFieldId);
return {
...base,
linked: {
datasourceId: linked.id,
labelFieldId: linked.primaryFieldId,
labelSearchable: primary !== void 0 && SEARCHABLE_TYPES.has(resolvedShape(primary).type)
}
};
}
//#endregion
export { renderDatasourceSchema };