Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mikro-orm

Package Overview
Dependencies
Maintainers
1
Versions
3406
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mikro-orm - npm Package Compare versions

Comparing version 0.5.5 to 0.5.6

1

dist/BaseEntity.d.ts

@@ -13,2 +13,3 @@ import { ObjectID } from 'bson';

init(): Promise<BaseEntity>;
assign(data: any): void;
toObject(parent?: BaseEntity): any;

@@ -15,0 +16,0 @@ toJSON(): any;

@@ -6,2 +6,3 @@ "use strict";

const Collection_1 = require("./Collection");
const Utils_1 = require("./Utils");
class BaseEntity {

@@ -35,2 +36,36 @@ constructor() {

}
assign(data) {
const em = MikroORM_1.getEntityManager();
const metadata = MikroORM_1.getMetadataStorage();
const meta = metadata[this.constructor.name];
const props = meta.properties;
Object.keys(data).forEach(prop => {
if (props[prop].reference === ReferenceType.MANY_TO_ONE && data[prop]) {
if (data[prop] instanceof BaseEntity) {
return this[prop] = data[prop];
}
if (data[prop] instanceof bson_1.ObjectID) {
return this[prop] = em.getReference(props[prop].type, data[prop].toHexString());
}
const id = typeof data[prop] === 'object' ? data[prop].id : data[prop];
if (id) {
return this[prop] = em.getReference(props[prop].type, id);
}
}
const isCollection = [ReferenceType.ONE_TO_MANY, ReferenceType.MANY_TO_MANY].includes(props[prop].reference);
if (isCollection && Utils_1.Utils.isArray(data[prop])) {
const items = data[prop].map((item) => {
if (item instanceof bson_1.ObjectID) {
return em.getReference(props[prop].type, item.toHexString());
}
if (item instanceof BaseEntity) {
return item;
}
return em.getReference(props[prop].type, item);
});
return this[prop] = new Collection_1.Collection(props[prop], this, items);
}
this[prop] = data[prop];
});
}
toObject(parent = this) {

@@ -37,0 +72,0 @@ const metadata = MikroORM_1.getMetadataStorage();

2

dist/Collection.d.ts

@@ -14,3 +14,3 @@ import { ObjectID } from 'bson';

getItems(): T[];
getIdentifiers(): ObjectID[];
getIdentifiers(field?: string): ObjectID[];
add(...items: T[]): void;

@@ -17,0 +17,0 @@ remove(...items: T[]): void;

@@ -48,4 +48,4 @@ "use strict";

}
getIdentifiers() {
return this.getItems().map(i => i._id);
getIdentifiers(field = '_id') {
return this.getItems().map(i => i[field]);
}

@@ -52,0 +52,0 @@ add(...items) {

import { getMetadataStorage, getEntityManager } from './MikroORM';
import { ObjectID } from 'bson';
import { Collection } from './Collection';
import { Utils } from './Utils';

@@ -45,2 +46,47 @@ export class BaseEntity {

assign(data: any) {
const em = getEntityManager();
const metadata = getMetadataStorage();
const meta = metadata[this.constructor.name];
const props = meta.properties;
Object.keys(data).forEach(prop => {
if (props[prop].reference === ReferenceType.MANY_TO_ONE && data[prop]) {
if (data[prop] instanceof BaseEntity) {
return this[prop] = data[prop];
}
if (data[prop] instanceof ObjectID) {
return this[prop] = em.getReference(props[prop].type, data[prop].toHexString());
}
const id = typeof data[prop] === 'object' ? data[prop].id : data[prop];
if (id) {
return this[prop] = em.getReference(props[prop].type, id);
}
}
const isCollection = [ReferenceType.ONE_TO_MANY, ReferenceType.MANY_TO_MANY].includes(props[prop].reference);
if (isCollection && Utils.isArray(data[prop])) {
const items = data[prop].map((item: any) => {
if (item instanceof ObjectID) {
return em.getReference(props[prop].type, item.toHexString());
}
if (item instanceof BaseEntity) {
return item;
}
return em.getReference(props[prop].type, item);
});
return this[prop] = new Collection(props[prop], this, items);
}
this[prop] = data[prop];
});
}
toObject(parent: BaseEntity = this): any {

@@ -47,0 +93,0 @@ const metadata = getMetadataStorage();

@@ -56,4 +56,4 @@ import { ObjectID } from 'bson';

getIdentifiers(): ObjectID[] {
return this.getItems().map(i => i._id);
getIdentifiers(field = '_id'): ObjectID[] {
return this.getItems().map(i => i[field]);
}

@@ -60,0 +60,0 @@

{
"name": "mikro-orm",
"version": "0.5.5",
"version": "0.5.6",
"description": "Simple typescript mongo ORM for node.js based on data-mapper, unit-of-work and identity-map patterns",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

import { ObjectID } from 'bson';
import { Author } from './entities/Author';
import { MikroORM } from '../lib';
import { Book } from './entities/Book';
import { BookTag } from './entities/BookTag';

@@ -34,2 +36,42 @@ /**

test('#assign() should update entity values', async () => {
const god = new Author('God', 'hello@heaven.god');
const jon = new Author('Jon Snow', 'snow@wall.st');
const book = new Book('Book', jon);
await orm.em.persist(book);
expect(book.title).toBe('Book');
expect(book.author).toBe(jon);
book.assign({ title: 'Better Book 1', author: god });
expect(book.author).toBe(god);
await orm.em.persist(god);
book.assign({ title: 'Better Book 2', author: god.id });
expect(book.author).toBe(god);
book.assign({ title: 'Better Book 3', author: jon._id });
expect(book.title).toBe('Better Book 3');
expect(book.author).toBe(jon);
});
test('#assign() should update entity collection', async () => {
const other = new BookTag('other');
other.id = null;
await orm.em.persist(other);
const jon = new Author('Jon Snow', 'snow@wall.st');
const book = new Book('Book', jon);
const tag1 = new BookTag('tag 1');
const tag2 = new BookTag('tag 2');
const tag3 = new BookTag('tag 3');
book.tags.add(tag1);
book.tags.add(tag2);
book.tags.add(tag3);
await orm.em.persist(book);
book.assign({ tags: [other._id] });
expect(book.tags.getIdentifiers()).toMatchObject([other._id]);
book.assign({ tags: [] });
expect(book.tags.getIdentifiers()).toMatchObject([]);
book.assign({ tags: [tag1.id, tag3.id] });
expect(book.tags.getIdentifiers('id')).toMatchObject([tag1.id, tag3.id]);
book.assign({ tags: [tag2] });
expect(book.tags.getIdentifiers()).toMatchObject([tag2._id]);
});
test('should have string id getter and setter', async () => {

@@ -36,0 +78,0 @@ const author = new Author('Jon Snow', 'snow@wall.st');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc