Installation:
composer require dovstone/mynosql
Add to config/app.php under providers:
DovStone\MyNoSQL\MyNoSQLServiceProvider::class,
Publish config (if available) with:
php artisan vendor:publish --provider="DovStone\MyNoSQL\MyNoSQLServiceProvider"
First Use Case:
Inject the HostConnection via Laravel's DI container:
use DovStone\MyNoSQL\HostConnection;
class MyController extends Controller {
public function __construct(HostConnection $db) {
$this->db = $db;
}
}
Or fetch via facade (if provided):
$db = \MyNoSQL::connection('mysql:host=localhost;dbname=mynosql', 'root', '');
Basic CRUD:
// Insert
$doc = $this->db->collection('users')->insert(['name' => 'John', 'email' => 'john@example.com']);
// Find
$user = $this->db->collection('users')->find($doc->id)->fetch();
// Update
$this->db->collection('users')->update($doc->id, ['name' => 'John Doe']);
// Delete
$this->db->collection('users')->delete($doc->id);
Service Provider Binding:
Bind the HostConnection to the container in AppServiceProvider:
$this->app->singleton('mynosql', function ($app) {
return new HostConnection(
config('mynosql.connection'),
config('mynosql.username'),
config('mynosql.password')
);
});
Eloquent-like Usage: Create a base model for collections:
class UserModel {
protected $collection = 'users';
protected $db;
public function __construct(HostConnection $db) {
$this->db = $db;
}
public function find($id) {
return $this->db->collection($this->collection)->find($id)->fetch();
}
public function all() {
return $this->db->collection($this->collection)->findAll()->fetch();
}
}
Query Builder Chaining: Leverage chainable methods for fluent queries:
$activeUsers = $this->db->collection('users')
->findBy(['active' => true])
->orderBy(['name' => 'asc'])
->limit(10)
->fetch();
Repository Pattern: Encapsulate logic in repositories:
class UserRepository {
public function __construct(HostConnection $db) {
$this->db = $db;
}
public function getActiveUsersWithLimit(int $limit) {
return $this->db->collection('users')
->findBy(['active' => true])
->limit($limit)
->fetch();
}
}
Transactions: Wrap operations in transactions (if supported):
DB::transaction(function () use ($db) {
$db->collection('users')->insert($userData);
$db->collection('logs')->insert($logData);
});
No Native Laravel Eloquent:
hasMany or belongsTo out of the box.Schema Limitations:
$db->collection('users')->insert([
'name' => 'Alice',
'address' => json_encode(['city' => 'NY', 'zip' => '10001'])
]);
Performance Quirks:
findAllBy and findBy with large datasets may be slow. Use limit/offset for pagination.findAll on large collections; use criteria-based queries instead.Error Handling:
try {
$user = $this->db->collection('users')->find($id)->fetch();
} catch (\Exception $e) {
Log::error("MyNoSQL Error: " . $e->getMessage());
abort(500);
}
Connection Management:
HostConnection instances. Creating new instances per request may cause overhead.Query Logging:
SET GLOBAL general_log = 'ON';
SET GLOBAL general_log_file = '/tmp/mynosql.log';
MyNoSQL translates NoSQL operations to SQL.Fetch Behavior:
fetch() returns raw arrays. Ensure data types match expectations (e.g., JSON-decoded fields).Criteria Syntax:
['status' => 'active']). For partial matches, use LIKE or full-text search manually:
$this->db->collection('users')
->findBy(['name' => ['like', '%ohn%']]) // Hypothetical; may need custom logic
->fetch();
Custom Query Methods:
HostConnection to add domain-specific queries:
class ExtendedHostConnection extends HostConnection {
public function findUsersByRole(string $role) {
return $this->collection('users')
->findBy(['role' => $role])
->fetch();
}
}
Event Listeners:
Event::listen('eloquent.saved', function ($model) {
// Sync with MyNoSQL if using hybrid storage
});
Middleware:
$db->collection('users')->insert($data)->then(function ($result) {
// Post-process result
});
Hybrid Storage:
// Eloquent for users, MyNoSQL for user preferences
$user = User::find(1);
$prefs = $this->db->collection('user_prefs')->find($user->id)->fetch();
How can I help you explore Laravel packages today?