_hsql
_hsql copied to clipboard
SQL 🤝 hyperscript.
_hsql
SQL 🤝 hyperscript.
requirements
SQL database running in the browser
installation
- include sql.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.10.3/sql-wasm.js"></script>
- include _hyperscript
<script src="https://unpkg.com/[email protected]/dist/_hyperscript.min.js"></script>
- include _hsql:
<script src="https://cdn.jsdelivr.net/gh/3c2f3e/_hsql/index.min.js"></script>
usage
statements
direct SQL statements
db 'CREATE TABLE users (firstname CHAR(50) NOT NULL, lastname CHAR(50) NOT NULL, age INT(3) NOT NULL)'
prepared SQL statements with unnamed parameters
db 'INSERT INTO users VALUES (?,?,?)' with ['John', 'Doe', 27]
prepared SQL statements with named parameters
db 'INSERT INTO users VALUES ($firstname,$lastname,$age)' with {$firstname: 'John', $lastname: 'Doe', $age: 27}
error handling
errors can be handled using catch
:
db 'INSERT INTO users VALUES (?,?,?)' with record
catch error
// Handle errors...
working with the DOM
using values from input fields
HTML
<input id="firstname" value="John" type="text">
<input id="lastname" value="Doe" type="text">
<input id="age" value="27" type="number">
hyperscript
set record to [#firstname.value, #lastname.value, #age.value as a Number]
db 'INSERT INTO users VALUES (?,?,?)' with record
creating tables from query results
table it // "it" is the result of the db command
put it into #results // HTML
examples
development
- [X] Integration of SQL.js
- [X] Running direct statements against SQL.js
- [X] Running prepared statements against SQL.js
- [X] Responding with HTML
- [ ] Native SQL statements using hyperscript (
dbselect * from users where id = <#id/>.value
)