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

pdo

v1.1.1

Published

PHP's PDO for node.js

Downloads

24

Readme

PHP + PDO + NodeJS

A bridge for using PHP's PDO within NodeJS

PHP's PDO has a wide range of database driver support. This module brings PDO's functionality to node.

Requires NodeJS >= 4.9.1

This module uses PHP under the hood, so requires PHP (>= 5.0.0) to be installed on the local system.

Your php.ini must be configured to enable the PDO drivers you require.

No other dependencies.

Feature requests & bug reports are welcome.

Install

npm i pdo

Example Usage

const PDO = require('pdo');

const db = new PDO()
await db.open(dsn);

await db.query(`
    INSERT INTO users
    SET ?
`, [
    {
        username: "admin",
        password: "hunter2"
    }
]);

await db.query(`
    SELECT *
    FROM users
    WHERE username = ?
    OR id IN (?)
`, [
    "admin",
    [1, 2, 3]
]);

await db.close();

A note on parameterised queries

As with all SQL interfaces, special care should be taken to ensure that all user inputs are correctly sanitised before being included in an SQL query. PHP's PDO supports parameterised queries to facilitate this, using question marks or colon-prefixed labels as placeholders for query parameters.

This module only supports question mark placeholders at this time, but (unlike PHP's PDO) can also expand objects and arrays as per the example above.

API

Methods

new PDO( options : object )

Available options:

  • expandPlaceholders : bool - Should queries expand array and object paramters into multiple placeholders? (default: true)
  • phpPath : string - Path to the PHP binary. (default: 'php')
  • closeCursorAfterExec : bool - Automatically call closeCursor() on each statement after execution. Note: Some drivers require this. (default: false)
  • stringifyFetches : bool - Automatically convert all returned values into strings. Note: Some drivers will perform this conversion regardless of this setting. (default: false)
  • emulatePrepares : bool - Should PDO prefer to emulate prepared statements? Note: PDO will always emulate prepared statements when not natively supported by your driver. (default: false)
  • timeoutSeconds : int - A query timeout in seconds. Use 0 to disable. (default: 0)

PDO::open( dsn : string ) : Promise

Open a PDO connection.

See http://uk1.php.net/manual/en/pdo.construct.php for information about DSNs.

Returns a Promise resolved when the connection is opened.
Promise will reject on error.

PDO::exec( sql : string [, params : Array] ) : Promise

Executes a query.
Returns a Promise which resolves when execution is complete.
Promise will reject on error.

PDO::query( sql : string [, params : Array] ) : Promise

PDO::queryAll( sql : string [, params : Array] ) : Promise

PDO::queryOne( sql : string [, params : Array] ) : Promise

PDO::queryColumn( sql : string [, params : Array [, columnIndex = 0 : integer ] ] ) : Promise

Execute a query and return a result.
Returns a Promise which resolves to the rows returned.

PDO::query() and PDO::queryAll() return all rows.
PDO::queryOne() returns a single row.
PDO::queryColumn() returns a single field from a single row.

Promise will reject on error.

PDO::close( ) : Promise

You should always close your PDO connection when no longer needed. This will allow the underlying PHP process to close, freeing up resources.

PDO::on( event : string, handler : function )

PDO is an event emitter. See below for list of events.

Events

error - fired when an error occurs with the child PHP process.