Adapters - Hookyns/unimapperjs GitHub Wiki

Adapters

Adapter do a lot of hard work. UniMapper create high level api but it's on Adapters to take and modify input from UniMapper and do what it wants. There are basic adapters which do just CRUD operations and advanced adapters for SQL databases (or some else dbs with solid/static structure) which have interface for migrations.

Base Interface

class SomeAdapter {

	/**
	 * Return true if database type need migrations (basically all SQL databases need it)
	 * @type {Boolean}
	 */
	static get needMigrations();

	/**
	 * @param {Object | String} connInfo Connection info which was given to $um.createDomain()
	 */
	constructor(connInfo)

	/**
	 * Return connection to database. Ideally from pool.
	 * @returns {*}
	 */
	async getConnection();

	/**
	 * Start transaction
	 * @param connection In which connection it should be created
	 */
	async startTransaction(connection);

	/**
	 * Rollback changes
	 * @param connection In which connection it should be rolled back
	 */
	async rollback(connection);

	/**
	 * Commit changes
	 * @param connection In which connection it should be commited
	 */
	async commit(connection);

	/**
	 * Insert new record
	 * @param {Entity} entity Instance of Entity
	 * @param {Object} data All entity fields { fieldname: value, ... }
	 * @param [connection] Connection is received if it's called in transaction
	 * @returns {Entity}
	 */
	async insert(entity, data, connection);

	/**
	 * Update record
	 * @param {{Function<Entity>} } entity Entity class
	 * @param {Object} data Only modified data; { fieldname; value, ... }
	 * @param {Object} [where]
	 * @param [connection] Connection is received if it's called in transaction
	 */
	async update(entity, data, where = {}, connection);

	/**
	 * Remove record
	 * @param {Function<Entity>} entity Entity class
	 * @param {Object} [where]
	 * @param [connection] Connection is received if it's called in transaction
	 */
	async remove(entity, where = {}, connection);

	/**
	 * Select records
	 * @param {Function<Entity>} entity Entity class
	 * @param {Array<String>} select List of fields which should be selected
	 * @param [conditions]
	 * @param {Array<{ field: String, order: "ASC" | "DESC"}>} [order]
	 * @param {Number} [limit]
	 * @param {Number} [skip]
	 */
	async select(entity, select = [], conditions, order = [], limit, skip);

	/**
	 * Dispose resources - define what you want..
	 * Basically end connection, pool etc.
	 */
	async dispose();

With Migrations

	/**
	 * Return list with entity names
	 * @returns {Array}
	 */
	async getListOfEntities();catch (e) {
			console.error(e.stack);
		}
	}

	/**
	 * Return entity structure with described fields
	 * @param tableName
	 * @returns {Object<{type: String, nullable: Boolean, length: Number, decimals: Number, primary: Boolean, unique: Boolean, autoIncrement: Boolean}>}
	 */
	async getEntityStructure(tableName);

	/**
	 * Create new not existing entity
	 * @param {String} name
	 * @param {Object<{type: String, nullable: Boolean, length: Number, decimals: Number, primary: Boolean, unique: Boolean, autoIncrement: Boolean}>} fields
	 */
	async createEntity(name, fields);

	/**
	 * Remove existing entity from database
	 * @param {String} entityName
	 */
	async removeEntity(entityName);

	/**
	 *  Add new field to entity
	 * @param {String} entityName
	 * @param {String} fieldName
	 * @param {{type: String, nullable: Boolean, length: Number, decimals: Number, primary: Boolean, unique: Boolean, autoIncrement: Boolean}} description
	 */
	async addField(entityName, fieldName, description);

	/**
	 * Remove field from entity
	 * @param {String} entityName
	 * @param {String} fieldName
	 */
	async removeField(entityName, fieldName);

	/**
	 * Cange existing field in entity
	 * @param {String} entityName
	 * @param {String} fieldName
	 * @param {{type: String, nullable: Boolean, length: Number, decimals: Number, primary: Boolean, unique: Boolean, autoIncrement: Boolean}} description
	 */
	async changeField(entityName, fieldName, description);

Hidden Things

List of hidden features (properties or methods) which should be important in adapter.

Entity

  • static getDescription() - return entity structure with fields and their types
  • entity.__properties Object with entity data
  • entity.__changedProperties Object with data which was changed
⚠️ **GitHub.com Fallback** ⚠️