@softr/sdk
Version:
The SDK for building Softr apps on TanStack Start. It lets an app interact with Softr: auth, datasources, workflows and integrations.
140 lines (139 loc) • 4.69 kB
TypeScript
import "../auth/user.js";
import { AnyConstructable, ConstructableOrderBy } from "./filter.js";
import { FieldSet, SortSet } from "./fields.js";
import { ActionData, ActionRecord, ActionShape, DatasourceAction, ReadShape } from "../action.js";
//#region src/datasource/record.d.ts
type GetOptions = {
id: string;
};
type WhereCallback<TShape extends ReadShape> = (q: FieldSet<TShape>) => AnyConstructable;
type OrderByCallback<TShape extends ReadShape> = (q: SortSet<ActionRecord<TShape>>) => ConstructableOrderBy | Array<ConstructableOrderBy>;
type ListBuilder<TShape extends ReadShape> = {
/**
* Narrows down which records come back.
*
* @example
* ds.list(posts).where((q) => q.title.contains("cafe"))
*/
where(build: WhereCallback<TShape>): ListBuilder<TShape>;
/**
* Sorts the records.
*
* @example
* ds.list(posts).orderBy((q) => q.publishedAt.asc)
*
* An array can be provided to solve tie-breakers.
*
* @example
* ds.list(posts).orderBy((q) => [q.views.desc, q.publishedAt.asc])
*/
orderBy(build: OrderByCallback<TShape>): ListBuilder<TShape>;
/** How many records to return. Defaults to 20, capped at 100. */
limit(count: number): ListBuilder<TShape>;
/**
* Where the page starts. Pass the `nextOffset` of a previous result, or `null` to start
* over. A result with `nextOffset: null` is the last page.
*
* @example
* const first = await ds.list(posts).limit(50)
* const second = await ds.list(posts).limit(50).offset(first.nextOffset)
*/
offset(offset: number | null): ListBuilder<TShape>;
} & Promise<ListResult<SoftrRecord<ActionRecord<TShape>>>>;
/**
* One record, fetched when awaited. There is no `orderBy`, `limit` or `offset`. The record is
* picked by id, so there is nothing to order or page through.
*
* @example
* const post = await ds.get(postDetail, { id: params.postId })
*/
type GetBuilder<TShape extends ReadShape> = {
/**
* Narrows which record the id may resolve to. A record that does not match the filter
* reads as `null`, the same as one that does not exist.
*
* @example
* const post = await ds
* .get(postDetail, { id: params.postId })
* .where((q) => q.author.is(user.id))
*/
where(build: WhereCallback<TShape>): GetBuilder<TShape>;
} & Promise<SoftrRecord<ActionRecord<TShape>> | null>;
interface SoftrRecord<TFields extends Record<string, unknown> = Record<string, unknown>> {
id: string;
fields: TFields;
}
interface ListResult<TRecord extends SoftrRecord = SoftrRecord> {
records: TRecord[];
total: number;
nextOffset: number | null;
}
type GetRecord<TShape extends ReadShape> = SoftrRecord<ActionRecord<TShape>> | null;
/**
* Queries a list of records from the datasource.
*
* @example
* const { records, total, nextOffset } = await ds
* .list(posts)
* .where((q) => q.publishedAt.lte(today))
* .orderBy((q) => q.publishedAt.desc)
* .limit(50)
*/
declare function listRecords<TShape extends ReadShape>(query: DatasourceAction<TShape>): ListBuilder<TShape>;
/**
* Gets a single record by ID.
*
* @example
* const record = await ds
* .get(posts, { id: "record-id" })
* .where((q) => q.publishedAt.lte(today))
*
* A filter may also be provided to narrow down
*
* @example
* const record = await ds
* .get(posts, { id: "record-id" })
* .where((q) => q.publishedAt.lte(today))
*/
declare function getRecord<TShape extends ReadShape>(query: DatasourceAction<TShape>, options: GetOptions): GetBuilder<TShape>;
/**
* Creates a record and returns it.
*
* @example
* const record = await ds.create(newPost, {
* data: { title: "Cafes", publishedAt: today },
* })
*/
declare function createRecord<TShape extends ActionShape & {
operation: "create";
}>(action: DatasourceAction<TShape>, options: {
data: ActionData<TShape>;
}): Promise<SoftrRecord<ActionRecord<TShape>>>;
/**
* Updates a record by ID and returns it. Only the fields in `data` change.
*
* @example
* const record = await ds.update(editPost, {
* id: "record-id",
* data: { title: "Cafes" },
* })
*/
declare function updateRecord<TShape extends ActionShape & {
operation: "update";
}>(action: DatasourceAction<TShape>, options: {
id: string;
data: ActionData<TShape>;
}): Promise<SoftrRecord<ActionRecord<TShape>>>;
/**
* Deletes a record by ID.
*
* @example
* await ds.delete(removePost, { id: "record-id" })
*/
declare function deleteRecord<TShape extends ActionShape & {
operation: "delete";
}>(action: DatasourceAction<TShape>, options: {
id: string;
}): Promise<void>;
//#endregion
export { GetRecord, WhereCallback, createRecord, deleteRecord, getRecord, listRecords, updateRecord };