0.0.56 • Published 4 months ago

dbwalker v0.0.56

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

A Library to walk the database using serverless-mysql

The most flexible MySQL query builder, using serverless-mysql to run and retrieve data.

Downloads Version License Rate this package

You can use objects, arrays and strings to build your queries using easy-to-understand methods.

IMPORTANT: this library is under development and stil in alpha. It's a very useful, but I don't recommend using it without a supervision.

Connection

You can connect to the database in the following ways:

// using a connection string
const db = new DBWalker("mysql://user:pass@host:port/base");
// parsing inline connection data
const db = new DBWalker({ host, ?port, user, pass, base });
// using array of connection data
const db = new DBWalker([host, ?port, user, pass, base]); // my fav to testing
// getting from ".env" file
const db = new DBWalker();

.env variables:

DBWALKER_HOST=
DBWALKER_PORT=
DBWALKER_USER=
DBWALKER_PASS=
DBWALKER_BASE=

Or .env variable string:

DBWALKER_STRING="mysql://user:pass@host:port/base"

Usage

Raw SQL example:

const raw = db.query("SELECT `alias`.* FROM `database`.`table` AS `alias` WHERE `alias`.`param` = ? ORDER BY ?", ["value", "field"]);

You can use select(), insert(), update(), delete() methods to build your queries.

const select = await db.select({
    table: "table", 
    ?columns: [
        "table_alias.field_name AS field_alias",
    ],
    ?fields: {
        field_alias: "table.field_name",
        field_name: "field_name",
        function_return: "(SELECT GROUP_CONCAT(value) FROM table_other WHERE table_other.field = table.field GROUP BY table_other.field)",
        other_function_return: "(SELECT other_table.field FROM table_other AS other_table WHERE other_table.field = table.field ORDER BY other_table.field LIMIT 1)",
    }, 
    ?joins, 
    ?where: [
        "field_name > 8.9", // field_name > 8.9
        { "field_name": "value" }, // field_name = value
        { "field_name": true }, // field_name = 1
        { "field_name": 88.9 }, // field_name = 88.9
        { is_null: "field_name" }, // field_name IS NULL
        { not_null: "field_name" }, // field_name IS NOT NULL
        { is_empty: "field_name" }, // field_name = ''
        { not_empty: "field_name" }, // field_name != ''
        { field: "field_name", between: ["param", "param"] }, // field_name BETWEEN param AND param
        { field: "field_name", not_between: ["param", "param"] }, // field_name BETWEEN param AND param
        { field: "field_name", in: ["options_array"] }, // field_name IN (options_array)
        { field: "field_name", not_in: ["options_array"] }, // field_name NOT IN (options_array)
        { field: "field_name", like: "value" }, // field_name LIKE '%value%'
        { field: "field_name", not_like: "value" }, // field_name NOT LIKE '%value%'
        { field: "field_name", start_with: "value" }, // field_name LIKE 'value%'
        { field: "field_name", end_with: "value" }, // field_name LIKE '%value'
        { field: "field_name", find_in_set: "value" }, // FIND_IN_SET(value, field_name)'
    ],
    ?group_by: ["fields"], 
    ?order_by: ["fields"], 
    ?limit: int, 
    ?offset: int
});

const insert = await db.insert({
    table: "table", 
    data: {
        field_name: "value"
    }
});

const update = await db.update({
    table: "table", 
    ?joins, 
    data: {
        field_name: "value"
    }, 
    where: [(...)]
});

const delete = await db.delete({
    table: "table", 
    ?joins,
    where: [(...)]
});

Tou can use .toString() method to return a MySQL string:

const select = db.select({
    table: "database.table AS table_alias", 
    columns: [
        "table_alias.column_name", 
        "table_alias.column_name AS column_alias"
    ]
}).toString();
SELECT table_alias.column_name, table_alias.column_name AS column_alias FROM `database`.`table` AS `table_alias`

Thanks to sql-formatter, we can return a pretty sql string using the .format() method as following:

const select = db.select({
    table: "database.table AS table_alias", 
    fields: { 
        field_alias: "table_alias.real_field_name"
    }
}).format();
SELECT
    table_alias.real_columnd_name AS `field_alias`
FROM 
    `database`.`table` AS `table_alias`
WHERE (...)

Using .run() returns a <Promise> with result or throw an error catchable.

Examples of use:

const results = await dbwalker.select(select).run();
// { success: bool, rows: int, data: array }

dbwalker.insert(insert).run() // { success: bool, insert_id: int, affected_rows: int }
    .then(res => {
    if(res.success) return dbwalker.select({
        table_name, 
        where: [
            { id: res.insert_id}
        ]
    });
});

dbwalker.update(update).run()// { success: bool, insert_id: int, affected_rows: int }
    .then(res => console.log(`${res.affected_rows} changed`))
    .catch(err => console.loc(err));

dbwalker.delete(update).run().catch(err => console.loc(err)).finaly(dbwalker.quit());
0.0.55

4 months ago

0.0.56

4 months ago

0.0.54

7 months ago

0.0.51

1 year ago

0.0.52

1 year ago

0.0.53

1 year ago

0.0.50

1 year ago

0.0.49

1 year ago

0.0.47

2 years ago

0.0.48

1 year ago

0.0.45

2 years ago

0.0.43

2 years ago

0.0.44

2 years ago

0.0.39

2 years ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago