How to use the pg.types.setTypeParser function in pg

To help you get started, we’ve selected a few pg examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Paxa / postbird / app / connection.js View on Github external
var types = require('pg').types;
var moment = require('moment')

var TIMESTAMPTZ_OID = 1184
var TIMESTAMP_OID = 1114
var customDateParser = (val) => {
  if (val === null) {
    return null;
  } else {
    var date = moment.parseZone(val)
    date.origValueString = val
    return date
  }
};

types.setTypeParser(TIMESTAMPTZ_OID, customDateParser)
types.setTypeParser(TIMESTAMP_OID, customDateParser)

var usingNativeLib = false;
// Disable loading pg-native until issue with openssl paths is solved
/*
try {
  if (process.platform == "darwin" || process.platform == "linux") {
    if (pg.native) {
      // @ts-ignore
      pg = pg.native;
      usingNativeLib = true;
    }
  }
} catch (error) {
  console.log("can not load pg-native, using pg");
  console.error(error);
github NLeSC / spot / server / server-postgres.js View on Github external
* 3. Set database connection string and table name
 */
var parseConnection = require('pg-connection-string').parse;

// use the native bindings for slightly more performance
var pg = require('pg').native;
var pool;

// Do not do any parsing for postgreSQL datetime types
var types = require('pg').types;
var SQLDatetimeTypes = [1082, 1083, 1114, 1184, 1182, 1266];
SQLDatetimeTypes.forEach(function (type) {
  types.setTypeParser(type, function (val) { return val; });
});
// Do not do any parsing for postgreSQL interval type
types.setTypeParser(1186, function (val) { return val; });

/**
 * Perform an database query, and perform callback with the result
 * @function
 * @params{Squel.expr} q
 * @params{function} cb
 * @params{scope} that scope for the callback
 */
function queryAndCallBack (q, cb, that) {
  pool.connect(function (err, client, done) {
    if (err) {
      return console.error('error fetching client from pool', err);
    }

    client.query("set intervalstyle = 'iso_8601'; set time zone 'GMT'; " + q.toString(), function (err, result) {
      done(err);
github fullstack-build / fullstack-one / packages / db / lib / DbAppClient / index.ts View on Github external
import { Service, Inject, Container } from "@fullstack-one/di";
import { ILogger, LoggerFactory } from "@fullstack-one/logger";
import { IEnvironment, Config } from "@fullstack-one/config";
import { BootLoader } from "@fullstack-one/boot-loader";
import { IDb } from "../types";
import { Client as PgClient, ClientConfig as PgClientConfig, types as PgTypes } from "pg";
import { IDbConfig, IDbAppClientConfig } from "../IDbConfig";
import { HookManager } from "./HookManager";
// stop pg from parsing dates and timestamps without timezone
PgTypes.setTypeParser(1114, (str: any) => str);
PgTypes.setTypeParser(1082, (str: any) => str);

export { PgClient };

@Service()
export class DbAppClient implements IDb {
  private applicationNamePrefix: string;
  private applicationName: string;
  private readonly credentials: IDbAppClientConfig;

  private readonly ENVIRONMENT: IEnvironment;
  private readonly logger: ILogger;
  private readonly config: IDbConfig;
  public readonly hookManager: HookManager;
  public readonly databaseName: string;
  public pgClient: PgClient;
github fullstack-build / fullstack-one / packages / db / lib / ORM / index.ts View on Github external
import { GracefulShutdown } from "@fullstack-one/graceful-shutdown";
import { EventEmitter } from "@fullstack-one/events";
import * as dbMigrationsObject from "../migrations";
import getClientManager, { IClientManager } from "./getClientManager";
import gracefullyRemoveConnection from "./gracefullyRemoveConnection";
import * as modelMeta from "./model-meta";
import { IOrmConfig } from "./types";

export { Connection, ConnectionManager, MigrationInterface, QueryBuilder } from "typeorm";
export { PostgresQueryRunner } from "typeorm/driver/postgres/PostgresQueryRunner";
export { PostgresConnectionOptions } from "typeorm/driver/postgres/PostgresConnectionOptions";
export * from "./decorator";

// stop pg from parsing dates and timestamps without timezone
PgTypes.setTypeParser(1114, (str) => str);
PgTypes.setTypeParser(1082, (str) => str);

@Service()
export class ORM {
  private readonly logger: ILogger;
  private readonly config: IOrmConfig;
  private readonly environment: IEnvironment;
  private readonly applicationNamePrefix: string;
  private readonly applicationName: string;
  private readonly clientManager: IClientManager;
  private readonly migrations: Array typeorm.MigrationInterface> = [];
  private readonly entities: Array any> = [];

  constructor(
    @Inject((type) => BootLoader) bootLoader: BootLoader,
    @Inject((type) => GracefulShutdown) gracefulShutdown: GracefulShutdown,
    @Inject((type) => LoggerFactory) loggerFactory: LoggerFactory,
github mikro-orm / mikro-orm / lib / connections / PostgreSqlConnection.ts View on Github external
      [1082, 1083, 1114].forEach(oid => types.setTypeParser(oid, str => new Date(str + 'Z'))); // date, time, timestamp types
      (defaults as any).parseInputDatesAsUTC = true;
github fullstack-build / fullstack-one / packages / db / lib / DbAppClient.ts View on Github external
import { Service, Inject, Container } from "@fullstack-one/di";
import { EventEmitter } from "@fullstack-one/events";
import { ILogger, LoggerFactory } from "@fullstack-one/logger";
import { IEnvironment, Config } from "@fullstack-one/config";
import { BootLoader } from "@fullstack-one/boot-loader";
import { IDb } from "./IDb";
import { Client as PgClient, ClientConfig as PgClientConfig, types as PgTypes } from "pg";
// stop pg from parsing dates and timestamps without timezone
PgTypes.setTypeParser(1114, (str) => str);
PgTypes.setTypeParser(1082, (str) => str);

export { PgClient };

@Service()
export class DbAppClient implements IDb {
  private applicationNamePrefix: string;
  private applicationName: string;
  // private credentials: PgClientConfig;
  private readonly credentials: any;

  // DI
  private readonly ENVIRONMENT: IEnvironment;
  private readonly config: Config;
  private readonly logger: ILogger;
  private readonly eventEmitter: EventEmitter;
github fullstack-build / fullstack-one / packages / db / lib / DbGeneralPool / index.ts View on Github external
import { Pool as PgPool, PoolConfig as PgPoolConfig, PoolClient as PgPoolClient, types as PgTypes } from "pg";
// stop pg from parsing dates and timestamps without timezone
PgTypes.setTypeParser(1114, (str) => str);
PgTypes.setTypeParser(1082, (str) => str);

export { PgPool, PgPoolClient };
import { Service, Inject, Container } from "@fullstack-one/di";
import { ILogger, LoggerFactory } from "@fullstack-one/logger";
import { IEnvironment, Config } from "@fullstack-one/config";
import { BootLoader } from "@fullstack-one/boot-loader";
import { GracefulShutdown } from "@fullstack-one/graceful-shutdown";
import { EventEmitter } from "@fullstack-one/events";
import { IDbGeneralPoolConfig } from "../IDbConfig";

@Service()
export class DbGeneralPool {
  private readonly logger: ILogger;
  private readonly config: IDbGeneralPoolConfig;
  private readonly eventEmitter: EventEmitter;
github fullstack-build / fullstack-one / packages / db / lib / DbAppClient.ts View on Github external
import { Service, Inject, Container } from "@fullstack-one/di";
import { EventEmitter } from "@fullstack-one/events";
import { ILogger, LoggerFactory } from "@fullstack-one/logger";
import { IEnvironment, Config } from "@fullstack-one/config";
import { BootLoader } from "@fullstack-one/boot-loader";
import { IDb } from "./IDb";
import { Client as PgClient, ClientConfig as PgClientConfig, types as PgTypes } from "pg";
// stop pg from parsing dates and timestamps without timezone
PgTypes.setTypeParser(1114, (str) => str);
PgTypes.setTypeParser(1082, (str) => str);

export { PgClient };

@Service()
export class DbAppClient implements IDb {
  private applicationNamePrefix: string;
  private applicationName: string;
  // private credentials: PgClientConfig;
  private readonly credentials: any;

  // DI
  private readonly ENVIRONMENT: IEnvironment;
  private readonly config: Config;
  private readonly logger: ILogger;
  private readonly eventEmitter: EventEmitter;
  private readonly CONFIG;
github fullstack-build / fullstack-one / packages / db / lib / DbAppClient / index.ts View on Github external
import { Service, Inject, Container } from "@fullstack-one/di";
import { ILogger, LoggerFactory } from "@fullstack-one/logger";
import { IEnvironment, Config } from "@fullstack-one/config";
import { BootLoader } from "@fullstack-one/boot-loader";
import { IDb } from "../types";
import { Client as PgClient, ClientConfig as PgClientConfig, types as PgTypes } from "pg";
import { IDbConfig, IDbAppClientConfig } from "../IDbConfig";
import { HookManager } from "./HookManager";
// stop pg from parsing dates and timestamps without timezone
PgTypes.setTypeParser(1114, (str: any) => str);
PgTypes.setTypeParser(1082, (str: any) => str);

export { PgClient };

@Service()
export class DbAppClient implements IDb {
  private applicationNamePrefix: string;
  private applicationName: string;
  private readonly credentials: IDbAppClientConfig;

  private readonly ENVIRONMENT: IEnvironment;
  private readonly logger: ILogger;
  private readonly config: IDbConfig;
  public readonly hookManager: HookManager;
  public readonly databaseName: string;
  public pgClient: PgClient;
github opentrials / api / config / pg_types.js View on Github external
const parser = pgTypes.arrayParser.create(value, (entry) => {
    let parsedEntry = entry;
    if (String(entry).toLowerCase() !== String(null)) {
      parsedEntry = parseDate(entry);
    } else {
      parsedEntry = null;
    }
    return parsedEntry;
  });

  return parser.parse();
}

pgTypes.setTypeParser(DATE_OID, parseDate);
pgTypes.setTypeParser(DATE_ARRAY_OID, parseDateArray);