Gathering detailed insights and metrics for faas-express
Gathering detailed insights and metrics for faas-express
Gathering detailed insights and metrics for faas-express
Gathering detailed insights and metrics for faas-express
npm install faas-express
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
13
$ npm i -g faas-express
$ faas-express init
$ faas-express new your-fn-name
# start dev
$ node your-fn-name
# build && push && deploy
$ faas-express build/push/deploy function-name -f your-fn-yaml.yml
# 安装faas-express,也可以运行faas-express init,会自动创建babel.config.js和.eslintrc.js,Dockerfile打包镜像必须
$ npm i faas-express
# 全局安装faas-express
$ npm i -g faas-express
# 也可以不安装全局模块,使用git库安装并配置scripts,只能使用yarn运行scripts,npm无法传递--参数
$ npm install git+ssh:git@code.aibee.cn:ued/faas-express.git
# 添加 "scripts": { "faas-express": "faas-express" }
$ 使用 yarn faas-express new your-fn-name
# 项目初始化
$ npm init
$ faas-express init
$ faas-express new your-fn-name
# 创建function
$ faas-express new your-fn-name
# 安装部署,部署your-fn-yaml.yml中所有functions使用.代替function-name
$ faas-express build/push/deploy function-name -f your-fn-yaml.yml
$ npm install
# 修改 your-fn-name/config.yml数据库等配置
# 编写代码models、routes、service
# 自动生成model,多个表名空格隔开。根据-f your-fn-yaml.yml中-n function-name的数据库配置生成,不指定-f-n默认读取functiom.yml第一个function的数据库配置,-d参数根据带修饰器模板生成代码
$ faas-express model jobs projects ... -n function-name -f your-fn-yaml.yml
# 自动生成route,根据models目录下model名称生成,多个model空格隔开。指定-f your-fn-yaml.yml中-n function-name的handler配置目录下去读models,不指定-f-n默认读取functiom.yml第一个function的handler目录配置,-d参数根据带修饰器模板生成代码
$ faas-express route jobs projects ... -n function-name -f your-fn-yaml.yml
# service 业务性复杂的函数写在service下,方便维护和调用
# 本地启动服务
$ node your-fn-name/index.js
# 修改 function.yml文件
# functions test-faas-node 为部署到openfaas后接口后缀 http://192.168.9.30:31112/function/test-faas-node
# build-arg docker镜像打包时传入环境变量
# handler 代码相对路径
# image docker镜像名
provider:
name: openfaas
gateway: http://192.168.9.30:31112
functions:
test-faas-node:
build-arg: NODE_ENV=development
lang: faas-express
handler: ./your-fn-name
image: registry.aibee.cn/bi-faas-web/test-faas-node:0.0.1
# build docker image,如果项目根目录下有Dockerfile会使用自定义的Dockerfile
faas-express build . -f ./function.yml
# push docker image
faas-express push . -f ./function.yml
# deploy function
faas-express deploy . -f ./function.yml
your-fn-name/config.yml
env:
host: localhost:3000
function: ""
root: /
schedule:
# 定时任务,每10秒执行方法 service.projects.test
projects.test: "*/10 * * * *"
配置参见 log4js-node
1import { variables } from "faas-express"; 2var logger = variables.log4js.getLogger("time"); 3logger.info("this is log");
your-fn-name/config.yml
log4j:
http:
level: info
type: dateFile
filename: logs/http
pattern: _yyyy-MM-dd.log
alwaysIncludePattern: true
layout:
type: 'pattern'
pattern: '%d %p %c %X{user} %m%n'
db:
level: info
type: console
layout:
type: json
time:
level: info
type: console
layout:
type: 'pattern'
pattern: '%d %p %c %X{user} %m%n'
配置参见 ioredis
1import { redis } from "faas-express"; 2redis.set("key", "value", 3000); 3redis.set("key", "value", "NX", "EX", 100); 4redis.get("key");
your-fn-name/config.yml
redis:
mode: sentinel/""
host: 127.0.0.1
port: 26379
password: 123456
name: mymaster
db: 1
# 连接失败重试次数,0会一直重试,默认5次
retry: 1
# 所有参数都在faas-express对象下
import { router, db, service, ... } from "faas-express";
# router 路由express.Router()
# db 数据库实例及所有模型
# service 所有定义的服务函数
# utils 公用工具函数
# config yml配置变量,参见yml,例如config.db.dbname
# variables 常用变量,{ config, log4js, utils, moment }
# sequelize sequelize实例
# Route 路由修饰器
# DB 数据库修饰器
# Service 服务函数修饰器
# Sequelize 原生Sequelize构造器
# 所有数据库相关对象都在db下
var { define, associate, sequelize, Sequelize, Model, Op, DataTypes, modelName... } = db;
# define 数据模型定义方法
# associate 数据模型关系定义
# sequelize sequelize实例
# sequelize 原生Sequelize构造器
# Model, Op, DataTypes 也可有Sequelize.DataTypes等获取
# db.modelName 所有定义的模型名称
# 所有服务函数相关对象都在service下
var { define, serviceName... } = service;
# define 服务函数定义方法
# serviceName 所有定义的服务名称
# 公用工具函数
var { wrap, wrapError, wrapWithPage, ... } = utils;
# wrap(data, error_no = 0, error_msg = "") 包装返回信息
# wrapError(error_msg, error_no = 500) 包装错误返回信息
# wrapWithPage(data, page, size) 包装返回值,包含分页
# getQueryWhere(query, Op, model, namespace) 生成查询条件,可直接给model查询条件where
# getPage(query) 返回page条件
# getSize(query) 返回size条件
# getSqlLimit(query) 返回mysql limit分页条件{offset,limit}
# getOrder(query, model, defaultOrder) 返回mysql order排序条件
# checkModelData(data, model) 校验对象数据和model验证,用于保存校验
# genTreeNodes(list, idKey = "id", pidKey = "parent_id", startWidth = "-1", labelKey = "name") list型树数据转children型树结构,element-ui tree格式
Example with sequelize models:
使用如下命令可自动生成
faas-express model projects -n function-name -f your-fn-yaml.yml
1/** 2 * your-fn-name/projects.js 3 **/ 4import { db, DB } from "faas-express"; 5var { sequelize, Sequelize, Model, Op, DataTypes } = db; 6 7db.define( 8 "projects", 9 { 10 id: { 11 type: DataTypes.BIGINT, 12 allowNull: false, 13 primaryKey: true, 14 autoIncrement: true 15 }, 16 category_id: { 17 type: DataTypes.INTEGER(11), 18 allowNull: false 19 }, 20 name: { 21 type: DataTypes.STRING(128), 22 allowNull: false 23 }, 24 status: { 25 type: DataTypes.INTEGER(4), 26 allowNull: true, 27 defaultValue: "0" 28 }, 29 date: { 30 type: DataTypes.DATEONLY, 31 allowNull: false 32 }, 33 label_configuration: { 34 type: DataTypes.TEXT, 35 allowNull: true, 36 get(){ 37 let json = this.getDataValue("label_configuration"); 38 if (json) { 39 try { 40 return JSON.parse(json); 41 } catch (e) { 42 return json; 43 } 44 } else { 45 return {}; 46 } 47 }, 48 set(val) { 49 try { 50 this.setDataValue("label_configuration", JSON.stringify(val)); 51 } catch (e) { 52 this.setDataValue("label_configuration", val); 53 } 54 } 55 }, 56 created_at: { 57 type: DataTypes.DATE, 58 allowNull: false 59 }, 60 updated_at: { 61 type: DataTypes.DATE, 62 allowNull: false 63 } 64 }, 65 { 66 tableName: "projects", 67 createdAt: "created_at", 68 updatedAt: "updated_at", 69 timestamps: true, 70 underscored: true, 71 } 72); 73 74db.associate(models => { 75 models.projects.hasMany(models.jobs, { foreignKey: "project_id", as: "jobs" }); 76};
使用修饰器方式
faas-express model projects -n function-name -f your-fn-yaml.yml -d
1/** 2 * your-fn-name/projects.js 3 **/ 4import { db, DB } from "faas-express"; 5var { sequelize, Sequelize, Model, Op, DataTypes } = db; 6 7@DB("projects") 8class ProjectsModel { 9 attributes = { 10 id: { 11 type: DataTypes.BIGINT, 12 allowNull: false, 13 primaryKey: true, 14 autoIncrement: true 15 }, 16 category_id: { 17 type: DataTypes.INTEGER(11), 18 allowNull: false 19 }, 20 ... 21 }; 22 options = { 23 tableName: "projects", 24 createdAt: "created_at", 25 updatedAt: "updated_at", 26 timestamps: true, 27 underscored: true, 28 }; 29 associate(models) { 30 models.projects.hasMany(models.jobs, { foreignKey: "project_id", as: "jobs" }); 31 }; 32)
Example with express HTTP API:
使用如下命令可自动生成
faas-express route projects -n function-name -f your-fn-yaml.yml
Routes:
1/**
2 * your-fn-name/projects.js
3 **/
4import { router as app, db, utils as util, service, variables, Route } from "faas-express";
5
6app.get("/projects", function(req, res) {
7 let page = util.getPage(req.query),
8 size = util.getSize(req.query),
9 offset = (page - 1) * size,
10 limit = size;
11 let where = util.getQueryWhere(req.query, db.Op, db.projects);
12 let order = util.getOrder(req.query, db.projects);
13 db.projects
14 .findAndCountAll({
15 where,
16 limit,
17 offset,
18 order
19 })
20 .then(data => {
21 res.json(util.wrapWithPage(data, page, size));
22 })
23 .catch(err => {
24 res.json(util.wrapError(err.message));
25 });
26});
使用修饰器方式
faas-express route projects -n function-name -f your-fn-yaml.yml -d
Routes:
1/**
2 * your-fn-name/projects.js
3 **/
4import { router as app, db, utils as util, service, variables, Route } from "faas-express";
5
6@Route("/projects", "get")
7getProjects(req, res) {
8 let page = util.getPage(req.query),
9 size = util.getSize(req.query),
10 offset = (page - 1) * size,
11 limit = size;
12 let where = util.getQueryWhere(req.query, db.Op, db.projects);
13 let order = util.getOrder(req.query, db.projects);
14 db.projects
15 .findAndCountAll({
16 where,
17 limit,
18 offset,
19 order
20 })
21 .then(data => {
22 res.json(util.wrapWithPage(data, page, size));
23 })
24 .catch(err => {
25 res.json(util.wrapError(err.message));
26 });
27})
Service Methods:
1/** 2 * your-fn-name/projects.js 3 **/ 4import { service, db, variables, Service } from "faas-express"; 5 6service.define("projects", { 7 /** 8 * 更新项目配置,同时更新日常编排任务配置 9 * @param {*} pid 10 * @param {*} params 11 */ 12 async updateProjectWithTask(pid, params) { 13 let label_configuration = params.label_configuration || {}; 14 return await db.sequelize.transaction(t => { 15 return Promise.all([ 16 db.projects.update(params, { 17 where: { id: pid }, 18 transaction: t 19 }), 20 db.daily_tasks.update( 21 { 22 assigned_group_list: label_configuration.assigned_group_list, 23 max_worker_count: label_configuration.max_worker_count 24 }, 25 { 26 where: { project_id: pid }, 27 transaction: t 28 } 29 ) 30 ]); 31 }); 32 }, 33 async test(){ 34 console.log("schedule doing"); 35 } 36});
使用修饰器方式
faas-express route projects -n function-name -f your-fn-yaml.yml -d
1/** 2 * your-fn-name/projects.js 3 **/ 4import { service, db, variables, Service } from "faas-express"; 5 6@Service("projects") 7class Projects { 8 /** 9 * 更新项目配置,同时更新日常编排任务配置 10 * @param {*} pid 11 * @param {*} params 12 */ 13 async updateProjectWithTask(pid, params) { 14 let label_configuration = params.label_configuration || {}; 15 return await db.sequelize.transaction(t => { 16 return Promise.all([ 17 db.projects.update(params, { 18 where: { id: pid }, 19 transaction: t 20 }), 21 db.daily_tasks.update( 22 { 23 assigned_group_list: label_configuration.assigned_group_list, 24 max_worker_count: label_configuration.max_worker_count 25 }, 26 { 27 where: { project_id: pid }, 28 transaction: t 29 } 30 ) 31 ]); 32 }); 33 }, 34 async test(){ 35 console.log("schedule doing"); 36 } 37};
swagger support:
1 /** 2 * @swagger 3 * definitions: 4 * projects: 5 * properties: 6 * category_id: 7 * type: string 8 * description: 分类ID 9 * name: 10 * type: string 11 * description: 项目名称 12 * status: 13 * type: integer 14 * description: 项目状态 15 */ 16 17 /** 18 * @swagger 19 * /projects: 20 * get: 21 * tags: 22 * - 项目管理 23 * description: 分页查询 24 * produces: 25 * - application/json 26 * parameters: 27 * - name: name 28 * description: 项目名称 29 * in: query 30 * type: string 31 * - name: page 32 * in: query 33 * type: integer 34 * - name: size 35 * in: query 36 * type: integer 37 * responses: 38 * 200: 39 * description: An array of projects 40 * schema: 41 * $ref: '#/definitions/projects' 42 */
express web服务框架 express
sequelize 数据库操作ORM框架 sequelize / sequelize blog
swagger 接口文档生成器 swagger / swagger example
start with node10-express template
No vulnerabilities found.
No security vulnerabilities found.