飞道科技

飞道科技文档总汇

飞道Web前端开发-服务开发调试

  1. 数据服务
    1. 数据新增
    2. 查询服务
    3. 数据修改
    4. 数据删除
  2. 调用第三方接口服务
  3. 调用node.js/npm模块服务
    1. 数据服务查询
      1. 单条件查询
      2. 多条件查询

数据服务

我们假设有以下四个表

明星表 Star

_id name sex
1 John 1
2 Jack 1
3 Jean 0

水果价格 FruitPrices

_id name price
1 apple 2.00
2 banana 3.00
3 tomato 0.00

明星爱好表 Hobby

_id star hobby
1 1 Eat, Drink, Smoke
2 2 Sleep
3 3 Eat, Drink, Smoke, Sleep

明星成名作品 Works

_id star works
1 1 Smoke in the night
2 2 Sleep in the day
3 3 Smoke and sleep day and night

其中,StarHobby 以及 FruitPrices 表在一个数据库上,而Works在另外一个数据库上,它们分别对应的systemid分别是10102020

数据新增

接下来,我们在目录service/src中,新建一个ts文件,名字叫star-insert.ts,为了省事,我直接复制一个模板文件

cp ./service/src/db-insert.ts ./service/src/star-insert.ts

例子已经有了,我们在原有例子上进行一些修改,使之符合我们的需求,我们的需求是什么?我们还没有需求,如果是在开发项目,项目经理会告诉你具体的需求,而我们现在是做教程例子,所以需求嘛,我们自己来编一个:

  1. 新增一种水果,名字叫番茄,还要给它定一个价格,就当五毛钱一公斤吧.我们有了字段的值,最后只缺少_id这个字段了,_id是唯一标识, 所以,我们使用uuid这个模块来生成一个唯一标识.
  2. 新增一个明星,名字叫悟能, 男性。
  3. 新增这个明星的爱好是“吃和睡”
  4. 新增这个明星的作品,一个是“九吃钉耙”,一个是“嫦娥仙子”
import { v4 as uuid } from 'uuid';
import insert from './atom/db/insert';

import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
}

interface Star extends IBaseTable {
  name: string;
  sex: 0 | 1;
}

interface FruitPrices extends IBaseTable {
  name: string;
  price: number;
}

interface Hobby extends IBaseTable {
  star: string;
  hobby: string;
}

interface Works extends IBaseTable {
  star: string;
  works: string;
}

async function atom(msg: Message) {
  const { exec, prepare } = insert(action_id, session_id, spaceid, '1000');
  prepare<FruitPrices>('fruitprices', [{ _id: uuid(), name: '番茄', price: 0.5 }]);
  const star_id = uuid();
  prepare<Star>('star', [{ _id: star_id, name: '悟能', sex: 1 }]);
  prepare<Hobby>('hobby', [{ _id: uuid(), star: star_id, hobby: '吃和睡' }]);
  const ret = await exec();
  if (ret.code !== 1) {
    return 'fail';
  }
  const { exec: exec2, prepare: prepare2 } = insert(action_id, session_id, spaceid, '2000');
  prepare2<Works>('works', [{ _id: uuid(), star: star_id, works: '九吃钉耙' }, { _id: uuid(), star: star_id, works: '嫦娥仙子' }]);
  const ret2 = await exec2();
  if (ret2.code === 1) {
    return 'success';
  } else {
    return 'fail';
  }
}

以上服务执行完成后

明星表 Star

_id name sex
1 John 1
2 Jack 1
3 Jean 0
0ea5 悟能 1

水果价格 FruitPrices

_id name price
1 apple 2.00
2 banana 3.00
3 tomato 0.00
xxx 番茄 0.5

明星爱好表 Hobby

_id star hobby
1 1 Eat, Drink, Smoke
2 2 Sleep
3 3 Eat, Drink, Smoke, Sleep
xxx 0ea5 吃和睡

明星成名作品 Works

_id star works
1 1 Smoke in the night
2 2 Sleep in the day
3 3 Smoke and sleep day and night
xxx 0ea5 九吃钉耙
xxx 0ea5 嫦娥仙子

查询服务

查询表Star中所有为男性的数据,并查询他们的成名作品Works,以及作品的总数

import count from './atom/db/count';
import query from './atom/db/query';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
  man?: boolean;
}

interface Star extends IBaseTable {
  name: string;
  sex: 0 | 1;
}

interface Works extends IBaseTable {
  star: string;
  works: string;
}

async function atom(msg: Message) {
  const { exec, prepare } = query(action_id, session_id, spaceid, '1000');
  prepare('star', ['_id', 'name'], { sex: msg.man ? 1 : 0 });
  const [stars] = await exec();
  const m = new Map<string, string>();
  const star_ids = (stars as Star[]).map((s) => {
    const id = s._id;
    m.set(id, s.name);
    return id;
  });
  const { exec: e2, prepare: p2 } = query(action_id, session_id, spaceid, '2000');
  const q = {
    star: {
      $in: star_ids
    }
  };
  p2('works', ['star', 'works'], q, 20, 1);
  const [all_works] = (await e2()) as [Works[]];
  const works_count = await (async () => {
    if (all_works.length < 20) {
      return all_works.length;
    } else {
      const { exec: e3, prepare: p3 } = count(action_id, session_id, spaceid, '2000');
      p3('works', q);
      const [n] = await e3();
      return n
    }
  })();
  const works = all_works.map((work) => {
    const { star: id, works } = work;
    return {
      star: m.get(id),
      works
    };
  });
  return {
    works,
    works_count
  };
}

最终返回的数据格式应如下所示:

{
  "works": [{"star": "John", "works": "Smoke in the night"}, {"star": "Jack", "works": "Sleep in the day"}, {"star": "悟能", "works": "九吃钉耙"}, {"star": "悟能", "works": "嫦娥仙子"}],
  "works_count": 4
}
star works
John Smoke in the night
Jack Sleep in the day
悟能 九吃钉耙
悟能 嫦娥仙子

数据修改

需求: 蕃茄涨价5块

涨价前

_id name price
1 apple 2.00
2 banana 3.00
3 tomato 0.00
xxx 番茄 0.5
import update from './atom/db/update';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
}

interface FruitPrices extends IBaseTable {
  name: string;
  price: number;
}

async function atom(msg: Message) {
  const { exec, prepare } = update(action_id, session_id, spaceid, '1000');
  prepare<FruitPrices>('fruitprices', [{ _id: 'xxx', price: 5.5 }]);
  // 5、执行修改,修改结果为JS对象,code值为0、1,1标示执行成功,0标示执行失败,result为失败信息
  const ret = await exec();
  if (ret.code === 1) {
    return 'success';
  } else {
    return 'fail';
  }
}

涨价后

_id name price
1 apple 2.00
2 banana 3.00
3 tomato 0.00
xxx 番茄 5.5

数据删除

需求:删除悟能的作品

删除前

_id star works
1 1 Smoke in the night
2 2 Sleep in the day
3 3 Smoke and sleep day and night
xxx 0ea5 九吃钉耙
xxx 0ea5 嫦娥仙子
import del from './atom/db/del';
import query from './atom/db/query';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
  star: string;
}

async function atom(msg: Message) {
  const { exec: e1, prepare: p1 } = query(action_id, session_id, spaceid, '1000');
  p1('star', ['_id'], {
    name: msg.star
  });
  const [stars] = await e1();
  if (stars.length !== 1) {
    return 'success';
  }
  const { exec, prepare } = del(action_id, session_id, spaceid, '2000');
  // 3、确定要获取数据的表名,删除条件,若没有条件则全部删除
  prepare('works', [{ star: stars[0]._id }]);
  // 5、执行删除,删除结果为JS对象,code值为0、1,1标示执行成功,0标示执行失败,result为失败信息
  const ret = await exec()
  if (ret.code === 1) {
    return 'success';
  } else {
    return 'fail';
  }
}

删除后

_id star works
1 1 Smoke in the night
2 2 Sleep in the day
3 3 Smoke and sleep day and night

调用第三方接口服务

调用node.js/npm模块服务

数据服务查询

单条件查询

op desc eg
en 等于 { field: value }
in in { field: { $in: values[] } }
nin not in { field: { $nin: values[] } }
lt 小于 { field: { $lt: value } }
gt 大于 { field: { $gt: value } }
lte 小于等于 { field: { $lte: value } }
gte 大于等于 { field: { $gte: value } }
ne 不等于 { field: { $ne: value } }
regex 模糊匹配 { field: { $regex: stringvalue, $options: ‘imxs’ } }
like 模糊匹配 { field: { $like: ‘%’+stringvalue+’%’ } }

注意

目前正则表达式只识别以^开头,并以$结尾的格式

多条件查询

op desc eg
and 并且 { $and: [{ field1: value }, { field2: value }] }
or 或者 { $or: [{ field1: value }, { field2: value }] }
nor 即不,也不 { $nor: [{ field1: value }, { field2: value }] }