सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
SurrealDB के लिए एक शुद्ध Dart क्लाइंट, जो स्वचालित पुनर्संबंध और प्रकार-सुरक्षित फ़ंक्शन प्रदान करता है। विभिन्न प्लेटफ़ॉर्मों पर बिना किसी बाधा के एकीकरण के लिए डिज़ाइन किया गया है, जो डेवलपर्स को Flutter ऐप्स में SurrealDB डेटाबेस के साथ कार्य करने की अनुमति देता है। पैकेज निचले स्तर की नेटवर्किंग विवरणों को छिपाता है, जिससे प्रश्नों, सब्सक्रिप्शन और प्रमाणीकरण के लिए साफ़ API प्रदान करता है। इसके प्रकार-सुरक्षित फ़ंक्शन रनटाइम त्रुटियों को कम करते हैं और विकास गति को बढ़ाते हैं। वास्तविक समय के ऐप्स के लिए आदर्श, जिन्हें विश्वसनीय डेटाबेस कनेक्टिविटी की आवश्यकता होती है।
उपयोग
रियल-टाइम डेटा सिंक्रनाइज़ेशन
प्लेटफॉर्म-स्थानीय डेटाबेस पहुँच
प्रकार-सुरक्षित क्वेरी निष्पादन
नेटवर्क खोए के बाद स्वचालित पुनर्संबंधन
सर्वरलेस बैकएंड इंटीग्रेशन
ऑफलाइन-प्रथम एप्लिकेशन विकास
मुख्य विशेषताएँ
कनेक्शन खोने पर स्वचालित पुनः कनेक्ट
टाइप्ड फंक्शन समर्थन
शुद्ध डार्ट कार्यान्वयन
बहुप्लेटफॉर्म संगतता
हल्का और कार्यक्षम
सुरक्षित प्रमाणीकरण का निपटान
इनके लिए उपयुक्त
SurrealDB का उपयोग करने वाले Flutter डेवलपर्स
वास्तविक समय डेटाबेस अपडेट की आवश्यकता वाले ऐप्स
प्लेटफॉर्म-अनुकूल डेटाबेस क्लाइंट की आवश्यकता वाले प्रोजेक्ट
विकासकर्ता प्रकार सुरक्षा को अग्रणी महत्व देते हैं
स्केलेबल बैकएंड सेवाओं के निर्माण करने वाली टीमें
अविश्वसनीय नेटवर्क स्थितियों वाले अनुप्रयोग
विचार योग्य बातें
एक चल रहे SurrealDB उदाहरण की आवश्यकता है
सिर्फ सरीअलडीबी-विशिष्ट विशेषताओं तक सीमित
जटिल प्रश्नों के लिए अतिरिक्त सेटअप की आवश्यकता हो सकती है
बाहरी SurrealDB सर्वर की स्थिरता पर निर्भर करता है
गैर-SurrealDB डेटाबेस के लिए उपयुक्त नहीं
प्रमाणीकरण विन्यास की आवश्यकता है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
SurrealDB Client For Dart & Flutter
This is a Dart client library for interacting with SurrealDB, a highly scalable, distributed, and real-time database. This library enables developers to connect to SurrealDB instances, execute queries, authenticate users, and interact with database resources via WebSocket communication.
Features
⚡ Connect and Disconnect: Establish or close a persistent WebSocket connection to a SurrealDB instance.
🔐 Authentication: Authenticate with a token to manage access to the database.
🗄️ Database Interaction: Use namespaces and databases, retrieve user information, and fetch database versions.
🔄 Live Queries: Support for live querying via WebSocket streams.
⚙️ Customizable Options: Set connection options and behavior using SurrealDBOptions.
Installation
Add the following to your pubspec.yaml:
dependencies:surrealdb:^1.0.0
Then run dart pub get or flutter pub get.
Usage
Connecting to SurrealDB
To connect to a SurrealDB, create a SurrealDB client instance and call the connect() method. You can also provide a token for authentication.
import'package:surrealdb/surrealdb.dart';
voidmain() async {
// Create a SurrealDB client instancefinal db = SurrealDB('ws://localhost:8000/rpc', token: 'your-auth-token');
// Connect to the database
db.connect();
// Wait for the connection to be establishedawait db.wait();
// Use a specific namespace and databaseawait db.use('namespace', 'database');
// Fetch version informationfinal version = await db.version();
print('SurrealDB version: $version');
// Close the connection when done
db.close();
}
Authentication
SurrealDB requires authentication to access resources. You can sign up, sign in, and authenticate users using the SurrealDB client.
Signing Up a User
To create a new user:
final token = await db.signup(
user: 'new_user',
pass: 'password123',
namespace: 'namespace',
database: 'database',
access: 'users',
);
print('User signed up with token: $token');
Signing In
To sign in an existing user:
final token = await db.signin(
user: 'existing_user',
pass: 'password123',
namespace: 'namespace',
database: 'database',
access: 'users',
);
print('User signed in with token: $token');
Token Authentication
You can also authenticate with a token:
await db.authenticate('your-auth-token');
Middleware
SurrealDB Flutter client supports middleware that allows you to intercept and modify requests before they are sent to the database. This is particularly useful for implementing token refresh functionality or adding logging.
Adding Middleware
You can add middleware using the addMiddleware method:
db.addMiddleware((method, params, next) async {
// Do something before the requestprint('Before request: $method with params: $params');
// Execute the requestfinal result = awaitnext();
// Do something after the requestprint('After request: $method with result: $result');
return result;
});
Multiple middleware functions can be added, and they will be executed in the order they were added.
Token Refresh Example
Here's how you can implement token refresh using middleware:
db.addMiddleware((method, params, next) async {
try {
// Try to execute the request normallyreturnawaitnext();
} catch (e) {
// If we get an authentication errorif (e.toString().contains('authentication invalid')) {
// Refresh the token (implement your token refresh logic)final newToken = awaitrefreshToken();
// Re-authenticate with the new tokenawait db.authenticate(newToken);
// Retry the original requestreturnawaitnext();
}
// For other errors, just rethrowrethrow;
}
});
This middleware will catch authentication errors, refresh the token, and retry the original request automatically.
Basic Queries
SurrealDB allows executing a variety of commands, including CRUD operations (Create, Read, Update, Delete) via queries. Here's how you can perform simple database operations.
3.1 Create Records
final data = {'title': 'My first post', 'content': 'Hello, SurrealDB!'};
final result = await db.create('posts', data);
print('Created record: $result');
3.2 Read Records
To retrieve records, you can use the select method.
final posts = await db.select('posts');
print('Fetched posts: $posts');
You can also use queries to filter or manipulate the data:
final specificPosts = await db.query(
r'SELECT * FROM posts WHERE title = $title',
{'title': 'My first post'},
);
print('Posts matching criteria: $specificPosts');
3.3 Update Records
Updating records is done by specifying the table and ID of the record:
final updatedData = {'title': 'Updated post title'};
await db.update('posts:id', updatedData);
print('Record updated successfully');