当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript sqlcmd-pg.Connection类代码示例

本文整理汇总了TypeScript中sqlcmd-pg.Connection的典型用法代码示例。如果您正苦于以下问题:TypeScript Connection类的具体用法?TypeScript Connection怎么用?TypeScript Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Connection类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: Connection

import {Connection} from 'sqlcmd-pg'

interface Package {
  id: number
  name: string
}

interface Statistic {
  package_id?: number
  day: Date
  downloads: number
}

export const db = new Connection({
  host: '127.0.0.1',
  port: 5432,
  database: 'npm-history',
})

// connect db log events to local logger
db.on('log', ev => {
  const args = [ev.format].concat(ev.args)
  logger[ev.level].apply(logger, args)
})

const NPM_EPOCH = new Date('2009-09-29')

/**
Custom-built 'INSERT INTO <table> (<columns>) VALUES (<row1>), (<row2>), ...;'
SQL query for inserting statistic rows. I don't think there are any limits on
the number of parameters you can have in a prepared query.
开发者ID:chbrown,项目名称:npm-history,代码行数:31,代码来源:database.ts

示例2: callback

 .execute((error: Error, rows: Package[]) => {
   if (error) return callback(error)
   if (rows.length > 0) return callback(null, rows[0])
   db.Insert('package')
   .set({name}).returning('*')
   .execute((insertError: Error, insertRows: Package[]) => {
     if (insertError) return callback(insertError)
     return callback(null, insertRows[0])
   })
 })
开发者ID:chbrown,项目名称:npm-history,代码行数:10,代码来源:database.ts

示例3: getRangeStatistics

      getRangeStatistics(name, start, end, (statsError, statistics) => {
        if (statsError) return callback(statsError)

        // 5. save the values we just fetched
        const [sql, args] = buildMultirowInsert(package_row.id, statistics)
        db.executeSQL(sql, args, (sqlError: Error) => {
          if (sqlError) return callback(sqlError)

          // 6. merge local and new statistics for the response
          const total_statistics = statistics.concat(local_statistics).sort((a, b) => a.day.getTime() - b.day.getTime())
          callback(null, total_statistics)
        })
      })
开发者ID:chbrown,项目名称:npm-history,代码行数:13,代码来源:database.ts

示例4: findOrCreatePackage

/**
Given a package name, return the full package row from the database, creating
one if needed.
*/
function findOrCreatePackage(name: string, callback: (error: Error, package_row?: Package) => void) {
  db.Select('package')
  .whereEqual({name})
  .execute((error: Error, rows: Package[]) => {
    if (error) return callback(error)
    if (rows.length > 0) return callback(null, rows[0])
    db.Insert('package')
    .set({name}).returning('*')
    .execute((insertError: Error, insertRows: Package[]) => {
      if (insertError) return callback(insertError)
      return callback(null, insertRows[0])
    })
  })
}
开发者ID:chbrown,项目名称:npm-history,代码行数:18,代码来源:database.ts

示例5: queryAverageDownloads

export function queryAverageDownloads(start: moment.Moment,
                                      end: moment.Moment,
                                      callback: (error: Error, packages?: {[index: string]: number}) => void) {
  db.Select('package_statistic')
  .add('name', 'AVG(downloads)::int AS average')
  .where('downloads > -1')
  .where('day >= ?', start.toDate())
  .where('day < ?', end.toDate())
  .groupBy('name')
  .orderBy('name')
  .execute((error: Error, rows: {name: string, average: number}[]) => {
    if (error) return callback(error)
    logger.info('averaged downloads for %d packages', rows.length)
    const packages: {[index: string]: number} = {}
    rows.forEach(row => packages[row.name] = row.average)
    callback(null, packages)
  })
}
开发者ID:chbrown,项目名称:npm-history,代码行数:18,代码来源:database.ts


注:本文中的sqlcmd-pg.Connection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。