UniMapperJS
UniMapperJS is universal Node.js (native ES6/ES7) LINQ-like object mapper (ORM/ODM) which can map whatever you create adapter for.
!! Under DEVELOPMENT !!
See wiki
Main Features
- Adapters - if your database isn't supported, you can write own adapter,
- migrations - migration scripts - you can check migration script before applying,
- seeding - seed default data into storage,
- more connections across adapters (eg. take data from MongoDB or two MongoDBs and insert it into MySQL),
- unit of work - tracking changes in transactions,
- updating just changed properties - saving resources,
- quering like C# LINQ to Entities (with JS function names)
TODO
- implement Entity.addUnique() which allow UNIQUE key over more fields
- implement Entity.addPrimary() which allow PRIMARY key over more fields
- implement bulk operations
- implement JOINs
- implement nested operations (eg. saving Enterprise should save all Employees in Enterprise.Employees collection too. Same rollbacking etc.)
- add beforeSave() method into entity class - allow some operation before saving
- add baseQuery property into entity class - it'll be used in ALL select queries (can be used for soft-delete)
- implement client-side execution (query from client will send AJAJ request to server which will handle it as normal server query; something like automatic GraphQL)
- add whereOr() and whereOrIf() into Query
Example
this repo.
You can clone and play with the example inDirectory structure of this example
- entities
- migrations
- domain.js
- create-migration.js
- run-migration.js
Create domain
domain.js
const $um = ;const MySqlAdapter = ; // Domain creation - connect to database via MySQL adapterconst domain = $um;exportsdomain = domain;
Create entities
Then create entities in given domain. In TypeScript you can declare entity like this one.
etities/Student.ts
;;;;
entities/Teacher.ts
;;;; /** * Teacher entity */
Migrate
Now you can run migraion.
create-migration.js
const $umjs = ;const $path = ;const domain = ; // Discove all entities from given path$umjs; // Run it in next event loop iteration;
New migration script is gonna be generated in folder ./migrations
like this one.
/** * Migration script */ moduleexports = { await adapter; await adapter; await adapter; };
You can run that migration script with
run-migration.js
const $path = ;const domain = ; ;
Insert some data
let teacher = firstName: "John" lastName: "Wick";await Teacher; let teacher2 = ;teacher2firstName = "John";teacher2lastName = "Smith";await Teacher; let student = name: "Bart Simpson" teacherId: teacherid;await Student; studentteacher = teacher2;await student;
Make some queries
// Teachers their first name contains "u"let teachers = await Teacher ;
// Students their name starts with 'P' or ends with 's'let startsWith = "P";let students = await Student // limit 5, skip 3 ;
let onlyActive = true;let activeSubjectsCount = await Subject // if (onlyActive) { .filter(s => s.active === true) } ;
let subjectNames = await Subject // select only names ; in SQL SELECT name FROM Subject ;
let subjectMap = await Subject ;