npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@scriptdb/dmr

v1.0.0

Published

DMR module resolver for script database

Downloads

7

Readme

@scriptdb/dmr

Database Module Resolver for the script database, providing dynamic module loading and resolution capabilities for database-related modules.

Features

  • Dynamic module loading: Load modules from directories at runtime
  • Module registry: Maintain a registry of all loaded modules
  • Require-like functionality: Access modules using a require-like API
  • Import functionality: Access modules using an import-like API
  • Context creation: Create a context with all loaded modules

Installation

bun add @scriptdb/dmr

Quick Start

import { DatabaseModuleResolver } from '@scriptdb/dmr';

// Resolve modules from a directory
const context = await DatabaseModuleResolver('./database-modules');

// Access modules directly from the context
const userModule = context.user;
const authModule = context.auth;

// Or use the require-like functionality
const utils = context.require('utils');

API Reference

DatabaseModuleResolver(basePath)

Resolves and loads modules from the specified directory, creating a context with all loaded modules.

await DatabaseModuleResolver(basePath: string): Promise<Record<string, any>>
  • basePath (string): The base directory path where modules are located

Returns a promise that resolves with a context object containing all loaded modules.

Context Methods

require(moduleName)

Loads a module by name.

context.require(moduleName: string): any
  • moduleName (string): The name of the module to load

Returns the module's default export if available, otherwise the module itself.

import(moduleName)

Asynchronously loads a module by name.

context.import(moduleName: string): Promise<{ default: any }>
  • moduleName (string): The name of the module to load

Returns a promise that resolves with an object containing the module's default export.

Module Structure

To be compatible with the Database Module Resolver, your modules should be TypeScript files (.ts) in the specified directory:

database-modules/
├── user.ts          # User module
├── auth.ts          # Authentication module
├── utils.ts         # Utility module
└── index.ts         # Optional main entry point

Each module can export functions, classes, or objects:

// database-modules/user.ts
export interface User {
  id: number;
  name: string;
  email: string;
}

export class UserService {
  async getUser(id: number): Promise<User> {
    // Implementation here
    return { id, name: 'John Doe', email: '[email protected]' };
  }
}

export default new UserService();

Examples

Basic Module Loading

import { DatabaseModuleResolver } from '@scriptdb/dmr';

// Load modules from the ./database-modules directory
const context = await DatabaseModuleResolver('./database-modules');

// Access modules directly
const userModule = context.user;
if (userModule && userModule.getUser) {
  const user = await userModule.getUser(1);
  console.log(user);
}

// Use the require-like functionality
const utils = context.require('utils');
if (utils && utils.formatDate) {
  const formattedDate = utils.formatDate(new Date());
  console.log(formattedDate);
}

Module with Default Export

// database-modules/database.ts
export class Database {
  private connection: any;

  constructor() {
    // Initialize connection
  }

  async query(sql: string, params?: any[]) {
    // Execute query
    return { sql, params, results: [] };
  }

  async close() {
    // Close connection
  }
}

export default new Database();

// main.ts
import { DatabaseModuleResolver } from '@scriptdb/dmr';

const context = await DatabaseModuleResolver('./database-modules');

// Access the default export
const db = context.database;
const result = await db.query('SELECT * FROM users');
console.log(result);

Using the Import Functionality

import { DatabaseModuleResolver } from '@scriptdb/dmr';

const context = await DatabaseModuleResolver('./database-modules');

// Use the import functionality to load a module
const module = await context.import('auth');
const authService = module.default;

if (authService && authService.authenticate) {
  const result = await authService.authenticate('username', 'password');
  console.log(result);
}

Advanced Usage with Type Safety

import { DatabaseModuleResolver } from '@scriptdb/dmr';

interface ModuleContext {
  user?: {
    getUser: (id: number) => Promise<{ id: number; name: string }>;
  };
  auth?: {
    authenticate: (username: string, password: string) => Promise<boolean>;
  };
  utils?: {
    formatDate: (date: Date) => string;
  };
  require: (moduleName: string) => any;
  import: (moduleName: string) => Promise<{ default: any }>;
}

const context = await DatabaseModuleResolver('./database-modules') as ModuleContext;

if (context.user && context.auth) {
  const user = await context.user.getUser(1);
  const isAuthenticated = await context.auth.authenticate('username', 'password');
  
  console.log(`User: ${user.name}, Authenticated: ${isAuthenticated}`);
}

Error Handling

The Database Module Resolver will throw an error if:

  • The specified directory doesn't exist
  • There are syntax errors in the module code
  • A module is not found when using require() or import()
import { DatabaseModuleResolver } from '@scriptdb/dmr';

try {
  const context = await DatabaseModuleResolver('./non-existent-directory');
} catch (error) {
  console.error('Failed to load modules:', error.message);
}

try {
  const module = context.require('non-existent-module');
} catch (error) {
  console.error('Module not found:', error.message);
}

Security Considerations

  • Only load modules from trusted sources
  • Validate module functionality before use
  • Consider implementing a sandboxed environment for untrusted modules
  • Limit module access to sensitive resources

License

MIT