Intro
In a fast-paced development environment, every second counts. One of the biggest hurdles developers face is the time-consuming setup of database configurations for testing and rapid prototyping. ExpressoTS offers a feature to address this pain point: In-Memory DB.
What is In-Memory DB?
In-Memory DB is not just another feature; it's a complete development environment provider. Designed to be easily injectable into the ExpressoTS dependency injection system as a singleton, it offers an efficient, in-memory database for developers.
The In-Memory DB class manages data in tables, represented by key-value pairs. Here, the key is the table name, and the value is an array of entities. This simple yet effective system allows developers to simulate a database entirely in-memory. This is invaluable for quick prototyping, testing, and other scenarios where setting up a full-fledged database might be overkill.
Ease of Debugging with DB Inspector
Testing and debugging are vital steps in application development. The DB Inspector feature elevates the InMemoryDB class by allowing tables to be printed directly to the console using console.table. Whenever any endpoint performs operations that read or write to the database, the table content is automatically printed, providing real-time insights into your data manipulations.
Note: The DB inspector is enabled by default, offering you immediate insights right from the first use.
Class Structure and Methods
Under the hood, InMemoryDB is built to be as straightforward as possible.
@provideSingleton(InMemoryDB)
class InMemoryDB {
private tables: Record<string, IEntity[]> = {};
// Method definitions ...
}
This decorator ensures that In Memory DB is a singleton, thus maintaining data consistency throughout the application's lifecycle.
In Memory DB offers the following key methods:
Retrieves or initializes a table by its name.
getTable(tableName: string): IEntity[]:
Prints the list of all existing tables.
showTables(): void;
Prints all records in a specific table for debugging purposes.
printTable(tableName: string): void:
Extending BaseRepository
Integration into your existing development workflow is as simple as extending the BaseRepository class. For instance, implementing a custom repository for managing User entities looks like this:
@provide(UserRepository)
class UserRepository extends BaseRepository<User> {
constructor() {
super("users");
}
// Custom methods, such as findByEmail, can also be implemented
}
The Road Ahead
Looking into the future, we plan to provide BaseRepository pattern for multiple types of databases, and all of it will be easily scaffoldable through our CLI. So stay tuned for more exciting updates!
Conclusion
In-Memory DB is more than just a feature; it's a leap towards smarter, more efficient development. By providing an out-of-the-box CRUD and Repository Pattern, along with In-Memory DB, ExpressoTS not only speeds up the development process but also serves as a real-time guide, showing how things are done even before you consult the official documentation.
Happy coding!
ExpressoTS is an MIT-licensed open source project. Your support is invaluable; contributions are always welcome!
Top comments (0)