Database

Learn more about the Database

Introduction

Our Framework has support for a wide range of database systems. This is provided by TypeORM. The DatabaseService is ready to use for your gamemode. Set up your credentials inside the .env and start using the service.

We don't teach you the interaction and creation of databases. If you have any questions about database basics, you should take a look at the docs.

Before you start

We chose TypeORM because the syntax is relatively easy for everyone to understand. But we also have to point out that some things in TypeORM are not really cleanly solved. In the following we would like to point out what you have to pay attention to when you work with TypeORM.

  • By nature, TypeORM creates a circular dependency when setting up a bi-directional relationship between 2 models. In ES modules circular dependencies are allowed. The compiler will warn you about this, but as long as this warning is limited to your entities, you don't need to worry.

  • It can happen that you get the following error message when starting the server XXX cannot access before initialization This is also a known problem "admittedly also a very annoying one" but also we can't work around this at the moment, because we have no influence on when which entity is registered in the config by the decorator. If you get this error, then go into the model in concern and make your import a string import. You will lose the type safety but this is the only way to get the problem under control.

Example for String Imports

/**
 * The TypeORM Way describe in docs
 */
@AutoAdd()
@Entity('firsts')
export class FirstEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(() => Second, second => second.first)
  seconds: Second[]
}

/**
 * Our Way not described in TypeORM Docs
 */
@AutoAdd()
@Entity('firsts')
export class FirstEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(() => Second, second => second.first)
  seconds: 'Second'[]
}

Another solution that works but also does not feel perfect

You can create an entry point file for your models and define the exports in the right order. I feels not really good, but you don't lose the Typings. We don't know how complicated it is with more and more models. But try and error can solve the problem with the right order.

You can see a working example on the picture below. Keep in mind, if you change the order inside index.ts they would not work anymore

If you known a better TypeScript Based ORM, let us know about it. We searching for a better alternative to solve this problems.

How to use

Using the DatabaseService is fairly simple. Create your entities, add the new @AutoAdd decorator and use your entity as normal.

@AutoAdd()
@Entity('yours')
export class YourEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;
}

@AutoAdd

This is a new decorator to provide a simple way for adding new Entities to your database service. Only add this to your TypeORM Entity and use this entity as expected. This decorator registered the entity for you.

@AutoAdd()
@Entity('yours')
export class YourEntity {

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;
}

Last updated