Accessing WRD from TypeScript

Schema API

To access data in WRD, import the generated schema. Eg for "example:mydata" you would use

import { mydataSchema } from "wh:wrd/example";

If generated files are missing or stale, run:

wh apply config

Open by tag with wrd(...)

You can also open schemas directly by tag in WebHare 6.0:

import { wrd } from "@webhare/wrd";

const schema = wrd("wrd:testschema");

For unknown/custom tags, provide an explicit type parameter:

import { wrd, type AnySchemaType } from "@webhare/wrd";

const schema = wrd<AnySchemaType>("example:dynamic_schema");

2. Basic CRUD

import { wrd } from "@webhare/wrd";

const schema = wrd("wrd:testschema");

const personId = await schema.insert("wrdPerson", {
    wrdLastName: "Doe",
    wrdContactEmail: "john.doe@beta.webhare.net",
    wrdauthAccountStatus: { status: "active" },
});

await schema.update("wrdPerson", personId, {
    wrdContactEmail: "john.doe.updated@beta.webhare.net",
});

const person = await schema.getFields("wrdPerson", personId, ["wrdContactEmail", "wrdLastName"]);

await schema.delete("wrdPerson", personId);

3. Querying and lookup helpers

Query builder

const rows = await schema
    .query("wrdPerson")
    .select({ id: "wrdId", lastName: "wrdLastName" })
    .where("wrdContactEmail", "=", "john.doe.updated@beta.webhare.net")
    .execute();

search(...) and find(...)

const foundById = await schema.search("wrdPerson", "wrdId", 12345);
const foundByFilter = await schema.find("wrdPerson", { wrdId: 12345 });

Exactly-one / at-most-one selection

const id = await schema
    .query("wrdPerson")
    .select("wrdId")
    .where("wrdId", "=", 12345)
    .executeRequireExactlyOne();

4. Type and attribute metadata

const personType = schema.getType("wrdPerson");

const typeExists = await personType.exists();
const attrs = await personType.listAttributes();
const emailAttr = await personType.describeAttribute("wrdContactEmail");

const typeDescription = await schema.describeType("wrdPerson");
const hasType = await schema.hasType("wrdPerson");

Working with custom attributes:

await personType.createAttribute("testDummy", { attributeType: "string" });
const created = await personType.describeAttribute("testDummy");
await personType.deleteAttribute("testDummy");

5. List and open schemas dynamically

import { listSchemas, openSchemaById, type AnySchemaType } from "@webhare/wrd";

const schemas = await listSchemas();
const first = schemas[0];

if (first) {
    const opened = await openSchemaById<AnySchemaType>(first.id);
    if (opened)
        await opened.exists();
}

6. Describe entities

import { describeEntity } from "@webhare/wrd";

const described = await describeEntity(12345);
/* returns:
    {
        wrdTag,
        wrdGuid,
        type,
        typeId,
        schema,
        schemaId
    }
    or null if not found
*/

7. Schema settings helpers

import { getSchemaSettings, updateSchemaSettings, wrd } from "@webhare/wrd";

const schema = wrd("system:usermgmt");

const settings = await getSchemaSettings(schema, ["domainSecret"]);

await updateSchemaSettings(schema, {
    domainSecret: settings.domainSecret,
});

8. Create, extend, and delete schemas

import { createSchema, extendSchema, deleteSchema, wrd, type AnySchemaType } from "@webhare/wrd";

const newSchemaId = await createSchema("example:newschema", {
    title: "Example schema",
    description: "Created from TypeScript",
    initialize: true,
});

const newSchema = wrd<AnySchemaType>("example:newschema");
await newSchema.exists();

await extendSchema("example:newschema", {
    schemaDefinitionXML: `
        <wrdschema>
            <types>
                <type tag="exampleType" title="Example Type" />
            </types>
        </wrdschema>
    `,
});

await