0.0.1 • Published 1 year ago

@hoangnguyennn/mysql-builder v0.0.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

MySQL Builder

Support building MySQL statements

Table of contents

Example Usage

const Post = new Model('posts')
Post.all() //  select * from `posts`
Post.where('id', 1).exec() // select * from `posts` where `id` = 1

API

Model

Model.prototype.all()

Return a query that retrieves all the records in the table

Return type: string

Post.all()
// select * from `posts`

Model.prototype.get()

Same as all() method

Return type: string

Post.get()
// select * from `posts`

Model.prototype.first()

Return a query that retrieves the first record in the table.

Return type: string

Post.first()
// select * from `posts` limit 1

Model.prototype.limit(value)

Specifies the maximum number of records the query will return.

Parameters:

  • value (number): The number of records will return

Return type: Query

const query = Post.limit(10)

query.exec()
// select * from `posts` limit 10

Model.prototype.offset(value)

Specifies the number of records the query will skip.

Parameters:

  • value (number): The number of records will skip

Return type: Query

const query = Post.limit(10).offset(20)

query.exec()
// select * from `posts` limit 10 offset 20

Model.prototype.orderBy(key, direction)

Sort query result in ascending or descending order

Parameters:

  • key (string): The specified column name to sort
  • direction (Direction, optional): Sort direction (asc or desc). Default is asc

Return type: Query

const query = Post.orderBy('title', 'desc')

query.exec()
// select * from `posts` order by `title` desc

Model.prototype.select(...keys)

Specifies which column is selected

Parameters:

  • ...keys (string[]): The list of columns to select

Return type: Query

const query = Post.select('id', 'title')

query.exec()
// select `id`, `title` from `posts`

Model.prototype.select(keys)

Specifies which column is selected

Parameters:

  • keys (string[]): The list of columns to select

Return type: Query

const query = Post.select(['id', 'title'])

query.exec()
// select `id`, `title` from `posts`

Model.prototype.select(key)

Specifies which column is selected

Parameters:

  • key (string): The column to select

Return type: Query

const query = Post.select(['title'])

query.exec()
// select `title` from `posts`

Model.prototype.where(key, operator, value)

Add condition to filter records

Parameters

Depending on the operator, the value takes a different type. As follows:

Return type: Query

/*** EXAMPLE 1 ***/
const query = Post.where('title', '=', 'test')

query.exec()
// select * from `posts` where `title` = 'test'

/*** EXAMPLE 2 ***/
const query = Post.where('title', 'in', ['test', 'test2'])

query.exec()
// select * from `posts` where `title` in ('test', 'test2')

/*** EXAMPLE 3 ***/
const query = Post.where('views', 'between', [100, 200])

query.exec()
// select * from `posts` where `views` between 100 and 200

Model.prototype.where(key, value)

Add condition to filter records. The operator is =

Parameters

  • key (string): The column to use for filtering
  • value (AlphaNumeric): The value to use for filtering

Return type: Query

Model.prototype.where(fn)

Sometimes you want to group several where clauses with parentheses, passing a parameter is a method, it will do it

const query = Post.where('id', 1).where((q) => {
  q.where('title', 'test').orWhere('title', 'test2')
})

query.exec()
// select * from `posts` where `id` = 1 and (`title` = 'test' or `title` = 'test2')

Model.prototype.whereIn(key, values)

Add condition to filter records. The operator is in

Parameters

  • key (string): The column to use for filtering
  • value (AlphaNumeric[]): The value to use for filtering

Return type: Query

Model.prototype.whereNotIn(key, values)

Add condition to filter records. The operator is not in

Parameters

  • key (string): The column to use for filtering
  • value (AlphaNumeric[]): The value to use for filtering

Return type: Query

Query

Query.prototype.exec()

Query.prototype.get()

Query.prototype.first()

Query.prototype.limit(value)

Query.prototype.offset(value)

Query.prototype.orderBy(key, direction = 'asc')

Query.prototype.orWhere(fn)

Query.prototype.orWhere(key, operator, value)

Query.prototype.orWhere(key, value)

Query.prototype.select(...keys)

Query.prototype.select(keys)

Query.prototype.select(key)

Query.prototype.where(fn)

Query.prototype.where(key, operator, value)

Query.prototype.where(key, value)

Query.prototype.whereIn(key, values)

Query.prototype.whereNotIn(key, values)

Interfaces

Direction

asc | desc

AlphaNumeric

string | number

Operator

InOperator | RangeOperator | ComparisonOperator | LikeOperator

InOperator

in | not in

RangeOperator

between | not between

ComparisonOperator

= | != | < | <= | > | >=

LikeOperator

like | not like

ansi-escapesargparseansi-regexanymatchansi-stylesbabel-jestbabel-plugin-dynamic-import-nodebabel-plugin-istanbulbabel-plugin-jest-hoistbabel-preset-current-node-syntaxbabel-preset-jestbalanced-matchbinary-extensionsbrace-expansionbracesbrowserslistbserbuffer-fromcamelcasecall-bindcallsitescaniuse-litechalkchar-regexci-infochokidarcjs-module-lexercliuicocollect-v8-coveragecolor-convertcolor-namecommanderconcat-mapconvert-source-mapcross-spawndebugdeepmergededentdefine-propertiesdetect-newlinediff-sequenceselectron-to-chromiumemoji-regexemitteryerror-exescape-string-regexpescaladeesprimaexecaexitexpectfill-rangefb-watchmanfind-upfast-json-stable-stringifyfs-readdir-recursivefs.realpathfunction-bindget-caller-filegensyncget-intrinsicget-package-typeget-streamglobglobalsglob-parentgraceful-fshashas-flaghas-property-descriptorshas-symbolshtml-escaperhuman-signalsimurmurhashimport-localinflightinheritsis-arrayishis-core-moduleis-extglobis-binary-pathis-fullwidth-code-pointis-generator-fnis-globis-numberistanbul-lib-instrumentistanbul-reportsistanbul-lib-reportistanbul-lib-source-mapsjest-changed-filesis-streamjest-circusistanbul-lib-coverageisexejest-clijest-diffjest-eachjest-configjest-docblockjest-get-typejest-haste-mapjest-leak-detectorjest-matcher-utilsjest-mockjest-pnp-resolverjest-message-utiljest-regex-utiljest-resolvejest-resolve-dependenciesjest-runnerjest-environment-nodejest-runtimejest-snapshotjest-utiljest-validatejest-workerjest-watcherjsescjs-tokensjs-yamljson-parse-even-better-errorsjson5lines-and-columnslevenlocate-pathkleurlru-cachemake-dirmakeerrormerge-streammicromatchmimic-fnmsminimatchnode-int64natural-comparenode-releasesnormalize-pathnpm-run-pathobject.assignonetimeobject-keysoncep-limitp-locatepath-existspath-keypath-is-absolutepath-parsepicocolorspicomatchpiratespifypkg-dirp-trypromptspretty-formatreact-isparse-jsonreaddirpresolve-cwdrequire-directoryresolveresolve-fromrimrafresolve.exportssafe-buffersemversignal-exitshebang-commandshebang-regexslashsisteransisource-map-supportsource-mapsprintf-jsstack-utilsstrip-ansistring-widthstring-lengthstrip-bomstrip-final-newlinestrip-json-commentssupports-colorsupports-preserve-symlinks-flagsupports-hyperlinksterminal-linktest-excludetmplto-fast-propertiesto-regex-rangetype-detecttype-festupdate-browserslist-dbwhichwalkerv8-to-istanbulwrappywrap-ansiwrite-file-atomicy18nyallistyargsyocto-queueyargs-parser
0.0.1

1 year ago