Simple yet useful library to avoid manually manipulating event sourcing databases and paradox / tardis events and entities.
The recommended way to install doctor is through npm:
npm i -g @irontitan/doctor
Doctor works based on a configuration file that defines your applications, with its entities and repositories. Besides that, this file provides all the information doctor needs to connect to your database.
The default filename is doctor.config.js
. The CLI will look for this file in the current working directory every time you invoke it, unless you pass the -f
or --file
flag. See global params
This is the minimum structure of a valid doctor configuration file:
const Person = require('path/to/your/event-entity/class')
const PersonRepository = require('path/to/your/event-repository/class')
module.exports = {
mongodb: {
uri: process.env.DATABASE_MONGODB_URI,
dbName: process.env.DATABASE_MONGODB_DBNAME
},
entities: {
person: {
entity: Person,
collection: 'people',
repository: (mongodbConnection) => new PersonRepository(mongodbConnection)
}
}
}
Breaking it down:
- mongodb
- uri:
- Type: String
- Description: Mongodb URI (including connection schema)
- Example:
mongodb://localhost:27017
- Required:
true
- dbName:
- Tyoe: String
- Description: Mongodb database name
- Example:
my-app
- Required:
true
- uri:
- entities
- [entity-name] : String
- entity:
- Type: EventEntity
- Description: Constructor of the entity class
- Required:
true
- collection
- Type: String
- Description: Name of the collection for this entity
- Required:
true
- repository:
- Type: Function
- Description: Function that receives a connection to mongodb and returns a specific EventRepository instance that will be used for all operations for this entity
- example:
- required:
false
- entity:
- [entity-name] : String
During following commands documentation, some concepts will be refered to. They are:
- Entity: The name of an entity, as described on the config file
- Entity id: The unique ID of a document of given entity
- Event: The unique id of an event inside an
events
array, or an event name - Timestamp: A date and, optionally, time, in the format
YYYY-MM-DD.HH:mm
, as momentjs format tokens
All following commands are invoked the same way:
doctor <command> [params]
All commands (except by help
) accept the following params:
-f, --file
: Config file path. Overrides default./doctor.config.js
-f, --verbose
: Enables verbose output
Prints help about commands
doctor help [command]
No params supported
Reduces the events of a given entity and returns its resulting state.
doctor reduce <entity> <entity-id> [--ignore <event>] [--until <timestamp>] [--save]
-i, --ignore
: Ignores given event. Can be repeated to ignore more than one event.-u, --until
: Ignores all events that happened after given timestamp. Takes precedence over--ignore
.-s, --save
: Saves the result to the database. This will ask for your confirmation before making any changes.
All global params are supported
This command goes through you config file an tells you if it's ok and usable by doctor.
doctor check
All global params are supported
This command calculates the time difference (in hours) between the first occurence of an event and the last occurence of another event.
doctor diff <entity> <entity-id> <first-event> <last-event>
All global params are supported
This command lists all entities described by the config file
doctor entities
All global params are supported
This command reduces and updates the states of a whole collection
doctor rebuild <collection>
All global params are supported
- Write tests
- Add ability to reduce multiple entities and even collections at once
- Add ability to use custom script to manipulate entities
- Make mongodb connection not required
- Improve documentation about custom repositories
PRs are, as always, very welcome.
Just follow StandardJS, use pnpm, and make sure you code builds (pnpm run build
) before submitting.