सार्वजनिक जानकारी से बना चयन मार्गदर्शन। केवल संदर्भ हेतु।
Sqlcool एक Flutter/Dart पैकेज है जो डेटाबेस बदलाव के लिए रिएक्टिव स्ट्रीम, बिल्ट-इन सेलेक्ट ब्लॉक पैटर्न और कस्टम डेटाबेस मॉडल के समर्थन के साथ SQLite डेटाबेस के साथ काम करने को सरल बनाता है। यह न्यूनतम बॉयलरप्लेट के साथ वास्तविक समय में डेटा सिंक्रनाइजेशन और कुशल प्रश्न प्राप्त करने में सक्षम है। फ्लटर ऐप में स्थायी स्थानीय डेटा संग्रहण के लिए हल्के, स्पष्ट समाधान की तलाश करने वाले डेवलपर्स के लिए डिज़ाइन किया गया है, Sqlcool प्रतिक्रियाशील प्रोग्रामिंग पैराडाइम में बिना किसी दिक्कत के एकीकृत होता है। पैकेज जटिल डेटाबेस ऑपरेशन के लिए लचीलापन बनाए रखते हुए उपयोग में आसानी पर जोर देता है।
उपयोग
ऑफलाइन-प्रथम ऐप्स में रियल-टाइम डेटा सिंक
डेटाबेस बदलावों के आधार पर प्रतिक्रियाशील UI अपडेट
संरचित डेटाबेस मॉडल के साथ स्थानीय राज्य का प्रबंधन
गतिशील डेटा लोडिंग के साथ फॉर्म बनाना
SQLite के साथ कैशिंग परतों का अनुप्रयोग
कस्टम मॉडल के साथ CRUD ऑपरेशन को सरल बनाना
मुख्य विशेषताएँ
डेटाबेस बदलाव सूचनाओं के लिए प्रतिक्रियाशील स्ट्रीम्स
राज्य प्रबंधन के लिए निर्मित चयन ब्लॉक पैटर्न
कस्टम डेटाबेस मॉडल परिभाषाओं का समर्थन
प्रश्नों और परिवर्तन के लिए सरल API
स्वचालित प्रकार-सुरक्षित मॉडल मैपिंग
हल्का और न्यूनतम निर्भरता
इनके लिए उपयुक्त
ऑफलाइन-क्षम फ्लटर ऐप्स बनाने वाले डेवलपर्स
प्रतिक्रियाशील अवस्था प्रबंधन का उपयोग करने वाली टीमें
वास्तविक समय डेटाबेस अपडेट की आवश्यकता वाले प्रोजेक्ट
संरचित स्थानीय डेटा स्थायित्व की आवश्यकता वाले ऐप्स
फ्लटर में SQLite सीखने वाले शुरुआती लोग
अक्सर स्थानीय डेटा इंटरैक्शन वाले एप्स
विचार योग्य बातें
केवल SQLite तक सीमित (अन्य डेटाबेस नहीं)
निर्मित अभियान प्रबंधन नहीं (हस्तकला या बाहरी उपकरण की आवश्यकता है)
जटिल स्कीमा बदलाव के लिए अतिरिक्त सेटअप की आवश्यकता हो सकती है
बड़े पैमाने पर सर्वर-साइड डेटाबेस संचालन के लिए आदर्श नहीं है
मोबाइल और डेस्कटॉप उपयोग के मामलों के लिए सबसे उपयुक्त
प्रतिक्रियाशील प्रोग्रामिंग अवधारणाओं को समझने की आवश्यकता है
मूल README
संबंधित सोर्स रिपॉज़िटरी के मूल README का स्नैपशॉट। नवीनतम सामग्री के लिए रिपॉज़िटरी देखें।
Sqlcool
A database helper library for Sqflite. Forget about implementation details and focus on the business logic.
A stream of database change events is available. Inspired by Rethinkdb
import'dart:async';
import'package:sqlcool/sqlcool.dart';
StreamSubscription changefeed;
changefeed = db.changefeed.listen((change) {
print("Change in the database:");
print("Query: ${change.query}");
if (change.type == DatabaseChange.update) {
print("${change.value} items updated");
}
});
// Dispose the changefeed when finished using it
changefeed.cancel();
Reactive select bloc
The bloc will rebuild itself on any database change because of the reactive
parameter set to true:
import'package:flutter/material.dart';
import'package:sqlcool/sqlcool.dart';
class_PageSelectBlocStateextendsState<PageSelectBloc> {
SelectBloc bloc;
@overridevoidinitState() {
super.initState();
this.bloc = SelectBloc(
table: "items", orderBy: "name", reactive: true);
}
@overridevoiddispose() {
bloc.dispose();
super.dispose();
}
@overrideWidgetbuild(BuildContext context) {
returnScaffold(
appBar: AppBar(title: Text("My app")),
body: StreamBuilder<List<Map>>(
stream: bloc.items,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
// the select query has not found anythingif (snapshot.data.length == 0) {
returnCenter(child: constText("No data"));
}
// the select query has resultsreturnListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
var item = snapshot.data[index];
returnListTile(
title: GestureDetector(
child: Text(item["name"]),
onTap: () => someFunction()),
);
});
} else {
// the select query is still runningreturnCircularProgressIndicator();
}
}),
);
}
}
classPageSelectBlocextendsStatefulWidget{
@override_PageSelectBlocStatecreateState() => _PageSelectBlocState();
}
Database models
New in 4.0.0: define models that have database methods. The main
advantage of this is to use only typed model data and avoid the
type conversions from maps for every query. It directly plugs custom
models into the database. Example:
In schema.dart:
final carTable = DbTable("car")
..varchar("name")
..integer("max_speed")
..real("price")
..integer("year")
..boolean("is_4wd", defaultValue: false)
..foreignKey("manufacturer", onDelete: OnDelete.cascade);
final manufacturerTable = DbTable("manufacturer")..varchar("name");
In car_model.dart:
import'package:sqlcool/sqlcool.dart';
// the database schemaimport'schema.dart';
// another modelimport'manufacturer_model.dart';
classCarwithDbModel{
Car(
{this.id,
this.name,
this.maxSpeed,
this.price,
this.year,
this.is4wd,
this.manufacturer});
/// define some class propertiesfinalString name;
finalint maxSpeed;
finaldouble price;
finalDateTime year;
finalbool is4wd;
// this is a foreign key to another modelManufacturer manufacturer;
/// [DbModel] required overrides@overrideint id;
/// the [Db] used/// pass it your main db@overrideDbget db => db;
/// the table schema representation/// check example/pages/dbmodels/schema.dart@overrideDbTableget table => carTable;
/// serialize a row to the database@overrideMap<String, dynamic> toDb() {
// we want the foreign key to be recordedassert(manufacturer?.id != null);
final row = <String, dynamic>{
"name": name,
"max_speed": maxSpeed,
"price": price,
"year": year.millisecondsSinceEpoch,
"is_4wd": is4wd,
"manufacturer": manufacturer.id
};
return row;
}
/// deserialize a row from database@overrideCarfromDb(Map<String, dynamic> map) {
final car = Car(
id: map["id"] asint,
name: map["name"].toString(),
maxSpeed: map["max_speed"] asint,
price: map["price"] asdouble,
year: DateTime.fromMillisecondsSinceEpoch(map["year"] asint),
is4wd: (map["is_4wd"].toString() == "true"),
);
// the key will be present only with join queries// in a simple select this data is not presentif (map.containsKey("manufacturer")) {
car.manufacturer =
Manufacturer().fromDb(map["manufacturer"] asMap<String, dynamic>);
}
return car;
}
/// Create a static join method for conveniencestaticFuture<List<Car>> selectRelated({String where, int limit}) async {
final cars = List<Car>.from(
awaitCar().sqlJoin(where: where, limit: limit, verbose: true));
return cars;
}
}
Then use the models:
/// car is an instance of [Car]await car.sqlInsert();
await car.sqlUpdate();
await car.sqlUpsert();
await car.sqlDelete();
final cars = Car.selectRelated(where: "speed>200");
// foreign keys are retrieved as model instancesprint(cars[0].manufacturer.name);