0.0.2 • Published 1 year ago

alertacoches-importer v0.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

This package is shared among the alertacoches workers.

Connecting to the database

To use it, first connect to the database like in the following code block:

const { mongoConnect } = require('alertacoches-importer');

const { models } = await mongoConnect(process.env.DB_NAME, process.env.DB_URI);
const { Search } = models;

Be sure to pass in the required arguments like in the following codeblock:

const importer = new RandomImporter(
  SOURCE_ID,
  process.env.REFRESH_HOURS,
  process.env.API_SOURCE,
  Search,
);
await importer.import();

Notice that the model, in this case Search, needs to be passed in as the fourth argument.

Extending Importer

Then simply import Importer and extend it with whatever class you want to use. Be sure to override the methods getAdsFromSource and parseToAd.

As an example, take the following code block:

const { Importer } = require('alertacoches-importer');
const RANDOM_DOMAIN = 'https://www.random.com/es/';

class RandomImporter extends Importer {
  constructor(...args) {
    super(...args);
  }
  // method to get ads from source
  async getAdsFromSource(url) {}

  parseToAd(ad, make, model, fuel) {}
}

module.exports = RandomImporter;

Development

To make and test changes with this package locally, run npm run dev, and make any needed changes. Each time you hit save, the changes will automatically be compiled.

To test it out in another repo, get the absolute path of this folder, e.g. /Users//code/alertacoches/alertacoches-importer/README.md, and then run npm i /Users/<username>/code/alertacoches/alertacoches-importer/README.md.

Doing so will install it as a local repo that you can use to test your worker. It should like something like:

  "dependencies": {
    "alertacoches-importer": "file:../../alertacoches-importer"
  }

Any time you make changes on this project and save them, restart the worker to test the latest changes.