Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for embedded objects #565

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example/lib/database.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'dart:async';

import 'package:example/date_time_converter.dart';
import 'package:example/task.dart';
import 'package:example/task_dao.dart';
import 'package:example/timestamp.dart';
import 'package:floor/floor.dart';
import 'package:sqflite/sqflite.dart' as sqflite;

part 'database.g.dart';

@Database(version: 1, entities: [Task])
@TypeConverters([DateTimeConverter])
@Database(version: 1, entities: [Task], embeds: [Timestamp])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to include the embeds here? doesn't it suffice to annotate the usage in the entities/views?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to include if we annotate the embedded class, if we annotate the variable like room then we don't need to include here.

abstract class FlutterDatabase extends FloorDatabase {
TaskDao get taskDao;
}
65 changes: 52 additions & 13 deletions example/lib/database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions example/lib/date_time_converter.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:floor/floor.dart';

class DateTimeConverter extends TypeConverter<DateTime, int> {
@override
DateTime decode(int databaseValue) => DateTime.fromMillisecondsSinceEpoch(databaseValue);

@override
int encode(DateTime value) => value.millisecondsSinceEpoch;
}
8 changes: 7 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:example/database.dart';
import 'package:example/task.dart';
import 'package:example/task_dao.dart';
import 'package:example/timestamp.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
Expand Down Expand Up @@ -177,7 +178,12 @@ class TasksTextField extends StatelessWidget {
if (message.trim().isEmpty) {
_textEditingController.clear();
} else {
final task = Task(null, message);
final task = Task(null, message,
Timestamp(
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
),
);
await dao.insertTask(task);
_textEditingController.clear();
}
Expand Down
6 changes: 5 additions & 1 deletion example/lib/task.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:example/timestamp.dart';
import 'package:floor/floor.dart';

@entity
Expand All @@ -7,7 +8,10 @@ class Task {

final String message;

Task(this.id, this.message);
// @ColumnInfo(name: '')
final Timestamp timestamp;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally in favor of having @embedded here, like in room to be able to more finely adjust where to embed and where to type convert... what are your opinions here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello sir!

I don't have a background in Android/Java so I was not aware of the room approach.

I think that annotating the class as an embedded type is easier as we need to annotate it just once, instead of annotating every time we declare it inside an entity class. For finely adjusting, as we have just the prefix string, I thought we could use the ColumnInfo name as we already do for other variables.

If you prefer the room approach I can modify it, just tell me :)


Task(this.id, this.message, this.timestamp);

@override
bool operator ==(Object other) =>
Expand Down
28 changes: 28 additions & 0 deletions example/lib/timestamp.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:floor/floor.dart';

@Embed()
class Timestamp {
@ColumnInfo(name: 'created_at')
final DateTime createdAt;

@ColumnInfo(name: 'updated_at')
final DateTime updatedAt;

Timestamp({required this.createdAt, required this.updatedAt});

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is Timestamp &&
runtimeType == other.runtimeType &&
createdAt == other.createdAt &&
updatedAt == other.updatedAt;

@override
int get hashCode => createdAt.hashCode ^ updatedAt.hashCode;

@override
String toString() {
return 'Timestamp{createdAt: $createdAt, updatedAt: $updatedAt}';
}
}
31 changes: 19 additions & 12 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -70,7 +70,7 @@ packages:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
version: "2.0.3"
build_runner_core:
dependency: transitive
description:
Expand All @@ -91,7 +91,7 @@ packages:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "8.0.5"
version: "8.0.6"
characters:
dependency: transitive
description:
Expand Down Expand Up @@ -161,7 +161,7 @@ packages:
name: dart_style
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.0.1"
fake_async:
dependency: transitive
description:
Expand All @@ -182,7 +182,7 @@ packages:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.0"
version: "6.1.1"
fixnum:
dependency: transitive
description:
Expand All @@ -196,7 +196,7 @@ packages:
path: "../floor"
relative: true
source: path
version: "1.0.1"
version: "1.1.0"
floor_annotation:
dependency: transitive
description:
Expand All @@ -210,7 +210,7 @@ packages:
path: "../floor_generator"
relative: true
source: path
version: "1.0.1"
version: "1.1.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -221,6 +221,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
frontend_server_client:
dependency: transitive
description:
name: frontend_server_client
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
glob:
dependency: transitive
description:
Expand Down Expand Up @@ -353,7 +360,7 @@ packages:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.4"
shelf_web_socket:
dependency: transitive
description:
Expand All @@ -379,7 +386,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
sqflite:
dependency: transitive
description:
Expand Down Expand Up @@ -407,7 +414,7 @@ packages:
name: sqlite3
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
version: "1.1.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -463,7 +470,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
timing:
dependency: transitive
description:
Expand Down Expand Up @@ -505,7 +512,7 @@ packages:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
version: "2.1.0"
yaml:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
sdk: flutter

dev_dependencies:
build_runner: ^2.0.0
build_runner: ^2.0.3
floor_generator:
path: ../floor_generator/
flutter_test:
Expand Down
1 change: 1 addition & 0 deletions floor_annotation/lib/floor_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export 'src/dao.dart';
export 'src/database.dart';
export 'src/database_view.dart';
export 'src/delete.dart';
export 'src/embed.dart';
export 'src/entity.dart';
export 'src/foreign_key.dart';
export 'src/fts.dart';
Expand Down
4 changes: 4 additions & 0 deletions floor_annotation/lib/src/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ class Database {
/// The entities the database manages.
final List<Type> entities;

/// The embeds the database manages.
final List<Type> embeds;

/// The views the database manages.
final List<Type> views;

/// Marks a class as a FloorDatabase.
const Database({
required this.version,
required this.entities,
this.embeds = const [],
this.views = const [],
});
}
5 changes: 5 additions & 0 deletions floor_annotation/lib/src/embed.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Embed {
const Embed();
}

const embed = Embed();
1 change: 1 addition & 0 deletions floor_generator/lib/misc/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abstract class AnnotationField {
static const onConflict = 'onConflict';

static const databaseVersion = 'version';
static const databaseEmbeds = 'embeds';
static const databaseEntities = 'entities';
static const databaseViews = 'views';

Expand Down
Loading