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").
                   where("wrdContactEmail", "=", "").

Or search for and manually retrieve indvidual entities:

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

And to insert/update/delete

const entityid = await mydataSchema.insert("wrdPerson", { "wrdContactEmail": "" });
await mydataSchema.update("wrdPerson", entityid, { "wrdContactEmail": "" });
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": "" });

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.