Models

Data model forms the basic foundation that drives rest of the application. Model represent the main application entity and are defined using one or more fields. Models can also be associated with one or more other models.

Models:

  • Represent the entities of your application domain
  • Map to the tables in your database
  • Form the foundation of the queries available in the generated Prisma code and are used to generate route views.

Model

Prisma schema

Model definition is used to generate Prisma schema, schema.prisma file which can be found in prisma folder. Generated definition includes

  • datasource, defaults to sqlite, but can be changed to any database that Prisma supports
  • model definition with field and attributes

Server Model/Data Layer

For every data model, a model.server.ts file is generated that includes Prisma queries for CRUD operations.

seekList

seekList is used to get list of items using cursor based efficient navigation. Includes support for advanced dynamic search and sort, and based on definition, can include related columns as well.

getList

Similar to seekList, getList is used to get list of items using skip/offset navigation. Includes support for advanced dynamic search and sort, and based on definition, can include related columns as well.

👉 For small tables, both seekList and getList will have no noticeable performance difference, however for larger tables, seekList ends up being more performant compared to getList. Find out more @ Seek vs Offset

getById

get_model_ById function is used to retrieve model by primary key. It is tuned to be efficient to get item and does not include related fields or child items, but can be included if required.

create

create is used to create item in database. For routes that have integrated child items, code will also create child items using Prisma relation to create atomic (transaction based) model.

update

update is used to update item in database. For routes that have integrated child items, code will also create, update and delete child items.

delete

delete is used to delete item from database. For routes that have integrated child items, prisma $transaction functionality is used to delete child items with main model.

Common Model

In addition to server model, generated codebase also includes common model file with following:

  • getNewModel = returns empty model
  • getModelFromForm = get typed model from FormData
  • getModelFromFormList = get list of typed models from FormData
  • validateModel = common validation logic

Process

Visual Editor allows you to create and manage your data models. A model has a unique name and set of fields that define the model properties. It can also include fields from other models use for reference and relations.

Create Model

To create a new model, start with Visual Editor for the app and select Model tab. Model tab shows list of models, selecting a model from list will show model details.

Click Add button on the Model list view. This will open up Add Model dialog. Enter Model Name and then Tab out. This will auto populate tableName. A primary key with pattern modelNameId is also added to fields list.

Add desired fields by entering Field Name. You can also change type of field:

  • Text
  • Number
  • Boolean
  • Date

Check Is Required to ensure that field is always filled.

If you need to set a default value, click Edit and in Edit Field dialog, expand Advanced Properties and set Default Value. Default Value will be reflected in generated Prisma schema and/or in Route Actions based on type of default value. If this is an always read only field, Check No Update and No Insert flags.

💡

When entering columns, just use Enter/Return key to process to next row. You can also navigate quickly with Up and Down keys

Add Relation

Relation are used to link one model to another and represent a JOIN in database. Relations are defined on descendent or child and connect to ancestor or parent model.

To define a relation, click Add Relation. This will open Select Model To Join To dialog. Select desired model from picker. This will auto add new field to the model, used to represent join field, also called Foreign Key field, and is based on Selected Models primary key.

If the join field is always required, Check Is Required. From SQL perspective, Is Required true => INNER JOIN and Is Required False => LEFT JOIN

Use Related Field to display a user friendly value in place of Foreign Key Field. RemixFast will try to infer You can change it any field from related model to your liking!

Audit Fields

Audit fields are normally used to track who created the record and when, as well as who last updated and when it was updated. Click Add Audit Fields to auto add following:

  • Created by = Default/Auto Populated with User creating record
  • Created date = Default/Auto Populated with date when record was created
  • Updated by = Default/Auto Populated with last User updating record
  • Updated date = Default/Auto Populated with date when record was last updated

Please note that above audit fields are enforced in route actions on server side and hence can not be changed from client input.

Include Field From Relation

Once the model is saved, you can include additional fields from the related models. Click Include Field From Relation. This will show Add Related Field dialog. Select Related Model and Related Field.

If you try to add related field from a child model/table, you will be prompted to select an aggregate function to aggregate related child rows.

Create View

Views are like virtual model, you can assemble them from existing model using Drag-n-drop Visual Query Builder. Add aggregate functions and set default sort.

To create a new model, start with Visual Editor for the app and select Model tab. Click drop down next to Add button and select Add View from the menu. This will open Build View dialog.

Select a model from which you will like to derive the view. Once select, model appears on right side model panel. Drag desired field from selected model and add it to Select panel. To aggregate data, add the same field from model and select Aggregate Function. Once you have added an aggregate field, please select Group By fields, as well as select Sort By fields. Without selecting Group By fields, view will not working properly!

💡 View gets translated into a virtual model with selected fields and to a Prisma groupBy query!

Pre-defined Models

RemixFast includes following pre-defined models for security functionality (RBAC)

  • User = Users of the application.
  • Password = Contains hash of user password
  • Role = Unique combination of rights that define what assignee can do.
  • UserRole = Used to keep track of roles assigned to a User.
  • Right = Application rights, includes all Models and Workflow Actions
  • RoleRight = Keeps track of rights assigned to a role.