@tailor-platform/sdk 1.11.0 → 1.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/dist/application-DM4zTgXU.mjs +4 -0
- package/dist/{application-BKBo5tGD.mjs → application-DnWZVbDO.mjs} +164 -26
- package/dist/application-DnWZVbDO.mjs.map +1 -0
- package/dist/cli/index.mjs +13 -19
- package/dist/cli/index.mjs.map +1 -1
- package/dist/cli/lib.d.mts +149 -17
- package/dist/cli/lib.mjs +132 -4
- package/dist/cli/lib.mjs.map +1 -1
- package/dist/configure/index.d.mts +4 -3
- package/dist/configure/index.mjs +16 -757
- package/dist/configure/index.mjs.map +1 -1
- package/dist/env-4RO7szrH.d.mts +66 -0
- package/dist/{index-D8zIUsWi.d.mts → index-BBr_q3vB.d.mts} +20 -11
- package/dist/{index-DcOTucF6.d.mts → index-Bid18Opo.d.mts} +473 -84
- package/dist/{jiti-ygK9KoRA.mjs → jiti-DuCiUfMj.mjs} +2 -2
- package/dist/{jiti-ygK9KoRA.mjs.map → jiti-DuCiUfMj.mjs.map} +1 -1
- package/dist/{job-l-pIR9IY.mjs → job-zGAXCidt.mjs} +1 -1
- package/dist/{job-l-pIR9IY.mjs.map → job-zGAXCidt.mjs.map} +1 -1
- package/dist/kysely/index.d.mts +25 -0
- package/dist/kysely/index.mjs +27 -0
- package/dist/kysely/index.mjs.map +1 -0
- package/dist/plugin/index.d.mts +105 -0
- package/dist/plugin/index.mjs +45 -0
- package/dist/plugin/index.mjs.map +1 -0
- package/dist/schema-BmKdDzr1.mjs +784 -0
- package/dist/schema-BmKdDzr1.mjs.map +1 -0
- package/dist/{src-CG8kJBI9.mjs → src-QNTCsO6J.mjs} +2 -2
- package/dist/{src-CG8kJBI9.mjs.map → src-QNTCsO6J.mjs.map} +1 -1
- package/dist/types-r-ZratAg.mjs +13 -0
- package/dist/types-r-ZratAg.mjs.map +1 -0
- package/dist/{update-D0muqqOP.mjs → update-B_W-UQnS.mjs} +1626 -390
- package/dist/update-B_W-UQnS.mjs.map +1 -0
- package/dist/utils/test/index.d.mts +2 -2
- package/dist/utils/test/index.mjs +1 -1
- package/docs/cli/tailordb.md +0 -12
- package/docs/generator/builtin.md +2 -2
- package/docs/plugin/custom.md +389 -0
- package/docs/plugin/index.md +112 -0
- package/docs/services/workflow.md +28 -0
- package/package.json +16 -23
- package/dist/application-BKBo5tGD.mjs.map +0 -1
- package/dist/application-a12-7TT3.mjs +0 -4
- package/dist/update-D0muqqOP.mjs.map +0 -1
- /package/dist/{chunk-CIV_ash9.mjs → chunk-C3Kl5s5P.mjs} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"job-
|
|
1
|
+
{"version":3,"file":"job-zGAXCidt.mjs","names":[],"sources":["../src/configure/services/workflow/job.ts"],"sourcesContent":["import type { TailorEnv } from \"@/configure/types/env\";\nimport type { JsonCompatible } from \"@/configure/types/helpers\";\nimport type { Jsonifiable, Jsonify, JsonPrimitive } from \"type-fest\";\n\n/**\n * Context object passed as the second argument to workflow job body functions.\n */\nexport type WorkflowJobContext = {\n env: TailorEnv;\n};\n\n/**\n * Allowed output types for workflow job body functions.\n * Includes Jsonifiable (JSON-serializable values including objects with toJSON like Date),\n * undefined, and void.\n */\nexport type WorkflowJobOutput = Jsonifiable | undefined | void;\n\n/**\n * Convert output type to what trigger returns after JSON serialization.\n * - Jsonifiable values are converted via Jsonify (Date -> string, etc.)\n * - undefined remains undefined\n * - void becomes void\n */\ntype JsonifyOutput<T> = T extends Jsonifiable ? Jsonify<T> : T;\n\n/**\n * Input type constraint for workflow jobs.\n * Accepts any type that is JSON-compatible (primitives, arrays, objects with JSON-compatible values).\n * Excludes objects with toJSON method (like Date) since they won't be serialized in input.\n */\nexport type WorkflowJobInput = undefined | JsonCompatible<unknown>;\n\n/**\n * WorkflowJob represents a job that can be triggered in a workflow.\n *\n * Type constraints:\n * - Input: Must be JSON-compatible (no Date/toJSON objects) or undefined. Interfaces are allowed.\n * - Output: Must be Jsonifiable, undefined, or void\n * - Trigger returns Jsonify<Output> (Date becomes string after JSON.stringify)\n */\nexport interface WorkflowJob<Name extends string = string, Input = undefined, Output = undefined> {\n name: Name;\n /**\n * Trigger this job with the given input.\n * At runtime, this is a placeholder that calls the body function.\n * During bundling, calls to .trigger() are transformed to\n * tailor.workflow.triggerJobFunction(\"<job-name>\", args).\n *\n * Returns Jsonify<Output> because the value passes through JSON.stringify.\n */\n trigger: [Input] extends [undefined]\n ? () => Promise<JsonifyOutput<Awaited<Output>>>\n : (input: Input) => Promise<JsonifyOutput<Awaited<Output>>>;\n body: (input: Input, context: WorkflowJobContext) => Output | Promise<Output>;\n}\n\n/**\n * Helper type to check if all property types are valid.\n * Uses -? to remove optional modifiers so all properties are treated uniformly.\n */\ntype AllPropertiesValid<T> = {\n [K in keyof T]-?: IsValidInput<T[K]> extends true ? true : false;\n}[keyof T] extends true\n ? true\n : false;\n\n/**\n * Check if a type contains any non-JSON-compatible values.\n * Returns `true` if the type is valid for input, `false` otherwise.\n *\n * Accepts:\n * - JSON primitives (string, number, boolean, null)\n * - undefined\n * - Optional primitives (e.g., string | undefined)\n * - Arrays of valid types\n * - Objects with valid field types\n *\n * Rejects:\n * - Objects with toJSON methods (like Date)\n * - Other non-JSON-serializable types\n */\ntype IsValidInput<T> = T extends undefined\n ? true\n : T extends JsonPrimitive\n ? true\n : T extends readonly (infer U)[]\n ? IsValidInput<U>\n : T extends object\n ? T extends { toJSON: () => unknown }\n ? false\n : AllPropertiesValid<T>\n : false;\n\n/**\n * Helper type to check if all property types are valid for output.\n * Uses -? to remove optional modifiers so all properties are treated uniformly.\n */\ntype AllPropertiesValidOutput<T> = {\n [K in keyof T]-?: IsValidOutput<T[K]> extends true ? true : false;\n}[keyof T] extends true\n ? true\n : false;\n\n/**\n * Check if a type is valid for output.\n * Returns `true` if the type is valid, `false` otherwise.\n *\n * Accepts:\n * - JSON primitives (string, number, boolean, null)\n * - undefined and void\n * - Optional primitives (e.g., string | undefined)\n * - Jsonifiable types (Date, objects with toJSON)\n * - Arrays of valid types\n * - Objects with valid field types\n */\ntype IsValidOutput<T> = T extends undefined | void\n ? true\n : T extends JsonPrimitive\n ? true\n : T extends readonly (infer U)[]\n ? IsValidOutput<U>\n : T extends object\n ? AllPropertiesValidOutput<T>\n : false;\n\n/**\n * Body function type with conditional constraint.\n * If input contains invalid types (like Date), the body type becomes `never` to cause an error.\n */\ntype WorkflowJobBody<I, O> =\n IsValidInput<I> extends true\n ? IsValidOutput<O> extends true\n ? (input: I, context: WorkflowJobContext) => O | Promise<O>\n : never\n : never;\n\n/**\n * Environment variable key for workflow testing.\n * Contains JSON-serialized TailorEnv object.\n */\nexport const WORKFLOW_TEST_ENV_KEY = \"TAILOR_TEST_WORKFLOW_ENV\";\n\nexport const createWorkflowJob = <const Name extends string, I = undefined, O = undefined>(config: {\n readonly name: Name;\n readonly body: WorkflowJobBody<I, O>;\n}): WorkflowJob<Name, I, Awaited<O>> => {\n return {\n name: config.name,\n // JSON.parse(JSON.stringify(...)) ensures the return value matches Jsonify<Output> type.\n // This converts Date objects to strings, matching actual runtime behavior.\n // In production, bundler transforms .trigger() calls to tailor.workflow.triggerJobFunction().\n trigger: async (args?: unknown) => {\n const env: TailorEnv = JSON.parse(process.env[WORKFLOW_TEST_ENV_KEY] || \"{}\");\n const result = await config.body(args as I, { env });\n return result ? JSON.parse(JSON.stringify(result)) : result;\n },\n body: config.body,\n } as WorkflowJob<Name, I, Awaited<O>>;\n};\n"],"mappings":";;;;;AA6IA,MAAa,wBAAwB;AAErC,MAAa,qBAA8E,WAGnD;AACtC,QAAO;EACL,MAAM,OAAO;EAIb,SAAS,OAAO,SAAmB;GACjC,MAAM,MAAiB,KAAK,MAAM,QAAQ,IAAI,0BAA0B,KAAK;GAC7E,MAAM,SAAS,MAAM,OAAO,KAAK,MAAW,EAAE,KAAK,CAAC;AACpD,UAAO,SAAS,KAAK,MAAM,KAAK,UAAU,OAAO,CAAC,GAAG;;EAEvD,MAAM,OAAO;EACd"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/// <reference path="./../user-defined.d.ts" />
|
|
2
|
+
import { TailordbDialect } from "@tailor-platform/function-kysely-tailordb";
|
|
3
|
+
import { ColumnType, ColumnType as ColumnType$1, Insertable, Insertable as Insertable$1, Kysely, Kysely as Kysely$1, KyselyConfig, KyselyConfig as KyselyConfig$1, Selectable, Selectable as Selectable$1, Transaction, Transaction as Transaction$1, Updateable, Updateable as Updateable$1 } from "kysely";
|
|
4
|
+
|
|
5
|
+
//#region src/kysely/index.d.ts
|
|
6
|
+
|
|
7
|
+
type Timestamp = ColumnType$1<Date, Date | string, Date | string>;
|
|
8
|
+
type Generated<T> = T extends ColumnType$1<infer S, infer I, infer U> ? ColumnType$1<S, I | undefined, U> : ColumnType$1<T, T | undefined, T>;
|
|
9
|
+
type Serial<T = string | number> = ColumnType$1<T, never, never>;
|
|
10
|
+
type TailordbKysely<DB$1> = Kysely$1<DB$1>;
|
|
11
|
+
type NamespaceDB<NS, N$1 extends keyof NS = keyof NS> = TailordbKysely<NS[N$1]>;
|
|
12
|
+
/**
|
|
13
|
+
* Create a namespace-aware getDB function for generated code.
|
|
14
|
+
* @returns A getDB function that creates Kysely instances for specific namespaces
|
|
15
|
+
*/
|
|
16
|
+
declare function createGetDB<NS>(): <const N$1 extends keyof NS & string>(namespace: N$1, config?: Omit<KyselyConfig$1, "dialect">) => TailordbKysely<NS[N$1]>;
|
|
17
|
+
type NamespaceTransaction<NS, K extends keyof NS | TailordbKysely<NS[keyof NS]> = keyof NS> = K extends TailordbKysely<infer DB> ? Transaction$1<DB> : K extends keyof NS ? Transaction$1<NS[K]> : never;
|
|
18
|
+
type NamespaceTableName<NS> = { [N in keyof NS]: keyof NS[N] }[keyof NS];
|
|
19
|
+
type NamespaceTable<NS, T extends NamespaceTableName<NS>> = { [N in keyof NS]: T extends keyof NS[N] ? NS[N][T] : never }[keyof NS];
|
|
20
|
+
type NamespaceInsertable<NS, T extends NamespaceTableName<NS>> = Insertable$1<NamespaceTable<NS, T>>;
|
|
21
|
+
type NamespaceSelectable<NS, T extends NamespaceTableName<NS>> = Selectable$1<NamespaceTable<NS, T>>;
|
|
22
|
+
type NamespaceUpdateable<NS, T extends NamespaceTableName<NS>> = Updateable$1<NamespaceTable<NS, T>>;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { type ColumnType, Generated, type Insertable, Kysely, type KyselyConfig, NamespaceDB, NamespaceInsertable, NamespaceSelectable, NamespaceTable, NamespaceTableName, NamespaceTransaction, NamespaceUpdateable, type Selectable, Serial, TailordbDialect, TailordbKysely, Timestamp, type Transaction, type Updateable, createGetDB };
|
|
25
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TailordbDialect, TailordbDialect as TailordbDialect$1 } from "@tailor-platform/function-kysely-tailordb";
|
|
2
|
+
import { Kysely, Kysely as Kysely$1 } from "kysely";
|
|
3
|
+
|
|
4
|
+
//#region src/kysely/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Kysely integration module for generated TailorDB code.
|
|
7
|
+
*
|
|
8
|
+
* Re-exports kysely and function-kysely-tailordb types through a single import path
|
|
9
|
+
* to avoid phantom dependency issues with pnpm, and provides namespace-aware
|
|
10
|
+
* utility types and factory functions used by the code generator.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Create a namespace-aware getDB function for generated code.
|
|
14
|
+
* @returns A getDB function that creates Kysely instances for specific namespaces
|
|
15
|
+
*/
|
|
16
|
+
function createGetDB() {
|
|
17
|
+
return function getDB(namespace, config) {
|
|
18
|
+
return new Kysely$1({
|
|
19
|
+
dialect: new TailordbDialect$1(new tailordb.Client({ namespace })),
|
|
20
|
+
...config
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { Kysely, TailordbDialect, createGetDB };
|
|
27
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["Kysely","TailordbDialect"],"sources":["../../src/kysely/index.ts"],"sourcesContent":["/**\n * Kysely integration module for generated TailorDB code.\n *\n * Re-exports kysely and function-kysely-tailordb types through a single import path\n * to avoid phantom dependency issues with pnpm, and provides namespace-aware\n * utility types and factory functions used by the code generator.\n */\n\nimport { TailordbDialect } from \"@tailor-platform/function-kysely-tailordb\";\nimport {\n type ColumnType,\n Kysely,\n type Insertable,\n type KyselyConfig,\n type Selectable,\n type Transaction as KyselyTransaction,\n type Updateable,\n} from \"kysely\";\n\nexport {\n type ColumnType,\n Kysely,\n type KyselyConfig,\n type Transaction,\n type Insertable,\n type Selectable,\n type Updateable,\n} from \"kysely\";\n\nexport { TailordbDialect } from \"@tailor-platform/function-kysely-tailordb\";\n\nexport type Timestamp = ColumnType<Date, Date | string, Date | string>;\nexport type Generated<T> =\n T extends ColumnType<infer S, infer I, infer U>\n ? ColumnType<S, I | undefined, U>\n : ColumnType<T, T | undefined, T>;\nexport type Serial<T = string | number> = ColumnType<T, never, never>;\n\nexport type TailordbKysely<DB> = Kysely<DB>;\nexport type NamespaceDB<NS, N extends keyof NS = keyof NS> = TailordbKysely<NS[N]>;\n\n/**\n * Create a namespace-aware getDB function for generated code.\n * @returns A getDB function that creates Kysely instances for specific namespaces\n */\nexport function createGetDB<NS>() {\n return function getDB<const N extends keyof NS & string>(\n namespace: N,\n config?: Omit<KyselyConfig, \"dialect\">,\n ): TailordbKysely<NS[N]> {\n const client = new tailordb.Client({ namespace });\n return new Kysely<NS[N]>({\n dialect: new TailordbDialect(client),\n ...config,\n });\n };\n}\n\nexport type NamespaceTransaction<NS, K extends keyof NS | TailordbKysely<NS[keyof NS]> = keyof NS> =\n K extends TailordbKysely<infer DB>\n ? KyselyTransaction<DB>\n : K extends keyof NS\n ? KyselyTransaction<NS[K]>\n : never;\n\nexport type NamespaceTableName<NS> = {\n [N in keyof NS]: keyof NS[N];\n}[keyof NS];\n\nexport type NamespaceTable<NS, T extends NamespaceTableName<NS>> = {\n [N in keyof NS]: T extends keyof NS[N] ? NS[N][T] : never;\n}[keyof NS];\n\nexport type NamespaceInsertable<NS, T extends NamespaceTableName<NS>> = Insertable<\n NamespaceTable<NS, T>\n>;\nexport type NamespaceSelectable<NS, T extends NamespaceTableName<NS>> = Selectable<\n NamespaceTable<NS, T>\n>;\nexport type NamespaceUpdateable<NS, T extends NamespaceTableName<NS>> = Updateable<\n NamespaceTable<NS, T>\n>;\n"],"mappings":";;;;;;;;;;;;;;;AA6CA,SAAgB,cAAkB;AAChC,QAAO,SAAS,MACd,WACA,QACuB;AAEvB,SAAO,IAAIA,SAAc;GACvB,SAAS,IAAIC,kBAFA,IAAI,SAAS,OAAO,EAAE,WAAW,CAAC,CAEX;GACpC,GAAG;GACJ,CAAC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/// <reference path="./../user-defined.d.ts" />
|
|
2
|
+
import { n as TailorEnv, r as TailorActor } from "../env-4RO7szrH.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/plugin/with-context.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Plugin executor factory function type.
|
|
8
|
+
* Takes context and returns an executor configuration.
|
|
9
|
+
* Returns unknown since the exact return type depends on createExecutor's generic params.
|
|
10
|
+
*/
|
|
11
|
+
type PluginExecutorFactory<Ctx> = (ctx: Ctx) => unknown;
|
|
12
|
+
/**
|
|
13
|
+
* Base args for plugin executor function operations.
|
|
14
|
+
* Provides typed access to runtime context without requiring specific record types.
|
|
15
|
+
*/
|
|
16
|
+
interface PluginFunctionArgs {
|
|
17
|
+
/** Workspace ID where the executor runs */
|
|
18
|
+
workspaceId: string;
|
|
19
|
+
/** Application namespace */
|
|
20
|
+
appNamespace: string;
|
|
21
|
+
/** Environment variables */
|
|
22
|
+
env: TailorEnv;
|
|
23
|
+
/** Actor (user) who triggered the event, null for system events */
|
|
24
|
+
actor: TailorActor | null;
|
|
25
|
+
/** Name of the TailorDB type */
|
|
26
|
+
typeName: string;
|
|
27
|
+
/** TailorDB connections by namespace */
|
|
28
|
+
tailordb: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Args for plugin executors triggered on record creation.
|
|
32
|
+
*/
|
|
33
|
+
interface PluginRecordCreatedArgs extends PluginFunctionArgs {
|
|
34
|
+
/** The newly created record */
|
|
35
|
+
newRecord: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Args for plugin executors triggered on record update.
|
|
39
|
+
*/
|
|
40
|
+
interface PluginRecordUpdatedArgs extends PluginFunctionArgs {
|
|
41
|
+
/** The record after update */
|
|
42
|
+
newRecord: Record<string, unknown>;
|
|
43
|
+
/** The record before update */
|
|
44
|
+
oldRecord: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Args for plugin executors triggered on record deletion.
|
|
48
|
+
*/
|
|
49
|
+
interface PluginRecordDeletedArgs extends PluginFunctionArgs {
|
|
50
|
+
/** The deleted record */
|
|
51
|
+
oldRecord: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Database schema type for plugins.
|
|
55
|
+
* Since plugins work with dynamic types, the schema uses Record types.
|
|
56
|
+
*/
|
|
57
|
+
type PluginDBSchema = Record<string, Record<string, unknown>>;
|
|
58
|
+
/**
|
|
59
|
+
* Base record type for TailorDB records.
|
|
60
|
+
* All records have an id field.
|
|
61
|
+
*/
|
|
62
|
+
type PluginRecord = {
|
|
63
|
+
id: string;
|
|
64
|
+
} & Record<string, unknown>;
|
|
65
|
+
/**
|
|
66
|
+
* Define a plugin executor that receives context at runtime.
|
|
67
|
+
* This allows executor definitions to be in separate files while
|
|
68
|
+
* still receiving dynamic values like typeName, generated types, and namespace.
|
|
69
|
+
* @param factory - Function that takes context and returns executor configuration
|
|
70
|
+
* @returns The same factory function (for type inference)
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* // executors/on-create.ts
|
|
74
|
+
* import { withPluginContext } from "@tailor-platform/sdk/plugin";
|
|
75
|
+
* import { createExecutor, recordCreatedTrigger } from "@tailor-platform/sdk";
|
|
76
|
+
* import { getDB } from "@tailor-platform/function-kysely-tailordb";
|
|
77
|
+
*
|
|
78
|
+
* interface MyContext {
|
|
79
|
+
* sourceType: TailorAnyDBType;
|
|
80
|
+
* historyType: TailorAnyDBType;
|
|
81
|
+
* namespace: string;
|
|
82
|
+
* }
|
|
83
|
+
*
|
|
84
|
+
* export default withPluginContext<MyContext>((ctx) =>
|
|
85
|
+
* createExecutor({
|
|
86
|
+
* name: `${ctx.sourceType.name.toLowerCase()}-on-create`,
|
|
87
|
+
* trigger: recordCreatedTrigger({ type: ctx.sourceType }),
|
|
88
|
+
* operation: {
|
|
89
|
+
* kind: "function",
|
|
90
|
+
* body: async (args) => {
|
|
91
|
+
* const db = getDB(ctx.namespace);
|
|
92
|
+
* await db.insertInto(ctx.historyType.name).values({
|
|
93
|
+
* recordId: args.newRecord.id,
|
|
94
|
+
* // ...
|
|
95
|
+
* }).execute();
|
|
96
|
+
* },
|
|
97
|
+
* },
|
|
98
|
+
* })
|
|
99
|
+
* );
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
declare function withPluginContext<Ctx>(factory: PluginExecutorFactory<Ctx>): PluginExecutorFactory<Ctx>;
|
|
103
|
+
//#endregion
|
|
104
|
+
export { type PluginDBSchema, type PluginExecutorFactory, type PluginFunctionArgs, type PluginRecord, type PluginRecordCreatedArgs, type PluginRecordDeletedArgs, type PluginRecordUpdatedArgs, withPluginContext };
|
|
105
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/plugin/with-context.ts
|
|
2
|
+
/**
|
|
3
|
+
* Define a plugin executor that receives context at runtime.
|
|
4
|
+
* This allows executor definitions to be in separate files while
|
|
5
|
+
* still receiving dynamic values like typeName, generated types, and namespace.
|
|
6
|
+
* @param factory - Function that takes context and returns executor configuration
|
|
7
|
+
* @returns The same factory function (for type inference)
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // executors/on-create.ts
|
|
11
|
+
* import { withPluginContext } from "@tailor-platform/sdk/plugin";
|
|
12
|
+
* import { createExecutor, recordCreatedTrigger } from "@tailor-platform/sdk";
|
|
13
|
+
* import { getDB } from "@tailor-platform/function-kysely-tailordb";
|
|
14
|
+
*
|
|
15
|
+
* interface MyContext {
|
|
16
|
+
* sourceType: TailorAnyDBType;
|
|
17
|
+
* historyType: TailorAnyDBType;
|
|
18
|
+
* namespace: string;
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* export default withPluginContext<MyContext>((ctx) =>
|
|
22
|
+
* createExecutor({
|
|
23
|
+
* name: `${ctx.sourceType.name.toLowerCase()}-on-create`,
|
|
24
|
+
* trigger: recordCreatedTrigger({ type: ctx.sourceType }),
|
|
25
|
+
* operation: {
|
|
26
|
+
* kind: "function",
|
|
27
|
+
* body: async (args) => {
|
|
28
|
+
* const db = getDB(ctx.namespace);
|
|
29
|
+
* await db.insertInto(ctx.historyType.name).values({
|
|
30
|
+
* recordId: args.newRecord.id,
|
|
31
|
+
* // ...
|
|
32
|
+
* }).execute();
|
|
33
|
+
* },
|
|
34
|
+
* },
|
|
35
|
+
* })
|
|
36
|
+
* );
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function withPluginContext(factory) {
|
|
40
|
+
return factory;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { withPluginContext };
|
|
45
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/plugin/with-context.ts"],"sourcesContent":["/**\n * Plugin executor context support for defining plugin executors in separate files.\n * This module provides utilities for creating type-safe plugin executors that receive\n * context (like type references and namespace) at runtime.\n */\n\nimport type { TailorActor, TailorEnv } from \"@/parser/types\";\n\n/**\n * Plugin executor factory function type.\n * Takes context and returns an executor configuration.\n * Returns unknown since the exact return type depends on createExecutor's generic params.\n */\nexport type PluginExecutorFactory<Ctx> = (ctx: Ctx) => unknown;\n\n// ============================================================================\n// Plugin Executor Args Types\n// ============================================================================\n\n/**\n * Base args for plugin executor function operations.\n * Provides typed access to runtime context without requiring specific record types.\n */\nexport interface PluginFunctionArgs {\n /** Workspace ID where the executor runs */\n workspaceId: string;\n /** Application namespace */\n appNamespace: string;\n /** Environment variables */\n env: TailorEnv;\n /** Actor (user) who triggered the event, null for system events */\n actor: TailorActor | null;\n /** Name of the TailorDB type */\n typeName: string;\n /** TailorDB connections by namespace */\n tailordb: Record<string, unknown>;\n}\n\n/**\n * Args for plugin executors triggered on record creation.\n */\nexport interface PluginRecordCreatedArgs extends PluginFunctionArgs {\n /** The newly created record */\n newRecord: Record<string, unknown>;\n}\n\n/**\n * Args for plugin executors triggered on record update.\n */\nexport interface PluginRecordUpdatedArgs extends PluginFunctionArgs {\n /** The record after update */\n newRecord: Record<string, unknown>;\n /** The record before update */\n oldRecord: Record<string, unknown>;\n}\n\n/**\n * Args for plugin executors triggered on record deletion.\n */\nexport interface PluginRecordDeletedArgs extends PluginFunctionArgs {\n /** The deleted record */\n oldRecord: Record<string, unknown>;\n}\n\n/**\n * Database schema type for plugins.\n * Since plugins work with dynamic types, the schema uses Record types.\n */\nexport type PluginDBSchema = Record<string, Record<string, unknown>>;\n\n/**\n * Base record type for TailorDB records.\n * All records have an id field.\n */\nexport type PluginRecord = { id: string } & Record<string, unknown>;\n\n/**\n * Define a plugin executor that receives context at runtime.\n * This allows executor definitions to be in separate files while\n * still receiving dynamic values like typeName, generated types, and namespace.\n * @param factory - Function that takes context and returns executor configuration\n * @returns The same factory function (for type inference)\n * @example\n * ```typescript\n * // executors/on-create.ts\n * import { withPluginContext } from \"@tailor-platform/sdk/plugin\";\n * import { createExecutor, recordCreatedTrigger } from \"@tailor-platform/sdk\";\n * import { getDB } from \"@tailor-platform/function-kysely-tailordb\";\n *\n * interface MyContext {\n * sourceType: TailorAnyDBType;\n * historyType: TailorAnyDBType;\n * namespace: string;\n * }\n *\n * export default withPluginContext<MyContext>((ctx) =>\n * createExecutor({\n * name: `${ctx.sourceType.name.toLowerCase()}-on-create`,\n * trigger: recordCreatedTrigger({ type: ctx.sourceType }),\n * operation: {\n * kind: \"function\",\n * body: async (args) => {\n * const db = getDB(ctx.namespace);\n * await db.insertInto(ctx.historyType.name).values({\n * recordId: args.newRecord.id,\n * // ...\n * }).execute();\n * },\n * },\n * })\n * );\n * ```\n */\nexport function withPluginContext<Ctx>(\n factory: PluginExecutorFactory<Ctx>,\n): PluginExecutorFactory<Ctx> {\n return factory;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiHA,SAAgB,kBACd,SAC4B;AAC5B,QAAO"}
|