sqlite_docsmain.tsx3 matches
1import { API_URL } from "https://esm.town/v/std/API_URL";
2import { LibsqlError, type TransactionMode } from "npm:@libsql/client";
3import { z } from "npm:zod";
3536async function execute(statement: InStatement): Promise<ResultSet> {
37const res = await fetch(`${API_URL}/v1/sqlite/execute`, {
38method: "POST",
39headers: {
4950async function batch(statements: InStatement[], mode?: TransactionMode): Promise<ResultSet[]> {
51const res = await fetch(`${API_URL}/v1/sqlite/batch`, {
52method: "POST",
53headers: {
1import { API_URL } from "https://esm.town/v/std/API_URL";
2import { ValTownBlobError } from "https://esm.town/v/std/ValTownBlobError";
3import { ValTownBlobNotFoundError } from "https://esm.town/v/std/ValTownBlobNotFoundError";
82async function list(prefix?: string): Promise<{ key: string; size: number; lastModified: string }[]> {
83let querystring = prefix ? `?prefix=${encodeURIComponent(prefix)}` : "";
84const res = await fetch(`${API_URL}/v1/blob${querystring}`, {
85headers: {
86Authorization: `Bearer ${Deno.env.get("valtown")}`,
9596async function delete_(key: string) {
97const res = await fetch(`${API_URL}/v1/blob/${encodeURIComponent(key)}`, {
98method: "DELETE",
99headers: {
108109async function get(key: string) {
110const res = await fetch(`${API_URL}/v1/blob/${encodeURIComponent(key)}`, {
111headers: {
112Authorization: `Bearer ${Deno.env.get("valtown")}`,
124125async function set(key: string, value: BodyInit) {
126const res = await fetch(`${API_URL}/v1/blob/${encodeURIComponent(key)}`, {
127method: "POST",
128headers: {
sqlite_adminREADME.md1 match
9To use it on your own Val Town SQLite database, [fork it](https://www.val.town/v/stevekrouse/sqlite_admin/fork) to your account.
1011It uses [basic authentication](https://www.val.town/v/pomdtr/basicAuth) with your [Val Town API Token](https://www.val.town/settings/api) as the password (leave the username field blank).
2223export async function createScreenshot(code: string, theme: string = "dark-plus"): Promise<URL> {
24const apiUrl = "https://sourcecodeshots.com/api/image/permalink";
25const { url } = await ky.post(apiUrl, {
26json: {
27code,
891. Click `Fork`
102. Change `location` (Line 4) to describe your location. It accepts fairly flexible English descriptions which it turns into locations via [nominatim's geocoder API](https://www.val.town/v/stevekrouse/nominatimSearch).
113. Click `Run`
12
weatherGPTREADME.md1 match
1If you fork this, you'll need to set `OPENAI_API_KEY` in your [Val Town Secrets](https://www.val.town/settings/secrets).
23
love_letterREADME.md7 matches
24In any other platform, I would have just given up and moved on. But Val Town is different. I was confident that I could address this issue in userspace, without having to wait for the platform to implement it.
2526Val Town allows you to run a val on a cron schedule, so I wrote a val that would fetch all the vals from the API, and store them as a sqlite table (did I mention that every user get it's own sqlite database ?).
2728```ts
36await options.sqlite.execute(createQuery);
3738let url = "https://api.val.town/v1/search/vals?query=%20&limit=100";
3940// fetch all vals, and store them in the sqlite table
70There was still a challenge to overcome: the table was part of my account database, and I didn't want to give everyone access to it (there are some sensitive tables in there).
7172One way to solve this issue would be to publish a stripped-down api that only allows a few predefined queries. But that would be boring, and I wanted to give users the full power of SQL.
7374So I decided to isolate the val table in a separate account. There is a neat trick to achieve this on val.town: each val get's it own email address, and email sent to vals can be forwarded to your own email address.
96## Publishing the Table
9798The val.town stdlib provides a neat `rpc` function that provides a simple way to expose a function as an API. So I decided to write a simple val that would run a query on the table, and return the result.
99100```ts
123```
124125Of course I don't expect the average val.town user to use shell commands to run queries, so I also built an [helper val](https://val.town/v/sqlite/db) to interact with the API, allowing users to run queries from their own vals.
126127```ts
133```
134135I've seen some really cool vals built on top of this API. Someone even wrote down [a guide](https://gist.github.com/tarasglek/076879bd06460e223bf8366031cf4c88) to help users interact with it from the command-line!
136137I hope that someone will build an search UI to interact with it at some point, but in the meantime, you can use a community-contributed [sqlite web interface](https://sqlite-explorer.web.val.run) to run queries on top of the `vals` table.
139## Val.town as a code-taking app
140141As I've tried to show, having both a runtime, an editor and an API on the same platform is quite a magic formula. It's probably why val.town resonates so much with me.
142143Using CodeSandbox, Stackblitz, Repl.it, Gitpod, Github Codespaces or Gitpod feels pretty much the same, everything still revolves around the same concept of a project/repository. They feel uninspired somehow, trying to replicate the desktop IDE experience in the browser, instead of embracing the new possibilities that the web platform offers.
23// Send a pushover message.
4// token, user, and other opts are as specified at https://pushover.net/api
5export default async function pushover({
6token,
16url: string;
17}) {
18return await fetch("https://api.pushover.net/1/messages.json", {
19method: "POST",
20headers: {