112 lines
2.4 KiB
Dart
112 lines
2.4 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:drift_flutter/drift_flutter.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
part 'app_database.g.dart';
|
|
|
|
@DataClassName('ProductRow')
|
|
class Products extends Table {
|
|
TextColumn get id => text()();
|
|
|
|
TextColumn get name => text()();
|
|
|
|
TextColumn get category => text()();
|
|
|
|
IntColumn get stockQuantity => integer()();
|
|
|
|
IntColumn get lowStockThreshold => integer()();
|
|
|
|
IntColumn get priceInCents => integer()();
|
|
|
|
TextColumn get imagePath => text().nullable()();
|
|
|
|
BoolColumn get active => boolean().withDefault(const Constant(true))();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {id};
|
|
}
|
|
|
|
@DataClassName('BarTabRow')
|
|
class BarTabs extends Table {
|
|
TextColumn get id => text()();
|
|
|
|
TextColumn get customerName => text()();
|
|
|
|
TextColumn get status => text().withDefault(const Constant('open'))();
|
|
|
|
DateTimeColumn get openedAt => dateTime()();
|
|
|
|
DateTimeColumn get closedAt => dateTime().nullable()();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {id};
|
|
}
|
|
|
|
@DataClassName('TabItemRow')
|
|
class TabItems extends Table {
|
|
TextColumn get id => text()();
|
|
|
|
TextColumn get tabId => text().references(
|
|
BarTabs,
|
|
#id,
|
|
onDelete: KeyAction.cascade,
|
|
)();
|
|
|
|
TextColumn get productId => text().references(
|
|
Products,
|
|
#id,
|
|
)();
|
|
|
|
TextColumn get productName => text()();
|
|
|
|
IntColumn get quantity => integer()();
|
|
|
|
IntColumn get unitPriceInCents => integer()();
|
|
|
|
DateTimeColumn get createdAt => dateTime()();
|
|
|
|
@override
|
|
Set<Column> get primaryKey => {id};
|
|
}
|
|
|
|
@DriftDatabase(
|
|
tables: [
|
|
Products,
|
|
BarTabs,
|
|
TabItems,
|
|
],
|
|
)
|
|
class AppDatabase extends _$AppDatabase {
|
|
AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());
|
|
|
|
@override
|
|
int get schemaVersion => 3;
|
|
|
|
@override
|
|
MigrationStrategy get migration {
|
|
return MigrationStrategy(
|
|
onCreate: (migrator) async {
|
|
await migrator.createAll();
|
|
},
|
|
onUpgrade: (migrator, from, to) async {
|
|
if (from < 2) {
|
|
await migrator.createTable(barTabs);
|
|
await migrator.createTable(tabItems);
|
|
}
|
|
|
|
if (from < 3) {
|
|
await migrator.addColumn(products, products.imagePath);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
static QueryExecutor _openConnection() {
|
|
return driftDatabase(
|
|
name: 'kooltab',
|
|
native: const DriftNativeOptions(
|
|
databaseDirectory: getApplicationSupportDirectory,
|
|
),
|
|
);
|
|
}
|
|
} |