Skip to content

Storm-Consulting/_hsql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

_hsql

SQL 🤝 hyperscript.

requirements

SQL database running in the browser

installation

  1. include sql.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.10.3/sql-wasm.js"></script>
  1. include _hyperscript
<script src="https://unpkg.com/hyperscript.org@0.9.12/dist/_hyperscript.min.js"></script>
  1. include _hsql:
<script src="https://cdn.jsdelivr.net/gh/Storm-Consulting/_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

  • 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)