@sugarcoated/fondant-sequence
v1.1.1
Published
SQL string builder.
Downloads
2
Readme
Face it, raw SQL strings are ugly. The longer one is, the uglier. Sequence attempts to solve this by implementing a chainable prototype (something that made jQuery famous) so that you can write SQL, in JavaScript, in the way it was meant to be written. This ranges from SELECT
and INSERT
statements, or even all JOIN
types.
As it stands, Sequence has been written in an adaptable way. SQL varies throughout iterations (of which I've used MSSQL, MySQL, WebSQL & SQLite), therefore please report an issue if you encounter syntax differences that can be addressed.
- View the source at src/Modules/Sequence
- View the test file at test/Globals/testSequence
- View the NPM package at @sugarcoated/fondant-sequence
API Reference
new Sequence([identifier])
Creates a new Sequence
instance.
identifier
is an optional parameter, expected as aString
, that identifies the beginning of an SQL statement. When noidentifier
is given, it is defaulted toSELECT
.new Sequence(); new Sequence('CREATE')
Upon construction, you will have access to the following properties:
modifier
is aSequencePart
representing the modifier of the SQL statement. This can beSELECT
,INSERT
,UPDATE
, DELETE,
CREATE,
ALTER`, etc.locator
is aSequencePart
representing the locator in the SQL statement. This is typicallyFROM
orINTO
.condition
is aSequencePart
representing the condition in the SQL statement; typically aWHERE
.joins
is aCollection
containing aSequencePart
for every joined table.group
is aSequencePart
representing the grouping in the SQL statement.order
is aSequencePart
representing the ordering in the SQL statement.use
is aSequencePart
representing an expression generated with anotherSequence
instance for use with creating tables and views.final
is aSequencePart
representing the final of the SQL statement. Currently the only possibility of afinal
is aLIMIT
in MySQL.
.all()
Implements the *
character from all SQL versions.
new Sequence().all()
new Sequence().distinct().all()
.distinct()
Implements the DISTINCT
clause from all SQL versions.
new Sequence().distinct()
.only(columns)
Implements the concept of specifying exact columns in a SELECT
query.
columns
is expected as aArray.<Array.<String, String, String>>
representing the type of selection to perform on a specific column for a group.const mySequence = new Sequence() .only([
// SQL FUNCTION, COLUMN, ALIAS ['COUNT', 'myColumn', 'MC'],
])
.top(amount)
Implements the TOP
clause from Microsoft SQL.
amount
is expected as aNumber
, representing an implication of the amount of records select in the query.new Sequence().top(10)
.view(name)
Implements the CREATE VIEW
clause from all SQL versions.
name
is expected as aString
, representing the name of a nonexistent view.new Sequence('CREATE').view('myView')
.update(table)
Implements the UPDATE
clause from all SQL versions.
table
is expected as aString
, representing the name of an existing table.new Sequence('UPDATE').update('myTable')
.from(table, [alias])
Implements the FROM
clause from all SQL versions.
table
is expected as aString
, representing the name of an existing table.alias
is an optional parameter, expected as aString
, representing the alias for that table. When no alias is given, thelocator
expression has noAS
clause.new Sequence().all().from('myTable', 'MT')
.into(table, columns)
Implements the INTO
clause from all SQL versions.
table
is expected as aString
, representing the name of an existing table.columns
is expected as anArray.<String>
, representing the name of columns on that table.new Sequence('INSERT').into('myTable', ['myColumn'])
.where(column)
Implements the WHERE
clause from all SQL versions.
column
is expected as aString
, representing a column within the table.new Sequence().all().from('myTable').where('myColumn') new Sequence().all().from('myTable').where('myColumn').where('myOtherColumn')
.join(type, table, [alias])
Implements the JOIN
clause from all SQL versions.
type
is expected as aString
, representing the type of join. This can beINNER
,OUTER
,LEFT
,RIGHT
, or anything supported by your SQL version.table
is expected as aString
, representing the name of an existing table.alias
is an optional parameter, expected as aString
, representing the alias for that table. If noalias
is provided, theJOIN
clause has noAS
appended.new Sequence().all().from('myTable').join('INNER', 'myOtherTable') new Sequence().all().from('myTable').join('INNER', 'myOtherTable', 'MOT')
.on(table, left_column, right_column)
Implements the ON
clause from all SQL versions.
table
is expected as aString
, representing the name of the table joined to.left_column
is expected as aString
, representing the column on the left.right_column
is expected as a `String, representing the column on the right.new Sequence() .all().from('myTable') .join('INNER', 'myOtherTable') .on('myOtherTable', 'myOtherColumn', 'myExtraColumn')
.with(sequence)
Embeds an instance of Sequence
within another instance. Used for creating tables and views.
sequence
is expected as aSequence
, representing the query to produce your view.new Sequence('CREATE').view('myVue').use(new Sequence().all().from('myTable'))
.groupBy(column, [ascending])
Implements the GROUP BY
clause from all SQL versions.
column
is expected as aString
, representing a column to group on.ascending
is an optional parameter, expected as aBoolean
, representing whether or not you want the results to be grouped from top to bottom or bottom to top. When noascending
is provided, the value is defaulted tofalse
.new Sequence().all().from('myTable').groupBy('myColumn') new Sequence().all().from('myTable').groupBy('myColumn', true)
.orderBy(column, [ascending])
Implements the ORDER BY
clause from all SQL versions.
column
is expected as aString
, representing the column to order by.ascending
is an optional parameter, expected as aBoolean
, representing whether or not you want the results to be ordered from top to bottom or bottom to top. When noascending
is provided, the value is defaulted tofalse
.new Sequence().all().from('myTable').orderBy('myColumn') new Sequence().all().from('myTable').orderBy('myColumn', true)
.limit([amount])
Implements the LIMIT
clause from MySQL.
amount
is an optional parameter, expected as aNumber
, representing the amount of records to limit the query to. When noamount
is provided, the value defaults to1
.new Sequence().all().from('myTable').limit() new Sequence().all().from('myTable').limit(10)