@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
89 lines (87 loc) • 3.03 kB
JavaScript
import { INTERNAL_KIND } from "./filter.js";
//#region src/datasource/fields.ts
function makeFieldSet(aliases) {
const set = {};
for (const alias of aliases) set[alias] = makeChain(alias);
set["$and"] = (...members) => group("AND", members);
set["$or"] = (...members) => group("OR", members);
return set;
}
function makeSortSet(aliases) {
const set = {};
for (const alias of aliases) set[alias] = {
asc: {
sortingField: alias,
sortType: "ASC",
[INTERNAL_KIND]: "ORDER_BY"
},
desc: {
sortingField: alias,
sortType: "DESC",
[INTERNAL_KIND]: "ORDER_BY"
}
};
return set;
}
/**
* Ands the filters of every `where` call into one, which is what an accumulated `where` needs.
*
* One filter goes through as it is. A group that is itself an AND contributes its members to the group
* built here rather than sitting inside it. So the request is no deeper than the filters the caller wrote.
*/
function andAll(filters) {
if (filters.length === 1) return filters[0];
return group("AND", filters.flatMap((filter) => filter[INTERNAL_KIND] === "LOGICAL_GROUP" && filter.logicalOperator === "AND" ? filter.filters : [filter]));
}
function group(logicalOperator, filters) {
return {
logicalOperator,
filters,
[INTERNAL_KIND]: "LOGICAL_GROUP"
};
}
/**
* One chain per alias, carrying every operator whatever the alias reads as. Only the types
* narrow: a call the types reject cannot reach here, so there is nothing for a per-type
* runtime chain to catch. Exported for a filter built on an alias the caller named, where no
* type stands behind the chain.
*/
function makeChain(subject) {
return {
is: (value) => leaf(subject, "IS", value),
isNot: (value) => leaf(subject, "IS_NOT", value),
isEmpty: () => leaf(subject, "IS_EMPTY"),
isNotEmpty: () => leaf(subject, "IS_NOT_EMPTY"),
contains: (value) => leaf(subject, "CONTAINS", value),
startsWith: (value) => leaf(subject, "STARTS_WITH", value),
endsWith: (value) => leaf(subject, "ENDS_WITH", value),
isOneOf: (values) => leaf(subject, "IS_ONE_OF", values),
isNoneOf: (values) => leaf(subject, "IS_NONE_OF", values),
hasAllOf: (values) => leaf(subject, "HAS_ALL_OF", values),
gt: (value) => leaf(subject, "GT", value),
gte: (value) => leaf(subject, "GTE", value),
lt: (value) => leaf(subject, "LT", value),
lte: (value) => leaf(subject, "LTE", value),
between: (start, end) => leaf(subject, "BETWEEN", [start, end]),
isNotBetween: (start, end) => leaf(subject, "IS_NOT_BETWEEN", [start, end])
};
}
function leaf(subject, operator, value) {
const probe = Array.isArray(value) ? value[0] : value;
if (typeof probe === "number") return {
subject,
operator,
value: Array.isArray(value) ? value.map(String) : String(value),
type: "NUMERIC",
[INTERNAL_KIND]: "FILTER"
};
return {
subject,
operator,
value,
type: typeof probe === "boolean" ? "BOOLEAN" : Array.isArray(value) ? "ARRAY" : "TEXT",
[INTERNAL_KIND]: "FILTER"
};
}
//#endregion
export { andAll, makeChain, makeFieldSet, makeSortSet };