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

@bogojob/mongofsmanager

v1.0.1

Published

file system manager based on mongodb document

Downloads

8

Readme

mongofsmanager

mongo filesystem manager

manages a filesystem based on mongodb document

This library is designed to manage a filesystem type structure saved as a document within a mongodb database.

Structure

of recursive tree type must have the following form:

{
  "_id": "61b8645a684702c3180ac9bf",
  "mail": "[email protected]",
  "firstname": "",
  "lastname": "",
  "size": 200000,
  "filesystem": {
    "nodes": [
      {
        "_id": "61bdca6e2aa6c5c6f4313450",
        "name": "cities",
        "type": "directory",
        "mime": "",
        "size": 200000,
        "childs": [
          {
            "_id": "61f826088e67b91c1d492f4b",
            "name": "IMG_20210906_105712.jpg",
            "type": "file",
            "mime": "image/jpeg",
            "size": 100000,
            "childs": []
          },
          {
            "_id": "62264f13293942dd585a5d5b",
            "name": "Venice",
            "type": "directory",
            "mime": "",
            "size": 100000,
            "childs": [
              {
                "_id": "622650c02caf5dffc8714d56",
                "name": "Piazza San Marco",
                "type": "file",
                "mime": "image/jpeg",
                "size": 100000,
                "childs": []
              }
            ]
          }
        ]
      }
    ]
  }
}

The only mandatory fields are:

  1. _id <string>
  2. filesystem <object>
  3. nodes <array>
  4. type <string> values 'directory' | 'file'
  5. childs <array>
  6. size <number>

which must respect the defined type.
All other fields shown in the example can have other names and be of other types as they are not treated by the library functions.

The subdocuments contained in the childs field in addition to containing obligatorily the type, childs, size fields may contain other fields.

Available operations

the operations made available by the package, are the classic operations that are carried out on a filesystem that is:

  • insert a new node that can be a directory or a file in any point of the structure
  • move the node
  • delete the node

Note: In case the created node is of type file, it will be up to the developer to decide where and how to save the content.

Note: after each operation, all updates to the size fields of the affected nodes are made. This keeps the size of each filesystem directory consistent.

How to use

Mongodb Schema example

import mongoose from 'mongoose';
const nodeSchema = new mongoose.Schema(
    {
        _id: mongoose.Types.ObjectId,
        name: String,
        type: { type: String, enum: ['directory', 'file'], default: 'directory' },
        mime: String,
        size: Number,
        childs: []
       
    });

const userSchema = new mongoose.Schema(
    {
        _id: mongoose.Types.ObjectId,
        mail: { type: String, unique: true },
        firstname: String,
        lastname: String,
        filesystem: { nodes: [nodeSchema], size: Number }
    }
);

const userModel = mongoose.model('user', userSchema);
export {
    userModel
   
}

example for add operation

import { MongoFS as mongofs } from './MongoFS.js';
/* import your mongodb model */
import { yourmodel } from './model.js'
const model = yourmodel;

/* setting variables */
/* document in mongodb on which crud operations will be done  */
const docid = "61b8645a684702c3180ac9bf";
/* node to find: !! must be a directory type !!
use: 000000000000000000000000 for add a node into filesystem root */
const tofind = "622650c02caf5dffc8714d56";

/* object node you want to add */
const payload = {
    "_id": new mongoose.Types.ObjectId(),
    "name": "filename",
    "type": "file",
    "mime": "image/jpeg",
    "size": 100000,
    "childs": []
}


let mfs = undefined;

/**prepare Mongodb connection**/
mongoose.connect(path your mongodb,()=>{
  console.log('mongodb connect: succefully ');
 (async () => {
    try {
        mfs = new mongofs({ tofind: tofind, docid: docid, model: model,payload:payload });
        await mfs.init();
        let n = await mfs.addfs();
        console.log('node: ' + JSON.stringify(n));
        process.exit(1);
    } catch (error) {
        console.log('error from class mongofs:' + error);
        process.exit(0);
    }
})()
})

example for remove operation

import { MongoFS as mongofs } from './MongoFS.js';
/* import your mongodb model */
import { yourmodel } from './model.js'
const model = yourmodel;

/* setting variables */
/* document in mongodb on which crud operations will be done  */
const docid = "61b8645a684702c3180ac9bf";
/* node to find e that must be removed */
const tofind = "622650c02caf5dffc8714d56";
/* object node that you wont add */
const payload = {
  "_id": new mongoose.Types.ObjectId(tofind)
}


let mfs = undefined;

/**prepare Mongodb connection**/
mongoose.connect(path your mongodb,()=>{
  console.log('mongodb connect: succefully ');
 (async () => {
    try {
        mfs = new mongofs({ tofind: tofind, docid: docid, model: model,payload:payload });
        await mfs.init();
        let n = await mfs.removefs();
        console.log('node: ' + JSON.stringify(n));
        process.exit(1);
    } catch (error) {
        console.log('error from class mongofs:' + error);
        process.exit(0);
    }
})()

})

example for move operation

import { MongoFS as mongofs } from './MongoFS.js';
/* import your mongodb model */
import { yourmodel } from './model.js'
const model = yourmodel;

/* setting variables */
/* document in mongodb on which crud operations will be done  */
const docid = "61b8645a684702c3180ac9bf";
/* node to find e that must be removed */
const tofind = "622650c02caf5dffc8714d56";
/* tofind will moved into this node. !! todest must be directory type !! */
const todest = "880be54e68a999c20a6b79c8";

let mfs = undefined;

/**prepare Mongodb connection**/
mongoose.connect(path your mongodb,()=>{
  console.log('mongodb connect: succefully ');
 (async () => {
    try {
        mfs = new mongofs({ tofind: tofind, todest:todest,docid: docid, model: model });
        await mfs.init();
        let n = await mfs.movefs();
        console.log('node: ' + JSON.stringify(n));
        process.exit(1);
    } catch (error) {
        console.log('error from class mongofs:' + error);
        process.exit(0);
    }
})()

})

table of options

| options | meaning | | ------ | ------ | | tofind | node subjected to operation | | todest | node of type directory that will receive another node in its childs field. Valid for move operation only. | | docid | id which identifies the document in the mongodb and which contains the filesystem on which to operate | | model | Mongo db Model Schema | | payload | this is an element that changes depending on the type of operation. (see examples)It is always an object. It is not present in the case of move |