1.3.17 • Published 6 months ago

dyfono v1.3.17

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Dyfono

Why Dyfono?

Dyfono is a word play of the word "Dífono", which in portuguese means: A letter with two sounds. It is also an acronym of Dynamic Find Options Notation. Just like a letter with two sounds, the object Search has two objects, Where and Order.

What is the purpose of this package?

This package aims to facilitate find options for TypeORM. It is a frontend and backend API to make dynamic queries using custom notation.

Instalation

npm install --save dyfono

Introduction and Usage

Given the following entities and relationships:

@Entity()
export class User {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({type: 'varchar'})
	username: string;

	@Column({type: 'varchar'})
	password: string;

	@OneToOne(() => Person)
	person: Person;

	@OneToOne(() => ProfilePicture)
	profilePicture: ProfilePicture;
}
@Entity()
export class ProfilePicture {
	@PrimaryGeneratedColumn()
	id: number;
}
@Entity()
export class Person {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({type: 'varchar'})
	name: string;

	@Column({type: 'varchar'})
	surname: string;

	@OneToOne(() => Address)
	address: Address;
}
@Entity()
export class Address {
	@PrimaryGeneratedColumn()
	id: number;

	@Column({type: 'varchar'})
	address: string;

	@Column({type: 'int'})
	zip: number;
}

To extract custom information, you have to write custom queries, and there are two ways to do that in TypeORM. The first is to write queries using raw SQL, which uses the query method. The other way is to use the find method and specify the params. In both ways you would have to write an API, either to write custom raw SQL, either to customize the params. This package is an API that customizes the params of the find method, but in a easier way.

Only using TypeORM:

//backend
userRepository.find({
	relations: {
		person: {
			address: true
		},
		profilePicture: true
	},
	where: [
		{
			person: {
				address: {
					state: "NY"
				}
			}
		},
		{
			profilePicture: {
				id: 1
			}
		}
	],
	order: {
		person: {
			address: {
				state: "ASC"
			}
		}
	}
})

Using Dyfono:

//frontend
const search: Search = {
	where: [
		{
			field: "person.address.state",
			searchTerm: "NY",
		},
		{
			field: "profilePicture.id",
			searchTerm: 1
		}
	],
	order: {
		field: "person.address.state",
		sortOrder: "ASC",
	}
};

//backend
userRepository.find({
	relations: getRelations(search.where), // search.where, not search.relations
	where: getWheres(search.where),
	order: getOrders(search.order),
})

With pure TypeORM, you need to know in advance what relations will be used in the search, whereas with Dyfono, you just type what you need to search. Plus, with only TypeORM, you would have to find a way to translate the params of your search in the frontend to a compatible find medthod param object in the backend. With Dyfono it is much easier as you can see, it provides an API in the frontend and in the backend!

Documentation

Docs

Donations

Don't feel obligated. This package is FREE!

☕️ buy me a coffee

1.3.7

6 months ago

1.3.6

6 months ago

1.3.5

6 months ago

1.3.4

6 months ago

1.3.3

6 months ago

1.3.2

7 months ago

1.3.10

6 months ago

1.3.13

6 months ago

1.3.14

6 months ago

1.3.11

6 months ago

1.3.12

6 months ago

1.3.17

6 months ago

1.3.15

6 months ago

1.3.16

6 months ago

1.3.9

6 months ago

1.3.8

6 months ago

1.3.1

7 months ago

1.3.0

7 months ago

1.2.5

7 months ago

1.2.2

7 months ago

1.2.1

7 months ago

1.2.0

7 months ago

1.1.4

7 months ago

1.1.3

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

7 months ago

1.0.1

8 months ago

1.0.0

8 months ago