1.0.15 ā€¢ Published 3 years ago

@easy-express/typeorm v1.0.15

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

A module that makes the use of TypeORM even easier by dynamically loading all entities inside a given directory before connecting to the database automatically using environment variables.

šŸ  Homepage

Introduction

To start using TypeORM's awesome capabilities, simply attach a DatabaseModule to your EasyExpress server and you're good to go!

Install

npm install @easy-express/typeorm

Setup

First, add the following environment variables (you can use a .env file):

DB_HOST='HOST'
DB_NAME='DATABASE_NAME'
DB_PORT=DATABASE_PORT
DB_USER='DATABASE_USERNAME'
DB_PASSWD='DATABASE_PASSWORD'
#(For dialect, choose one of the following: )
DB_DIALECT= 'mysql' | 'mssql' | 'sqlite' | 'postgres' | 'mariadb' | 'mongodb'

Next, run the generate_entities.sh script to generate the entities from your database using the credentials from your .env file. The entities will be placed into your src directory.

Finally, add the following compiler options to your tsconfig.json:

{
  "compilerOptions": {
    // ...
    "strictPropertyInitialization": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
    // ...
  }
}

Usage

Create a new DatabaseModule and pass it a path to the directory containing all of the entities that were generated by the generate_entities.sh script. Then, attach it to your EasyExpressServer -- the DatabaseModule automatically connects to the database using the environment variables you supplied during the setup process and lets you start using TypeORM right away.

Example

import { EasyExpressServer } from '@easy-express/server';
import { DatabaseModule } from '@easy-express/typeorm';
import * as dotenv from 'dotenv';
import { getRepository } from 'typeorm';

// import entities here...
import { Balances } from './entities/Balances';

// load env vars from .env file
dotenv.config();

// create a new server
let server = new EasyExpressServer();

// define routes for your server

// ...

server.instance.get('/', (req, res) => {
  res.send('Hello World!');
});

// ...

// example of a route getting data from database using TypeORM
server.instance.get('/balances', async (req, res) => {
  // this works because the DatabaseModule establishes a connection to the database when you attach it to the server
  return getRepository(Balances)
    .find()
    .then((result) => {
      res.send(result);
    });
});

// ...

// attach a new database module to the server
server
  .attachModule(new DatabaseModule(__dirname + '/entities/'))
  .then(() => {
    // Start the server after you've attached the database module (this way you wait until the connection to the database is established before starting the server)
    server.start();
  })
  .catch((e) => {
    console.error(e);
    return e;
  });

Author

šŸ‘¤ Leonard Parisi

šŸ¤ Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page. You can also take a look at the contributing guide.

Show your support

Donations are greatly appreciated!

Bitcoin Wallet Address: 3PjDkxNznkx7HVmVdfSMYgGJeeJzoDH7e6

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.9

3 years ago