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

@nodesandbox/repo-framework

v0.1.0

Published

A TypeScript framework to simplify CRUD operations using Mongoose and Node.js.

Downloads

95

Readme

@nodesandbox/repo-framework

A TypeScript framework designed to simplify CRUD operations in Node.js applications using Mongoose.

Features

  • Simplified CRUD operations: A streamlined approach to performing CRUD actions on Mongoose models.

  • Repository Pattern: Provides a generic repository pattern for interacting with models.

  • Service Layer: Allows for the creation of reusable services that encapsulate business logic.

  • Search and Filters: Built-in support for search and filters across entity fields.

  • Plugins: Several essential plugins are included by default to enhance functionality:

    • Audit Trail: Tracks user actions and modifications in the database, helping to maintain accountability. Requires the currentUserId to be set via AsyncStorage.
    • History Tracking: Keeps a record of changes made to documents for better data management.
    • Indexing: Allows for efficient data retrieval by creating indexes on specified fields.
    • Soft Delete: Enables marking records as deleted without removing them from the database, preserving historical data.
    • Versioning: Manages versions of documents to track changes over time.

    You can also add or exclude plugins as needed to customize your implementation.

Installation

npm install @nodesandbox/repo-framework mongoose

Ensure you also have mongoose installed in your project.

Usage

Define a Model

You can create a base schema and extend it for your specific needs:

import { createBaseSchema, BaseModel } from '@nodesandbox/repo-framework';

const TODO_MODEL_NAME = 'Todo';

const todoSchema = createBaseSchema<ITodoModel>({
  name: { type: String, required: true },
  slug: { type: String, unique: true, required: true },
  description: { type: String },
  completed: { type: Boolean, default: false },
  dueDate: { type: Date },
  priority: {
    type: String,
    enum: ['low', 'medium', 'high'],
    default: 'medium',
  },
}, {
  modelName: TODO_MODEL_NAME,
});

const TodoModel = new BaseModel<ITodoModel>(
  TODO_MODEL_NAME,
  todoSchema
).getModel();

export { TodoModel };

Define a Repository

Extend the base repository to add custom methods for your model:

import { BaseRepository } from '@nodesandbox/repo-framework';
import { Model } from 'mongoose';

export class TodoRepository extends BaseRepository<ITodoModel> {
  constructor(model: Model<ITodoModel>) {
    super(model);
  }

  async findIncomplete(): Promise<ITodoModel[]> {
    return this.model.find({ completed: false }).exec();
  }

  async findByPriority(priority: string): Promise<ITodoModel[]> {
    return this.model.find({ priority }).exec();
  }
}

Define a Service

Extend the base service to encapsulate business logic:

import { BaseService } from '@nodesandbox/repo-framework';

class TodoService extends BaseService<ITodoModel, TodoRepository> {
  constructor() {
    const todoRepo = new TodoRepository(TodoModel);
    super(todoRepo, true, []);
    this.allowedFilterFields = ['dueDate', 'completed', 'priority'];
    this.searchFields = ['title', 'description'];
  }

  async getTodos(filters: Record<string, any>): Promise<any> {
    const { page = 1, limit = 10, sort, search = '', priority, completed, upcoming } = filters;
    const query: any = {};
    if (priority) query.priority = priority;
    if (completed !== undefined) query.completed = completed === 'true';
    if (upcoming) {
      const days = parseInt(upcoming as string) || 7;
      const futureDate = new Date();
      futureDate.setDate(futureDate.getDate() + days);
      query.dueDate = { $gte: new Date(), $lte: futureDate };
    }
    const sortObject = sort ? parseSortParam(sort) : {};
    return this.findAll({
      query,
      sort: sortObject,
      page: parseInt(page),
      limit: parseInt(limit),
      searchTerm: search as string,
    });
  }
}

Example

This framework provides a flexible structure for various CRUD operations, suitable for any application using Node.js, Express, and MongoDB.

Contributing

We welcome contributions! Please submit a pull request or open an issue if you have any suggestions.

License

MIT © 2024 NodeSandbox