Accessing WRD in TypeScript

NOTE The actual WRD implementation is still in HareScript. The TypeScript WRD API attempts to hide this as much as possible and requires you to use @webhare/whdb beginWork and commitWork, but you may encounter some visibility issues as the actual database changes will take place in a HareScript transaction running in parallel to your TypeScript transaction.

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 this file doesn't exist or is out of date, run wh update-generated-files

You can then query your schema, eg:

await mydataSchema.selectFrom("wrdPerson").
                   select("wrdId").
                   where("wrdContactEmail", "=", "user@beta.webhare.net").
                   execute();

Or search for and manually retrieve indvidual entities:

const orgid = await mydataSchema.search("wrdOrganization", "orgunitid", orgunitid);
const orgdata = await mydataSchema.getFields("wrdOrganization", orgid, ["wrdOrgName"]);

And to insert/update/delete

const entityid = await mydataSchema.insert("wrdPerson", { "wrdContactEmail": "user@beta.webhare.net" });
await mydataSchema.update("wrdPerson", entityid, { "wrdContactEmail": "user2@beta.webhare.net" });
await mydataSchmea.delete("wrdPerson", entityid);

Dynamic schemas

If you're using dynamic schema metadata (ie. you're not configuring your data model through wrdschema files) you will not be able to rely (completely) on the generated TypeScript files.

To manually open a schema for which you have metadata:

import { MydataSchemaType } from "wh:wrd/example";
const myschema = new WRDSchema<MydataSchemaType>("example:mydata");

const entityid = await myschema.insert("wrdPerson", { "wrdContactEmail": "user@beta.webhare.net" });

If you have no metadata definition or if you don't want ot use TypeScript, you can simply use new WRDSchema without providing a type.