Token导航 LogoToken导航TokenDH.com
开发需要联网github未标认证来源可访问许可证需确认审计通过

bknd-define-relationshipbknd 定义关系

Agent Skill

bknd-define-relationship 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

318

周安装

13

GitHub Stars

3

下载量

103
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:bknd-define-relationship(bknd 定义关系)
来源仓库:https://github.com/cameronapak/bknd-skills
仓库路径:skills/bknd-define-relationship
安装命令:
npx skills add https://github.com/cameronapak/bknd-skills --skill bknd-define-relationship
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/cameronapak/bknd-skills --skill bknd-define-relationship

简介

bknd-define-relationship 用于建立实体间关联关系,适合在 Codex、Claude、Cursor、Gemini CLI 中实现一对多、多对多等数据模型连接。

  • 适用场景包括文章与作者归属、标签与帖子关联及分类层级嵌套等典型应用场景。
  • 核心能力是支持 many-to-one、one-to-one、many-to-many 和 self-referencing 四种关系类型。
  • 使用方式推荐先用 UI 模式直观设置,再导出为 Code 配置便于团队协作和版本控制。
  • 定义关系时会自动创建外键约束,删除父实体时需考虑子实体的 cascade 行为设置。

SKILL.md

Define Entity Relationships

Create relationships between entities in Bknd (foreign keys, references, associations).

Prerequisites

  • At least two entities exist (see bknd-create-entity)
  • For code mode: Access to your schema file

Relationship Types

TypeUse CaseExample
Many-to-OneChild belongs to one parentPosts → User (author)
One-to-OneExclusive 1:1 pairingUser → Profile
Many-to-ManyBoth sides have multiplePosts ↔ Tags
Self-ReferencingEntity references itselfCategories → Parent Category

When to Use UI vs Code

Use UI Mode When

  • Quick prototyping
  • Visual learners
  • Non-developers setting up relationships

Use Code Mode When

  • Version control needed
  • Reproducible schema
  • Custom options (mappedBy, connectionTable)
  • Team collaboration

UI Approach

Step 1: Access Data Section

  1. Start server: npx bknd run
  2. Open http://localhost:1337
  3. Navigate to Data section

Step 2: Add Relation Field

  1. Click on the child entity (e.g., posts)
  2. Click + Add Field
  3. Select Relation field type
  4. Choose the target entity (e.g., users)
  5. Select relationship type:

- Many-to-One: Multiple posts can belong to one user - One-to-One: One post has exactly one user - Many-to-Many: Posts can have many tags, tags can have many posts

Step 3: Configure Options

  • Field Name: Name for the foreign key (e.g., author creates author_id)
  • Required: Toggle if relationship is mandatory

Step 4: Save and Sync

  1. Click Save Field
  2. Click Sync Database to apply changes

Code Approach

Relationships are defined in the second argument to em():

const schema = em(
  {
    // Entity definitions (first argument)
  },
  ({ relation, index }, entities) => {
    // Relationship definitions (second argument)
  }
);

Many-to-One

Child belongs to one parent. Most common relationship type.

import { em, entity, text } from "bknd";

const schema = em(
  {
    users: entity("users", { email: text().required() }),
    posts: entity("posts", { title: text().required() }),
  },
  ({ relation }, { users, posts }) => {
    relation(posts).manyToOne(users);
  }
);

Auto-generated: users_id foreign key column on posts table

Custom field name with mappedBy:

({ relation }, { users, posts }) => {
  relation(posts).manyToOne(users, {
    mappedBy: "author",  // Creates author_id instead of users_id
  });
}

One-to-One

Exclusive 1:1 relationship. Each child belongs to exactly one parent.

const schema = em(
  {
    users: entity("users", { email: text().required() }),
    profiles: entity("profiles", { bio: text() }),
  },
  ({ relation }, { users, profiles }) => {
    relation(profiles).oneToOne(users);
  }
);

Note: One-to-one relationships cannot use $set operator (maintains exclusivity).

Many-to-Many

Both entities can have multiple of the other. Junction table created automatically.

const schema = em(
  {
    posts: entity("posts", { title: text().required() }),
    tags: entity("tags", { name: text().required() }),
  },
  ({ relation }, { posts, tags }) => {
    relation(posts).manyToMany(tags);
  }
);

Auto-generated: posts_tags junction table with posts_id and tags_id columns

Custom junction table name:

({ relation }, { posts, tags }) => {
  relation(posts).manyToMany(tags, {
    connectionTable: "post_tags",  // Custom junction table name
  });
}

Extra fields on junction table:

({ relation }, { users, courses }) => {
  relation(users).manyToMany(courses, {
    connectionTable: "enrollments",
  }, {
    // Extra fields on junction table
    enrolled_at: date(),
    completed: boolean(),
    grade: number(),
  });
}

Self-Referencing

Entity references itself. Common for hierarchies (categories, comments, org charts).

const schema = em(
  {
    categories: entity("categories", { name: text().required() }),
  },
  ({ relation }, { categories }) => {
    relation(categories).manyToOne(categories, {
      mappedBy: "parent",      // FK field: parent_id
      inversedBy: "children",  // Reverse navigation
    });
  }
);

Usage:

  • category.parent_id → Points to parent category
  • Query children: api.data.readMany("categories", {where: {parent_id: 5}})

Alternative: Direct Foreign Key

Instead of relation(), use .references() on a number field:

const schema = em({
  users: entity("users", { email: text().required() }),
  posts: entity("posts", {
    title: text().required(),
    author_id: number().references("users.id"),
  }),
});

Difference: .references() is simpler but doesn't create inverse navigation or support many-to-many.

Relation Options

ManyToOne / OneToOne Options

OptionTypeDefaultDescription
mappedBystringTarget entity nameFK field name (e.g., authorauthor_id)
inversedBystringSource entity nameReverse navigation name
requiredbooleanfalseRelationship is mandatory

ManyToMany Options

OptionTypeDefaultDescription
connectionTablestring{source}_{target}Junction table name

Querying Relations

Load Related Data (with)

const api = app.getApi();

// Load posts with their author
const posts = await api.data.readMany("posts", {
  with: {
    users: { select: ["email", "name"] },
  },
});
// Result: [{ id: 1, title: "...", users: { email: "...", name: "..." } }]

Filter by Relation

// Posts by specific author
const posts = await api.data.readMany("posts", {
  where: { author_id: 5 },
});

// Using join for complex filters
const posts = await api.data.readMany("posts", {
  join: {
    users: { where: { email: "john@example.com" } },
  },
});

Many-to-Many Operations

// Attach tags to post
await api.data.updateOne("posts", 1, {
  tags: { $attach: [1, 2, 3] },  // Tag IDs
});

// Detach tags
await api.data.updateOne("posts", 1, {
  tags: { $detach: [2] },
});

// Replace all tags
await api.data.updateOne("posts", 1, {
  tags: { $set: [4, 5] },
});

Many-to-One Operations

// Set author on post
await api.data.updateOne("posts", 1, {
  users: { $set: 5 },  // User ID
});

Common Patterns

Blog with Authors and Tags

const schema = em(
  {
    users: entity("users", {
      email: text().required().unique(),
      name: text(),
    }),
    posts: entity("posts", {
      title: text().required(),
      content: text(),
      published: boolean(),
    }),
    tags: entity("tags", {
      name: text().required().unique(),
    }),
  },
  ({ relation }, { users, posts, tags }) => {
    // Post has one author
    relation(posts).manyToOne(users, { mappedBy: "author" });

    // Posts have many tags
    relation(posts).manyToMany(tags);
  }
);

E-commerce Orders

const schema = em(
  {
    customers: entity("customers", { email: text().required() }),
    orders: entity("orders", { total: number() }),
    products: entity("products", { name: text().required(), price: number() }),
  },
  ({ relation }, { customers, orders, products }) => {
    // Order belongs to customer
    relation(orders).manyToOne(customers);

    // Order has many products (with quantity)
    relation(orders).manyToMany(products, {
      connectionTable: "order_items",
    }, {
      quantity: number().required(),
      unit_price: number().required(),
    });
  }
);

Nested Categories

const schema = em(
  {
    categories: entity("categories", {
      name: text().required(),
      slug: text().required().unique(),
    }),
  },
  ({ relation }, { categories }) => {
    relation(categories).manyToOne(categories, {
      mappedBy: "parent",
      inversedBy: "children",
    });
  }
);

// Usage: Get all children of category 5
const children = await api.data.readMany("categories", {
  where: { parent_id: 5 },
});

Common Pitfalls

Entity Not Found

Error: Entity "user" not found

Fix: Entity names are plural by convention. Use users not user.

// Wrong
relation(posts).manyToOne(user);

// Correct
relation(posts).manyToOne(users);

Circular Reference Error

Error: Circular dependency detected

Fix: For self-referencing, use proper options:

// Correct self-reference
relation(categories).manyToOne(categories, {
  mappedBy: "parent",
  inversedBy: "children",
});

Foreign Key Naming Conflict

Error: Field "users_id" already exists

Fix: Use mappedBy to specify a different field name:

// If you already have users_id, use a different name
relation(posts).manyToOne(users, { mappedBy: "author" });  // Creates author_id

Many-to-Many $set on One-to-One

Error: Cannot use $set on one-to-one relation

Fix: One-to-one maintains exclusivity differently. Use $create instead:

// For one-to-one
await api.data.updateOne("users", 1, {
  profiles: { $create: { bio: "Hello" } },
});

Missing Entity in Destructure

Error: Cannot read property 'manyToOne' of undefined

Fix: Ensure entity is destructured from second callback parameter:

// Wrong - missing users in destructure
({ relation }, { posts }) => {
  relation(posts).manyToOne(users);  // users is undefined
}

// Correct
({ relation }, { users, posts }) => {
  relation(posts).manyToOne(users);
}

Relation Changes Not Applying

Problem: Added relation but not seeing FK column.

Fixes:

  1. Restart server (schema syncs on startup)
  2. Verify relation is in second em() argument
  3. Check for syntax errors

Verification

Check Foreign Key Created

npx bknd debug paths
# Look for the FK field in entity output

Test Relation in Code

const api = app.getApi();

// Create parent
const user = await api.data.createOne("users", { email: "test@example.com" });

// Create child with relation
const post = await api.data.createOne("posts", {
  title: "Test Post",
  author_id: user.data.id,
});

// Load with relation
const loaded = await api.data.readOne("posts", post.data.id, {
  with: { users: true },
});
console.log(loaded.data.users);  // { id: 1, email: "test@example.com" }

DOs and DON'Ts

DO:

  • Use plural entity names (users, posts)
  • Use mappedBy for semantic field names (author instead of users)
  • Define relations in the second em() argument
  • Use .references() for simple FK without navigation

DON'T:

  • Use singular entity names in relations
  • Create manual FK fields when using relation() (it creates them automatically)
  • Use $set on one-to-one relations
  • Forget to destructure entities in the callback

Related Skills

  • bknd-create-entity - Create entities before defining relationships
  • bknd-add-field - Add fields including .references() for simple FKs
  • bknd-crud-read - Query related data with with and join
  • bknd-crud-update - Use $attach, $detach, $set for relation updates
  • bknd-query-filter - Advanced filtering on relations

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

39.73%
按下载量换算41

Claude

29.59%
按下载量换算30

Cursor

17.56%
按下载量换算18

Gemini CLI

8.55%
按下载量换算9

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills