npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eagle-mongo

v1.0.1

Published

- [eagle-mongo](#eagle-mongo) - [connect](#connect) - [createMongoModel](#createmongomodel) - [findSchema](#findschema) - [findOneSchema](#findoneschema) - [updateOneSchema](#updateoneschema) - [updateManySchema](#updatemanyschema) - [addSch

Downloads

153

Readme

eagle-mongo

// model.js
const eagleMongo = require('eagle-mongo')

const model = eagleMongo.model('customCollection', {
  sat_id: String,
  inst_id: String,
  prod_name: String,
  group: String,
  scientific_owner: [String],
  quality_owner: [String],
  develop_owner: [String],
  exist_owner: Boolean,
  exist_algorithms: Boolean,
  prod_status: String,
  notes: String
})

model.method.initData = function (body) {
  for (const p in body) {
    this[p] = body[p]
  }
} 

module.export = model

// main.js
const condition = SyslogModel.getCondition({
  type: type,
  system: system,
  tag: tag,
  year: year,
  month: month,
  date: date
})
try {
  const doc = await SyslogModel.findSchema({
    condition: condition,
    sort: { timestamp: -1 },
    limit: limit,
    skip: skip
  })
  return res.status(200).json(doc)
} catch (error) {
  return next(error)
}

connect

connect(mongooseUri[, options])

连接MongoDB

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

mongooseUri (string): 连接字符串(与Mongoose一致)

[options] (object): 连接参数

  • socketTimeoutMS (number): 连接超时时间 默认15000ms
  • serverSelectionTimeoutMS (number): 选择服务器过程中的等待时间 默认15000ms
  • maxPoolSize (number): 连接池最大连接数 默认500

返回值

(Promise)

例子

const optMongoose = {
  socketTimeoutMS: 15000,
  keepAlive: true,
  useNewUrlParser: true,
  useUnifiedTopology: true,
  useCreateIndex: true
}
const mongooseUri = `mongodb://${cnf.mongodb.uid}:${cnf.mongodb.pwd}@${cnf.mongodb.host}:${cnf.mongodb.port}/${cnf.mongodb.database}`
mongoose.connect(mongooseUri, optMongoose).then(
  () => {
    console.log('mongodb connect successfully')
    redisClient = redis.createClient(optRedis)
    redisClient.on('error', function (error) {
      console.warn(error.message)
      console.warn('Unable cache data to Redis')
      redisClient.end(true)
      return resolve(null)
    })
    redisClient.on('connect', () => {
      console.log('Redis connect successfully')
      return resolve(redisClient)
    })
  },
  err => {
    return reject(err)
  }
)

createMongoModel

createMongoModel(collectionName, model,[, options])

创建MongoModel

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

collectionName (string): collection名

model (object): 文档设计对象,model示例

[options] (object): 参数

  • initData (Function): 自定义初始化函数,默认使用defaultInitData
  • toJSON (object): 与mongoose toJSON 参数一致,默认{ virtuals: true }

返回值

(mongoose.Model): Mongoose的Model对象

例子

const SyslogModel = MongooseHelp.createMongoModel('system_log', syslogSchema, {
  initData: initData
})

function initData (data) {
  for (const p in data) {
    this[p] = data[p]
  }
  this.code = this.code || ares.uuid()
  this.timestamp = moment().utc()
  this.year = this.year || moment(data.timestamp).get('year')
  this.month = this.month || (moment(data.timestamp).get('month') + 1)
  this.date = this.date || moment(data.timestamp).format('YYYY-MM-DD')
}

// ... ...
const doc = await SyslogModel.findSchema({
  condition: condition,
  sort: { timestamp: -1 },
  limit: limit,
  skip: skip
})

findSchema

findSchema(args)

查询(返回多条记录)

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

args (object):

  • condition (object): mongoose 查询条件
  • sort? (object): mongoose 排序条件
  • limit? (number): mongoose 指定查询将返回的最大文档数量
  • skip? (number): mongoose 指定要跳过的文档数量

暂不支持 populate? (object): mongoose 指定应该与其他文档一起填充的路径

返回值

([object]): 查询结果的数组

例子

async function getSysLog (req, res, next) {
  const type = req.query.type
  const system = req.query.system
  const tag = req.query.tag
  const startTime = req.query.start
  const endTime = req.query.end
  const skip = Number(req.query.skip || 0)
  const limit = Number(req.query.limit || 100)
  const year = req.query.year
  const month = req.query.month
  const date = req.query.date
  const condition = SyslogModel.getCondition({
    type: type,
    system: system,
    tag: tag,
    year: year,
    month: month,
    date: date
  })
  if (startTime) {
    condition.timestamp = condition.timestamp || {}
    condition.timestamp.$gte = moment(startTime, 'YYYYMMDDHHmm00').utc()
  }
  if (endTime) {
    condition.timestamp = condition.timestamp || {}
    condition.timestamp.$lte = moment(endTime, 'YYYYMMDDHHmm00').utc()
  }
  try {
    const doc = await SyslogModel.findSchema({
      condition: condition,
      sort: { timestamp: -1 },
      limit: limit,
      skip: skip
    })
    return res.status(200).json(doc)
  } catch (error) {
    return next(error)
  }
}

findOneSchema

findOneSchema(condition)

查询(返回一条记录)

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

condition (object): mongoose查询条件

返回值

(object): 查询结果

例子

async function getSysLog (req, res, next) {
  const type = req.query.type
  const system = req.query.system
  const tag = req.query.tag
  const startTime = req.query.start
  const endTime = req.query.end
  const skip = Number(req.query.skip || 0)
  const limit = Number(req.query.limit || 100)
  const year = req.query.year
  const month = req.query.month
  const date = req.query.date
  const condition = SyslogModel.getCondition({
    type: type,
    system: system,
    tag: tag,
    year: year,
    month: month,
    date: date
  })
  try {
    const doc = await SyslogModel.findOneSchema(condition)
    return res.status(200).json(doc)
  } catch (error) {
    return next(error)
  }
}

updateOneSchema

updateOneSchema(args)

更新文档(一条)

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

args (object):

  • condition (object): mongoose 查询条件
  • updateQuery (object): mongoose 更新内容
  • options? (object): mongoose 更新参数

返回值

(object): mongoose raw

例子

async function updateServer (req, res, next) {
  try {
    const condition = { _id: req.params._id }
    const updateQuery = { $set: {} }
    for (const p in req.body) {
      updateQuery.$set[p] = req.body[p]
    }
    const raw = await ServerModel.updateOneSchema({
      condition: condition,
      updateQuery: updateQuery
    })
    return res.status(200).json(raw)
  } catch (error) {
    return next(error)
  }
}

updateManySchema

updateManySchema(args)

更新文档(多条)

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

args (object):

  • condition (object): mongoose 查询条件
  • updateQuery (object): mongoose 更新内容
  • options? (object): mongoose 更新参数

返回值

(object): mongoose raw

例子

async function updateServer (req, res, next) {
  try {
    const condition = { _id: req.params._id }
    const updateQuery = { $set: {} }
    for (const p in req.body) {
      updateQuery.$set[p] = req.body[p]
    }
    const raw = await ServerModel.updateManySchema({
      condition: condition,
      updateQuery: updateQuery
    })
    return res.status(200).json(raw)
  } catch (error) {
    return next(error)
  }
}

addSchema

addSchema(body)

插入文档

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

body (object): 文档对象

返回值

(object): mongoose 文档对象

例子

async function addSysLog (req, res, next) {
  const body = req.body
  try {
    const doc = await SyslogModel.addSchema(body)
    return res.status(200).json(doc)
  } catch (error) {
    return next(error)
  }
}

deleteOneSchema

deleteOneSchema(condition)

删除文档(单一)

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

condition (object): mongoose 查询条件

例子

async function deleteServer (req, res, next) {
  try {
    const condition = { _id: req.params._id }
    await ServerModel.deleteOneSchema(condition)
    return res.status(200).end()
  } catch (error) {
    return next(error)
  }
}

deleteManySchema

deleteManySchema(condition)

删除文档

版本历史

| 版本 | 变更 | |-------|------| | 1.0.0 | 新增 |

参数

condition (object): mongoose 查询条件

例子

async function deleteServer (req, res, next) {
  try {
    const condition = { _id: req.params._id }
    await ServerModel.deleteManySchema(condition)
    return res.status(200).end()
  } catch (error) {
    return next(error)
  }
}

defaultInitData

function defaultInitData (data) {
  for (const p in data) {
    this[p] = data[p]
  }
}

model示例

const sysLogModel = {
  /* 系统名称 */
  code: { type: String },
  source: { type: String },
  /* 日志类型 trace debug info warn error fatal */
  type: { type: String, default: 'info' },
  /* 时间戳 */
  timestamp: { type: Date },
  /* 日志内容 */
  message: { type: String },
  /* 元信息 包括但不限于线程 ID、进程 ID、用户 ID、主机名等其他与日志消息相关的元数据信息 */
  metadata: { type: Object },
  /* 标签 */
  tag: { type: [String] },
  /* 日期 */
  year: { type: Number },
  month: { type: Number },
  date: { type: String }
}