How to use the typeorm.getManager function in typeorm

To help you get started, we’ve selected a few typeorm examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github javieraviles / node-typescript-koa-rest / src / controller / user.ts View on Github external
public static async getUsers(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // load all users
        const users: User[] = await userRepository.find();

        // return OK status code and loaded users array
        ctx.status = 200;
        ctx.body = users;
    }
github hungeroxc / oxc-blog-server / src / controller / comment.ts View on Github external
async createComment(ctx: Context) {
        const { articleId, content, userId } = ctx.request.body
        const articleRepository = getManager().getRepository(Article)
        const commentRepository = getManager().getRepository(Comment)
        const userRepository = getManager().getRepository(User)
        // 查询目标文章和用户
        const targetArticle = await articleRepository.findOne({ where: { id: articleId }, relations: ['comments'] })
        const targetUser = await userRepository.findOne({ where: { id: userId }, relations: ['comments'] })
        // 创建comment并存储
        const newComment = commentRepository.create({
            content
        })
        await commentRepository.save(newComment)
        // 存储到文章和用户中去
        targetArticle.comments = [...targetArticle.comments, newComment]
        targetUser.comments = [...targetUser.comments, newComment]
        await userRepository.save(targetUser)
        await articleRepository.save(targetArticle)
        // 查找最新评论发回去
        const resComment = await commentRepository
github Aionic-Apps / aionic-core / src / api / components / global / user / service.ts View on Github external
import { bind } from 'decko';
import { Like, Repository, FindConditions, getManager, FindManyOptions, FindOneOptions } from 'typeorm';

import { CacheService } from '@services/cache';

import { User } from './model';

export class UserService {
	private readonly defaultRelations: string[] = ['userRole', 'assignee', 'tasksWatched'];

	private readonly cacheService: CacheService = new CacheService();

	private readonly repo: Repository = getManager().getRepository(User);

	/**
	 * Read all users from db
	 *
	 * @param options Find options
	 * @param cached Read users from cache
	 * @returns Returns an array of users
	 */
	@bind
	public readUsers(options: FindManyOptions = {}, cached: boolean = false): Promise {
		try {
			if (Object.keys(options).length) {
				return this.repo.find({
					relations: this.defaultRelations,
					...options
				});
github mprove-io / mprove / src / models / store / get-users-repo.ts View on Github external
export function getUsersRepo(manager?: EntityManager) {
  let entityManager = manager ? manager : getManager();

  return entityManager.getCustomRepository(repositories.UserRepository);
}
github pietrzakadrian / bank / backend / src / services / currency.service.ts View on Github external
constructor() {
    this.logger = new Logger(__filename);
    this.currencyRepository = getManager().getRepository(Currency);
    this.userRepository = getManager().getRepository(User);
    this.billRepository = getManager().getRepository(Bill);
    this.additionalRepository = getManager().getRepository(Additional);
  }
github danielwii / asuna-node-server / src / modules / core / auth / admin-auth.service.ts View on Github external
async createUser(username: string, email: string, password: string, roleNames?: string[]): Promise {
    const { hash, salt } = PasswordHelper.encrypt(password);
    const roles = _.isEmpty(roleNames) ? null : await this.roleRepository.findByNames(roleNames);

    let user = await this.getUser({ username, email });
    if (!user) {
      user = this.userRepository.create({ email, username, isActive: true });
    }
    logger.log(`found user ${r(user)}`);
    user.password = hash;
    user.salt = salt;
    user.roles = roles;
    return getManager().save(user);
  }
github kinecosystem / marketplace-server / scripts / src / models / orders.ts View on Github external
async countAllByOffer(userId: string): Promise> {
		const results: Array<{ offer_id: string, cnt: number }> = await getManager().query(
			`SELECT
					orders.offer_id, COUNT(DISTINCT(orders.id)) as cnt
				FROM orders
				LEFT JOIN orders_contexts
				ON orders.id = orders_contexts.order_id
				WHERE
					(status = $1 OR (status IN ($2) AND expiration_date > $3))
					AND orders_contexts.user_id = ($4)
				GROUP BY offer_id`, ["completed", ["pending"], new Date(), userId]);
		const map = new Map();
		for (const res of results) {
			map.set(res.offer_id, res.cnt);
		}
		return map;
	},
github pietrzakadrian / bank / backend / src / services / languages.service.ts View on Github external
constructor() {
    this.logger = new Logger(__filename);
    this.languagesRepository = getManager().getRepository(Language);
  }