open-index/open-npm huggingface dataset:
npm package: @mongoosejs/migrations
Description: Mongoose Migration Framework
Latest version: 0.4.0

# migrations
Mongoose Migration Framework

### Getting Started

```javascript
// Import the migration framework. Put this import **before** any other imports,
// except dotenv or other environment configuration
const migrations = require('@mongoosejs/migrations');

const Character = require('./models/character');
const mongoose = require('mongoose');

run().catch(err => { console.error(err); process.exit(-1); });

async function run() {
  await mongoose.connect('mongodb://localhost:27017/migrations_examples');
  // Tell the migration framework that a migration is starting.
  // Set `restart` option if you want to restart a previously failed migration.
  await migrations.startMigration({ restart: process.env.RESTART });

  try {
    // Put any migration logic here - update, save, eachAsync, etc.
    await Character.updateMany({}, { $set: { ship: 'USS Enterprise' } });
  } finally {
    // Make sure to always call `endMigration()`, even if errors occur
    await migrations.endMigration();
    await mongoose.disconnect();
  }
}
```

### UI Setup

You can run the Migration Framework UI as an executable using the provided `migration-ui` command:

```
env PORT=3001 env MONGO_URI="mongodb://localhost:27017/mydb" ./node_modules/.bin/migration-ui
```

You can also import the Migration Framework UI to your Express app:

**Note:** you are responsible for securing the Migration Framework UI. There is currently no restrictions on the UI or API: anyone that can access the Migration Framework UI's URL can read migration data.
**Note:** do **not** run the Migration Framework UI in the same process that runs migrations.
npm package: @mongoosejs/task
Description: Scheduled tasks with Mongoose
Latest version: 0.6.3

# task
Scheduled tasks with Mongoose

## Getting Started

Requires Node >= 14 and Mongoose >= 6.7.0

```javascript
const mongoose = require('mongoose');

// Task is a Mongoose model that has several helpful methods and statics
// for working with scheduled tasks.
const Task = require('@mongoosejs/task')();

// Register a handler for a named task and start polling for `sayHello` tasks
Task.registerHandler('sayHello', function sayHello() {
  console.log('Hello, World!');
});
Task.startPolling();

// Will print 'Hello, World!' after approximately 1 second
await Task.schedule('sayHello', new Date(Date.now() + 1000));
```

## Polling Options

You can pass `getCurrentTime()` to `startPolling()` to supply a custom clock for polling,
which is useful for demos or tests that advance time automatically.

```javascript
let currentTime = new Date('2023-06-01T00:00:00.000Z');

Task.startPolling({
  getCurrentTime: () => new Date(currentTime)
});

currentTime = new Date(currentTime.valueOf() + 60_000);
```

## Params

The 2nd param to `Task.schedule()` is an object that this framework will call the handler function with.

```javascript
Task.registerHandler('sayHello', function sayHello(params) {
  console.log(`Hello, ${params.name}!`);
});

// Will print 'Hello, Friend!' after approximately 1 second
await Task.schedule(
  'sayHello',
  new Date(Date.now() + 1000),
  { name: 'Friend' }
);
```

## Repeating Tasks

The 3rd param to `Task.schedule()` is called `repeatAfterMS`.
If `repeatAfterMS` is set, this framework will immediately reschedule the task to run after `repeatAfterMS` once the original task is done.

```javascript
Task.registerHandler('sayHello', function sayHello(params) {
  console.log(`Hello, ${params.name}!`);
});

// Will print 'Hello, Friend!' every 5 seconds, after a 1 second
// initial delay
await Task.schedule(
  'sayHello',
  new Date(Date.now() + 1000),
  { name: 'Friend' },
  5000
);
```