Skip to content

@drupal-js-sdk/entity

Overview

Adapter-based entity loader. Register one or more adapters (e.g., JSON:API) and load entities via a single, consistent facade.

Usage

JavaScript
import { Drupal } from "@drupal-js-sdk/core";
import { DrupalEntity } from "@drupal-js-sdk/entity";
import { JsonApiEntityAdapter } from "@drupal-js-sdk/jsonapi";

const drupal = new Drupal({ baseURL: "https://example.com" });
const entities = new DrupalEntity(drupal)
  .registerAdapter("jsonapi", (ctx) => new JsonApiEntityAdapter(ctx))
  .setDefaultAdapter("jsonapi");

// Optionally type attributes in TS; in JS just call load
const loader = entities.entity({ entity: "node", bundle: "article" });
const article = await loader.load("123");

Public API

class EntityLoader<T>

load

load(id: string, options?: EntityLoadOptions): Promise<EntityRecord<T>>
Example
JavaScript
const node = await loader.load("1", { jsonapi: { query: { include: "uid" } } });

class EntityService

Constructor

constructor(drupal: CoreInterface)

registerAdapter

registerAdapter(key: string, factory: EntityAdapterFactory): this
Example
JavaScript
service.registerAdapter("jsonapi", (ctx) => new JsonApiEntityAdapter(ctx));

setDefaultAdapter

setDefaultAdapter(key: string): this
Example
JavaScript
service.setDefaultAdapter("jsonapi");

entity

entity<T>(id: EntityIdentifier, adapterKey?: string): EntityLoader<T>
Example
JavaScript
const loader = service.entity({ entity: "node", bundle: "page" }, "jsonapi");

class DrupalEntity (facade)

Constructor

constructor(drupal: CoreInterface)

registerAdapter

registerAdapter(key: string, factory: EntityAdapterFactory): this

setDefaultAdapter

setDefaultAdapter(key: string): this

entity

entity<T>(id: EntityIdentifier, adapterKey?: string): EntityLoader<T>

Types

  • EntityIdentifier, EntityAttributes, EntityRecord, EntityLoadOptions
  • EntityAdapterContext, EntityAdapter, EntityAdapterFactory

Notes

  • Adapters encapsulate backend specifics (e.g., JSON:API).
  • Uses the HTTP client and config from the provided CoreInterface.