plv8 是postgres 的一个比较强大的插件,社区有人基于babel,browserify 实现了一个方便的require 模块加载
实际上官方也有介绍过了类似的基于数据库存储js,然后通过eval 动态加载代码,以下是一个简单的试用

环境准备

  • docker-compose 文件
 
version: '3.6'
services:
  postgres:
    image: dalongrong/plv8:2.3.12
    ports:
    - "5432:5432"
    environment: 
    - "POSTGRES_PASSWORD=dalong"
  graphql-engine:
    image: hasura/graphql-engine:v1.0.0-beta.9
    ports:
    - "8080:8080"
    depends_on:
    - "postgres"
    environment:
      HASURA_GRAPHQL_DATABASE_URL: postgres://postgres:dalong@postgres:5432/postgres
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
  • node-plv8 项目
    package.json
 
{
  "name": "node-plv8",
  "version": "1.0.0",
  "main": "app.js",
  "bin": "app.js",
  "license": "MIT",
  "dependencies": {
    "cuid": "^2.1.6",
    "knex": "^0.20.1",
    "lodash": "^4.17.15",
    "pg": "^7.12.1",
    "plv8": "^2.1.4",
    "uuid": "^3.3.3"
  },
  "scripts": {
    "init:app": "node app"
  }
}

app.js
进行模块的注册,目前包含了lodash以及一个自己的

 
// setup plv8 connection
const PLV8 = require('plv8')
const knex = require('knex')
const knexHandle = knex({
    client: 'pg',
    connection: {
      host: "127.0.0.1",
      user: "postgres",
      password: "dalong",
      database: "postgres"
    }
  })
const plv8 = new PLV8(knexHandle)
// setup a log listener
plv8.on('log:error', msg => {
  console.error(msg)
})
// install the lodash module so that it can be loaded (via require()) later
plv8.install({modulePath:require.resolve('lodash'),moduleName:"lodash"})
  .then(() => {
    // eval some code
    return plv8.eval(() => {
      const _ = require('lodash')
      return _.map([ 1, 2, 3 ], e => e + 1)
    })
  })
  .then(result => {
   console.log(result)
  }).catch(err=>{
      console.log(err)
  })
    plv8.install({modulePath:require.resolve("./login"),moduleName:"login"})
  .then(() => {
    // eval some code
    return plv8.eval(() => {
      const login = require('login')
      return login
    })
  })
  .then(result => {
   console.log(result)
  }).catch(err=>{
      console.log(err)
  })

login.js 模块

module.exports = {
    name:"dalong",
    age:333
}

运行&&测试

  • 启动docker 服务
docker-compose up -d
  • 运行nodejs demo 服务
yarn init:app
  • 效果
    目前有关于pg 通信的一些异常log,可能是和代码很就已经没维护,包版本兼容pg 的问题,后边
    调试研究下
 
yarn run v1.17.3
$ node app
(node:56198) UnhandledPromiseRejectionWarning: error: syntax error at or near ":"
    at Connection.parseE (/Users/dalong/mylearning/hasura-project/plv8/node-plv8/node_modules/pg/lib/connection.js:604:11)
    at Connection.parseMessage (/Users/dalong/mylearning/hasura-project/plv8/node-plv8/node_modules/pg/lib/connection.js:401:19)
    at Socket.<anonymous> (/Users/dalong/mylearning/hasura-project/plv8/node-plv8/node_modules/pg/lib/connection.js:121:22)
    at Socket.emit (events.js:182:13)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
(node:56198) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:56198) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Use async .hasTable to check if table exists and then use plain .createTable. Since .createTableIfNotExists actually just generates plain "CREATE TABLE IF NOT EXIST..." query it will not work correctly if there are any alter table queries generated for columns afterwards. To not break old migrations this function is left untouched for now, but it should not be used when writing new code and it is removed from documentation.
Use async .hasTable to check if table exists and then use plain .createTable. Since .createTableIfNotExists actually just generates plain "CREATE TABLE IF NOT EXIST..." query it will not work correctly if there are any alter table queries generated for columns afterwards. To not break old migrations this function is left untouched for now, but it should not be used when writing new code and it is removed from documentation.
{ name: 'dalong', age: 333 }
[ 2, 3, 4 ]
 

sql 方式调用

  • 函数定义
CREATE or replace FUNCTION dalong2() RETURNS json AS
$$
    const login = require('login')
    return JSON.stringify(login);
$$
LANGUAGE plv8;
  • 查询
SET plv8.start_proc = 'v8.plv8_init';
select * from dalong2();
 
  • 效果
{"name":"dalong","age":333}

简单原理说明

node plv8 将生成的js 通过browserify 以及babel 转换,存储在v8 schema 中的modules 表中
同时注册了SET plv8.start_proc = 'v8.plv8_init'; 需要的plv8.init 函数,同时也注册了几个依赖的
函数,代码的执行通过的是eval 函数,整体上还是比较简单的,但是实现的功能确实很方便的

参考资料

https://pgxn.org/dist/plv8/doc/plv8.html
https://github.com/plv8/plv8
https://plv8.github.io/
https://github.com/rongfengliang/plv8-require-learning

plv8 require 模块试用的更多相关文章

  1. <5>Lua多返回值和require模块

    1.多返回值 --1: 一个lua函数可以返回多个返回值: --2: 定义多个变量来接受多返回值 --3: lua的unpack函数,解开表里的单个的值; 结果 2.require模块 --1: 第一 ...

  2. Node.js require 模块加载原理 All In One

    Node.js require 模块加载原理 All In One require 加载模块,搜索路径 "use strict"; /** * * @author xgqfrms ...

  3. nodejs的require模块及路径

    在nodejs中,模块大概可以分为核心模块和文件模块. 核心模块是被编译成二进制代码,引用的时候只需require表示符即可,如(require('net')). 文件模块,则是指js文件.json文 ...

  4. webToImage (网页转图片)模块试用分享

    模块介绍: 本模块封装了把 webview 转换成图片的功能.调用本模块的transImage接口,可把当前 webview显示的内容转换成一张图片.注意,本模块只能把当前的webview页面转换为图 ...

  5. nginx unit nodejs 模块试用(续)

      最新(应该是18 年了)nginx unit 发布了新的版本,对于nodejs 的支持有很大的改进,上次测试过,问题还是 比较多,这次使用新版本在测试下对于nodejs 的支持,以及以前block ...

  6. nginx unit nodejs 模块试用

      unit 对于nodejs 的支持是在10.25 发布的,基本能用,但是依然有好多问题,当前在测试的时候就发现,请求之后会block , 相关的issue 已经有人反馈了,最好使用源码编译,方便测 ...

  7. angular的require模块的使用($requireProvider的作用)

      今天我们学习一下angular的另一个几乎被忽略的模块angular-require 先给出链接地址(git:)   https://github.com/Treri/angular-requir ...

  8. require模块开发(一)

    1.require下载和加载 1.1 下载 工欲善其事必先利其器,先下载require.js下载地址, 然后添加 require.js 到 scripts 目录 1.2 加载 然后加载require ...

  9. sublime 打开import require 模块文件的url 或路径的插件

    结果一番周折,终于发现sublime提供的一个插件(open url)可以实现打开import file 路径的文件,比如import demo from “../path” 的文件在新窗口或者新标签 ...

随机推荐

  1. vue+element 给表格添加数据,页面不实时刷新的问题

    由于页面加载时,使用了keep-alive,keep-alive具有数据缓存作用,当在添加页面添加成功时,返回主页面没有立即更新.数据有缓存. 解决办法如下: 将获取数据列表的方法放到activate ...

  2. EasyNetQ笔记

    Each call to Subscribe creates a new queue consumer. If you call Subscribe two times with the same m ...

  3. table布局 常见问题总结

    table实用属性: 属性 值 作用 描述 table-layout auto 自动计算列宽 对table和td.th指定的宽度无效 浏览器会计算所有单元格的内容宽度才能得出一列宽度 (默认值) fi ...

  4. PIE SDK内存矢量数据的创建

    1.功能简介 GIS将地理空间数据表示为矢量数据和栅格数据.矢量数据模型使用点.线和多边形来表示具有清晰空间位置和边界的空间要素,如控制点.河流和宗地等,每个要素被赋予一个ID,以便与其属性相关联.栅 ...

  5. HTML Web Workers

    Web worker 是运行在后台的 JavaScript,不会影响页面的性能. 什么是 Web Worker? 当在 HTML 页面中执行脚本时,页面是不可响应的,直到脚本已完成. Web work ...

  6. Python常用模块大全(转)

    os模块:os.remove() 删除文件 os.unlink() 删除文件 os.rename() 重命名文件 os.listdir() 列出指定目录下所有文件 os.chdir() 改变当前工作目 ...

  7. 【转】Why BIOS loads MBR into 0x7C00 in x86 ?

    最近在读Linux的grub的stage1中看到“BIOS执行INT 0x19,加载MBR内容至0x7c00,然后跳转执行”,为什么一定是0x7c00这个地址. 作者刚好在下面推荐了这篇文件,刚好顺手 ...

  8. ApiPost(中文版postman)如何发送一个随机数或者时间戳?

    什么是ApiPost内建变量:ApiPost提供了5个内建变量,如下: {{$guid}} //生成GUID {{$timestamp}} //当前时间戳 {{$microTimestamp}} // ...

  9. Python 二维码制作

    Python 二维码制作 先介绍python 二维码制作的第三方库 QRCode .MyQR QRCode    生成这个二维码只用三行 import qrcode img = qrcode.make ...

  10. TP5.0整合webuploader实现多图片上传功能

    在https://github.com/fex-team/webuploader 下载webuploader并解压,解压后放到public里面.其中我把解压缩后的文件夹改名为webuploader,放 ...