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

@ewb/mongoose-export-import

v2.0.0

Published

Mongoose. Export and Import models

Downloads

113

Readme

EXPORT & IMPORT

This is used to easily export and import mongoDB documents with help from mongoose.
For now this is used with Express. If anyone want to use this outside express, shout out and I will change the typing of req. ( You can use req as any to overwrite this )
Will work fine.

Export

mongooseExportOrganization.export(req)

Export function just needs the express object.

Import

const json = req.file.buffer.toString();
mongooseExportOrganization.import(req, json, req.body);

req.body will overwrite any value in first step. Helpfull when importing to change name of imported document.
These functions are recursive. So you can add as many remote connections you want in remote connections.

Object example setup

const mongooseExportOrganization = new MongooseExport<
  OrganizationDoc,
  OrganizationModel,
  IRequest
>({
  model: 'Organization',
  field: 'organization',
  exclude: [
    'members',
    'invitations',
    'hasAccess'
  ],
  exportQuery: (req) => ({
    _id: req.organization._id
  }),
  importQuery: (req) => ({
    members: [{
      user: req.user._id,
      permissions: Permissions.Admin
    }]
  }),
  afterImport: async (req) => {
    // Or solve the importQuery with this 
    await Organization.findByIdAndUpdate(req.organization._id, {
      $push: {
        members: {
          user: req.user._id,
          permissions: Permissions.Admin
        }
      }
  }),
  remote: [{
    model: 'Book',
    field: 'books',
    baseQuery: (req) => {
     return {
       organization: req.organization._id
     };
    },
    exportQuery: () => ({
      archived: false
    }),
    exclude: [
      'archived'
      'invitations',
      'sharedBooks',
      'hasAccess',
      'sharedChapters'
    ]
  }]
})

Result from Organization export:

{
  "__id": "5c0648c533f81bbf35216d8b",
  "logo": {
    "src": "w08dnlewvsonghu5nj2k",
    "mime": "image/png",
    "alt": "Software_logo-09-2.png"
  },
  "name": "Inwise",
  "overviewName": "Inwise testområde",
  "books": [
    {
      "__id": "5d597afe1165090017c922c3",
      "_order": 2245134024441863,
      "icon": "Chapter",
      "name": "En enkel innføring i e-læring",
      "organization": "5c0648c533f81bbf35216d8b"
    }
  ]
}

##Export

Model

The model to query

Field

"field" is used to save value to parent body. Organization with be saved to req.organization and book will be saved to req.organization.books

baseQuery

Object used to query the correct model.
Parent will only find 1 object.
Remote objects will always be arrays.
If need be you can go trough other path like 'organization' to filter query even more.

exportQuery

Extra query, only for export operation.

Remote

Exports models that are linked to the project with {project: project._id} Will save queried models to the provided 'field'

Exclude

Exclude fields. Will delete them before exporting

ReplaceIds

Only used in Import. #Note: All objects with _id will now have a value called __id to be used in import.

Populate

Populate fields with mongoose. You should add these to children if you want to remove the special values as _id & created.

##Import

API use

Model

The model to query

Field

Is used to save values to the req object to be used in later query functions. The req object is updated and passed in right before the query to mongoose.

Query

This is the objects that's passed into mongoose create. This is spread out with the content from 'file'.

Remote

Remote models that are not linked directly to the model with populate. You can use the parent 'field' key to get the newly created _id of the imported project to reference it in the newly created model.

Exclude

Not used in import

ReplaceIds

Import will first add _id to all objects in parallel with each other. And all object with a key that matches replaceIds will then search through req.ids and match the id with ids[i].oldId and replace it with ids[i].newId.

Populate

Not used in import

AfterImport ( req => void )

This can be used in special cases where other remotely models need other remote models.
Or if you find any case that's not supported by import.
Up to you and your imagination :D