定制 hyvr/ray-db 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hyvr/ray-db

最新稳定版本:1.0.4

Composer 安装命令:

composer require hyvr/ray-db

包简介

README 文档

README

Fishline logo RayDB is a lightweight NoSQL database built in PHP that relies solely on plain JSON files, with no third-party dependencies. Its simple file-based design makes it ideal for static websites or projects that need an easy, manageable way to store content. It’s intended for handling a few gigabytes of data under low to medium workloads, making it suitable for low-traffic environments where a full traditional database would be unnecessary.

⚡ Get started

RayDB can be installed from the composer package manager. Make sure you installed PHP and Composer from the requirments section before continuing. Run the below command to install the RayDB.

composer require hyvr/ray-db

Example usage is given below. More usage examples are details given in the below section.

<?php
  require 'vendor/autoload.php';

  use Hyvr\RayDB\Ray;

  $ray = new Ray(__DIR__.'/buckets');

  $bucket = $ray->bucket('fruits');

  $bucket->insert([
    'name' => 'apple',
    'color' => 'red',
    'size' => ['small', 'medium'],
    'orgin' => [
      'name' => 'India',
      'code' => 'IN'
    ]
  ]);

  $fruits = $bucket->query()->sortBy('name')->get();
?>

📂 Concept

RayDB saves all the data in JSOn files. These can be created or edited manually. All database have a folder where these JSON files will be saved. Here is the core concept of RayDB explained below.

  • Buckets are like folders inside the database where similar JSON documents are saved.
  • Document is a JSON file where the actual data is saved. Each document will have an unique _id. _id of the document and the file name should be same. You can give an _id during insert, if not that system will give a random unique string.

🎩 Requirments

The RayDB has a few system requirements. You should ensure that your local system has the following minimum PHP version and extensions:

  • PHP >= 8.0.0
  • Composer >= 1.0.0

🚀 Usage

Examples on how to use RayDB is given in grouped sections below.

Initializing
<?php
  require 'vendor/autoload.php';

  use Hyvr\RayDB\Ray;

  // Initializing database 
  $ray = new Ray(__DIR__.'/buckets');

  // Initializing database with config 
  $ray = new Ray(__DIR__.'/buckets', [
    'folder_permission' => 0777,
    'pretty' => true
  ]);

  // Initializing database also create the database folder
  $ray = new Ray(__DIR__.'/buckets', [], true);
?>
Insert
<?php
  require 'vendor/autoload.php';

  use Hyvr\RayDB\Ray;

  $ray = new Ray(__DIR__.'/buckets');

  // Create a bucket called 'fruits' and insert a document
  $bucket = $ray->bucket('fruits');

  $bucket->insert([
    'name' => 'apple',
    'color' => 'red',
    'size' => ['small', 'medium'],
    'orgin' => [
      'name' => 'India',
      'code' => 'IN'
    ]
  ]);

  // Insert a document with id
  $bucket->insert([
    '_id' => 'apple',
    'name' => 'apple',
    'color' => 'red'
  ]);

  // Bulk insert
  $bucket->insert([
    [
      'name' => 'apple',
      'color' => 'red'
    ],[
      'name' => 'orange',
      'color' => 'orange'
    ]
  ]);

  // Another method to insert with object
  $apple = $ray->bucket('fruits')->new('apple');
  $apple->name = 'apple';
  $apple->color = 'red';
  $apple->save();
?>
Query
<?php
  require 'vendor/autoload.php';

  use Hyvr\RayDB\Ray;

  $ray = new Ray(__DIR__.'/buckets');

  $bucket = $ray->bucket('fruits');

  // Get a document with id
  $apple = $bucket->find('apple');

  echo $apple->name;
  echo $apple->color;

  // Query all the documents in the bucket
  $fruits = $bucket->query()->get();

  foreach($fruits as $fruit){
    echo $fruit->name;
    echo $fruit->color;
  }

  // Query using condition
  $fruits = $bucket->query()->where('color', 'red')->get();
  
  $fruits = $bucket->query()->where('size', '>=', 100)->get();

  foreach($fruits as $fruit){
    echo $fruit->name;
    echo $fruit->color;
  }

  // Query using condition and get first
  $fruit = $bucket->query()->where('color', 'red')->first();

  echo $fruit->name;
  echo $fruit->color;
?>
Update
<?php
  require 'vendor/autoload.php';

  use Hyvr\RayDB\Ray;

  $ray = new Ray(__DIR__.'/buckets');

  $bucket = $ray->bucket('fruits');

  // Find a document with id and update it
  $apple = $ray->bucket('fruits')->find('apple');
  $apple->color = 'red';
  $apple->save();

  // Bulk update with id
  $bucket->update([
    [
      '_id' => 'apple',
      'name' => 'apple',
      'color' => 'red'
    ],[
      '_id' => 'orange',
      'name' => 'orange',
      'color' => 'orange'
    ]
  ]);
?>
Upsert
<?php
  require 'vendor/autoload.php';

  use Hyvr\RayDB\Ray;

  $ray = new Ray(__DIR__.'/buckets');

  $bucket = $ray->bucket('fruits');

  // Upsert using id
  $apple = $ray->bucket('fruits')->newOrUpdate('apple');
  $apple->color = 'red';
  $apple->save();

  // Bulk upsert with id
  $bucket->updateOrInsert([
    [
      '_id' => 'apple',
      'name' => 'apple',
      'color' => 'red'
    ],[
      '_id' => 'orange',
      'name' => 'orange',
      'color' => 'orange'
    ]
  ]);
?>
Sort
<?php
  require 'vendor/autoload.php';

  use Hyvr\RayDB\Ray;

  $ray = new Ray(__DIR__.'/buckets');

  $bucket = $ray->bucket('fruits');

  // Sort by ascending
  $fruits = $bucket->query()->sort('color')->get();

  // Sort by descending
  $fruits = $bucket->query()->sort('color', 'desc')->get();

  // Sort by ascending with condition
  $fruits = $bucket->query()->where('color', 'red')->sort('color')->get();
?>

📃 License

RayDB is licensed under the MIT License.

统计信息

  • 总下载量: 14
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固