@palmares/databases
Version:
Add support for working with databases with palmares framework
112 lines • 6.19 kB
TypeScript
import type { FoundMigrationsFileType, OriginalOrStateModelsByNameType, StateModelsType } from './types';
import type { DatabaseAdapter } from '../engine';
import type { InitializedModelsType } from '../types';
/**
* The state is used to keep track how the models were for every migration file. On the migration files
* we know how the model was at a specific moment in time. The original models defined by the user in the
* `models.ts` file will keep track of the state of the models at the current time, it's like a "picture"
* of the models right now.
*
* When creating a new migration file automatically we need to know what the state were, and how it will be.
* How it were we can get from the previous migrations, how it will be we can get from the "picture" of how it
* is right now.
*
* This is what the state is for. States keeps track of the state of the models at a specific moment in time based
* on the migration files.
*/
export declare class State {
modelsByName: StateModelsType;
initializedModelsByName: OriginalOrStateModelsByNameType;
stateNumber: number;
private constructor();
/**
* Gets the model instance by a given model name. If the model does not exist
* we will create a new model.
*
* @param modelName - The name of the model to get the model instance for.
*
* @returns - Returns a model instance with all of the fields and options.
*/
get(modelName: string): Promise<StateModelsType[string]>;
/**
* Sets a new model in the state. Probably this is not needed because we are changing the values in place.
* But since problems might occur we will keep it.
*
* @param modelName - The name of the model to set.
* @param modifiedModel - The modified model instance to set.
*/
set(modelName: string, modifiedModel: StateModelsType[string]): Promise<void>;
/**
* Creates a new model with a different name than the original one. Instead of it being `${nameOfTheModel}`
* it becomes `State${nameOfTheModel}` so, like `Users` will be `StateUsers`, this because the state will
* clash with the original ones when being initialized.
*
* After that we add this to the `modelsByModelName` object with the ORIGINAL model name. And then call `.get()`
* to retrieve the model to the user.
*
* @param modelName - The name of the model that is being created.
*
* @return - Returns the newly created model. We use the .get method because if we have any side effects to the
* model we will also run them.
*/
newModel(modelName: string): Promise<StateModelsType[string]>;
/**
* Removes a specific model from the state.
*
* @param modelName - The name of the model to remove.
*/
remove(modelName: string): Promise<void>;
/**
* This method is used to initialized the models created inside the state, so what we do is that
* first we recreate the state of the database using this model and after that we translate all of the
* models to something that the engine instance is able to understand and interpret.
*
* @param engineInstance - The engine instance to use to initialize the models.
*/
initializeStateModels(engineInstance: DatabaseAdapter): Promise<InitializedModelsType[]>;
/**
* Retrieves all of the models that were initialized in the state by it's original name.
* By default we will duplicate the engine instance so we can initialize the models in the engine
* instance without worrying if the names will crash with one another.
*
* For example: if we have a `StateUsers` model, we will return `Users` as the original name.
*
* Generally this should be used to send the models when running the migration files. We also send
* a function called closeEngineInstance so we are able to close the connection to the database
* after we are done with the migration.
*
* @param engineInstance - The engine instance that is being used, so we are able to duplicate it,
* and close the connection afterwards.
*
* @return - Returns an object with the models that were initialized in the state and the engine
* instance.
*/
geInitializedModelsByName(engineInstance: DatabaseAdapter): Promise<{
initializedModels: OriginalOrStateModelsByNameType;
closeEngineInstance: ((_engine: DatabaseAdapter) => Promise<void>) | undefined;
}>;
/**
* The factory method that should be called to create a new instance of the state.
* The state is used to keep track how the models were for every migration file. On the migration files
* we know how the model was at a specific moment in time. The original models defined by the user in the
* `models.ts` file will keep track of the state of the models at the current time, it's like a "picture"
* of the models right now.
*
* When creating a new migration file automatically we need to know what the state were, and how it will be.
* How it were we can get from the previous migrations, how it will be we can get from the "picture" of how it
* is right now.
*
* @param foundMigrations - All of the migration files that were found for a specific engine instance.
* @param untilMigration - Until what migration file we want to retrieve the state?
* @param untilOperationIndex - Until what migration operation index we want to retrieve the state? For example
* we can run the migration until the migration file `002_auto_create_users_table.ts` but on this migration we can
* have multiple operations. This enables us to run the migration until the operation index `1` which is the
* second operation in the migration file. This granularity is used for recreating the state when running the
* migrations.
*
* @return - Returns the state instance that was created by traversing each migration file and operation in order.
*
*/
static buildState(foundMigrations: FoundMigrationsFileType[], untilMigration?: string, untilOperationIndex?: number): Promise<State>;
}
//# sourceMappingURL=state.d.ts.map