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

@datacapy/om

v0.2.0

Published

Datacapy object model: repositories, relations and data sources

Readme

@datacapy/om

Part of the DataCapy monorepo.

Object Document Mapping (ODM) framework for Node.js, with support for SQL, document and key-value databases. MySQL, MongoDB, Redis and an in-memory mock are bundled, and you can add your own datasource. Build your model from schemas, repositories and services.

Key Features

  • Model Elements - Define your application with Schemas, Repositories, and Services
  • Relations - Comprehensive relation system (hasOne, hasMany, belongsTo, etc.)
  • Validation - Built-in and custom validators with type-casting
  • Field-Level Encryption - Transparent at-rest encryption for sensitive schema fields
  • Multi-DataSource Support - Dynamic datasource routing for multi-tenant architectures
  • Composite Keys - Advanced multi-field relation matching
  • Query Optimisation - Automatic query optimisation for efficient database operations

Quick Start

Define a Schema

import { Schema } from '@datacapy/om'

const userSchema = new Schema({
  name: { type: String, required: true },
  email: { type: String, validator: 'email' },
  age: { type: Number, min: 0 },
})

Create a Repository

import { Repo } from '@datacapy/om'

export class RepoUser extends Repo<User> {
  constructor() {
    super({
      name: 'user',
      schema: userSchema,
      relations: {
        posts: {
          type: 'hasMany',
          repo: 'post',
          pkey: '_id',
          key: 'authorId',
        },
      },
    })
  }
}

Use in Services

const repo = new RepoUser()

// Find with relations
const users = await repo.find(
  {},
  {
    populate: {
      posts: true,
    },
  }
)

// Insert
await repo.insert({
  name: 'Alice',
  email: '[email protected]',
  age: 30,
})

Core Concepts

Model Elements

  • Schemas - Define data structure and validation rules
  • Repositories - Handle data persistence and queries
  • Services - Implement business logic and coordinate repositories

Relations

Support for common relation types:

  • hasOne / hasMany - Parent has related children
  • belongsToOne / belongsToMany - Child belongs to parent(s)
  • hasManyCount - Count of related documents
  • Embedded relations for nested data structures

See Relations Documentation for details.

Validation

Built-in validators:

  • required, notNull, notEmpty
  • length (min/max for strings and arrays)
  • regex pattern matching
  • email format validation
  • Custom validators

See Validation Documentation for details.

Dynamic DataSource Resolution

Multi-tenant support with runtime datasource routing:

import { DataSourceContext } from '@datacapy/om'

// Create context for routing
const context = DataSourceContext.fromDataSources({
  workspace: { lookupKey: workspaceId },
})

// Use context in queries
const surveys = await repo.find(query, { context })

See DataSource Context Documentation for details.

Documentation

Core Documentation

Advanced Features

Development

Multi-DataSource Configuration

Register Lookup Implementation

import { DataSourceLookup } from '@datacapy/om'

const workspaceLookup: DataSourceLookup = {
  async lookup(dataSourceName, lookupKey) {
    // Fetch workspace configuration
    const workspace = await db.workspaces.findOne({ _id: lookupKey })

    return {
      type: 'mongodb',
      config: {
        uri: workspace.databaseUri,
        database: workspace.databaseName,
      },
    }
  },
}

modelManager.setDataSourceLookup('workspace', workspaceLookup)

Configure Repositories

export class RepoSurvey extends Repo<Survey> {
  constructor() {
    super({
      name: 'survey',
      dataSource: 'workspace', // Dynamic routing
    })
  }
}

Use in Services

async getAll({ workspaceId }) {
  const context = DataSourceContext.fromDataSources({
    workspace: { lookupKey: workspaceId }
  })

  return await this.getRepo('survey').find({}, { context })
}

Examples

Cross-DataSource Relations

const context = DataSourceContext.fromDataSources({
  workspace: { lookupKey: workspaceId },
  tenant: { lookupKey: tenantId },
})

await repoSurvey.findOne(surveyId, {
  context,
  populate: {
    tenant: true, // Automatically uses 'tenant' context
  },
})

Composite Key Relations

relations: {
  participant: {
    type: 'belongsToOne',
    repo: 'surveyParticipant',
    key: 'participantId',
    keys: {
      surveyId: 'surveyId',
      workspaceId: 'workspaceId'
    }
  }
}

License

BSD-3-Clause