1.3.9 • Published 3 years ago

@janiscommerce/query-builder v1.3.9

Weekly downloads
1
License
ISC
Repository
github
Last release
3 years ago

query-builder

Build Status Coverage Status npm version

Prepare and execute SELECT, INSERT, UPDATE, REMOVE queries from SQL database.


Instalation

npm install @janiscommerce/query-builder

Configuration

You must have installed both Knex and SQL driver you will use and tables created.


API

  • new QueryBuilder(knex, models), Query Builder constructor.

    • knex, Knex module with the initial configuration.
    • model Model instance. The Model must have table, field, joins, etc. structure define. See more
  • insert(items) ASYNCHRONOUS, Execute INSERT Query.

    • items - Object to Insert or Array of Objects to Insert.
    • Returns, an array, depends on SQL-Database you will use See More Details.
  • save(items) ASYNCHRONOUS, Execute INSERT Query with Upsert (Updated de duplicate rows, and insert the new ones).

    • items - Object to Insert or Array of Objects to Insert.
    • Returns, object depends on SQL-Database-Druver you will use.
  • update(values, filters) ASYNCHRONOUS, Execute UPDATE Query.

    • values will be updated to.
    • filters (where clause) conditions to filter rows. See Filters.
    • Returns, number of rows updated.
  • remove(filters, joins) ASYNCHRONOUS, Execute REMOVE Query.

    • filters (where clause) conditions to filter rows. See Filters.
    • joins if you need to joins table. See Joins.
    • Returns, object depends on SQL-Database-Druver you will use.
  • get( parametres ) ASYNCHRONOUS, Execute SELECT Query.

    • parametres , type object, Parametres for the query, filters, joins, limits.
    • Returns, array with the rows founds.

Parametres

Field

To select a specific field, use fields as key with the selected fields.

See More

Special Functions

It's posible add special functions in the queries, as a key in parametres object.

See More

Joins

Joins are Automatic.

See More

Filters

To filter by fields, use filters as key.

See More

Order

To order, use order key.

See More

Pagination

To use pagination, use limit and page keys.

See More


Errors

The errors are informed with a QueryBuilderError with the proper message for each error.

The codes are the following:

CodeDescription
1Invalid Model
2Invalid Knex
3Invalid Fields
4Invalid Select Functions
5Invalid Joins
6Invalid Filters
7Invalid Flags
8Invalid Orders
9Invalid Groups
10Invalid Limits
11Invalid Table
12No Items
13No Values
14Nothing Select

Usage

If you want an example using MySQL. See Here

const QueryBuilder = require('@janniscommerce/query-builder');

// knex is already with init config
// model is an instance of a Model Class

const queryBuilder = new QueryBuilder(knex, model);

// Insert Items
// item an object with valid fields
// Could be multiple items
await queryBuilder.insert(item);

// Save item which could already exist
await queryBuilder.save(item)

// Update any Items
// values, object whit fields to change
// filters, object with the correct filters
await queryBuilder.update(values, filters);

// Remove any Items
// joins, object with table joins define if it's possible
await queryBuilder.remove(filters);

// Get Items
// Get All
const resultsAll = await queryBuilder.get();

// Get with options
// params, object with the options define
const results = await queryBuilder.get(params);
1.3.9

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago