0.1.0 • Published 4 years ago

@usportslive/database-models v0.1.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

@usportslive/database-models

This package works as an ORM for a specific database scheme used in the u-sports-live ecosystem. It uses sequelize to design a series of models that communicate with the database and perform queries/mutations.

Getting Started

To install the package you can simply run:

npm install @usportslive/database-models

After that you can start using it by just importing the module

// database.js

const { initialize } = require("@usportslive/database-models");

module.exports = initialize(
  "<database-name>",
  "<user>",
  "<password>",
  {} // Options object
);
// index.js

const {
  sequelize,
  User,
  Role,
  Sport,
  Team,
  Comment
  // ...
} = require('./database.js');

sequelize.authenticate()
  .then(async () => {
    
    const user = await User.findByPk('<user-id>', {
      include: ['role', 'person']
    });

    const teams = await Team.findAnCountAll(/**/);
  });

As you can see, the package returns a sequelize (^5) instance that has all models, associations and hooks already loaded in it.

ParameterDescription
dbThe name of the database
userThe username which is used to authenticate against the database.
passwordThe password which is used to authenticate against the database.
optionsAn object with options (see more)

Test

Test are available although you need to have a mysql database with the credentials used in connection.spec.ts to make it work. You can run it with:

npm test