图书馆商店API

自1.3.0版本起

Node-RED编辑器中的导入/导出对话框提供了一种将流程和节点保存到本地库的方法。

这个本地库由Storage API管理。默认情况下会存储在~/.node-red/lib目录下。

Library Store API 是一种插件机制,可用于提供将内容存储在其他位置(而不仅仅是本地文件)的库。

Node-RED提供了一个文件存储插件,可用于添加存储在本地文件系统中的库。例如,可以通过Dropbox等工具在共享文件系统上创建库,从而更轻松地与您合作的其他开发者共享流程。

添加文件存储库

  1. 编辑您的Node-RED设置文件 - 通常是 ~/.node-red/settings.js
  2. 找到editorTheme部分,如果不存在library部分就添加一个。
  3. 在该部分下添加一个sources数组。在该数组中您可以添加任意数量的新文件存储源。

     editorTheme: {
         library: {
             sources: [
                 {
                     id: "team-collaboration-library",
                     type: "node-red-library-file-store",
                     path: "/Users/tom/work/team-library/",
                     label: "Team collaboration",
                     icon: "font-awesome/fa-users"
                 }
             ]
         },
     }
    

配置对象可以包含以下属性:

属性 描述
id Required
A unique, url-safe, identifier for the library. Should contain only letters, numbers and the symbols - _.
type Required
Must be set to node-red-library-file-store
path Required
The absolute path to the where the library should be stored
label An optional label to use in the editor, otherwise the id will be used.
icon An optional icon from FontAwesome 4.7.
types By default the library will be used to store all types of object. It can be restricted to certain types by setting this property to an array of the acceptable types.
For example, to restrict it to just flows, set this property to ["flows"].
readOnly To make this a read-only library so it can only be used to import from, set this property to true.

创建新的存储插件

要创建一个由不同类型存储支持的存储库,您需要开发一个新的插件。

该插件以npm模块形式打包,包含一个package.json文件。

以下代码可作为该插件的起点。您还应参考File Store插件

package.json

{
    "name": "your-custom-library-store",
    "version": "1.0.0",
    "description": "A Custom Library plugin for Node-RED",
    "keywords": [
        "node-red"
    ],
    "node-red": {
        "plugins": {
            "customstore": "store.js"
        }
    }
}

store.js

module.exports = function(RED) {

    // This must be a unique identifier for the library store type
    const PLUGIN_TYPE_ID = "node-red-library-custom-store";

    class CustomStorePlugin {

        /**
         * @param {object} config an object containing the configuration for an
         *                        instance of the store
         */
        constructor(config) {
            // Required properties
            this.type = PLUGIN_TYPE_ID;
            this.id = config.id;
            this.label = config.label;
        }

        /**
         * Initialise the store.
         */
        async init() {
        }

        /**
         * Get an entry from the store
         * @param {string} type The type of entry, for example, "flow"
         * @param {string} path The path to the library entry
         * @return if 'path' resolves to a single entry, it returns the contents
         *         of that entry.
         *         if 'path' resolves to a 'directory', it returns a listing of
         *         the contents of the directory
         *         if 'path' is not valid, it should throw a suitable error
         */
        async getEntry(type,path) {
            throw new Error("Not implemented")
        }

        /**
         * Save an entry to the library
         * @param {string} type The type of entry, for example, "flow"
         * @param {string} path The path to the library entry
         * @param {object} meta An object of key/value meta data about the entry
         * @param {string} body The entry contents
         */
        async saveEntry(type,path,meta,body) {
            throw new Error("Not implemented")
        }
    }

    // Register the plugin.
    RED.plugins.registerPlugin(PLUGIN_TYPE_ID, {
        // This tells Node-RED the plugin is a library source plugin
        type: "node-red-library-source",
        class: CustomStorePlugin
    })
}