How alexandrajs/sandstorm works
Project type and purpose
• Sandstorm is an open‐source Node.js library (no standalone frontend or microservice layers). It exposes a programmatic API for defining schemas, instantiating models, querying a MongoDB backend, and enforcing type/validation rules.
File structure
• Top‐level folders:
• src – the core implementation
• bench – performance benchmarks against model, schema, types
• test – unit and integration tests (includes a docker‐compose.yml to spin up MongoDB)
• Key source files in src:
• sandstorm.js (the main class that users instantiate)
• schema.js (registry + schema‐definition logic)
• model.js (model instance, set/merge/save/delete operations)
• cursor.js (abstraction over MongoDB cursors)
• types/* and properties/* (typecasting + validation logic)
• index.js – library entrypoint, exports Sandstorm API.
• package.json – declares dependencies (e.g., mongodb driver, promises).
README for insights
• Users register schemas via Sandstorm.register(), creating an internal registry.
• They create models, call set/merge, then save() – under the hood executed via MongoDB driver.
• Queries via Sandstorm.find() return Cursor instances with toArray(), delete(), etc.
Main components
a. Client Application code (the “User”)
b. Sandstorm API Layer
· index.js (exports)
· sandstorm.js (core singleton/class)
c. Schema Registry/Subsystem
· schema.js
· types/ (string, number, boolean, objectId, mixed, etc.)
· properties/ (validation + casting strategies per type)
d. Model Layer
· model.js (Model instances)
· cursor.js (Query results abstraction)
e. Persistence Layer
· MongoDB driver (external service)
· MongoDB instance (spun up in tests or provided by user)
f. Benchmark & Test Harness (optional side components)
· bench scripts (bench/*.js)
· test suite (integration via docker-compose + unit tests)
Relationships and interactions
• The Client calls Sandstorm.register() → Schema Registry builds internal Schema + Type/Property handlers
• Client calls Sandstorm.create() → Model instance linked to a schema
• Model.set()/merge() → invokes Properties to cast/validate fields
• Model.save()/delete() → uses MongoDB driver to persist or remove documents
• Client calls Sandstorm.find() → returns Cursor → executes a Mongo query → cursor.js wraps driver cursor
• Cursor.toArray(), Cursor.delete() → further interactions with the driver
Architectural patterns
• Registry pattern for schemas
• Factory pattern for creating property/type handlers
• Strategy pattern for validation (per‐type logic in properties/*)
• Wrapper/Adapter around the MongoDB driver (cursor.js, model.js)
• Modular code organization for extensibility (types and properties directories)
• Clear component labels (with file names)
• Optional: note protocols (e.g., “uses MongoDB wire protocol”)