An open API service indexing awesome lists of open source software.

https://github.com/damir-sijakovic/jsondb

Simple file key/value database utility function.
https://github.com/damir-sijakovic/jsondb

json json-database php

Last synced: about 2 months ago
JSON representation

Simple file key/value database utility function.

Awesome Lists containing this project

README

          

# JsonDb

## Overview

The `JsonDb` class provides a simple interface for storing and retrieving data in a JSON file, acting as a lightweight database. It allows for basic CRUD (Create, Read, Update, Delete) operations on the JSON data.

## Usage

### Initialization

To use the `JsonDb` class, first include it in your PHP script and instantiate it with the path to the JSON file you want to use as your database.

```php
require_once 'JsonDb.php';
$db = new JsonDb("path/to/yourfile.json");
```

### Methods

#### `__construct($fileName)`

- **Description**: Constructor method; initializes the JsonDb instance with the specified file name.
- **Parameters**:
- `$fileName`: Path to the JSON file to be used as a database.
- **Returns**: An instance of the JsonDb class.

#### `get($key = null)`

- **Description**: Retrieves the value associated with a specific key from the JSON file. If no key is provided, it returns the entire data array.
- **Parameters**:
- `$key`: (Optional) The key of the value to retrieve.
- **Returns**: The value associated with the specified key, or the entire data array if no key is provided. Returns `null` if the file does not exist or the key is not found.

#### `set($key, $value)`

- **Description**: Sets the value for a specific key in the JSON file. If the key already exists, the value is updated. If the key does not exist, it is created.
- **Parameters**:
- `$key`: The key to set or update in the file.
- `$value`: The value to associate with the key.
- **Returns**: `true` on success, or `false` on failure.

#### `delete($key)`

- **Description**: Deletes a key-value pair from the JSON file.
- **Parameters**:
- `$key`: The key of the value to delete.
- **Returns**: `true` if the key was successfully deleted, or `false` if the key does not exist or the file could not be updated.

## Private Methods

These methods are used internally by the class and are not intended to be used directly.

#### `getFileContents()`

- **Description**: Retrieves the contents of the JSON file.
- **Returns**: The contents of the file as a string, or `null` if the file does not exist.

#### `decodeJson($jsonData)`

- **Description**: Decodes a JSON string into an associative array.
- **Parameters**:
- `$jsonData`: The JSON string to decode.
- **Returns**: The decoded JSON as an associative array, or `null` on error.

## Examples

### Reading Data

```php
// Get all data
$data = $db->get();
print_r($data);

// Get data by key
$value = $db->get('someKey');
echo "Value: $value\n";
```

### Writing Data

```php
// Set a new value
if ($db->set('newKey', 'newValue')) {
echo "Data saved successfully.\n";
}
```

### Deleting Data

```php
// Delete a key-value pair
if ($db->delete('keyToRemove')) {
echo "Key removed successfully.\n";
}
```