phly/sqlite-resource

Library for persisting JSON resources in SQLite

dev-master 2013-05-29 21:51 UTC

This package is auto-updated.

Last update: 2024-12-11 15:09:49 UTC


README

This is a "toy" library designed to make it easy to prototype persistence for RESTful APIs. It essentially allows you to create a SQLite database with the following columns:

  • id, which represents an identifier. This is autogenerated by the library, and will be a 32 character hexidecimal string (i.e. characters 0-9 and a-f).
  • data, a JSON-encoded string representing a resource. This will always be returned as an associative array.

Installation

The easiest way to install is via Composer. Download the composer installer, and create a composer.json file as follows:

{
    "minimum-stability": "dev",
    "require": {
        "phly/sqlite-resource": "dev-master"
    }
}

Then run composer install.

Schema

A schema is available in schema.sqlite.sql. Essentially:

CREATE TABLE IF NOT EXISTS collection (
    id CHARACTER(32) PRIMARY KEY,
    data BLOB NOT NULL
);

Alter it as you see fit to change the table name.

Initialization

In order to instantiate Phly\SqliteResource\SqliteResource, you will need an instance of PDO. Typical instantiation will look like this:

use PDO;
use Phly\SqliteResource\SqliteResource;

$pdo = new PDO('sqlite:/path/to/database.db');
$resource = new SqliteResource($pdo, 'table-name');

API

The API supports the following operations:

  • create(array $data) - Inserts the resource into the database and returns it, with a new "id" key with the newly created identifier.
  • fetch($id) - Fetches the resource with the given identifier. If no resource is found, an exception is thrown.
  • fetchAll($limit = null, $offset = null) - Fetches all resources from the database, returning an array of arrays. If a limit is given, returns only the number of resources indicated; if an offset is given along with a limit, returns resources from that offset
  • patch($id, array $data) - Merges the provided $data with the record that already exists in the database identified by $id and saves it. The merged resource is returned.
  • update($id, array $data) - Replaces the resource identified by $id with the $data. Returns the resource.
  • delete($id) - Deletes the resource identified by $id.

Caveats

Do not use this in production. Use a document database, or a relational database with a real schema that maps to the fields in your resources.

LICENSE

This module is licensed using the BSD 2-Clause License:

Copyright (c) 2013, Matthew Weier O'Phinney
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.