@thaumaturgy/zod
Version:
Thaumaturgy is a fixtures and seeding library for TypeScript.
60 lines (59 loc) • 2.57 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Realm = void 0;
const core_1 = require("@thaumaturgy/core");
const entity_name_1 = require("./entity-name");
/**
* A realm is an isolated environment that entities may be registered with.
*
* Entity names must be unique within a realm.
*/
class Realm {
constructor() {
this.realm = new core_1.Realm();
/**
* Defines an entity in the realm using the specified manifester and persister.
*/
this.define = (Entity, options) => {
this.realm.define({ C: Entity, name: (0, entity_name_1.extractEntityName)(Entity) }, options);
};
/**
* Manifests an instance of the specified entity.
*
* @param Entity The entity to manifest.
* @param overrides The overrides to pass to the manifester.
*/
this.manifest = (Entity, overrides = {}) => {
return this.realm.manifest({ C: Entity, name: (0, entity_name_1.extractEntityName)(Entity) }, overrides);
};
/**
* Persists an instance of the specified entity.
*
* @param Entity The entity to persist.
* @param context The context to pass to the persister.
* @param overrides The overrides to pass to the persister.
*/
this.persist = (Entity, context, overrides = {}) => __awaiter(this, void 0, void 0, function* () {
return this.realm.persist({ C: Entity, name: (0, entity_name_1.extractEntityName)(Entity) }, context, overrides);
});
this.persistLeaves = (context) => __awaiter(this, void 0, void 0, function* () {
return this.realm.persistLeaves(context);
});
}
/**
* Clears all of the entities within the realm.
*/
clear() {
this.realm.clear();
}
}
exports.Realm = Realm;