FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
wilhelm
wilhelmjson-db
Public
Like
json-db
Home
Code
7
examples
3
src
3
.vtignore
AGENTS.md
README.md
deno.json
test.ts
Branches
1
Pull requests
Remixes
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data โ€“ all from the browser, and deployed in milliseconds.
Sign up now
Code
/
Code
/
Search
https://wilhelm--1d6a439a946411f080a60224a6c84d84.web.val.run
README.md

JsonDB - A Document Database for Val Town

JsonDB is a powerful document database library that provides a MongoDB-like interface over Val Town's SQLite. It abstracts away the complexity of SQL while maintaining the reliability and performance of SQLite.

Features

  • ๐Ÿš€ Document-based: Store and query JSON documents like MongoDB
  • ๐Ÿ” Powerful Queries: Filter, sort, and paginate your data
  • ๐Ÿ“Š Collection Management: Create, drop, and manage collections
  • ๐Ÿ”„ Migrations: Built-in migration system for schema changes
  • ๐ŸŒ REST API: Ready-to-use HTTP endpoints for Val Town
  • ๐Ÿ“ˆ Statistics: Get insights into your database usage
  • ๐Ÿ›ก๏ธ TypeScript: Full TypeScript support with type safety

Quick Start

Basic Usage

import { jsonDb } from "./src/json-db.ts"; // Create a collection and insert a document const user = await jsonDb.insert("users", { name: "John Doe", email: "john@example.com", age: 30, preferences: { theme: "dark", notifications: true } }); // Find documents const users = await jsonDb.find("users", { where: { age: { $gte: 25 } }, sort: [{ field: "name", direction: "asc" }], limit: 10 }); // Update documents await jsonDb.updateById("users", user.id, { age: 31, preferences: { theme: "light" } }); // Delete documents await jsonDb.deleteById("users", user.id);

Collection Management

// Create a collection await jsonDb.createCollection("products"); // List all collections const collections = await jsonDb.listCollections(); // Get collection statistics const stats = await jsonDb.getCollectionStats("users"); // Drop a collection await jsonDb.dropCollection("old_collection");

Advanced Queries

// Find with complex filtering const results = await jsonDb.find("products", { where: { category: "electronics", price: { $lt: 500 }, inStock: true }, sort: [ { field: "price", direction: "asc" }, { field: "name", direction: "desc" } ], limit: 20, offset: 0 }); // Count documents const count = await jsonDb.count("products", { category: "electronics" }); // Batch operations const products = [ { name: "Laptop", price: 999.99, category: "electronics" }, { name: "Mouse", price: 29.99, category: "electronics" } ]; await jsonDb.insertMany("products", products);

REST API

JsonDB includes a complete REST API for Val Town. Use the HTTP example to create endpoints:

// GET /collections/users/documents // POST /collections/users/documents // PUT /collections/users/documents/:id // DELETE /collections/users/documents/:id

API Endpoints

  • GET /health - Health check
  • GET /stats - Database statistics
  • GET /collections - List collections
  • POST /collections - Create collection
  • DELETE /collections/:name - Delete collection
  • GET /collections/:name/documents - Find documents
  • POST /collections/:name/documents - Create document
  • GET /collections/:name/documents/:id - Get document by ID
  • PUT /collections/:name/documents/:id - Update document
  • DELETE /collections/:name/documents/:id - Delete document

Migrations

JsonDB includes a powerful migration system:

import { migrationManager } from "./src/migrations.ts"; // Add a custom migration migrationManager.addMigration({ version: 5, name: "add_user_roles", up: async () => { // Add new fields or tables await sqlite.execute(` ALTER TABLE _documents ADD COLUMN role TEXT DEFAULT 'user' `); }, down: async () => { // Rollback changes await sqlite.execute(` ALTER TABLE _documents DROP COLUMN role `); } }); // Run migrations const results = await migrationManager.migrate(); // Rollback to specific version await migrationManager.rollback(3);

Examples

Check out the examples/ directory for complete examples:

  • basic-usage.ts - Core functionality examples
  • val-town-http.ts - REST API implementation

API Reference

JsonDB Class

Collection Management

  • createCollection(name: string): Promise<boolean>
  • dropCollection(name: string): Promise<boolean>
  • listCollections(): Promise<string[]>
  • getCollectionStats(name: string): Promise<CollectionStats | null>

Document Operations

  • insert(collection: string, document: Document): Promise<InsertResult>
  • insertMany(collection: string, documents: Document[]): Promise<InsertResult[]>
  • find(collection: string, options?: QueryOptions): Promise<Document[]>
  • findById(collection: string, id: string): Promise<Document | null>
  • update(collection: string, where: Record<string, any>, update: Partial<Document>): Promise<UpdateResult>
  • updateById(collection: string, id: string, update: Partial<Document>): Promise<UpdateResult>
  • delete(collection: string, where: Record<string, any>): Promise<DeleteResult>
  • deleteById(collection: string, id: string): Promise<DeleteResult>
  • count(collection: string, where?: Record<string, any>): Promise<number>

Database Management

  • getStats(): Promise<{ collections: number; totalDocuments: number; collectionsStats: CollectionStats[] }>

Types

interface Document { id?: string; [key: string]: any; } interface QueryOptions { limit?: number; offset?: number; sort?: { field: string; direction: 'asc' | 'desc' }[]; where?: Record<string, any>; } interface CollectionStats { name: string; count: number; createdAt: string; lastModified: string; }

Installation

  1. Copy the src/ directory to your Val Town project
  2. Import and use:
import { jsonDb } from "./src/json-db.ts";

Val Town Integration

JsonDB is specifically designed for Val Town and uses:

  • Val Town's SQLite standard library
  • Proper error handling for serverless environment
  • TypeScript support with Val Town types
  • REST API patterns for HTTP vals

Performance Tips

  1. Use indexes: The migration system creates indexes for common fields
  2. Batch operations: Use insertMany() for multiple documents
  3. Pagination: Use limit and offset for large datasets
  4. Collection cleanup: Drop unused collections to save space

Limitations

  • JSON field queries are limited to simple equality matching
  • Complex aggregation queries require custom SQL
  • No built-in full-text search (can be added via migrations)
  • Document size limited by SQLite's TEXT storage

Contributing

This library is designed for Val Town projects. Feel free to extend it with additional features or optimizations specific to your use case.

License

MIT License - feel free to use in your Val Town projects!

HTTP
  • simple-http.val.ts
    wilhelm--1dโ€ฆ84.web.val.run
  • val-town-http.ts
    wilhelm--1dโ€ฆ84.web.val.run
Code
examplessrc.vtignoreAGENTS.mdREADME.mddeno.jsontest.ts
Go to top
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Product
FeaturesPricing
Developers
DocsStatusAPI ExamplesNPM Package Examples
Explore
ShowcaseTemplatesNewest ValsTrending ValsNewsletter
Company
AboutBlogCareersBrandhi@val.town
Terms of usePrivacy policyAbuse contact
ยฉ 2025 Val Town, Inc.