Convergence
Install
npm install --save convergence
First a little bit of dream code.
A practical use case says more than a 1000 words!
Lets declare an employee model, this gateway will define a typed schema and return a collection of functions to operate on the underlying store such as find, findOne, update, insert, upsert.
const types defineModel engines = ; const store = {};const engine = engines;const collection = 'employees'; const EmployeeModel = { const definition = id: typesUUID name: typesSTRING lastName: typesSTRING job: typesJSON ; return ;};
That´s it, now we can call the model methods as we are used to.
const EmployeeModel = ; EmployeeModel ; EmployeeModel ;
The main feature of this Table Gateway lib is that it allows you to change the underlying
database engine seamlessly. Check this implementation.
models/employee.js
const types defineModel = ; const collection = 'employees'; const EmployeeModel = { const definition = id: typesUUID name: typesSTRING lastName: typesSTRING job: typesJSON ; return ;};
models/users.js
const types defineModel = ; const collection = 'users'; const UserModel = { const definition = id: typesUUID username: typesSTRING email: typesSTRING hashedPassword: typesSTRING salt: typesJSON ; return ;};
Of course we don´t want to be declaring models everywhere, so we can do so in a file dedicated to it.
models/index.js
let models; const getModels = { if models return models; const UserModel = engine; const EmployeeModel = engine; models = UserModel EmployeeModel; return models;}; moduleexports = getModels;
We use the closure of this file to cache the models, so they will just be created once.