npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typeorm-test-helper

v1.0.5

Published

Testing database is a utility library that simplifies the testing process for services that interact with a database using TypeORM. It provides a convenient way to set up and tear down a test database, create the necessary data sources, and access the sto

Downloads

10

Readme

typeorm-test-helper

typeorm-test-helper is a library designed to facilitate the process of managing test databases. It allows users to seamlessly set up, interact with, and tear down databases using TypeORM. This library aims to streamline testing services that interact with databases.

Installation

npm install typeorm-test-helper

Usage

To use the library, import the StorageBuilder class and instantiate it. Pass in your storage service constructor and an array of database configurations:

import { StorageBuilder } from 'typeorm-test-helper';

let storageBuilder = new StorageBuilder<MyStorageService>(
    MyStorageService,
    [
        {config: dbConfig1, databaseType: 'postgres', entities: [MyEntity1, MyEntity2], systemDatabase: 'postgres', migrationsPath: '/path/to/your/migrations'},
        {config: dbConfig2, databaseType: 'mysql', entities: [MyEntity3, MyEntity4], systemDatabase: 'mysql', migrationsPath: '/path/to/your/migrations'}
    ]
);

You can then use your StorageBuilder instance to set up the test database:

await storageBuilder.setup();

To get your storage service instances:

let myStorageServices = storageBuilder.getStorageServices();

When you're finished, tear down the test database:

await storageBuilder.teardown();

Configuration

The StorageBuilder class constructor requires two parameters:

  • storageServiceConstructor: A constructor function for your storage service class. This should be a new instance of your storage service class, which will be initialized with a DataSource and your provided configuration.

  • databaseConfigs: An array of configuration objects for your database connections. Each object should contain the following properties:

    • config: A configuration object for your database connection, containing properties like host, port, username, password.

    • databaseType: A string indicating the type of database being used. Options include 'postgres', 'mysql', 'mariadb', 'sqlite', 'mssql', and 'oracle'.

    • entities: An array of Functions. Each function should represent a entity class that will be used by TypeORM.

    • systemDatabase: A string indicating the system database name.

    • migrationsPath: A string representing the relative or absolute path to your migrations files.

Example of a StorageBuilder instance:

let storageBuilder = new StorageBuilder<MyStorageService>(
    MyStorageService,
    [
        {config: dbConfig1, databaseType: 'postgres', entities: [MyEntity1, MyEntity2], systemDatabase: 'postgres', migrationsPath: '/path/to/your/migrations'},
        {config: dbConfig2, databaseType: 'mysql', entities: [MyEntity3, MyEntity4], systemDatabase: 'mysql', migrationsPath: '/path/to/your/migrations'}
    ]
);

The created StorageBuilder instance can then be used to manage your test databases.

Notes

  • This library can be used with the following databases: 'postgres', 'mysql', 'mariadb', 'sqlite', 'mssql', 'oracle'. It assumes that you have the selected database installed and running on your machine.

  • The library creates and deletes databases as needed for testing. The test database's name is automatically generated to prevent collisions.

  • Make sure to provide the entities used by TypeORM dynamically when using the library.

Contributing

We welcome contributions to improve this library. Feel free to submit issues and pull requests.

Accessing and Using Repositories

After setting up the StorageBuilder, you can access your TypeORM repositories, allowing you to perform operations such as creating, reading, updating, and deleting records.

Here's an example:

// Get the DataSources from the storage builder
const appDataSources = storageBuilder.getAppDataSources();

// Use the DataSources' managers to get your repositories
const userRepository1 = appDataSources[0].manager.getRepository(User);
const userRepository2 = appDataSources[1].manager.getRepository(User);

// Now you can use the repositories to interact with the databases
const newUser1 = userRepository1.create({ /* your user data here */ });
await userRepository1.save(newUser1);

const newUser2 = userRepository2.create({ /* your user data here */ });
await userRepository2.save(newUser2);

The repositories returned by getRepository are instances of TypeORM's Repository class. You can use these to perform a variety of operations on the corresponding tables in the database. You can find more information on how to use repositories in the TypeORM documentation.

Remember to replace User with your own entity class. Ensure that this class is part of the entities array in your database connection options.