Skip to content

Commit

Permalink
fix(core): disable auto flushing by default [BC] (#79)
Browse files Browse the repository at this point in the history
Having auto flushing enabled by default was a poor choice, as it could produce multiple small transactions without user knowing it. EM.persist() now won't flush anything (by default), so ORM will behave the same way as others like hibernate or doctrine.

BREAKING CHANGES:
With default `autoFlush` value, em.persist(), em.removeEntity() and em.remove(e: IEntity) will not flush the EM. Either use second flush parameter or flush method variants (like em.removeAndFlush()).

Closes #63
  • Loading branch information
B4nan committed Jul 25, 2019
1 parent c10f8e8 commit 273945a
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 223 deletions.
14 changes: 7 additions & 7 deletions lib/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ export class EntityManager {
return this.driver.count(entityName, where);
}

async persist(entity: IEntity | IEntity[], flush = this.config.get('autoFlush')): Promise<void> {
persist(entity: IEntity | IEntity[], flush = this.config.get('autoFlush')): void | Promise<void> {
if (flush) {
await this.persistAndFlush(entity);
return this.persistAndFlush(entity);
} else {
this.persistLater(entity);
}
Expand All @@ -247,20 +247,20 @@ export class EntityManager {
}
}

async remove<T extends IEntityType<T>>(entityName: EntityName<T>, where: T | any, flush = this.config.get('autoFlush')): Promise<number> {
remove<T extends IEntityType<T>>(entityName: EntityName<T>, where: any, flush = this.config.get('autoFlush')): void | Promise<number> {
entityName = Utils.className(entityName);

if (Utils.isEntity(where)) {
await this.removeEntity(where, flush);
return 1;
const ret = this.removeEntity(where, flush);
return ret ? ret.then(() => 1) : ret;
}

return this.nativeDelete(entityName, where);
}

async removeEntity(entity: IEntity, flush = this.config.get('autoFlush')): Promise<void> {
removeEntity<T extends IEntityType<T>>(entity: T, flush = this.config.get('autoFlush')): void | Promise<void> {
if (flush) {
await this.removeAndFlush(entity);
return this.removeAndFlush(entity);
} else {
this.removeLater(entity);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/entity/EntityRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export class EntityRepository<T extends IEntityType<T>> {
constructor(private readonly em: EntityManager,
protected readonly entityName: EntityName<T>) { }

async persist(entity: T | IEntity[], flush = this.em.config.get('autoFlush')): Promise<void> {
await this.em.persist(entity, flush);
persist(entity: T | IEntity[], flush = this.em.config.get('autoFlush')): void | Promise<void> {
return this.em.persist(entity, flush);
}

async persistAndFlush(entity: IEntity | IEntity[]): Promise<void> {
Expand Down Expand Up @@ -42,7 +42,7 @@ export class EntityRepository<T extends IEntityType<T>> {
return this.em.find<T>(this.entityName, {}, populate as string[], orderBy, limit, offset);
}

async remove(where: T | FilterQuery<T> | IPrimaryKey, flush = this.em.config.get('autoFlush')): Promise<number> {
remove(where: T | FilterQuery<T> | IPrimaryKey, flush = this.em.config.get('autoFlush')): void | Promise<number> {
return this.em.remove(this.entityName, where, flush);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Configuration {
entitiesDirs: [],
entitiesDirsTs: [],
tsConfigPath: process.cwd() + '/tsconfig.json',
autoFlush: true,
autoFlush: false,
strict: false,
logger: () => undefined,
baseDir: process.cwd(),
Expand Down
16 changes: 8 additions & 8 deletions tests/EntityHelper.mongo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('EntityAssignerMongo', () => {
const bible = new Book('Bible', god);
const bible2 = new Book('Bible pt. 2', god);
const bible3 = new Book('Bible pt. 3', new Author('Lol', 'lol@lol.lol'));
await orm.em.persist([bible, bible2, bible3]);
await orm.em.persistAndFlush([bible, bible2, bible3]);
orm.em.clear();

const newGod = (await orm.em.findOne(Author, god.id, ['books.author']))!;
Expand All @@ -58,7 +58,7 @@ describe('EntityAssignerMongo', () => {
const bible = new Book('Bible', god);
god.favouriteAuthor = god;
bible.publisher = new Publisher('Publisher 1');
await orm.em.persist(bible);
await orm.em.persistAndFlush(bible);
orm.em.clear();

const author = (await orm.em.findOne(Author, god.id, ['favouriteAuthor', 'books.author', 'books.publisher']))!;
Expand All @@ -73,7 +73,7 @@ describe('EntityAssignerMongo', () => {

test('#init() should populate the entity', async () => {
const author = new Author('Jon Snow', 'snow@wall.st');
await orm.em.persist(author);
await orm.em.persistAndFlush(author);
orm.em.clear();

const jon = orm.em.getReference(Author, author.id!);
Expand All @@ -84,7 +84,7 @@ describe('EntityAssignerMongo', () => {

test('#init() should refresh the entity if its already loaded', async () => {
const author = new Author('Jon Snow', 'snow@wall.st');
await orm.em.persist(author);
await orm.em.persistAndFlush(author);
orm.em.clear();

const jon = await orm.em.findOne(Author, author.id);
Expand All @@ -98,13 +98,13 @@ describe('EntityAssignerMongo', () => {
const god = new Author('God', 'hello@heaven.god');
const jon = new Author('Jon Snow', 'snow@wall.st');
const book = new Book('Book2', jon);
await orm.em.persist(book);
await orm.em.persistAndFlush(book);
expect(book.title).toBe('Book2');
expect(book.author).toBe(jon);
EntityAssigner.assign(book, { title: 'Better Book2 1', author: god, notExisting: true });
expect(book.author).toBe(god);
expect((book as any).notExisting).toBe(true);
await orm.em.persist(god);
await orm.em.persistAndFlush(god);
EntityAssigner.assign(book, { title: 'Better Book2 2', author: god.id });
expect(book.author).toBe(god);
EntityAssigner.assign(book, { title: 'Better Book2 3', author: jon._id });
Expand All @@ -114,7 +114,7 @@ describe('EntityAssignerMongo', () => {

test('#assign() should update entity collection', async () => {
const other = new BookTag('other');
await orm.em.persist(other);
await orm.em.persistAndFlush(other);
const jon = new Author('Jon Snow', 'snow@wall.st');
const book = new Book('Book2', jon);
const tag1 = new BookTag('tag 1');
Expand All @@ -123,7 +123,7 @@ describe('EntityAssignerMongo', () => {
book.tags.add(tag1);
book.tags.add(tag2);
book.tags.add(tag3);
await orm.em.persist(book);
await orm.em.persistAndFlush(book);
EntityAssigner.assign(book, { tags: [other._id] });
expect(book.tags.getIdentifiers('_id')).toMatchObject([other._id]);
EntityAssigner.assign(book, { tags: [] });
Expand Down
8 changes: 4 additions & 4 deletions tests/EntityHelper.mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe('EntityHelperMySql', () => {
const god = new Author2('God', 'hello@heaven.god');
const jon = new Author2('Jon Snow', 'snow@wall.st');
const book = new Book2('Book2', jon);
await orm.em.persist(book);
await orm.em.persistAndFlush(book);
expect(book.title).toBe('Book2');
expect(book.author).toBe(jon);
book.assign({ title: 'Better Book2 1', author: god, notExisting: true });
expect(book.author).toBe(god);
expect((book as any).notExisting).toBe(true);
await orm.em.persist(god);
await orm.em.persistAndFlush(god);
book.assign({ title: 'Better Book2 2', author: god.id });
expect(book.author).toBe(god);
book.assign({ title: 'Better Book2 3', author: jon.id });
Expand All @@ -36,7 +36,7 @@ describe('EntityHelperMySql', () => {

test('#assign() should update entity collection [mysql]', async () => {
const other = new BookTag2('other');
await orm.em.persist(other);
await orm.em.persistAndFlush(other);
const jon = new Author2('Jon Snow', 'snow@wall.st');
const book = new Book2('Book2', jon);
const tag1 = new BookTag2('tag 1');
Expand All @@ -45,7 +45,7 @@ describe('EntityHelperMySql', () => {
book.tags.add(tag1);
book.tags.add(tag2);
book.tags.add(tag3);
await orm.em.persist(book);
await orm.em.persistAndFlush(book);
book.assign({ tags: [other.id] });
expect(book.tags.getIdentifiers()).toMatchObject([other.id]);
book.assign({ tags: [] });
Expand Down
Loading

0 comments on commit 273945a

Please sign in to comment.