Hi, I am trying to mock SelectQueryBuilder, but I am getting error: TypeError: Cannot read property 'select' of undefined
this is the code I am trying to run:
import * as typeorm from 'typeorm'
import { Connection, EntityManager, getConnection } from 'typeorm'
import { mocked } from 'ts-jest/utils'
jest.mock('typeorm')
class HighlightEntity {
id: string
name: string
deleted: boolean
}
const getEntityManagerMock = async (): Promise<EntityManager> => {
const mockedGetConnection = mocked(getConnection, true)
// inside the test case
mockedGetConnection.mockImplementation(
() =>
({
isConnected: true,
} as Connection),
)
const manager = new EntityManager(mockedGetConnection('default'))
manager.save = jest.fn().mockImplementation(() => Promise.resolve())
manager.transaction = jest.fn().mockImplementation((fn) => fn(manager))
return manager
}
describe('SelectQueryBuilder check before select', function () {
it('getById method passed', async () => {
const id = 1234
getEntityManagerMock().then((manager) => {
const query = manager
.createQueryBuilder()
.select('id', 'id')
.addSelect('name', 'name')
.from(HighlightEntity, 'highlight')
.where('id = :id', {
id,
})
.andWhere('deleted = false')
const res = query.getRawOne()
expect(res).toBeDefined()
})
})
})
What do I miss here?
Can somebody help me in the below issue ?
await getManager().transaction(async transactionalEntityManager => {
await transactionalEntityManager.save(hotelEntity);
const mappings = await this.getHotelMappingsByChannel(
hotelEntity.hotelId,
hotelEntity.channel,
);
await mapHotelCallback(new Hotel(hotelEntity), mappings);
});
I was writing the unit tests for this functionality but I'am bit stuck in mocking the getManager method of typeorm module.
Hello everybody. Could someone please see this issue? I can't save entities with nullable properties:
Hello everybody. Could someone please see this issue? I can't save entities with nullable properties:
typeorm/typeorm#8435
Don't know may be @Column('text', { nullable: true, default: null })
Or shorten
@Column('text', { default: null })
@Column({ type: 'text', default: null })
Hi everybody. I create AbstractEntity
export abstract class AbstractEntity {
@CreateDateColumn({ name: 'created_at', type: 'timestamp', default: () => 'CURRENT_TIMESTAMP(6)' })
createdAt: Date;
@UpdateDateColumn({
name: 'updated_at',
type: 'timestamp',
default: () => 'CURRENT_TIMESTAMP(6)',
onUpdate: 'CURRENT_TIMESTAMP(6)',
})
updatedAt: Date;
}
And entity UserEntity
@Entity({ name: 'users' })
export class UserEntity extends AbstractEntity<UserDto> {
@PrimaryGeneratedColumn('increment')
id: number;
@Column({ name: 'full_name' })
fullName?: string;
@Column({ name: 'date_of_birth', nullable: true })
dateOfBirth?: Date;
}
When I run migration "yarn migration:generate initialize-db", the generated SQL is
CREATE TABLE \
users` (`created_at` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), `updated_at` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `id` int NOT NULL AUTO_INCREMENT, ...
The field in AbstractEntity is created first before id, fullName ... How can I change order of filed id, fullName, ... before the created_at updated_at ?
Hey, just wanted to know. Is that correct way to create entity?
```const { Column, Entity, PrimaryGeneratedColumn } = require("typeorm");
@Entity({ name: "guild_configurations" })
export class GuildConfiguration {
@PrimaryGeneratedColumn("int", { nullable: true })
id; //: number;
@Column("int", { nullable: true }, { unique: true, name: "guild_id" })
guildId; //: string;
@Column("text", { nullable: true })
prefix = "?"; //: string = "?";
@Column("text", { nullable: true }, { name: "welcome_channel_id" })
welcomeChannelId; //: string;
}
await this.createQueryBuilder()
.relation('purchaseOrderItems')
.of(existPurchaseOrder)
.add( newPoItems );
I have this code, i want to add this new item to this repository, but nothing happend.Hello guys, how to save geojson object save to postgres with typeorm? I am installed dep postgis and created column 'geometry'.
my geojson obj:
{
type: 'Feature',
id: 'PersilHak.8BC60991AD183269E0530C1D140A4E08',
geometry: { type: 'MultiPolygon', coordinates: [ [Array] ] },
geometry_name: 'BATAS',
properties: {
PENGGUNAAN: 'Kosong',
}
}
my function to save
await repo.save({data: item})
@Entity()
export class Collection {
@ObjectIdColumn()
id !: string ;
@Column()
symbol !: string
@Column()
name !: string
@Column()
description !: string
@Column()
image !: string
@Column()
totalItems !: number
@Column()
createdAt!: string
@Column ()
categories!: string[];
@Column()
stats!: Stats[];
@Column()
isListed!: boolean;
}
const collection = getMongoRepository(Collection)
await collection.findOne({ where: { symbol : { $eq : symbol}}})
Collection {
id: 6285e19cfc929d11a2ec35fd,
symbol: 'solpunks',
name: 'SolPunks',
description: 'SolPunks are one of the very first NFTs on the Solana blockchain. Each of these 10,000 SolPunks has attributes that make them unique according to a defined rarity system.',
discord: 'https://discord.com/invite/solpunks',
image: 'https://bafkreiaoifjzhau3clwdbtap7mekvdardfg25xl24kbexc7syvws35ifk4.ipfs.nftstorage.link/',
twitter: 'https://twitter.com/PunksOnSolana',
website: '',
categories: [ 'pfps' ],
stats: []
}
Hello guys,
We are migrating from express/knex to nest/typeorm
We are working with micro services all connected to the same database (yes we know it's not how it's supposed to work but for back office it's much simpler)
Entities are on different applications so we can't add decortors like @ManyToOne() and when We use migration:generate this drop our existing foreign keys.
Is there a way to prevent this from happening?
If I create a pull request to add the possibility to generate migration without creating or dropping foreign keys is this something that would be merged?
Thanks for your help