Socket
Socket
Sign inDemoInstall

@datawrapper/orm

Package Overview
Dependencies
48
Maintainers
21
Versions
88
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @datawrapper/orm

A database abstraction layer for Datawrapper


Version published
Weekly downloads
51
increased by2450%
Maintainers
21
Created
Weekly downloads
 

Readme

Source

datawrapper-orm

A database abstraction layer for Datawrapper

Usage:

Add the package to your repository using:

npm i --save "@datawrapper/orm"

In your app you need to initialize the ORM before you can do anything else with it. It's a good idea to do this in your apps main entry point:

const orm = require('@datawrapper/orm');

orm.init({
    dialect: 'mysql',
    host: '127.0.0.1',
    port: 3306,
    user: '...',
    password: '...',
    database: '...'
});

Then you can load the models using:

const { Chart, User } = require('@datawrapper/orm/models');

Note that this will initialize the entire model, which assumes that your database user has access to all the database tables. If this is not the case you can load individual models using

const User = require('@datawrapper/orm/models/User');

Plugins

The ORMs functionality can be extended with plugins. This is needed, for example, when new database tables are needed. The plugin API follows the convention of plugins in datawrapper/api.

A simple ORM plugin that does nothing looks like this:

/* config.js */
plugins: {
    'my-orm-plugin': {
        my_name: 'Steve'
    }
}

/* orm.js */
module.exports = {
    register: async (ORM, config) => {
        console.log(`Hi I am ${config.my_name}!`)
        // logs "Hi I am Steve!" on registration
    }
}

There are 2 interesting properties on the ORM object that help with plugin access.

  • ORM.plugins is an object with all configured plugins. They are not registered by default. Since standard Models are not defined after ORM.init() either, it wouldn't make sense to do that for plugins.

This is how you register a plugin:

await ORM.init()
const { plugins } = ORM

const MyORMPlugin = require(plugins['my-orm-plugin'].requirePath)
await MyORMPlugin.register(ORM, plugins['my-orm-plugin'])

This method is very useful for tests where you only need a special plugin. There is also a helper method to register all plugins. It is in functionality similar to requiring all models with require('@datawrapper/orm/models').

  • ORM.registerPlugins will register all plugins.
await ORM.init()
await ORM.registerPlugins()

FAQs

Last updated on 22 Nov 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc