Skip to content

Storm-Consulting/_hsql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

8 Commits

Repository files navigation

_hsql

SQL 🤝 hyperscript.

requirements

SQL database running in the browser

installation

  1. includesql.js
<scriptsrc= "https://cdnjs.cloudflare /ajax/libs/sql.js/1.10.3/sql-wasm.js"></script>
  1. include_hyperscript
<scriptsrc= "https://unpkg /[email protected]/dist/_hyperscript.min.js"></script>
  1. include _hsql:
<scriptsrc= "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 usingcatch:

db'INSERT INTO users VALUES (?,?,?)'withrecord
catcherror
// Handle errors...

working with the DOM

using values from input fields

HTML

<inputid= "firstname"value= "John"type= "text">
<inputid= "lastname"value= "Doe"type= "text">
<inputid= "age"value= "27"type= "number">

hyperscript

setrecordto[#firstname.value,#lastname.value,#age.valueasaNumber]
db'INSERT INTO users VALUES (?,?,?)'withrecord

creating tables from query results

tableit// "it" is the result of the db command
putitinto#results// HTML

examples

development

  • Integration of SQL.js
  • Running direct statements against SQL.js
  • Running prepared statements against SQL.js
  • Responding with HTML
  • Native SQL statements using hyperscript (dbselect * from users where id = <#id/>.value)