Pure depedency-less NodeJS Type-safe SQL query bulder
We don't have a package at npm registry yet, but you can install it from GitHub:
npm i https://github.com/isaqueks/powersql.git#release
(You can remove the #release
if you want the pure TypeScript development version)
- Import the PowerSQL module:
const { PowerSQL, PowerSQLDefaults, PowerSQLTable, PowerSQLTableColumn } = require('powersql');
- Create the query using
PowerSQL
andPowerSQLDefaults
:
const myTable = new PowerSQLTable('myTable', [
new PowerSQLTableColumn('id', 'INT', ['PRIMARY KEY']),
new PowerSQLTableColumn('name', 'TEXT')
])
const sqlQuery = PowerSQL(
PowerSQLDefaults.createTable(myTable)
)
// CREATE TABLE IF NOT EXISTS ...
const sqlQuery = PowerSQL(
PowerSQLDefaults.select('*'),
PowerSQLDefaults.from(myTable),
PowerSQLDefaults.where(
PowerSQLDefaults.equal(
'name',
PowerSQLDefaults.param(
'The name I want to select',
'TEXT'
)
)
)
)
// SELECT * FROM myTable WHERE name = 'The name I want to select';
const data = {
name: 'John'
}
const sqlQuery = PowerSQL(
PowerSQLDefaults.selectObject(myTable, data)
)
// SELECT * FROM myTable WHERE name = 'John';
Full documentation will be provided later. PowerSQL stills in development and should only be used for tiny and small queries.
- PowerSQL does escape quotes when using
PowerSQLDefaults.param
:PowerSQLDefaults.param("my 'string'", 'TEXT')
- PowerSQL only builds the SQL query, it doesn't do any database connection.
- Don't use PowerSQL for complex queries and serious projects. It sills in development and is going to be a lot improved, but it stills being tested and fixed.
- Feel free to open an Issue or a Pull request! PowerSQL is totally open-source, we are waiting your contribution!