@sugarcoated/fondant-sequence
v1.1.1
Published
SQL string builder.
Downloads
6
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.
identifieris an optional parameter, expected as aString, that identifies the beginning of an SQL statement. When noidentifieris given, it is defaulted toSELECT.new Sequence(); new Sequence('CREATE')
Upon construction, you will have access to the following properties:
modifieris aSequencePartrepresenting the modifier of the SQL statement. This can beSELECT,INSERT,UPDATE, DELETE,CREATE,ALTER`, etc.locatoris aSequencePartrepresenting the locator in the SQL statement. This is typicallyFROMorINTO.conditionis aSequencePartrepresenting the condition in the SQL statement; typically aWHERE.joinsis aCollectioncontaining aSequencePartfor every joined table.groupis aSequencePartrepresenting the grouping in the SQL statement.orderis aSequencePartrepresenting the ordering in the SQL statement.useis aSequencePartrepresenting an expression generated with anotherSequenceinstance for use with creating tables and views.finalis aSequencePartrepresenting the final of the SQL statement. Currently the only possibility of afinalis aLIMITin 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.
columnsis 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.
amountis 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.
nameis 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.
tableis 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.
tableis expected as aString, representing the name of an existing table.aliasis an optional parameter, expected as aString, representing the alias for that table. When no alias is given, thelocatorexpression has noASclause.new Sequence().all().from('myTable', 'MT')
.into(table, columns)
Implements the INTO clause from all SQL versions.
tableis expected as aString, representing the name of an existing table.columnsis 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.
columnis 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.
typeis expected as aString, representing the type of join. This can beINNER,OUTER,LEFT,RIGHT, or anything supported by your SQL version.tableis expected as aString, representing the name of an existing table.aliasis an optional parameter, expected as aString, representing the alias for that table. If noaliasis provided, theJOINclause has noASappended.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.
tableis expected as aString, representing the name of the table joined to.left_columnis expected as aString, representing the column on the left.right_columnis 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.
sequenceis 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.
columnis expected as aString, representing a column to group on.ascendingis 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 noascendingis 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.
columnis expected as aString, representing the column to order by.ascendingis 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 noascendingis 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.
amountis an optional parameter, expected as aNumber, representing the amount of records to limit the query to. When noamountis provided, the value defaults to1.new Sequence().all().from('myTable').limit() new Sequence().all().from('myTable').limit(10)
