README
关于fancy-tool
fancy-tool用来封装一些可以在不同团队或项目中使用的一些共通方法或共通模块。
项目地址
npm 仓库:https://www.npmjs.com/package/fancy-tool#appmodels
gitlab 仓库: https://gitlab.wks.wistron.com.cn/mld700/fancy-tool
安装
npm install fancy-tool
加入我们
contents
⬆
TransformNumber主要用于数值的转换
⬆
parameters- ROUND:四舍五入取整
- *100:乘以100
- %:添加百分号
- .1:保留一位小数
- .2:保留两位小数
- .3:保留三位小数
- /12:除以12
- THOUSAND:千分位形式
- SYMBOL:添加正号或负号
⬆
examples// 在 .js 文件中引入
var transformNumber = require('fancy-tool').TransformNumber;
// 在 .ts 文件中引入
import { TransformNumber } from 'fancty-tool/index';
// 保留1位小数 输出:1.2
transformNumber.transform('1.234', '.1');
transformNumber.transform('1.234#.1');
// 多个参数可以逗号分隔,循环处理,注意参数的顺序
// 转换为百分比 输出:45%
transformNumber.transform('0.45', '*100,%');
transformNumber.transform('0.45#*100,%');
// 数字后紧跟的参数拥有最高优先级
transformNumber.transform('1.2345#.1', '.3'); // 输出:1.2
transformNumber.transform('36#/12', '*100'); // 输出:3
⬆
AppModels封装前端访问loopback模型Restful API的常用方法
⬆
import/require// 在 .js 文件引入
var appModels = require('fancy-tool').AppModels;
// 在 .ts 文件引入
import { AppModels } from 'fancy-tool/index';
⬆
appInit初始化AppModels实例
AppModels.appInit({
baseURL: 'http://xxx.xxx.com/api', // 必填
headers: { // 选填
...
}
});
⬆
about filter更多使用方式可以参考:https://loopback.io/doc/en/lb3/Querying-data.html
⬆
find从数据库中查找与筛选器匹配所有模型实例
成功返回:[{...}, {...}, ...]
失败返回:[]
// 查询 Users 表中所有记录
AppModels.find('Users').then(res=>{
console.log(res);
});
// 使用筛选器查询
AppModels.find('Users', {
where: {
name: 'jack',
age: 18,
}
});
// 使用筛选器过滤指定字段
AppModels.find('Users', {
fields: {
name: true,
age: true,
}
});
⬆
update从数据库中更新与筛选器匹配的模型实例
成功返回:更新的记录数
失败返回:-1
AppModels.update('Users', {
id: 1,
}, {
name: 'jack',
age: 18,
});
⬆
count统计数据库中与筛选器匹配的模型实例个数
成功返回:匹配的记录数
失败返回:-1
AppModels.count('Users', {
name: 'jack',
age: 18,
});
⬆
insert创建新的模型实例并保存到数据库中,新的模型实例中不要包含id字段
成功返回:插入的记录实例, { id: xx, ... }
失败返回:null
AppModels.insert('Users', {
name: 'jack',
age: 18,
});
⬆
exists根据主键id检查数据库中是否存在模型实例
成功返回:true/false
失败返回:null
AppModels.exist('Users', 1);
⬆
replaceById根据主键id替换数据库中的模型实例,主键id不可替换
成功返回:替换的记录实例, { id: xx, ... }
失败返回:null
AppModels.replaceById('Users', 1, {
name: 'jack',
age: 18,
});