export const upsertNormalizedTags = internalMutation({
args: {
tags: v.array(
v.union(
v.object({
type: v.literal("none"),
}),
v.object({
type: v.literal("existing"),
existingId: v.id("normalized_tags"),
}),
v.object({
type: v.literal("new"),
newTag: v.object({
name: v.string(),
category: v.optional(tagCategoryValidator),
}),
}),
),
),
},
handler: (ctx, args) =>
// ^ I want this to return only the created items and filter out any empty values
Effect.forEach(args.tags, (tag) =>
Effect.gen(function* () {
if (tag.type === "existing") {
// will return error if the tag does not exist
return yield* ctx.db.get(tag.existingId).pipe(Effect.orDie);
}
// will return the new tag ids
if (tag.type === "new") {
return yield* ctx.db
.insert("normalized_tags", {
name: tag.newTag.name,
normalizedName: tag.newTag.name.toLowerCase().trim(),
category: tag.newTag.category,
})
.pipe(Effect.option);
}
// For demonstration purposes
return Option.none();
}),
),
});
export const upsertNormalizedTags = internalMutation({
args: {
tags: v.array(
v.union(
v.object({
type: v.literal("none"),
}),
v.object({
type: v.literal("existing"),
existingId: v.id("normalized_tags"),
}),
v.object({
type: v.literal("new"),
newTag: v.object({
name: v.string(),
category: v.optional(tagCategoryValidator),
}),
}),
),
),
},
handler: (ctx, args) =>
// ^ I want this to return only the created items and filter out any empty values
Effect.forEach(args.tags, (tag) =>
Effect.gen(function* () {
if (tag.type === "existing") {
// will return error if the tag does not exist
return yield* ctx.db.get(tag.existingId).pipe(Effect.orDie);
}
// will return the new tag ids
if (tag.type === "new") {
return yield* ctx.db
.insert("normalized_tags", {
name: tag.newTag.name,
normalizedName: tag.newTag.name.toLowerCase().trim(),
category: tag.newTag.category,
})
.pipe(Effect.option);
}
// For demonstration purposes
return Option.none();
}),
),
});