@laigma/mock-db
v1.0.3
Published
Base de datos en memoria para tests funcionales
Downloads
405
Maintainers
Readme
MockDB
MockDB
genera una base de datos en memoria para uso en tests
Instalación
npm install @laigma/mock-db
Uso
Inicialización
Crea un directorio que contenga tus archivos JSON. Cada archivo JSON debe contener un objeto que se cargará en la base de datos en memoria. El nombre del archivo (sin la extensión .json) será la clave de este objeto en la base de datos.
// ejemplo de json/file1.json
{
"key1": "value1",
"key2": "value2"
}
- Inicializa MockDB en tu proyecto. Asegúrate de pasar la ruta absoluta del directorio JSON al método init de FakeDb.
// src/index.ts
import MockDB from "@laigma/mock-db";
import path from "path";
// Obtener la ruta absoluta del directorio JSON
const jsonDirectory = path.resolve(__dirname, "../json");
const mockDb = new MockDB();
mockDb.init(jsonDirectory); // Inicializar la base de datos con el directorio JSON
console.log(mockDb.getDb()); // Imprimir la base de datos en la consola
Métodos Disponibles
- getDb() Devuelve una copia de la base de datos en memoria.
const dbCopy = mockDb.getDb();
console.log(dbCopy);
- setData(key: string, value: unknown) Establece un valor en la base de datos en memoria.
mockDb.setData("newKey", { some: "data" });
- getData(key: string) Obtiene un valor de la base de datos en memoria por su clave.
const value = mockDb.getData("newKey");
console.log(value);
Contribuciones
Las contribuciones son bienvenidas. Por favor, abre un issue o envía un pull request.
Licencia
Este proyecto está bajo la Licencia ISC. Consulta el archivo LICENSE para más detalles.