Skip to content

embed

embed<T>(options): HookFunction<T>

Defined in: embed.ts:60

Returns a hook that embeds the just-written record’s text and upserts it into options.vectors — the auto-embed-on-write hook. Attach to after.create/after.update/after.patch (never after.find/after.get/after.remove — there is no freshly-written record to embed there):

app.service("articles").hooks({
after: {
create: [embed({ vectors: articlesVectorRepo, provider: openaiEmbeddings(), field: ["title", "body"] })],
update: [embed({ vectors: articlesVectorRepo, provider: openaiEmbeddings(), field: ["title", "body"] })],
patch: [embed({ vectors: articlesVectorRepo, provider: openaiEmbeddings(), field: ["title", "body"] })],
},
});

Idempotent by construction: upsertVector(id, vector, data) is an upsert keyed on the source record’s id — re-running this hook for the same id (a retry, or a second edit) replaces the vector at that id rather than creating a duplicate, on every adapter (@mantlejs/pinecone, @mantlejs/qdrant, @mantlejs/mongodb’s MongoVectorRepository, @mantlejs/knex’s KnexVectorRepository). No extra bookkeeping is needed for that guarantee — it comes from the adapter contract, not from this hook.

Non-fatal on failure: an embedding-provider error or a vector-upsert error is caught here and never rethrown — the primary write this hook runs after has already succeeded, and a degraded/unavailable embedding provider must never turn that into a failed request.

T extends Record<string, unknown>

EmbedOptions<T>

HookFunction<T>