ApemanDB

8.7.4 · active · verified Wed Apr 22

ApemanDB is a database abstraction layer built as a thin wrapper around the popular Sequelize ORM, specifically optimized for use within the 'apeman' project ecosystem. It simplifies database interactions by abstracting direct Sequelize usage, particularly for defining models through a structured JSON schema. The library is currently at version 8.7.4 and, while no explicit release cadence is documented, its version history suggests active development. A key differentiator is its tight integration with `apemanenv` for environment-specific database configurations, allowing developers to manage database settings (like dialect, host, credentials) in a centralized, declarative manner. This approach aims to streamline database setup and model management for apeman web applications, reducing boilerplate associated with raw Sequelize initialization and model definition.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates setting up `apemanenv` for database configuration, defining a model using `apemandb`'s JSON schema, and initializing the `apemandb` instance to synchronize schema and perform basic CRUD operations.

const path = require('path');
const apemanenv = require('apemanenv');
const apemandb = require('apemandb');

// 1. Define environment configuration (env/database.json)
// In a real project, this would be loaded by apemanenv automatically.
// For quickstart, we'll simulate the structure.
const envDir = path.join(__dirname, 'env');
require('fs').mkdirSync(envDir, { recursive: true });
require('fs').writeFileSync(path.join(envDir, 'database.json'), JSON.stringify({
  "default": {
    "DIALECT": "sqlite",
    "SCHEMA": "apeman-demo-web",
    "STORAGE": ":memory:"
  },
  "development": {
    "SCHEMA": "apeman-demo-web_dev",
    "STORAGE": "./tmp/dev-database.db"
  },
  "test": {
    "DIALECT": "sqlite",
    "SCHEMA": "apeman-demo-web_test",
    "STORAGE": ":memory:"
  }
}, null, 2));

// 2. Define a model (db/models/user.json)
const modelsDir = path.join(__dirname, 'db', 'models');
require('fs').mkdirSync(modelsDir, { recursive: true });
require('fs').writeFileSync(path.join(modelsDir, 'user.json'), JSON.stringify({
  "$name": "User",
  "$description": "A user model",
  "$attributes": {
    "username": {
      "$type": "STRING",
      "$unique": true
    },
    "introText": {
      "$type": "STRING(1024)",
      "$nullable": true
    }
  }
}, null, 2));

async function main() {
  // Load environment variables
  const envConfig = apemanenv(__dirname); // Simulates env loading from __dirname
  
  // Initialize ApemanDB with configuration and models
  const db = apemandb.create(envConfig, { modelsDir: modelsDir });

  try {
    // Connect to the database and synchronize models (create tables)
    await db.sync({ force: true }); // `force: true` drops tables before recreating
    console.log('Database and tables synchronized!');

    // Create a new user record
    const newUser = await db.models.User.create({ username: 'john_doe', introText: 'Hello world!' });
    console.log('Created user:', newUser.toJSON());

    // Find a user
    const foundUser = await db.models.User.findOne({ where: { username: 'john_doe' } });
    console.log('Found user:', foundUser.toJSON());

  } catch (error) {
    console.error('Database operation failed:', error.message);
  } finally {
    // Close the database connection if it's not an in-memory SQLite
    if (db.sequelize.options.dialect !== 'sqlite' || db.sequelize.options.storage !== ':memory:') {
      await db.sequelize.close();
      console.log('Database connection closed.');
    }
  }
}

main();

view raw JSON →