Skip to content

main · v3.2 (unreleased)

The TypeScript workspace

Users · Reference · CurrentMarkdownAgent index

Status: Current. Where this is implemented names the code and the tests.

The TypeScript workspace runs a workspace in a browser or in Node.js. It works with the database through the JSON API of a server, as the Remote adapter of .NET does, and follows the design that The workspace explains. This page lists its libraries, the code that the generator writes, how a client creates a connection, and where its API differs from the .NET workspace. The platform’s tests run it against the Core test server with Core’s test domain; the sample application has no TypeScript client.

typescript/modules is an nx workspace. A library in libs is imported by its path:

Import pathHolds
@allors/system/common/protocol-jsonThe requests and the responses of the JSON API, as TypeScript types.
@allors/system/workspace/metaThe interfaces of the metadata, such as MetaPopulation, Class and RoleType.
@allors/system/workspace/meta-jsonLazyMetaPopulation, which builds the metadata from the data that the generator writes.
@allors/system/workspace/domainThe interfaces of the workspace, such as IWorkspace, ISession, IObject and IStrategy, and the pulls, with their extents, predicates and results.
@allors/system/workspace/adaptersWhat the adapter builds on: the session, the strategy, the records and PrototypeObjectFactory.
@allors/system/workspace/adapters-jsonThe adapter over the JSON API: DatabaseConnection and the interface IDatabaseJsonClient.
@allors/core/workspace/meta, @allors/core/workspace/meta-json, @allors/core/workspace/domainThe generated code of Core’s test domain. The tests import it as @allors/default/workspace/meta, meta-json and domain.
@allors/core/workspace/derivations-customThe workspace rules of Core’s test domain.
@allors/core/workspace/derivationsOnly an import of @allors/system/workspace/derivations, a library that the repository no longer has.

The libraries whose names end in -tests hold the tests. The platform publishes no package of these libraries, and typescript/modules/package.json is private. The platform’s tests compile the source of each library: typescript/modules/tsconfig.base.json maps the import paths of the table to it, and a library that uses the generated code maps them again in its own tsconfig.json, together with the paths @allors/default/workspace/meta, meta-json and domain, which lead to Core’s generated code. The platform has no application with a TypeScript client to test this with; an application maps the same paths to the libraries in its copy of the platform.

The templates are in typescript/modules/templates:

TemplateWrites
workspace.meta.ts.stgThe typed metadata, M, with builders of pulls, results, selects and includes.
workspace.meta.json.ts.stgThe metadata as data, data, which LazyMetaPopulation reads.
workspace.domain.ts.stgAn interface for each class and each interface of the workspace, with a property for each role and association, canRead… for each role with Origin.Database and canWrite… for each of those that is not derived, and canExecute… and a Method for each method.

Core’s generator, in dotnet/Core/Database/Generate, renders them for the workspace Default of Core’s test domain, into libs/core/workspace/meta/src/lib/generated, and the same folder of meta-json and of domain, which Git ignores. The target Generate of the build runs it. The sample’s generator renders the templates of the .NET workspace only. An application that wants the TypeScript workspace adds the three templates to its generator, with folders of its own. The generated domain imports its metadata from @allors/default/workspace/meta, so the application maps that path to its generated metadata, as the platform’s tests map it to Core’s.

When a derived role has an active workspace rule, reading it returns the value the rule computes, even when canRead… is false. Otherwise, a role that the user may not read returns null, or an empty array when it holds several objects. The properties of unit roles and of roles that hold one object are typed with | null.

The adapter calls the server through an object of the application’s that implements IDatabaseJsonClient:

export interface IDatabaseJsonClient {
pull(pullRequest: PullRequest): Promise<PullResponse>;
sync(syncRequest: SyncRequest): Promise<SyncResponse>;
push(pushRequest: PushRequest): Promise<PushResponse>;
invoke(invokeRequest: InvokeRequest): Promise<InvokeResponse>;
access(accessRequest: AccessRequest): Promise<AccessResponse>;
permission(permissionRequest: PermissionRequest): Promise<PermissionResponse>;
}

Each method posts its request as JSON to the endpoint of the same name, such as /allors/pull, with the bearer token of the user, and returns the JSON of the answer. The interface has no sign-in: the client signs in at /allors/Authentication/Token, as Sign in describes, and keeps the token. FetchClient, in the tests, is such a client, with one difference: it signs in at TestAuthentication/Token of the Core test server, which issues a token for an existing user name without checking the password.

The test fixture builds the metadata with new LazyMetaPopulation(data) and then the connection:

let nextId = -1;
const configuration: Configuration = {
name: 'Default',
metaPopulation,
objectFactory: new PrototypeObjectFactory(metaPopulation),
idGenerator: () => nextId--,
rules: ruleBuilder(this.m),
};
return new DatabaseConnection(configuration, this.jsonClient);

idGenerator gives the negative ids of new objects, and rules the workspace rules, which a session turns on with activate. Pass an empty array when there are none: the type declares rules optional, but createWorkspace() throws a TypeError without it. connection.createWorkspace() creates the workspace, and workspace.createSession() a session.

A pull is an object literal, and the kind of an extent or a predicate names its type:

const pull: Pull = {
extent: {
kind: 'Filter',
objectType: m.C1,
predicate: {
kind: 'Equals',
propertyType: m.C1.Name,
value: name,
},
},
};
const result = await session.pull([pull]);
return result.collection<C1>(m.C1)[0];
  • session.pull takes a pull or a list of pulls. result.collection takes a name, a class, an interface, a role type or an association type, ignores the case of a name, and returns an empty list when the result has no such collection. result.object returns one object, by a name or a type, and result.value one value, by a name.
  • session.push() sends the changes, and session.invoke runs a method or a list of methods, with isolated and continueOnError in its options.
  • session.call(procedure, ...pulls) runs a procedure and pulls, as Procedures describes.
  • session.create(m.C1) creates an object, and object.strategy.delete() deletes a new object before its push.
Subject.NETTypeScript
A push or an invoke that reports errorsReturns the result, with HasErrorsThrows a ResultError, whose result holds the errors
A pull to a custom routeCallAsync(args, name), on the Remote adapterNot available
The arguments of a pullNot sent by the Remote adapterSent
The dependencies of the rules that a session activatedNot sentSent with each pull
A text for the tracing of the databaseNonesession.context, sent with each pull, sync, push and invoke
A role that holds an object that is not in the sessionThrowsTakes the object from the connection’s records when another session pulled it; otherwise throws an Error with the message Object is not present in session.
Deleting an objectNot availablestrategy.delete(), for a new object only; an object of the database throws
The roles of a derivation errorEach has a relation type, and the object when the session holds it; for a new object the object is nullroles reads each relation as a pair, r[0] and r[1], while the JSON API sends an object with i and r, so each role has no object and no relation type
  • The libraries are in typescript/modules/libs, the templates in typescript/modules/templates, and the generator that renders them for Core in dotnet/Core/Database/Generate/Program.cs.
  • The session of the adapter is typescript/modules/libs/system/workspace/adapters-json/src/lib/session/session.ts, which throws the ResultError of a push or an invoke. The roles of a derivation error are read in adapters-json/src/lib/database/derivation-error.ts, and a pull is turned into JSON in adapters-json/src/lib/json/to-json.ts.
  • typescript/modules/libs/system/workspace/adapters-json-tests runs the tests against the Core test server, with FetchClient and the fixture in its src/lib; Build and test says how to run them. The libraries system/workspace/adapters-tests, domain-tests, meta-tests and meta-json-tests run without a server. No test reads the roles of a derivation error.