
Security News
pnpm 10.12 Introduces Global Virtual Store and Expanded Version Catalogs
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.
unimapperjs
Advanced tools
Universal node.js (native ES6/ES7) LINQ-like object mapper (ORM/ODM) for all types of databases (SQL, NoSQL).
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
domain.js
const $um = require("unimapperjs");
const MySqlAdapter = require("unimapperjs/adapters/MySqlAdapter");
// Domain creation - connect to database via MySQL adapter
const domain = $um.createDomain(MySqlAdapter, { // connection string or object with options - specific to adapter
host: '127.0.0.1',
user: 'test',
password: 'test',
database: "test"
});
exports.domain = domain;
Then create entities in given domain. In TypeScript you can declare entity like this one.
etities/Student.ts
import {type} from "unimapperjs";
import {Entity} from "unimapperjs/src/Entity";
import {domain} from "../domain";
import {Teacher} from "./Teacher";
@domain.entity()
export class Student extends Entity<Student>
{
/**
* Student Id
*/
id: number;
/**
* Student name
*/
name: string;
/**
* Student's teacher id
*/
teacherId: number;
/**
* Navigation property to Teacher
*/
teacher: Promise<Teacher>;
static map(map: Student) {
const {Teacher} = require("./Teacher");
map.id = <any>type.uuid;
map.name = <any>type.string.length(100);
map.teacherId = <any>type.number;
map.teacher = <any>type.foreign(Teacher.name)
.withForeign<Student>(s => s.teacherId);
}
}
entities/Teacher.ts
import {type} from "unimapperjs";
import {Entity} from "unimapperjs/src/Entity";
import {domain} from "../domain";
import {Student} from "./Student";
/**
* Teacher entity
*/
@domain.entity()
export class Teacher extends Entity<Teacher>
{
/**
* Teacher ID
*/
id: number;
/**
* First name
*/
firstName: string;
/**
* Last name
*/
lastName: string;
/**
* Navigations property to assigned students
*/
students: Promise<Array<Student>>;
/**
* Mapping
*/
static map(map: Teacher) {
const {Student} = require("./Student");
map.id = <any>type.number.primary().autoIncrement();
map.firstName = <any>type.string.length(50);
map.lastName = <any>type.string.length(50);
map.students = <any>type.foreign(Student.name)
.hasMany<Student>(s => s.teacherId);
}
}
Now you can run migraion.
create-migration.js
const $umjs = require("unimapperjs");
const $path = require("path");
const {domain} = require("./domain");
// Discove all entities from given path
$umjs.initEntitiesFrom($path.resolve(__dirname, "entities"));
// Run it in next event loop iteration
setImmediate(async () => {
await domain.createMigration($path.resolve(__dirname, "migrations"));
await domain.dispose();
});
New migration script is gonna be generated in folder ./migrations
like this one.
/**
* Migration script
*/
module.exports = {
up: async function up(adapter) {
await adapter.createEntity("Student", {
"id": {
"type": "String"
, "length": 37
, "primary": true
}
, "name": {
"type": "String"
, "length": 100
}
, "teacherId": {
"type": "Number"
, "length": 11
}
});
await adapter.createEntity("Teacher", {
"id": {
"type": "Number"
, "length": 11
, "primary": true
, "autoIncrement": true
}
, "firstName": {
"type": "String"
, "length": 50
}
, "lastName": {
"type": "String"
, "length": 50
}
});
await adapter.addForeignKey("Student", "teacherId", "Teacher", "fk_Student_teacherId_Teacher_id");
}
};
You can run that migration script with
run-migration.js
const $path = require("path");
const {domain} = require("./domain");
setImmediate(async () => {
await domain.runMigration($path.resolve(__dirname, "migrations"));
await domain.dispose();
});
let teacher = new Teacher({
firstName: "John",
lastName: "Wick"
});
await Teacher.insert(teacher);
let teacher2 = new Teacher();
teacher2.firstName = "John";
teacher2.lastName = "Smith";
await Teacher.insert(teacher2);
let student = new Student({
name: "Bart Simpson",
teacherId: teacher.id
});
await Student.insert(student);
student.teacher = teacher2;
await student.save();
// Teachers their first name contains "u"
let teachers = await Teacher.getAll()
.filter(t => t.firstName.includes("u"))
.sort(t => t.firstName)
.exec();
// Students their name starts with 'P' or ends with 's'
let startsWith = "P";
let students = await Student.getAll()
.filter(s => s.name.startsWith($) || s.name.endsWith("s"), startsWith)
.sortDesc(s => s.name)
.slice(3, 8) // limit 5, skip 3
.exec();
let onlyActive = true;
let activeSubjectsCount = await Subject.getAll()
.filterIf(s => s.active === true, onlyActive) // if (onlyActive) { .filter(s => s.active === true) }
.count()
.exec();
let subjectNames = await Subject.getAll()
.sort(s => s.name)
.map(s => s.name) // select only names ; in SQL SELECT name FROM Subject
.exec();
let subjectMap = await Subject.getAll()
.sort(s => s.name)
.map(s => ({
id: s.id,
name: s.name
}))
.exec();
FAQs
Universal node.js (native ES6/ES7) LINQ-like object mapper (ORM/ODM) for all types of databases (SQL, NoSQL).
The npm package unimapperjs receives a total of 0 weekly downloads. As such, unimapperjs popularity was classified as not popular.
We found that unimapperjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.
Security News
Amaro 1.0 lays the groundwork for stable TypeScript support in Node.js, bringing official .ts loading closer to reality.
Research
A deceptive PyPI package posing as an Instagram growth tool collects user credentials and sends them to third-party bot services.