1.1.6 • Published 6 years ago

mysql-query-decorator v1.1.6

Weekly downloads
18
License
MIT
Repository
github
Last release
6 years ago

Build Status

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
    }
}
1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago