Generate Project Code
Use npx gauze project <dir> create entity <project_dir> <entity_file> to validate an entity definition and generate the project code that represents that entity across your application.
Start with Create a Definition if you still need to write the entity definition file itself. Create the database migration first. Read Create a Migration before running this command.
Generate the Entity
After creating the entity definition file and migration, run:
npx gauze project ${project_dir} create entity ${project_dir} ./article.jsThat flow:
- Validates the entity config
- Creates the generated project modules for that entity
- Wires the entity into the project's exports
- Updates the generated GraphQL operation files for that entity
Custom vs Built-in
Use npx gauze project <dir> create entity <project_dir> <entity_file> for project entities. npx gauze create gauze is for working on Gauze itself rather than normal application development.
After Generation
Run migrations, then inspect:
- Inspect
abstract/entities/<entity>.jsfor the source entity definition. - Inspect
structure/entities/<entity>/for the generated structure modules. - Inspect
database/interfaces/graphql/operations/<entity>/for the generated GraphQL operation files.
If the entity should be queryable through the database and system GraphQL realms, register its generated definitions in:
- Update
database/interfaces/graphql/schema.jsto register the database realm definitions. - Update
system/interfaces/graphql/schema.jsto register the system realm definitions.
In both files, add the entity to the realm's ENTITIES and METHODS maps using the generated type constants from structure/entities/<entity>/.
For example, the registration shape is:
import * as $structure from "../structure/index.js";
const ENTITIES = {
article: $structure.entities.article.database.graphql.TYPE__GRAPHQL__DATABASE__ARTICLE__STRUCTURE,
};
const METHODS = {
article: {
create: $structure.entities.article.database.graphql.TYPE__GRAPHQL__DATABASE__ARTICLE__CREATE__STRUCTURE,
read: $structure.entities.article.database.graphql.TYPE__GRAPHQL__DATABASE__ARTICLE__READ__STRUCTURE,
update: $structure.entities.article.database.graphql.TYPE__GRAPHQL__DATABASE__ARTICLE__UPDATE__STRUCTURE,
delete: $structure.entities.article.database.graphql.TYPE__GRAPHQL__DATABASE__ARTICLE__DELETE__STRUCTURE,
count: $structure.entities.article.database.graphql.TYPE__GRAPHQL__DATABASE__ARTICLE__COUNT__STRUCTURE,
},
};Use the matching database.graphql or system.graphql constants for the schema file you are editing.
If the entity relates to other entities, update structure/relationships.js as well. Relationships are defined as a simple map where the key is the "from" GraphQL type constant and the value is an array of "to" GraphQL type constants that it can relate to.
For example:
import { TYPE__GRAPHQL__DATABASE__ARTICLE__STRUCTURE } from "./entities/article/database/graphql.js";
import { TYPE__GRAPHQL__DATABASE__AUTHOR__STRUCTURE } from "./entities/author/database/graphql.js";
import { TYPE__GRAPHQL__DATABASE__TAG__STRUCTURE } from "./entities/tag/database/graphql.js";
const DATABASE_RELATIONSHIPS__RELATIONSHIP__STRUCTURE = {
[TYPE__GRAPHQL__DATABASE__ARTICLE__STRUCTURE]: [
TYPE__GRAPHQL__DATABASE__AUTHOR__STRUCTURE,
TYPE__GRAPHQL__DATABASE__TAG__STRUCTURE,
],
[TYPE__GRAPHQL__DATABASE__AUTHOR__STRUCTURE]: [TYPE__GRAPHQL__DATABASE__ARTICLE__STRUCTURE],
[TYPE__GRAPHQL__DATABASE__TAG__STRUCTURE]: [TYPE__GRAPHQL__DATABASE__ARTICLE__STRUCTURE],
};
const SYSTEM_RELATIONSHIPS__RELATIONSHIP__STRUCTURE = DATABASE_RELATIONSHIPS__RELATIONSHIP__STRUCTURE;
export {
DATABASE_RELATIONSHIPS__RELATIONSHIP__STRUCTURE,
SYSTEM_RELATIONSHIPS__RELATIONSHIP__STRUCTURE,
};In this example, article can relate to author and tag, and those entities can relate back to article. Add or remove entries based on which entity types should be traversable in your generated relationship structure.