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 🙏

© 2024 – Pkg Stats / Ryan Hefner

monbernate

v1.0.2

Published

Mongoose-based ODM inspired by Hibernate and TypeORM

Downloads

6

Readme

monbernate

Mongoose-based ODM inspired by Hibernate and TypeORM

npm version npm license npm type definitions

Install

First, install reflect metadata and monbernate via npm

npm install reflect-metadata monbernate

Add string below in the top of main app file (main.ts, for example)

import 'reflect-metadata';

Add flags "experimentalDecorators": true and "emitDecoratorMetadata": true in tsconfig.json file

Setup

Declare database repositories, as in example below

import { RecursivePopulateRepository, Repository } from '../../src';
import { Entity } from '../constants/enums';
import { userModel } from '../models';
import { RecursivelyPopulatedUser } from '../types/recursive-populate';
import { IUserDocument, IUserPopulated } from '../types/user';
import { EntityNotFoundError } from '../errors/entity-not-found.error';

@Repository({
  entityName: Entity.USER,
  getModel: () => userModel,
  entityType: Entity.USER,
  notFoundErrorClass: EntityNotFoundError,
})
export class UserRepository extends RecursivePopulateRepository<
  IUserDocument,
  IUserPopulated,
  RecursivelyPopulatedUser
> {}

Then use declared repositories in app

Usage

BaseRepository.save(dto)
const user: IUserDocument = await userRepository.save({ email: '[email protected]' });
BaseRepository.count(filter)
const userAmount: number = await userRepository.count({ email: '[email protected]' });
BaseRepository.get(filter, projection, limit, skip, sort)

Get all entities:

const allUsers: LeanDocument<IUserDocument>[] = await userRepository.get();

Get entities by filter conditions:

const byFirstName: LeanDocument<IUserDocument>[] = await userRepository.get({
  firstName: 'admin',
});

Get only needed fields (using projection):

const onlyFirstName: { firstName: string }[] = await userRepository.get(
  {},
  { firstName: true, _id: false },
);

Get all entities with all fields and limit=100, skip=20 and desc sorting by email field:

const withAdditionalParams: LeanDocument<IUserDocument>[] = await userRepository.get(
  {},
  {},
  100,
  20,
  { email: -1 },
);
BaseRepository.distinct(filter, field)
const emails: string[] = await userRepository.distinct({}, 'email');
BaseRepository.isExists(filter)
const isExists: boolean = await userRepository.isExists({ email: '[email protected]' });
BaseRepository.isExistsWithId(id)
const isExistsWithId: boolean = await userRepository.isExistsWithId('123');
BaseRepository.getById(id, projection, options)
const byId: LeanDocument<IUserDocument> = await userRepository.getById('123');
BaseRepository.getByIdOrNull(id, projection, options)
const byIdNullable: LeanDocument<IUserDocument> | null = await userRepository.getByIdOrNull(
  '123',
);
BaseRepository.getOne(filter, projection, options)
const getOne: LeanDocument<IUserDocument> = await userRepository.getOne({ firstName: 'admin' });
BaseRepository.getOneOrNull(filter, projection, options)
const getOneNullable: LeanDocument<IUserDocument> | null = await userRepository.getOneOrNull({
  firstName: 'admin',
});
BaseRepository.updateById(id, update, projection)
const updateById: LeanDocument<IUserDocument> = await userRepository.updateById('123', {
  email: '[email protected]',
});
BaseRepository.updateByIdOrReturnNull(id, update, projection)
const updateByIdNullable: LeanDocument<IUserDocument> | null =
  await userRepository.updateByIdOrReturnNull('123', {
    email: '[email protected]',
  });
BaseRepository.updateOne(filter, update, projection)
const updateOne: LeanDocument<IUserDocument> = await userRepository.updateOne(
  { email: '[email protected]' },
  { email: '[email protected]' },
);
BaseRepository.updateOneOrReturnNull(filter, update, projection)
const updateOneNullable: LeanDocument<IUserDocument> | null =
  await userRepository.updateOneOrReturnNull(
    { email: '[email protected]' },
    { email: '[email protected]' },
  );
BaseRepository.updateMany(filter, update)
await userRepository.updateMany({ firstName: 'admin' }, { firstName: 'user' });
BaseRepository.deleteMany(filter)
await userRepository.deleteMany({ firstName: 'admin' });
BaseRepository.deleteById(id, projection)
const deleteById: LeanDocument<IUserDocument> = await userRepository.deleteById('123');
BaseRepository.deleteByIdOrReturnNull(id, projection)
const deleteByIdNullable: LeanDocument<IUserDocument> | null =
  await userRepository.deleteByIdOrReturnNull('123');
PartialPopulateRepository.getPartiallyPopulatedById(id, projection, options)
const byId: Omit<IUserPopulated, 'todos'> & {
  todos: Pick<ITodo, 'comments'>;
} = await userRepository.getPartiallyPopulatedById('123', {
  __all: true, // grab all non-populate fields
  todos: { comments: true },
});
PartialPopulateRepository.getPartiallyPopulatedByIdOrReturnNull(id, projection, options)
const byIdNullable: Pick<IUserPopulated, 'todos'> | null =
  await userRepository.getPartiallyPopulatedByIdOrReturnNull('123', { todos: null });
PartialPopulateRepository.getOnePartiallyPopulated(filter, projection, options)
const getOne: Omit<IUserPopulated, 'todos'> & {
  todos: Pick<ITodo, 'comments'>;
} = await userRepository.getOnePartiallyPopulated({ email: '[email protected]' }, {
  __all: true, // grab all non-populate fields
  todos: { comments: 1 },
});
PartialPopulateRepository.getOnePartiallyPopulatedOrReturnNull(filter, projection, options)
const getOneNullable: { todos: MongoId[] } | null =
  await userRepository.getOnePartiallyPopulatedOrReturnNull(
    {
      email: '[email protected]',
    },
    { todos: true },
  );
PartialPopulateRepository.getPartiallyPopulated(filter, projection, limit, skip, sort)
const users: Pick<IUserPopulated, 'firstName' | 'todos'>[] =
  await userRepository.getPartiallyPopulated(
    { email: '[email protected]' },
    { firstName: true, email: 0, lastName: false, todos: null },
    100,
    20,
    { email: 1 },
  );
PartialPopulateRepository.updatePartiallyPopulatedById(id, update, projection)
const updateById: Pick<IUserPopulated, 'todos'> =
  await userRepository.updatePartiallyPopulatedById(
    '123',
    { firstName: 'admin' },
    { todos: null },
  );
PartialPopulateRepository.updatePartiallyPopulatedByIdOrReturnNull(id, update, projection)
const updateByIdNullable: Pick<IUserPopulated, 'todos'> | null =
  await userRepository.updatePartiallyPopulatedByIdOrReturnNull(
    '123',
    { firstName: 'admin' },
    { todos: null },
  );
PartialPopulateRepository.updateOnePartiallyPopulated(filter, update, projection)
const updateOne: Pick<IUserPopulated, 'todos'> = await userRepository.updateOnePartiallyPopulated(
  { email: '[email protected]' },
  { firstName: 'admin' },
  { todos: null },
);
PartialPopulateRepository.updateOnePartiallyPopulatedOrReturnNull(filter, update, projection)
const updateOneNullable: Pick<IUserPopulated, 'todos'> | null =
  await userRepository.updateOnePartiallyPopulatedOrReturnNull(
    { email: '[email protected]' },
    { firstName: 'admin' },
    { todos: null },
  );
RecursivePopulateRepository

Methods similar like in PartialPopulateRepository, but populate object is more deeper

type Return = Omit<RecursivelyPopulatedUser, 'todos'> & {
  todos: Pick<ITodo, 'text'> & {
    comments: {
      author: Pick<IUser, 'email' | 'todos'>;
    };
  };
};

const users: Return[] = await userRepository.getRecursivelyPopulated(
  {},
  {
    __all: true,
    todos: {
      text: true,
      comments: {
        author: {
          email: true,
          todos: true,
        },
      },
    },
  },
);
MixinRepository
const repository: CommentRepository | TodoRepository = todoOrCommentRepository.byEntityType(
  Entity.COMMENT,
);

const commentsAmount: number = await repository.count({});

Other

More examples