Provider
A class responsible for managing the communications with a data source.
It is recommended to use an existing Provider for your use case. This guide is for implementing a data source that doesn't currently have a provider.
Definition
- Library: tramway-core-connection
- File: https://github.com/tramwayjs/connection/blob/master/src/core/Provider.js
Create
tramway create:provider MySQLProvider --add-dependency-injection --key provider.mysql
Locations
- Implementation:
src/providers
- Dependency Injection:
src/config/services/providers.js
API
Function | Usage |
---|---|
constructor() | Handles database configuration |
getOne(id: any, collection: string) | Returns item from the connection |
getMany(ids: any[], collection: string) | Passes an array of items for an array of ids. |
find(conditions: string/Object, collection: string) | Returns an array of items for a query on specific conditions. This may be done by object or query string depending on your implementation |
has(id: any, collection: string) | Checks if item exists |
hasThese(ids : any[], collection: string) | Checks if a set of items exists |
count(conditions: any, collection: string) | Gets a count of items that meet the conditions. |
create(item: Entity, collection: string): Entity | Creates an object in the database from an Entity or standard object |
update(id: any, item: Entity, collection: string): Entity | Updates the item found with the given id |
delete(id: any, collection: string) | Removes an item from the datastore and returns it |
deleteMany(ids : any[], collection: string) | Removes items from the datastore and returns them |
query(query: string/Object, values: Object, collection: string) | Meant as an override based on your datastore because we can't always rely on simple CRUD |
createCollection(collection: string) | A method encompassing the steps to create a collection programatically |
To create a provider, extend the class.
import { Provider } from 'tramway-core-connection';
Example
import {Provider} from 'tramway-core-connection';import mysql from 'mysql';export default class MySQLProvider extends Provider {/**** @param {Object} params Configurable params as per https://github.com/mysqljs/mysql#connection-options*/constructor(params = {}) {super();this.params = params;this.connect(params);this.connection.on('error', err => {if ('ECONNRESET' === err.code) {this.connect(params);} else {throw err;}});}connect(params) {if (!params) {params = this.params;}const {database, username, password, dialect, host, ...rest} = params;this.connection = mysql.createConnection({host,user: username,password,database,...rest,});}closeConnection() {this.connection.end();}/*** @param {number|string} id* @param {string} tableName* @returns {Promise<Object>}** @memberOf Provider*/async getOne(id, tableName) {const template = `SELECT * FROM ?? WHERE id = ?`;let query = mysql.format(template, [tableName, id]);let results = await this.execute(query);const [result] = results;return result;}/*** Recommended to use other functions first.* @param {string} query* @param {[] | Object} values** @memberOf Provider*/async query(query, values) {query = mysql.format(query, values);return await this.execute(query);}async execute(query) {return new Promise((resolve, reject) => {return this.connection.query(query, (error, results, fields) => {if (error) {reject(error);}return resolve(results);});});}}
Dependency Injection
import { MySQLProvider } from '../../providers';export default {"provider.mysql": {"class": MySQLProvider,"constructor": [{"type": "parameter", "key": "mysql"},]}}