The AOC Framework
github logodiscord logo

GETTING STARTED

Database #

In the default application (created with aoc-cli), you will find a folder called sql with a file named 0001-your-application-name-bootstrap.sql.

The Initial Sql File #

This file contains the creation of the public.meta table, which includes columns for creation user, creation date, modification user, and modification date.

This table is intended to be inherited by all tables in the database. These columns are managed internally by the framework.

The file also creates the users.aoc_user table, with fields for username, email, password, and full name. This table can be modified as long as the changes are reflected in the framework configuration.

The field mapping is done in app.module.ts on the client:

  AocAppModule.forRoot(
    Config,
    new AocUserConfig(AocUser, { fieldMap: { username: 'username', password: 'pass' }}),
    ...

and on the server, in the user-definition.ts file:

@AocUserDefinition(new AocUserConfig(AocUser, {
  fieldMap: {
    username: 'username',
    password: 'pass'
  }
}))
class UserDefinition {}

Apply Sql Files To The Database #

As you create your application, you will need to create more tables and relationships. You can add new files like 0002-whatever.sql, 0003-another-table.sql. In each file, you can add or modify tables, relationships, or insert rows, etc.

With the aoc-cli generate database command, you can execute all .sql files found in that folder. Run the command with --help or refer to the documentation section on aoc-cli.

Obviously, you can also manually execute SQL commands on your server or use any other migration technique you find suitable.

If you want your transactions to enter the application's lifecycle (executing mikro-orm subscribers, for example, and other hooks), we recommend running them through mikro-orm, with entities in memory, etc.

Redo Models And Entities #

Every time the database structure changes, you need to update the TypeScript models and entities to reflect the new structure and be able to use it in your application code.

  • Models: Models are TypeScript files like aoc-user.ts, client.ts, invoice.ts, contact.ts, which are located on the client side. They are comparable to POJO, POCO, etc.
  • Entities: Entities are similar to models but follow mikro-orm, with the necessary decorators and configurations.
  • ModelConfigs: These are extended configurations used on the client side.

To redo both models and entities, we will use aoc-cli generate entities. Add --help to see the command help. The necessary file that contains the configuration to correctly generate both models and entities is called aoc-entities-config.json. You will find it at the root of your application, accompanied by a file aoc-entities-secrets.json to specify (or override) more local parameters such as database connection or paths to save models and entities.

The Aoc-entities-config.json File #

The JSON definition schema aoc-entities-config.schema.json provides an explanation of how to structure this file. In the following table, we explain the properties:

Property Description
schemas An array with the names of the schemas you want to process.
entityBaseClass The base class that all other entities will inherit from.
modelBaseClass The base class that all other models will inherit from.
db Database connection parameters.
modelTargetPath Absolute or relative path where models will be saved.
entityTargetPath Absolute or relative path where entities will be saved.
modelConfigTargetPath Absolute or relative path where modelconfigs will be saved.
jsonColumns Array of JSON structures used in database fields.
pivotTables Definitions of NM pivot tables (see mikro-orm)
constEnums TypeScript const enums definitions for columns.
virtualProperties Virtual properties you want to generate in your entities (see mikro-orm)
eager Automatic loading of entities without using populate (see mikro-orm)
lazy Array of lazy loading fields in schema.table.field format
embedTables Array of tables for embeddable classes that will be embedded in entities.
embedMap Object defining mapping between tables and embeddables.
cascades Object defining cascade delete for foreign keys.
threadSafe Array of tables (entities) that will be thread-safe for concurrency in API operations.
inherits Tables that inherit from another table other than the base.

Any of these parameters can be in the aoc-entities-secrets.json file, and they will be applied.

Please note, browse Issues and Discussions in Github for more information

© 2024 Atlantis of Code. All rights reserved.
All trademarks are the property of their respective owners.