import { z, type ZodObject, type ZodRawShape } from "zod"; import { customAlphabet } from "nanoid"; import { zodObjectValidator } from "../utils/zod-utils"; const idCreator = customAlphabet("abcdefghijklmnopqrstuvwxyz0123456789", 16); export const datasourceSystemProperties = z.strictObject({ $id: z .string() .describe("Id") .default(() => idCreator()), $creationDate: z.iso .datetime() .describe("Creation Date") .default(() => new Date().toISOString()), $lastModificationDate: z.iso .datetime() .describe("Last modified") .default(() => new Date().toISOString()), }); export const datasourceSystemIndexes = [ { name: "system_idx_creationDate", fields: ["$creationDate"], }, { name: "system_idx_lastModificationDate", fields: ["$lastModificationDate"], }, ]; export const datasourceInternalManifest = z.strictObject({ id: z .string() .regex(/^[a-z0-9_]+$/) .describe( "Unique identifier of the datasource. Used to reference the datasource in the system. Use a sql-safe string ([a-z0-9_]+).", ), label: z.string().describe("Label of the datasource displayed in the UI"), description: z.string().max(200).optional().describe("Description of the datasource, displayed in the UI"), provider: z.literal("internal").describe("Internal datasource saved locally in Upstart."), hidden: z .boolean() .default(false) .optional() .describe("Whether the datasource is hidden from the UI. Used for some auth-related datasources."), schema: z .custom>(zodObjectValidator) .describe("A Zod object schema, each property representing a field in database."), labelBy: z .string() .optional() .describe( "Field name to use as label when referencing items of this datasource. Used in UI. Should be one of the schema string fields.", ), indexes: z .array( z.object({ name: z.string().describe("Index name"), fields: z.array(z.string()).describe("Fields to index"), unique: z.boolean().optional().describe("Creates a unique index"), fulltext: z.boolean().optional().describe("Creates a fulltext index for search"), }), ) .default([]) .describe( "Indexes to create on the datasource. Use it to enforce uniqueness or improve query performance or when the field is used in a foreign key relationship.", ), foreignKeys: z .array( z .object({ field: z.string().describe("Field in this datasource"), foreignDatasourceId: z.string().describe("Foreign Datasource ID"), foreignField: z.string().describe("Field in the foreign datasource"), }) .describe( "Defines a foreign key relationship between this datasource and another datasource. Always make sure to add indexes on both the source and target fields.", ), ) .default([]) .optional() .describe("Foreign key relationships to other datasources."), }); export type Datasource = z.infer; export type DatasourceItemQueuePayload = { siteId: string; table: string; rowId: string; operation: "INSERT" | "UPDATE" | "DELETE"; row: Record | null; };