sync product to local

This commit is contained in:
efrilm
2025-09-20 03:10:05 +07:00
parent 3022d8de9f
commit f104390141
21 changed files with 4461 additions and 514 deletions
+87
View File
@@ -0,0 +1,87 @@
import 'dart:async';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
class DatabaseHelper {
static DatabaseHelper? _instance;
static Database? _database;
DatabaseHelper._internal();
static DatabaseHelper get instance {
_instance ??= DatabaseHelper._internal();
return _instance!;
}
Future<Database> get database async {
_database ??= await _initDatabase();
return _database!;
}
Future<Database> _initDatabase() async {
String path = join(await getDatabasesPath(), 'pos_database.db');
return await openDatabase(
path,
version: 1,
onCreate: _onCreate,
onUpgrade: _onUpgrade,
);
}
Future<void> _onCreate(Database db, int version) async {
// Products table
await db.execute('''
CREATE TABLE products (
id TEXT PRIMARY KEY,
organization_id TEXT,
category_id TEXT,
sku TEXT,
name TEXT,
description TEXT,
price INTEGER,
cost INTEGER,
business_type TEXT,
image_url TEXT,
printer_type TEXT,
metadata TEXT,
is_active INTEGER,
created_at TEXT,
updated_at TEXT
)
''');
// Product Variants table
await db.execute('''
CREATE TABLE product_variants (
id TEXT PRIMARY KEY,
product_id TEXT,
name TEXT,
price_modifier INTEGER,
cost INTEGER,
metadata TEXT,
created_at TEXT,
updated_at TEXT,
FOREIGN KEY (product_id) REFERENCES products (id) ON DELETE CASCADE
)
''');
// Create indexes for better performance
await db.execute(
'CREATE INDEX idx_products_category_id ON products(category_id)');
await db.execute('CREATE INDEX idx_products_name ON products(name)');
await db.execute('CREATE INDEX idx_products_sku ON products(sku)');
await db.execute(
'CREATE INDEX idx_products_description ON products(description)');
}
Future<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
// Handle database upgrades here
}
Future<void> close() async {
final db = await database;
await db.close();
_database = null;
}
}
+29
View File
@@ -0,0 +1,29 @@
import 'package:sqflite/sqflite.dart';
class DatabaseMigrationHandler {
static Future<void> migrate(
Database db, int oldVersion, int newVersion) async {
if (oldVersion < 2) {
// Add indexes for better performance
await db.execute(
'CREATE INDEX IF NOT EXISTS idx_products_name_search ON products(name)');
await db.execute(
'CREATE INDEX IF NOT EXISTS idx_products_sku_search ON products(sku)');
}
if (oldVersion < 3) {
// Add full text search support
await db.execute(
'CREATE VIRTUAL TABLE products_fts USING fts5(name, sku, description, content=products, content_rowid=rowid)');
await db.execute(
'INSERT INTO products_fts SELECT name, sku, description FROM products');
}
if (oldVersion < 4) {
// Add sync tracking
await db.execute('ALTER TABLE products ADD COLUMN last_sync_at TEXT');
await db.execute(
'ALTER TABLE products ADD COLUMN sync_version INTEGER DEFAULT 1');
}
}
}