diff --git a/.changeset/brave-flies-dance.md b/.changeset/brave-flies-dance.md new file mode 100644 index 0000000..93633bb --- /dev/null +++ b/.changeset/brave-flies-dance.md @@ -0,0 +1,5 @@ +--- +"@graphprotocol/grc-20": patch +--- + +Add Graph.deleteEntity to delete entities diff --git a/.claude/settings.local.json b/.claude/settings.local.json index 3439543..56c2309 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -1,5 +1,12 @@ { "permissions": { - "allow": ["Bash(pnpm build:*)", "Bash(pnpm test:*)", "Bash(pnpm lint:*)", "Bash(pnpm lint:fix:*)"] + "allow": [ + "Bash(pnpm build:*)", + "Bash(pnpm test:*)", + "Bash(pnpm lint:*)", + "Bash(pnpm lint:fix:*)", + "Bash(ls:*)", + "Bash(grep:*)" + ] } } diff --git a/src/graph/delete-entity.test.ts b/src/graph/delete-entity.test.ts new file mode 100644 index 0000000..97048d6 --- /dev/null +++ b/src/graph/delete-entity.test.ts @@ -0,0 +1,24 @@ +import { describe, expect, it } from 'vitest'; +import { Id } from '../id.js'; +import { toGrcId } from '../id-utils.js'; +import { deleteEntity } from './delete-entity.js'; + +describe('deleteEntity', () => { + it('should create a delete entity operation with valid ID', () => { + const id = Id('5cade5757ecd41ae83481b22ffc2f94e'); + const result = deleteEntity({ id }); + + expect(result.id).toBe(id); + expect(result.ops).toHaveLength(1); + expect(result.ops[0]).toMatchObject({ + type: 'deleteEntity', + id: toGrcId(id), + }); + }); + + it('should throw an error when ID validation fails', () => { + const id = 'invalid-id'; + + expect(() => deleteEntity({ id })).toThrow('Invalid id: "invalid-id"'); + }); +}); diff --git a/src/graph/delete-entity.ts b/src/graph/delete-entity.ts new file mode 100644 index 0000000..5d6415e --- /dev/null +++ b/src/graph/delete-entity.ts @@ -0,0 +1,21 @@ +import { deleteEntity as grcDeleteEntity } from '@geoprotocol/grc-20'; +import { Id } from '../id.js'; +import { assertValid, toGrcId } from '../id-utils.js'; +import type { CreateResult, DeleteEntityParams } from '../types.js'; + +/** + * Deletes an entity. + * + * @example + * ```ts + * const { ops } = deleteEntity({ id: entityId }); + * ``` + * + * @param params – {@link DeleteEntityParams} + * @returns The operations to delete the entity. + */ +export const deleteEntity = ({ id }: DeleteEntityParams): CreateResult => { + assertValid(id, '`id` in `deleteEntity`'); + + return { id: Id(id), ops: [grcDeleteEntity(toGrcId(id))] }; +}; diff --git a/src/graph/index.ts b/src/graph/index.ts index 580ab4e..f5b6cad 100644 --- a/src/graph/index.ts +++ b/src/graph/index.ts @@ -12,6 +12,7 @@ export * from './create-property.js'; export * from './create-relation.js'; export * from './create-space.js'; export * from './create-type.js'; +export * from './delete-entity.js'; export * from './delete-relation.js'; export * from './update-entity.js'; export * from './update-relation.js';