1.1.6 • Published 6 years ago
mysql-query-decorator v1.1.6
MYSQL Query decorator
A decorator to facilicate the usage of mysql queries With this library, you can execute a query and get its rows or an assembled object from an assembler
npm install mysql-query-decorator
or
yarn add mysql-query-decorator
Create connection
MYSQLQuery.use({
host: 'localhost',
user: 'root',
database: 'adsmanager'
});
Create a query
Without values and without assembler
@query('SELECT * from clients')
select() {}
const executeQuery = async () => {
const result = await select();
}
With values and without assembler
Include the values you want to use in the query in object in a return statement
@query('SELECT * from clients where email = :email')
select(email: string) {
return {
email
}
}
const executeQuery = async () => {
const result = await select('test@gmail.com');
}
With assembler
Create an assembler that implements IAssembler
class Mock {
constructor(private readonly id: number, private readonly name: string, private readonly email: string) {}
}
class MockAssembler implements IAssembler<Mock> {
public assemble(rows: any): Key {
return new Mock(rows['id'], rows['name'], rows['email'])
}
}
class Repository {
@query<Mock>('SELECT * from mock where id = :id', new MockAssembler)
public select(id: number) {
return {
id
}
}
}
class MockController {
constructor(private readonly repo: Repository) {}
public async get() {
let result = await this.repo.select(84)
console.log(result)
}
}
Other examples
Insert
@query("INSERT into client(name, email) values(:name, :email)")
insert(name: string, email: string) {
return {
name,
email
}
}
Update
@query('UPDATE clients set name = :name where id = :id')
update(name: string, id: number) {
return {
name,
id
}
}
Delete
@query('DELETE from clients where id = :id')
delete(id: number) {
return {
id
}
}