1.1.21 • Published 1 year ago
lycq v1.1.21
#打造个人专用的工具库
mongodb包含以下函数
注意使用前,必须安装 npm i mongodb
函数 | 说明 |
---|---|
newMongoClient | 封装好的链接数据库函数,传入连接即可使用,并且是promise风格 |
ObjectId | 用于使用_id查询数据时必须添加的函数,就是原来mongodb.ObjectId |
MongoClient | 用于链接数据库的函数,就是原来mongodb.MongoClient |
const mongo=require('lycq').mongo;
let dbUrl="mongodb://name:password@127.0.0.1:27017/dbname"
/**
* [newMongoClient 用Promise封装的mongodb数据库连接函数以同步方式运行]
* @param {[str]} dbUrl:数据库链接
* @return {[obj]} [反回数据库对象]
*/
let main=async ()=>{
let client=await mongo.newMongoClient(dburl);
let db_table=client.collection("table");
}
/**
* [ObjectId mongodb的_id]
* @param {[str]} _id:mongodb的_id
* @return {[obj]} [反回对象]
*/
mongo.ObjectId(_id);
/**
* [MongoClient mongodb驱动模块自带的MongoClient对象]
*/
mongo.MongoClient.connect(dbUrl,{useNewUrlParser: true,useUnifiedTopology: true}, async (err, client)=> {
try{
if(err){
console.error(`发生错误的数据库连接:${dbUrl}`);
}else{
console.log(`连接成功:${dbUrl}`);
let doc= client.db();
}
}catch (e) {
console.error(e.message);
console.error(`连接异常:${dbUrl}`);
}
})
tool包含以下函数
函数 | 说明 |
---|---|
repair | 字符串补全,默认补0 |
formatTime | 时间格式转换 |
uuid | 创建一个uuid |
randomString | 创建随机字符串 |
send | 发送 http 或 https 请求 |
getIP | 转换IP地址 |
const tool=require('lycq').tool;
/**
* [repair 给字符串补0,增加长度]
* @param {[num]} n:输出长度
* @param {[num]} v:传入的值
* @return {[str]} [反回补全字符串]
*/
tool.repair(n,v)
/**
* [formatTime 时间戳格式函数]
* @param {[num,obj,str]} time :时间戳
* @param {[str]} format :Y年M月D日 h:m:s:i
* @return {[str]} [反回格式化后日期字符串]
*/
let doc=formatTime(time,"YMDhmsi"); //202106171210220840
或者
let doc=tool.formatTime(time,"Y-M-D h:m:s:i"); //2021-06-17 12:09:59:0430
/**
* [randomString 返回随机字符串]
* @param {[num]} len:限制随机数长度,默认36
* @return {[str]} [反回随机字符串]
*/
tool.randomString(len);
/**
* [uuid 创建uuid]
* @param {[num]} v:第一个字符串的时间戳转的进制,如8,16,32,最大不能超过32
* @param {[num]} n:增加多组随机字符串,
* @param {[num]} m:每一组随机字符串的长度,
* @return {[str]} [反回uuid字符串]
*/
//默认值是 uuid(32,1,8),不传值也可以,直接uuid()
uuid(v,n,m)
// 17a182866d8-cthab7de-mhs4knfh-bydbf8rd-hiknbh4d
/**
* [send 发送http 或者 https]
* @param {[json]} options:发送请求的配置
* @param {[json]} contents:发送的参数
* @return {[str,json]} doc:返回获得的数据
*/
const tool=require('lycq').tool;
async function main(){
let options={
type:"http",// http 和 https
host: "127.0.0.1", //IP或者域名
port: 3000, //端口
path: "/register/sendCheakCode/v1000", //接口
method:'GET',// POST 和 GET
};
let contents={
aa:"11" //这里是参数
}
let doc=await tool.send(options,contents);
console.log(doc);
}
main()
/**
* [getIP 转换IP地址]
* @param {[obj]} req:要转换IP地址的对象
* @return {[str]} doc:返回IP地址
*/
tool.getIP(req);
/**
* [trim 左右去空格]
* @param {str} data:要去空格的数据
* @return {[str]} doc:已经去空格的数据
*/
tool.trim(data)
/**
* [dataType 返回传入数据的类型]
* @param {str} data:要判断的数据
* @return {[str]} doc:返回数据类型字符串
*/
tool.dataType(data)
注意判断的时候,必须是是否等于字以下字符串
if(too.dataType("123")=="[object String]"){
}
[object Boolean] 布尔类型
[object Number] 数字类型
[object String] 字符串类型
[object Undefined] 未定义类型
[object Null] 不存在类型
[object Array] 数组类型
[object Object] 对象类型
[object Function] 函数类型
/**
* [timeOut 阻塞函数]
* @param {num} t:要阻塞多少毫秒
*/
await tool.timeOut(t);
1.1.19
1 year ago
1.1.18
1 year ago
1.1.21
1 year ago
1.1.20
1 year ago
1.1.9
3 years ago
1.1.8
3 years ago
1.1.7
3 years ago
1.1.6
3 years ago
1.1.5
3 years ago
1.1.4
3 years ago
1.1.3
3 years ago
1.1.12
3 years ago
1.1.11
3 years ago
1.1.10
3 years ago
1.1.16
3 years ago
1.1.15
3 years ago
1.1.14
3 years ago
1.1.13
3 years ago
1.1.2
3 years ago
1.1.1
3 years ago
1.1.0
3 years ago
1.0.9
3 years ago
1.0.8
3 years ago
1.0.7
3 years ago
1.0.6
3 years ago
1.0.5
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.1
3 years ago
1.0.0
3 years ago