Compare commits

..

34 commits

Author SHA1 Message Date
thomashii
5ac098e3f5 Upgrade to SDK 2.16.x 2022-04-26 08:02:17 +08:00
thomashii
b1bd5c341e Update to support v6.0.0 2022-03-15 13:25:51 +08:00
Thomas Hii
b5fea5806d
Merge pull request #1 from outersky/angel3-orm
fix: Present<Message> should be Message
2022-02-11 23:22:38 +08:00
outersky
290b018ab9
fix README 2022-02-04 10:32:26 +08:00
outersky
65f62b75c0
fix: Present<Message> should be Message
Present<*> is not supported now by default
2022-02-04 10:26:20 +08:00
thomashii
3b67c9670d Updated ORM template 2021-09-25 18:45:23 +08:00
thomashii
7401143635 Updated pacakges 2021-08-19 08:29:50 +08:00
thomashii
8e00405e2e Updated packages 2021-08-19 08:20:07 +08:00
thomashii
b120ba505f Updated the ORM example 2021-07-25 17:11:58 +08:00
thomashii
fab33a543c Updated ORM 2021-07-17 12:52:48 +08:00
thomashii
a32652b683 Updated angel3-orm 2021-06-14 08:08:25 +08:00
thomashii
9eb81a541d Updated README 2021-06-11 12:28:14 +08:00
thomashii
9697137ae8 Updated README 2021-06-11 12:09:42 +08:00
thomashii
d11df03d97 Updated README 2021-06-11 12:08:20 +08:00
thomashii
636f33de37 Updated angel3_orm 2021-06-11 12:00:10 +08:00
thomashii
bf9bf67fa8 Migrated to NNBD 2021-05-16 16:25:29 +08:00
thomashii
a32bb29af6 Fixed to work with sdk 2.10.x 2021-03-09 00:17:42 +08:00
thomashii
10192bd29a ORM template for SDK 2.12.x 2021-03-07 20:52:36 +08:00
thomashii
233f10b384 Updated dependencies 2021-02-19 22:34:24 +08:00
Tobe O
54afecd3bc Merge branch 'master' into orm 2019-10-09 14:26:50 -04:00
Tobe O
1482cb2ff7 Merge branch 'master' into orm 2019-10-09 14:21:34 -04:00
Tobe O
007f926f1b Merge branch 'master' into orm 2019-05-01 19:33:06 -04:00
Tobe O
514b697b00 Use app logger for orm 2019-05-01 19:32:31 -04:00
Tobe O
ece9e5d4fa Merge branch 'master' into orm 2019-05-01 19:30:04 -04:00
Tobe O
0d0cb707c2 Use only stable deps 2019-04-29 13:57:47 -04:00
Tobe O
ffbc93c668 Merge branch 'master' into orm 2019-04-29 12:48:17 -04:00
Tobe O
5186ae8111 merge from master 2019-04-28 14:55:31 -04:00
Tobe O
91ff5f4e11 Formatting 2019-04-18 12:24:11 -04:00
Tobe O
772f74a908 merge from master 2019-04-18 12:23:23 -04:00
Tobe Osakwe
850ccb71b1
Update dev.dart 2019-02-06 23:00:45 -05:00
Tobe Osakwe
a3d80686b4
Update dev.dart 2019-02-06 16:59:05 -05:00
Tobe Osakwe
d42dd2befb
Update routes.dart 2019-01-23 14:45:44 -05:00
Tobe Osakwe
478d45bf47
Update pubspec.yaml 2019-01-23 14:43:27 -05:00
Tobe O
84f5ae5403 orm boilerplate 2.x 2019-01-11 19:44:28 -05:00
24 changed files with 465 additions and 73 deletions

View file

@ -1,10 +1,11 @@
.dart_tool
.idea
.pub
.vscode
.metals
.git
.github
.packages
logs/
test/
build/
.analysis-options
.packages
*.g.dart
pubspec.lock

3
.github/FUNDING.yml vendored
View file

@ -1,3 +0,0 @@
# These are supported funding model platforms
github: [thosakwe]

7
.gitignore vendored
View file

@ -11,7 +11,6 @@
.project
.pub/
.scripts-bin/
.metals/
build/
**/packages/
@ -58,8 +57,6 @@ pubspec.lock
# Mongo Explorer plugin:
.idea/mongoSettings.xml
.vscode/
## File-based project format:
*.iws
@ -81,7 +78,7 @@ crashlytics-build.properties
fabric.properties
### VSCode template
.vscode/*
.vscode/
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
@ -91,3 +88,5 @@ logs/
*.pem
.DS_Store
server_log.txt
.metals/

View file

@ -1,12 +0,0 @@
Primary Authors
===============
* __[Thomas Hii](dukefirehawk.apps@gmail.com)__
Thomas is the current maintainer of the code base. He has refactored and migrated the
code base to support NNBD.
* __[Tobe O](thosakwe@gmail.com)__
Tobe has written much of the original code prior to NNBD migration. He has moved on and
is no longer involved with the project.

View file

@ -2,6 +2,4 @@
## 1.0.0
* Changed to use `angel3` packages
* Updated to support NNBD
* Updated linter to `package:lints`
* Initial version

View file

@ -1,25 +0,0 @@
# Contribution
Any help from the open-source community is always welcome and needed:
1. Found an issue?
- Please [fill a bug report][tracker] with error message and steps to reproduce it.
2. Wish a feature?
- Open a feature request with use cases.
3. Are you using and liking the project?
- Create an article about your use case
- Do a post on your likes and dislikes
- Make a donation.
4. Are you a developer?
- Fix a bug and send a [pull request][pull_request]
- Implement a new feature
- Improve the Unit Tests
- Improve the [User Guide][doc] and send a [document pull request][doc_repo]
5. Have you already helped in any way?
- **Many thanks to the contributors and everybody that uses this project!**
[tracker]: https://github.com/dukefirehawk/angel/issues
[pull_request]: https://github.com/dukefirehawk/angel/pulls
[doc]: https://angel3-docs.dukefirehawk.com
[doc_repo]: https://github.com/dukefirehawk/angel3-guide/pulls

View file

@ -21,3 +21,4 @@ CMD dart ./bin/prod.dart -p 3000 -a 0.0.0.0
# Use -j flag to set higher number of isolates
#CMD dart ./bin/prod.dart -p 3000 -a 0.0.0.0 -j 50

View file

@ -1,11 +1,37 @@
# ORM Starter Application for Angel3 framework
# Basic Starter Application for Angel3 framework
This is a basic starter application for [Angel3 framework](https://angel3-framework.web.app) which is a full-stack Web framework in Dart.
This is an ORM starter application for [Angel3 framework](https://angel3-framework.web.app) which is a full-stack Web framework in Dart. The default database is `postgresql`. `mysql` support is still in active development.
## Installation & Setup
1. Download and install [Dart](https://dart.dev/get-dart).
2. Install `postgresql` version 10, 11, 12 and 13
3. Create a new user and database in postgres using `psql` cli. For example:
```sql
postgres=# create database appdb;
postgres=# create user appuser with encrypted password 'App1970#';
postgres=# grant all privileges on database appdb to appuser;
```
4. Update the `postgres` section in the `config/default.yaml` file with the newly created user and database name.
```yaml
postgres:
host: localhost
port: 5432
database_name: appdb
username: appuser
password: App1970#
useSSL: false
time_zone: UTC
```
5. Run the migration to generate `migrations` and `greetings` tables in the database.
```bash
dart bin/migrate.dart up
```
### Development
@ -17,6 +43,24 @@ This is a basic starter application for [Angel3 framework](https://angel3-framew
2. Modify the code and watch the changes applied to the application
3. Insert a message into DB:
```bash
curl -H "Content-Type: application/json" -X POST -d '{"message":"OK_Message" }' "http://localhost:3000/greetings/"
```
or
```bash
curl -X POST -d 'message=OK_Message2' "http://localhost:3000/greetings/"
```
4. Query DB:
```
http://localhost:3000/greetings/
```
### Production
1. Run the following command:
@ -32,3 +76,5 @@ This is a basic starter application for [Angel3 framework](https://angel3-framew
Visit the [Developer Guide](https://angel3-docs.dukefirehawk.com/guides) for dozens of guides and resources, including video tutorials, to get up and running as quickly as possible with Angel3.
Examples and complete projects can be found [here](https://angel3-framework.web.app/#/examples).
You can also view the [API Documentation](https://pub.dev/documentation/angel3_framework/latest/).

View file

@ -1,10 +1,10 @@
import 'dart:io';
import 'package:logging/logging.dart';
import 'package:angel/angel.dart';
import 'package:belatuk_pretty_logging/belatuk_pretty_logging.dart';
import 'package:angel3_container/mirrors.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_hot/angel3_hot.dart';
import 'package:angel/angel.dart';
import 'package:logging/logging.dart';
void main() async {
// Watch the config/ and web/ directories for changes, and hot-reload the server.
@ -24,5 +24,5 @@ void main() async {
var server = await hot.startServer('127.0.0.1', 3000);
print(
'[Angel3] server listening at http://${server.address.address}:${server.port}');
'[angel] server listening at http://${server.address.address}:${server.port}');
}

28
bin/migrate.dart Normal file
View file

@ -0,0 +1,28 @@
import 'package:angel/src/config/plugins/orm.dart';
import 'package:angel/models.dart';
import 'package:angel3_configuration/angel3_configuration.dart';
import 'package:angel3_migration_runner/angel3_migration_runner.dart';
import 'package:angel3_migration_runner/postgres.dart';
import 'package:file/local.dart';
import 'package:logging/logging.dart';
void main(List<String> args) async {
// Enable the logging
Logger.root.level = Level.INFO;
Logger.root.onRecord.listen((rec) {
print('${rec.time}: ${rec.level.name}: ${rec.loggerName}: ${rec.message}');
if (rec.error != null) {
print(rec.error);
print(rec.stackTrace);
}
});
var fs = LocalFileSystem();
var configuration = await loadStandaloneConfiguration(fs);
var connection = await connectToPostgres(configuration);
var migrationRunner = PostgresMigrationRunner(connection, migrations: [
GreetingMigration(),
]);
return await runMigrations(migrationRunner, args);
}

View file

@ -1,6 +1,6 @@
import 'package:angel/angel.dart';
import 'package:angel3_container/mirrors.dart';
import 'package:angel3_production/angel3_production.dart';
import 'package:angel/angel.dart';
// NOTE: By default, the Runner class does not use the `MirrorsReflector`, or any
// reflector, by default.

View file

@ -1,5 +1,13 @@
# Default server configuration.
jwt_secret: INSECURE_DEFAULT_SECRET
host: 127.0.0.1
mongo_db: mongodb://localhost:27017/angel
port: 3000
postgres:
host: localhost
port: 5432
database_name: appdb
username: appuser
password: App1970#
useSSL: false
time_zone: UTC

1
lib/models.dart Normal file
View file

@ -0,0 +1 @@
export 'src/models/greeting.dart';

View file

@ -1,4 +1,3 @@
/// Configuration for this Angel instance.
import 'package:angel3_configuration/angel3_configuration.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_jael/angel3_jael.dart';

View file

@ -0,0 +1,38 @@
import 'dart:async';
import 'dart:io';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_orm_postgres/angel3_orm_postgres.dart';
import 'package:postgres/postgres.dart';
Future<void> configureServer(Angel app) async {
try {
var connection = await connectToPostgres(app.configuration);
await connection.open();
var executor = PostgreSqlExecutor(connection, logger: app.logger);
app
..container.registerSingleton<QueryExecutor>(executor)
..shutdownHooks.add((_) => connection.close());
} catch (e) {
app.logger.severe("Failed to connect to PostgreSQL. ORM disabled.", e);
}
}
Future<PostgreSQLConnection> connectToPostgres(Map configuration) async {
var postgresConfig = configuration['postgres'] as Map? ?? {};
var connection = PostgreSQLConnection(
postgresConfig['host'] as String? ?? 'localhost',
postgresConfig['port'] as int? ?? 5432,
postgresConfig['database_name'] as String? ??
Platform.environment['USER'] ??
Platform.environment['USERNAME'] ??
'',
username: postgresConfig['username'] as String?,
password: postgresConfig['password'] as String?,
timeZone: postgresConfig['time_zone'] as String? ?? 'UTC',
timeoutInSeconds: postgresConfig['timeout_in_seconds'] as int? ?? 30,
useSSL: postgresConfig['use_ssl'] as bool? ?? false);
return connection;
}

View file

@ -1,7 +1,8 @@
/// Custom plugins go here.
import 'dart:async';
import 'package:angel3_framework/angel3_framework.dart';
import 'orm.dart' as orm;
Future configureServer(Angel app) async {
// Include any plugins you have made here.
await app.configure(orm.configureServer);
}

View file

@ -0,0 +1,12 @@
import 'package:angel3_migration/angel3_migration.dart';
import 'package:angel3_serialize/angel3_serialize.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:optional/optional.dart';
part 'greeting.g.dart';
@serializable
@orm
abstract class _Greeting extends Model {
@SerializableField(isNullable: false)
String? get message;
}

View file

@ -0,0 +1,267 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'greeting.dart';
// **************************************************************************
// MigrationGenerator
// **************************************************************************
class GreetingMigration extends Migration {
@override
void up(Schema schema) {
schema.create('greetings', (table) {
table.serial('id').primaryKey();
table.varChar('message');
table.timeStamp('created_at');
table.timeStamp('updated_at');
});
}
@override
void down(Schema schema) {
schema.drop('greetings');
}
}
// **************************************************************************
// OrmGenerator
// **************************************************************************
class GreetingQuery extends Query<Greeting, GreetingQueryWhere> {
GreetingQuery({Set<String>? trampoline}) {
trampoline ??= <String>{};
trampoline.add(tableName);
_where = GreetingQueryWhere(this);
}
@override
final GreetingQueryValues values = GreetingQueryValues();
GreetingQueryWhere? _where;
@override
Map<String, String> get casts {
return {};
}
@override
String get tableName {
return 'greetings';
}
@override
List<String> get fields {
return const ['id', 'message', 'created_at', 'updated_at'];
}
@override
GreetingQueryWhere? get where {
return _where;
}
@override
GreetingQueryWhere newWhereClause() {
return GreetingQueryWhere(this);
}
static Greeting? parseRow(List row) {
if (row.every((x) => x == null)) return null;
var model = Greeting(
id: row[0].toString(),
message: (row[1] as String?),
createdAt: (row[2] as DateTime?),
updatedAt: (row[3] as DateTime?));
return model;
}
@override
Optional<Greeting> deserialize(List row) {
return Optional.ofNullable(parseRow(row));
}
}
class GreetingQueryWhere extends QueryWhere {
GreetingQueryWhere(GreetingQuery query)
: id = NumericSqlExpressionBuilder<int>(query, 'id'),
message = StringSqlExpressionBuilder(query, 'message'),
createdAt = DateTimeSqlExpressionBuilder(query, 'created_at'),
updatedAt = DateTimeSqlExpressionBuilder(query, 'updated_at');
final NumericSqlExpressionBuilder<int> id;
final StringSqlExpressionBuilder message;
final DateTimeSqlExpressionBuilder createdAt;
final DateTimeSqlExpressionBuilder updatedAt;
@override
List<SqlExpressionBuilder> get expressionBuilders {
return [id, message, createdAt, updatedAt];
}
}
class GreetingQueryValues extends MapQueryValues {
@override
Map<String, String> get casts {
return {};
}
String? get id {
return (values['id'] as String?);
}
set id(String? value) => values['id'] = value;
String? get message {
return (values['message'] as String?);
}
set message(String? value) => values['message'] = value;
DateTime? get createdAt {
return (values['created_at'] as DateTime?);
}
set createdAt(DateTime? value) => values['created_at'] = value;
DateTime? get updatedAt {
return (values['updated_at'] as DateTime?);
}
set updatedAt(DateTime? value) => values['updated_at'] = value;
void copyFrom(Greeting model) {
message = model.message;
createdAt = model.createdAt;
updatedAt = model.updatedAt;
}
}
// **************************************************************************
// JsonModelGenerator
// **************************************************************************
@generatedSerializable
class Greeting extends _Greeting {
Greeting({this.id, required this.message, this.createdAt, this.updatedAt});
@override
final String? id;
@override
final String? message;
@override
final DateTime? createdAt;
@override
final DateTime? updatedAt;
Greeting copyWith(
{String? id, String? message, DateTime? createdAt, DateTime? updatedAt}) {
return Greeting(
id: id ?? this.id,
message: message ?? this.message,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt);
}
@override
bool operator ==(other) {
return other is _Greeting &&
other.id == id &&
other.message == message &&
other.createdAt == createdAt &&
other.updatedAt == updatedAt;
}
@override
int get hashCode {
return hashObjects([id, message, createdAt, updatedAt]);
}
@override
String toString() {
return 'Greeting(id=$id, message=$message, createdAt=$createdAt, updatedAt=$updatedAt)';
}
Map<String, dynamic> toJson() {
return GreetingSerializer.toMap(this);
}
}
// **************************************************************************
// SerializerGenerator
// **************************************************************************
const GreetingSerializer greetingSerializer = GreetingSerializer();
class GreetingEncoder extends Converter<Greeting, Map> {
const GreetingEncoder();
@override
Map convert(Greeting model) => GreetingSerializer.toMap(model);
}
class GreetingDecoder extends Converter<Map, Greeting> {
const GreetingDecoder();
@override
Greeting convert(Map map) => GreetingSerializer.fromMap(map);
}
class GreetingSerializer extends Codec<Greeting, Map> {
const GreetingSerializer();
@override
GreetingEncoder get encoder => const GreetingEncoder();
@override
GreetingDecoder get decoder => const GreetingDecoder();
static Greeting fromMap(Map map) {
if (map['message'] == null) {
throw FormatException("Missing required field 'message' on Greeting.");
}
return Greeting(
id: map['id'] as String?,
message: map['message'] as String?,
createdAt: map['created_at'] != null
? (map['created_at'] is DateTime
? (map['created_at'] as DateTime?)
: DateTime.parse(map['created_at'].toString()))
: null,
updatedAt: map['updated_at'] != null
? (map['updated_at'] is DateTime
? (map['updated_at'] as DateTime?)
: DateTime.parse(map['updated_at'].toString()))
: null);
}
static Map<String, dynamic> toMap(_Greeting model) {
if (model.message == null) {
throw FormatException("Missing required field 'message' on Greeting.");
}
return {
'id': model.id,
'message': model.message,
'created_at': model.createdAt?.toIso8601String(),
'updated_at': model.updatedAt?.toIso8601String()
};
}
}
abstract class GreetingFields {
static const List<String> allFields = <String>[
id,
message,
createdAt,
updatedAt
];
static const String id = 'id';
static const String message = 'message';
static const String createdAt = 'created_at';
static const String updatedAt = 'updated_at';
}

View file

@ -1,8 +1,9 @@
/// This app's route configuration.
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_orm/angel3_orm.dart';
import 'package:angel3_static/angel3_static.dart';
import 'package:file/file.dart';
import 'controllers/controllers.dart' as controllers;
import '../models/greeting.dart';
/// Put your app routes here!
///
@ -17,6 +18,33 @@ AngelConfigurer configureServer(FileSystem fileSystem) {
// Render `views/hello.jl` when a user visits the application root.
app.get('/', (req, res) => res.render('hello'));
app.get('/greetings', (req, res) {
var executor = req.container!.make<QueryExecutor>();
var query = GreetingQuery();
return query.get(executor);
});
app.post('/greetings', (req, res) async {
await req.parseBody();
if (!req.bodyAsMap.containsKey('message')) {
throw AngelHttpException.badRequest(message: 'Missing "message".');
} else {
var executor = req.container!.make<QueryExecutor>();
var message = req.bodyAsMap['message'].toString();
var query = GreetingQuery()..values.message = message;
var optional = await query.insert(executor);
return optional.value;
}
});
app.get('/greetings/:message', (req, res) {
var message = req.params['message'] as String;
var executor = req.container!.make<QueryExecutor>();
var query = GreetingQuery()..where!.message.equals(message);
return query.get(executor);
});
// Mount static server at web in development.
// The `CachingVirtualDirectory` variant of `VirtualDirectory` also sends `Cache-Control` headers.
//

View file

@ -1,4 +1,3 @@
/// Declare services here!
import 'dart:async';
import 'package:angel3_framework/angel3_framework.dart';

View file

@ -1,6 +1,6 @@
name: angel
version: 1.0.0
description: A basic starter application template for Angel3 framework
description: An ORM starter application for Angel3 framework
publish_to: none
environment:
sdk: '>=2.16.0 <3.0.0'
@ -9,17 +9,23 @@ dependencies:
angel3_configuration: ^6.0.0
angel3_framework: ^6.0.0
angel3_jael: ^6.0.0
angel3_migration: ^6.0.0
angel3_orm: ^6.0.0
angel3_orm_postgres: ^6.0.0
angel3_serialize: ^6.0.0
angel3_production: ^6.0.0
angel3_static: ^6.0.0
angel3_validate: ^6.0.0
belatuk_pretty_logging: ^4.0.0
optional: ^6.0.0
logging: ^1.0.0
dev_dependencies:
angel3_hot: ^6.0.0
angel3_migration_runner: ^6.0.0
angel3_orm_generator: ^6.0.0
angel3_serialize_generator: ^6.0.0
angel3_test: ^6.0.0
build_runner: ^2.0.3
io: ^1.0.0
test: ^1.21.0
lints: ^1.0.0

View file

@ -1,7 +1,7 @@
import 'package:angel/angel.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_test/angel3_test.dart';
import 'package:test/test.dart';
import 'package:angel/angel.dart';
// Angel also includes facilities to make testing easier.
//

View file

@ -3,8 +3,8 @@
<head>
<title>{{ title ?? 'Angel' }}</title>
<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="/css/site.css">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Lato:100" >
<link rel="stylesheet" type="text/css" href="/css/site.css">
<link rel="icon" href="/images/favicon.png">
</head>
<body>