• 使用plv8+ shortid npm包构建一个短唯一id服务


    plv8 是一个很强大的pg 扩展插件,我们可以直接额使用js 增强sql ,shortid 是一个用来生成短连接id 很方便的类库
    因为shortid 是一个npm 模块,我们需要使用一种方法使用require 的方式引用包,这个我们可以通过 plv8 node 模块解决
    以下是一个简单的demo

    使用plv8 node 模块注册shortid 原理

    使用plv8 node 模块主要是方便快速的生成plv8 pg 插件可使用的npm 包(包含依赖的处理,基于browserify的处理)
    原理实际上比较简单,主要包含以下

    • 生成包含依赖的js
    browserify +bable node api
    • require 的钩子
      基于plv8 提供的plv8.start_proc,同时我们通过数据级别的session 配置简单应用端连接需要的set 执行
      参考如下:
    ALTER DATABASE postgres SET "plv8.start_proc" TO "v8.plv8_init";
    • 基于plv8 创建 调用shortid 的函数
      内容如下:
     
    CREATE or replace FUNCTION shortid() RETURNS text AS
    $$
       const shortid = require('shortid');
       const result = shortid.generate();
       return result;
    $$
    LANGUAGE plv8;

    注册shortid demo

    • 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",
        "shortid": "^2.2.15",
        "uuid": "^3.3.3"
      },
      "scripts": {
        "init:app": "node app"
      }
    }
    • 调用plv8 npm 模块实现npm包注册
    // 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)
    })
      plv8.install({modulePath:require.resolve("shortid"),moduleName:"shortid"})
      .then(() => {
        // eval some code
        return plv8.eval(() => {
          const shortid = require('shortid')
          return shortid.generate()
        })
      })
      .then(result => {
       console.log(result)
      }).catch(err=>{
          console.log(err)
      })

    短连接服务模型

    为了演示,模型比较简单,主要是一个自增id 以及shortid 的存储,shortid 的生成通过调用
    我们创建的函数shortid

    • 数据库表
     
    CREATE TABLE shortids (
        id integer DEFAULT nextval('shorids_id_seq'::regclass) PRIMARY KEY,
        shortid text
    );
    -- Indices -------------------------------------------------------
    CREATE UNIQUE INDEX shorids_pkey ON shortids(id int4_ops);
     
     
    • 插入操作
    insert into shortids(shortid) values(shortid());
    • 数据效果

    说明

    从shortid 的算法上,随机性比较高的,一般的猜测比较难,我们通过plv8 以及强大的js能力,很方便的就可以设计一个灵活的短连接服务

    参考资料

    http://knexjs.org/#Installation-pooling
    https://github.com/langateam/node-plv8
    https://github.com/plv8/plv8
    https://github.com/dylang/shortid
    https://github.com/rongfengliang/plv8-require-learning

  • 相关阅读:
    详解javascript的深拷贝与浅拷贝
    fis3+vue+pdf.js制作预览PDF文件或其他
    那一年,2020
    H5移动端IOS/Android兼容性总结,持续更新中…
    浏览器里点击复制到剪贴板的小方法
    elementUI upload 对图片的宽高做校验
    CSS实现核辐射警告标志
    github常用命令
    偶遇vue-awesome-swiper的坑
    gauge+python+vscode搭建自动化测试框架
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/11829668.html
Copyright © 2020-2023  润新知