From 9e3e88abe6f6f9410f37a542ec6e75c4d1becd3d Mon Sep 17 00:00:00 2001 From: debuggerx01 Date: Thu, 9 Dec 2021 14:50:23 +0800 Subject: [PATCH] feat: createdAt + updatedAt should default to CURRENT_TIMESTAMP --- .../orm/angel_orm/lib/src/query_values.dart | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/orm/angel_orm/lib/src/query_values.dart b/packages/orm/angel_orm/lib/src/query_values.dart index dca69bef..2b265cf4 100644 --- a/packages/orm/angel_orm/lib/src/query_values.dart +++ b/packages/orm/angel_orm/lib/src/query_values.dart @@ -16,6 +16,19 @@ abstract class QueryValues { String compileInsert(Query query, String tableName) { var data = Map.from(toMap()); + var now = DateTime.now(); + if (data.containsKey('created_at') && data['created_at'] == null) { + data['created_at'] = now; + } + if (data.containsKey('createdAt') && data['createdAt'] == null) { + data['createdAt'] = now; + } + if (data.containsKey('updated_at') && data['updated_at'] == null) { + data['updated_at'] = now; + } + if (data.containsKey('updatedAt') && data['updatedAt'] == null) { + data['updatedAt'] = now; + } var keys = data.keys.toList(); keys.where((k) => !query.fields.contains(k)).forEach(data.remove); if (data.isEmpty) { @@ -44,6 +57,19 @@ abstract class QueryValues { if (data.isEmpty) { return ''; } + var now = DateTime.now(); + if (data.containsKey('created_at') && data['created_at'] == null) { + data.remove('created_at'); + } + if (data.containsKey('createdAt') && data['createdAt'] == null) { + data.remove('createdAt'); + } + if (data.containsKey('updated_at') && data['updated_at'] == null) { + data['updated_at'] = now; + } + if (data.containsKey('updatedAt') && data['updatedAt'] == null) { + data['updatedAt'] = now; + } var b = StringBuffer('SET'); var i = 0;