库存储 API

自 1.3.0 版本起

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

此本地库由存储 API管理。默认情况下,它存储在~/.node-red/lib目录下。

库存储 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 必需
库的唯一、URL 安全标识符。应只包含字母、数字和符号-_
类型 必需
必须设置为node-red-library-file-store
path 必需
库应存储的绝对路径
label 在编辑器中使用的可选标签,否则将使用id
icon 来自FontAwesome 4.7的可选图标。
类型 默认情况下,该库将用于存储所有类型的对象。可以通过将此属性设置为可接受类型的数组来限制其存储特定类型。
例如,要将其限制为仅流程,请将此属性设置为["flows"]
readOnly 要使此库为只读库,以便只能用于导入,请将此属性设置为true

创建新的存储插件

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

该插件打包为 npm 模块,带有一个package.json文件。

以下代码可用作插件的起点。您还应该参考文件存储插件

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
    })
}