npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itemku/general-repository

v0.7.10

Published

General Repository for common tasks and adding query helper in Laravel style

Downloads

163

Readme

itemku TypeORM General Repository

General Repository for common tasks and adding query helper in Laravel style

How to use

Install

yarn install @itemku/general-repository

Quickstart

Extends Model & Add Scope

  1. always extends CommonModel, this will have standard properties on created_at and updated_at also some default scope for searching


@Entity()

export  class  TestModel  extends  CommonModel {
.
.
.
}
  1. Add column & scope for filter , format should be : scopeXXXX(query: ProxyQuery, id) ex :
	public  scopeUserId(query: ProxyQuery<TestModel>, id: number) {{

		return  query.andWhere('user_id = :user_id', { user_id:  id });

	}
  1. You could also have more than 1 parameter or no parameter at all ex :
	public  scopeUserActive(query: ProxyQuery<TestModel>) {{

		return  query.andWhere('is_active= :is_active', { is_active:  true});

	}

	public  scopeUserTest(query: ProxyQuery<TestModel>, id1: number, id2: number) {{
		let query = query.andWhere('user_id = :user_id', { user_id:  id1 });
		query = query.andWhere('id_transaction = :id_transaction ', { id_transaction:  id2 });
		return query;
	}

Extends General Repository

  1. Extends General Repository
export class TestRepository extends GeneralRepository<TestModel> {
  constructor(entity: EntityManager) {
    super(TestModel, entity);
  }
}
  1. Override Common Filter

protected  commonFilter(queryBuilder: ProxyQuery<TestModel>, filter) {

	super.commonFilter(queryBuilder, filter);



	const  userId = getDefault(filter['user_id']);


	if (userId != undefined) {

	queryBuilder = queryBuilder.UserId(userId);

	}


}

API - Standard Scope

This scope already included in CommonModel no need to create again

1) scopeId(query: ProxyQuery, id: any)

if there's any primary key that's different (not ID) you could override primaryKey() and return the correct Primary Key

public  scopeId(query: ProxyQuery<CommonModel>, id: any) {

	return  query.andWhere(this.constructor.name + '.' + this.primaryKey() + ' = :id', {id:  id});

}

2) scopeIds(query: ProxyQuery, ids: Array)

if there's any primary key that's different (not ID) you could override primaryKey() and return the correct Primary Key

public  scopeIds(query: ProxyQuery<CommonModel>, ids: Array<any>) {

	return  query.andWhere(this.constructor.name + '.' + this.primaryKey() + ' IN (:...ids)', {ids:  ids});

}

3) scopeExcludeIds(query: ProxyQuery, ids: Array)

if there's any primary key that's different (not ID) you could override primaryKey() and return the correct Primary Key

public  scopeExcludeIds(query: ProxyQuery<CommonModel>, ids: Array<any>) {

	return  query.andWhere(this.constructor.name + '.' + this.primaryKey() + ' NOT IN (:...ids)', {ids:  ids});

}

4) scopeIsActive(query: ProxyQuery, is_active = true)

public  scopeIsActive(query: ProxyQuery<CommonModel>, is_active = true) {

	return  query.andWhere(this.constructor.name + '.' + 'is_active = :is_active', {is_active:  is_active});

}

5) scopeIsFinished(query: ProxyQuery, is_finished = false)

public  scopeIsFinished(query: ProxyQuery<CommonModel>, is_finished = false) {

	return  query.andWhere(this.constructor.name + '.' + 'is_finished = :is_finished', {is_finished:  	is_finished});

}

6) scopeStatus(query: ProxyQuery, is_finished = false)

public  scopeStatus(query: ProxyQuery<CommonModel>, status: number) {

	return  query.andWhere(this.constructor.name + '.' + 'status = :status', {status:  status});

}

7) scopeStartDate(query: ProxyQuery, date: string)

public  scopeStartDate(query: ProxyQuery<CommonModel>, date: string) {

	return  query.andWhere(this.constructor.name + '.' + 'created_at >= :start_date', {start_date:  date + ' 00:00:00'});

}

8) scopeStartDate(query: ProxyQuery, date: string)

public  scopeStartDate(query: ProxyQuery<CommonModel>, date: string) {

	return  query.andWhere(this.constructor.name + '.' + 'created_at >= :start_date', {start_date:  date + ' 00:00:00'});

}

9) scopeEndDate(query: ProxyQuery, date: string)

public  scopeEndDate(query: ProxyQuery<CommonModel>, date: string) {

	return  query.andWhere(this.constructor.name + '.' + 'created_at <= :end_date', {end_date:  date + ' 23:59:59'});

}

10) scopeUpdateStartDate(query: ProxyQuery, date: string)

public  scopeUpdateStartDate(query: ProxyQuery<CommonModel>, date: string) {

	return  query.andWhere(this.constructor.name + '.' + 'updated_at >= :update_start_date', {update_start_date:  date + ' 00:00:00'});

}

10) scopeUpdateEndDate(query: ProxyQuery, date: string)

public  scopeUpdateEndDate(query: ProxyQuery<CommonModel>, date: string) {

	return  query.andWhere(this.constructor.name + '.' + 'updated_at <= :update_end_date', {update_end_date:  date + ' 23:59:59'});

}

11) scopeOrderByLatest(query: ProxyQuery)

public  scopeOrderByLatest(query: ProxyQuery<CommonModel>) {

	return  query.addOrderBy(this.constructor.name + '.' + 'created_at', 'DESC');

}

11) scopeOrderByOldest(query: ProxyQuery)

public  scopeOrderByOldest(query: ProxyQuery<CommonModel>) {

	return  query.addOrderBy(this.constructor.name + '.' + 'created_at', 'ASC');

}

12) scopeOrderByLatestUpdate(query: ProxyQuery)

public  scopeOrderByLatestUpdate(query: ProxyQuery<CommonModel>) {

	return  query.addOrderBy(this.constructor.name + '.' + 'updated_at', 'DESC');

}

13) scopeLastModifiedBy(query: ProxyQuery, id: any)

public  scopeLastModifiedBy(query: ProxyQuery<CommonModel>, id: any) {

	return  query.andWhere(this.constructor.name + '.' + 'last_modified_by = :last_modified_by', {last_modified_by:  id});

}

13) scopeLastModifiedById(query: ProxyQuery, id: any)


public  (query: ProxyQuery<CommonModel>, id: any) {

	return  query.andWhere(this.constructor.name + '.' + 'last_modified_by_id = :last_modified_by_id', {last_modified_by_id:  id});

}

Full Example

Model


@Entity()

export  class  TestModel  extends  CommonModel {

	@PrimaryColumn()

	@Generated()

	id: string;



	@Column()

	user_id: number;



	@Column()

	balance: number;



	@Column()

	withdraw_balance: number;



	public  scopeUserId(query: ProxyQuery<TestModel>, id: number) {{

	return  query.andWhere('user_id = :user_id', { user_id:  id });

	}
}

Repository

export class TestRepository extends GeneralRepository<TestModel> {
  constructor(entity: EntityManager) {
    super(TestModel, entity);
  }

  protected commonFilter(queryBuilder: ProxyQuery<TestModel>, filter) {
    super.commonFilter(queryBuilder, filter);

    const userId = getDefault(filter['user_id']);

    if (userId != undefined) {
      queryBuilder = queryBuilder.UserId(userId);
    }
  }
}