Git Product home page Git Product logo

moleculer-database's Introduction

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

moleculer-database's People

Contributors

icebob avatar cameronmema avatar github-actions[bot] avatar rosko avatar maxinminax avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.