Files
metabuilder/dbal/backends/sqlite/schema.sql
johndoe6345789 49f40177b5 Generated by Spark: I was thinking more like this, you can replace python with ts if you like: dbal/
README.md
  LICENSE
  AGENTS.md

  api/                          # Language-agnostic contract (source of truth)
    schema/
      entities/                 # Entity definitions (conceptual models)
        user.yaml
        session.yaml
        ...
      operations/               # CRUD + domain operations (semantic, not SQL)
        user.ops.yaml
        ...
      errors.yaml               # Standard error codes (conflict, not_found, etc.)
      capabilities.yaml         # Feature flags per backend (tx, joins, ttl, etc.)
    idl/
      dbal.proto                # Optional: RPC/IPC contract if needed
      dbal.fbs                  # Optional: FlatBuffers schema if you prefer
    versioning/
      compat.md                 # Compatibility rules across TS/C++

  common/                       # Shared test vectors + fixtures + golden results
    fixtures/
      seed/
      datasets/
    golden/
      query_results/
    contracts/
      conformance_cases.yaml

  ts/                           # Development implementation in TypeScript
    package.json
    tsconfig.json
    src/
      index.ts                  # Public entrypoint (creates client)
      core/
        client.ts               # DBAL client facade
        types.ts                # TS types mirroring api/schema
        errors.ts               # Error mapping to api/errors.yaml
        validation/             # Runtime validation (zod/io-ts/etc.)
          input.ts
          output.ts
        capabilities.ts         # Capability negotiation
        telemetry/
          logger.ts
          metrics.ts
          tracing.ts
      adapters/                 # Backend implementations (TS)
        prisma/
          index.ts
          prisma_client.ts      # Wraps Prisma client (server-side only)
          mapping.ts            # DB <-> entity mapping, select shaping
          migrations/           # Optional: Prisma migration helpers
        sqlite/
          index.ts
          sqlite_driver.ts
          schema.ts
          migrations/
        mongodb/
          index.ts
          mongo_driver.ts
          schema.ts
      query/                    # Query builder / AST (no backend leakage)
        ast.ts
        builder.ts
        normalize.ts
        optimize.ts
      runtime/
        config.ts               # DBAL config (env, URLs, pool sizes)
        secrets.ts              # Secret loading boundary (server-only)
      util/
        assert.ts
        retry.ts
        backoff.ts
        time.ts
    tests/
      unit/
      integration/
      conformance/              # Runs common/contract vectors on TS adapters
      harness/
        setup.ts

  cpp/                          # Production implementation in C++
    CMakeLists.txt
    include/
      dbal/
        dbal.hpp                # Public API
        client.hpp              # Facade
        types.hpp               # Entity/DTO types
        errors.hpp
        capabilities.hpp
        telemetry.hpp
        query/
          ast.hpp
          builder.hpp
          normalize.hpp
        adapters/
          adapter.hpp           # Adapter interface
          sqlite/
            sqlite_adapter.hpp
          mongodb/
            mongodb_adapter.hpp
          prisma/
            prisma_adapter.hpp  # Usually NOT direct; see note below
        util/
          expected.hpp
          result.hpp
          uuid.hpp
    src/
      client.cpp
      errors.cpp
      capabilities.cpp
      telemetry.cpp
      query/
        ast.cpp
        builder.cpp
        normalize.cpp
      adapters/
        sqlite/
          sqlite_adapter.cpp
          sqlite_pool.cpp
          sqlite_migrations.cpp
        mongodb/
          mongodb_adapter.cpp
          mongo_pool.cpp
        prisma/
          prisma_adapter.cpp    # See note below (often an RPC bridge)
      util/
        uuid.cpp
        backoff.cpp
    tests/
      unit/
      integration/
      conformance/              # Runs common/contract vectors on C++ adapters
      harness/
        main.cpp

  backends/                     # Backend-specific assets not tied to one lang
    sqlite/
      schema.sql
      migrations/
    mongodb/
      indexes.json
    prisma/
      schema.prisma
      migrations/

  tools/                        # Codegen + build helpers (prefer Python)
    codegen/
      gen_types.py              # api/schema -> ts/core/types.ts and cpp/types.hpp
      gen_errors.py
      gen_capabilities.py
    conformance/
      run_all.py                # runs TS + C++ conformance suites
    dev/
      lint.py
      format.py

  scripts/                      # Cross-platform entrypoints (Python per your pref)
    build.py
    test.py
    conformance.py
    package.py

  dist/                         # Build outputs (gitignored)
  .github/
    workflows/
      ci.yml

  .gitignore
  .editorconfig
2025-12-24 20:13:18 +00:00

163 lines
5.0 KiB
SQL

CREATE TABLE users (
id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
role TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE credentials (
id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
first_login INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (username) REFERENCES users(username) ON DELETE CASCADE
);
CREATE TABLE sessions (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
token TEXT NOT NULL UNIQUE,
expires_at TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_activity TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
CREATE INDEX idx_sessions_user_id ON sessions(user_id);
CREATE INDEX idx_sessions_expires_at ON sessions(expires_at);
CREATE TABLE page_views (
id TEXT PRIMARY KEY,
slug TEXT NOT NULL UNIQUE,
title TEXT NOT NULL,
description TEXT,
level INTEGER NOT NULL,
layout TEXT NOT NULL,
is_active INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_page_views_level ON page_views(level);
CREATE INDEX idx_page_views_is_active ON page_views(is_active);
CREATE TABLE component_hierarchy (
id TEXT PRIMARY KEY,
page_id TEXT NOT NULL,
parent_id TEXT,
component_type TEXT NOT NULL,
"order" INTEGER NOT NULL DEFAULT 0,
props TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (page_id) REFERENCES page_views(id) ON DELETE CASCADE,
FOREIGN KEY (parent_id) REFERENCES component_hierarchy(id) ON DELETE CASCADE
);
CREATE INDEX idx_component_hierarchy_page_id ON component_hierarchy(page_id);
CREATE INDEX idx_component_hierarchy_parent_id ON component_hierarchy(parent_id);
CREATE INDEX idx_component_hierarchy_page_order ON component_hierarchy(page_id, "order");
CREATE TABLE workflows (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
description TEXT,
trigger TEXT NOT NULL,
trigger_config TEXT NOT NULL,
steps TEXT NOT NULL,
is_active INTEGER NOT NULL DEFAULT 1,
created_by TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (created_by) REFERENCES users(id)
);
CREATE INDEX idx_workflows_trigger ON workflows(trigger);
CREATE INDEX idx_workflows_is_active ON workflows(is_active);
CREATE TABLE lua_scripts (
id TEXT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
description TEXT,
code TEXT NOT NULL,
is_sandboxed INTEGER NOT NULL DEFAULT 1,
allowed_globals TEXT NOT NULL,
timeout_ms INTEGER NOT NULL DEFAULT 5000,
created_by TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (created_by) REFERENCES users(id)
);
CREATE INDEX idx_lua_scripts_is_sandboxed ON lua_scripts(is_sandboxed);
CREATE TABLE packages (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
version TEXT NOT NULL,
description TEXT,
author TEXT NOT NULL,
manifest TEXT NOT NULL,
is_installed INTEGER NOT NULL DEFAULT 0,
installed_at TEXT,
installed_by TEXT,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (installed_by) REFERENCES users(id),
UNIQUE(name, version)
);
CREATE INDEX idx_packages_is_installed ON packages(is_installed);
CREATE TRIGGER update_users_timestamp
AFTER UPDATE ON users
BEGIN
UPDATE users SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
CREATE TRIGGER update_credentials_timestamp
AFTER UPDATE ON credentials
BEGIN
UPDATE credentials SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
CREATE TRIGGER update_sessions_timestamp
AFTER UPDATE ON sessions
BEGIN
UPDATE sessions SET last_activity = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
CREATE TRIGGER update_page_views_timestamp
AFTER UPDATE ON page_views
BEGIN
UPDATE page_views SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
CREATE TRIGGER update_component_hierarchy_timestamp
AFTER UPDATE ON component_hierarchy
BEGIN
UPDATE component_hierarchy SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
CREATE TRIGGER update_workflows_timestamp
AFTER UPDATE ON workflows
BEGIN
UPDATE workflows SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
CREATE TRIGGER update_lua_scripts_timestamp
AFTER UPDATE ON lua_scripts
BEGIN
UPDATE lua_scripts SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;
CREATE TRIGGER update_packages_timestamp
AFTER UPDATE ON packages
BEGIN
UPDATE packages SET updated_at = CURRENT_TIMESTAMP WHERE id = NEW.id;
END;