expeditavoluptas
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
91 lines (72 loc) • 1.99 kB
text/typescript
import { Collection, Entity, Index, ManyToMany, PrimaryKey, Property, Unique } from '@mikro-orm/core';
import { FullTextType, MikroORM } from '@mikro-orm/postgresql';
export class Artist {
id!: number;
name: string;
searchableName!: string;
constructor(artist: any) {
this.id = artist.id;
this.name = artist.name;
this.searchableName = artist.name;
}
}
export class Song {
id!: number;
title: string;
artists = new Collection<Artist>(this);
searchableTitle!: string;
constructor(song: any) {
this.id = song.id;
this.title = song.title;
this.searchableTitle = song.title;
}
}
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [Song],
dbName: 'mikro_orm_test_3696',
});
await orm.schema.refreshDatabase();
});
afterAll(async () => {
await orm.close(true);
});
test('GH issue 3696', async () => {
const artist = orm.em.create(Artist, {
name: 'Taylor Swift',
searchableName: 'Taylor Swift',
});
const song = orm.em.create(Song, {
title: 'Anti-Hero',
searchableTitle: 'Anti--Hero',
});
song.artists.add(artist);
await orm.em.flush();
orm.em.clear();
const results = await orm.em.find(Song, {
searchableTitle: { $fulltext: 'anti' },
artists: { searchableName: { $fulltext: 'taylor' } },
}, { populate: ['artists'] });
expect(results).toHaveLength(1);
expect(results[0]).toMatchObject({
title: 'Anti-Hero',
searchableTitle: "'anti':1 'hero':2",
});
expect(results[0].artists[0]).toMatchObject({
name: 'Taylor Swift',
searchableName: "'swift':2 'taylor':1",
});
});