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

@moleculer/database

v0.2.1

Published

Advanced Database Access Service for Moleculer microservices framework

Downloads

562

Readme

Moleculer logo

Integration Test Coverage Status Known Vulnerabilities NPM version

@moleculer/database

Advanced Database Access Service for Moleculer microservices framework. Use it to persist your data in a database.

This project is in work-in-progress. Be careful using it in production.

this module follows the one database per service pattern. To learn more about this design pattern and its implications check this article. For multiple entities/tables per service approach check FAQ.

Features

  • multiple pluggable adapters (NeDB, MongoDB, Knex)
  • common CRUD actions for RESTful API with caching
  • pagination, field filtering support
  • field sanitizations, validations
  • read-only, immutable, virtual fields
  • field permissions (read/write)
  • ID field encoding
  • data transformation
  • populating between Moleculer services
  • create/update/remove hooks
  • soft delete mode
  • scopes support
  • entity lifecycle events
  • Multi-tenancy

Install

npm i @moleculer/database nedb

Installing nedb is optional. It can be good for prototyping.

Usage

Define the service

// posts.service.js

const DbService = require("@moleculer/database").Service;

module.exports = {
    name: "posts",
    mixins: [
        DbService({
            adapter: "NeDB"
        })
    ],

    settings: {
        fields: {
            id: { type: "string", primaryKey: true, columnName: "_id" },
            title: { type: "string", max: 255, trim: true, required: true },
            content: { type: "string" },
            votes: { type: "number", integer: true, min: 0, default: 0 },
            status: { type: "boolean", default: true },
            createdAt: { type: "number", readonly: true, onCreate: () => Date.now() },
            updatedAt: { type: "number", readonly: true, onUpdate: () => Date.now() }
        }
    }
}

Call the actions

// sample.js

// Create a new post
let post = await broker.call("posts.create", {
    title: "My first post",
    content: "Content of my first post..."    
});
console.log("New post:", post);
/* Results:
New post: {
  id: 'Zrpjq8B1XTSywUgT',
  title: 'My first post',
  content: 'Content of my first post...',
  votes: 0,
  status: true,
  createdAt: 1618065551990
}
*/

// Get all posts
let posts = await broker.call("posts.find", { sort: "-createdAt" });
console.log("Find:", posts);

// List posts with pagination
posts = await broker.call("posts.list", { page: 1, pageSize: 10 });
console.log("List:", posts);

// Get a post by ID
post = await broker.call("posts.get", { id: post.id });
console.log("Get:", post);

// Update the post
post = await broker.call("posts.update", { id: post.id, title: "Modified post" });
console.log("Updated:", post);

// Delete a user
const res = await broker.call("posts.remove", { id: post.id });
console.log("Deleted:", res);

Try it in your browser on repl.it

Documentation

You can find here the documentation.

Benchmark

There is some benchmark with all adapters. You can find the results here.

License

The project is available under the MIT license.

Contact

Copyright (c) 2022 MoleculerJS

@MoleculerJS @MoleculerJS