Skip to content

Commit 607689f

Browse files
committed
added example code
1 parent 33017ad commit 607689f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+271
-83
lines changed

README.md

+24-25

config.xml

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?xml version='1.0' encoding='utf-8'?>
2-
<widget id="io.ionic.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
3-
<name>MyApp</name>
4-
<description>An awesome Ionic/Cordova app.</description>
5-
<author email="hi@ionicframework" href="http://ionicframework.com/">Ionic Framework Team</author>
2+
<widget id="com.typeorm.ionic" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
3+
<name>TypeORM-Ionic</name>
4+
<description>A simple app to test typeorm with cordova sqlite in ionic</description>
5+
<author email="[email protected]">
6+
Daniel Lang
7+
</author>
68
<content src="index.html" />
79
<access origin="*" />
810
<allow-navigation href="http://ionic.local/*" />
@@ -19,11 +21,6 @@
1921
<preference name="BackupWebStorage" value="none" />
2022
<preference name="SplashMaintainAspectRatio" value="true" />
2123
<preference name="FadeSplashScreenDuration" value="300" />
22-
23-
<!--
24-
Change these to configure how the splashscreen displays and fades in/out.
25-
More info here: https://github.com/apache/cordova-plugin-splashscreen
26-
-->
2724
<preference name="SplashShowOnlyFirstTime" value="false" />
2825
<preference name="SplashScreen" value="screen" />
2926
<preference name="SplashScreenDelay" value="3000" />
@@ -82,10 +79,14 @@
8279
<splash height="960" src="resources/ios/splash/Default@2x~iphone.png" width="640" />
8380
<splash height="480" src="resources/ios/splash/Default~iphone.png" width="320" />
8481
<splash height="2732" src="resources/ios/splash/Default@2x~universal~anyany.png" width="2732" />
82+
<icon height="1024" src="resources/ios/icon/icon-1024.png" width="1024" />
8583
</platform>
8684
<plugin name="ionic-plugin-keyboard" spec="~2.2.1" />
8785
<plugin name="cordova-plugin-whitelist" spec="1.3.1" />
8886
<plugin name="cordova-plugin-statusbar" spec="2.2.2" />
8987
<plugin name="cordova-plugin-device" spec="1.1.4" />
9088
<plugin name="cordova-plugin-splashscreen" spec="~4.0.1" />
89+
<plugin name="cordova-sqlite-storage" spec="^2.0.4" />
90+
<engine name="ios" spec="~4.4.0" />
91+
<engine name="android" spec="~6.2.3" />
9192
</widget>

ionic.config.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"name": "ionic-example",
33
"app_id": "",
4-
"type": "ionic-angular"
4+
"type": "ionic-angular",
5+
"integrations": {
6+
"cordova": {}
7+
}
58
}

package.json

+62-37
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
11
{
2-
"name": "ionic-example",
3-
"version": "0.0.1",
4-
"author": "Ionic Framework",
5-
"homepage": "http://ionicframework.com/",
6-
"private": true,
7-
"scripts": {
8-
"clean": "ionic-app-scripts clean",
9-
"build": "ionic-app-scripts build",
10-
"lint": "ionic-app-scripts lint",
11-
"ionic:build": "ionic-app-scripts build",
12-
"ionic:serve": "ionic-app-scripts serve"
13-
},
14-
"dependencies": {
15-
"@angular/common": "4.1.3",
16-
"@angular/compiler": "4.1.3",
17-
"@angular/compiler-cli": "4.1.3",
18-
"@angular/core": "4.1.3",
19-
"@angular/forms": "4.1.3",
20-
"@angular/http": "4.1.3",
21-
"@angular/platform-browser": "4.1.3",
22-
"@angular/platform-browser-dynamic": "4.1.3",
23-
"@ionic-native/core": "3.12.1",
24-
"@ionic-native/splash-screen": "3.12.1",
25-
"@ionic-native/status-bar": "3.12.1",
26-
"@ionic/storage": "2.0.1",
27-
"ionic-angular": "3.6.1",
28-
"ionicons": "3.0.0",
29-
"rxjs": "5.4.0",
30-
"sw-toolbox": "3.6.0",
31-
"zone.js": "0.8.12"
32-
},
33-
"devDependencies": {
34-
"@ionic/app-scripts": "2.1.4",
35-
"typescript": "2.3.4"
36-
},
37-
"description": "An Ionic project"
38-
}
2+
"name": "ionic-example",
3+
"version": "1.0.0",
4+
"author": "Daniel Lang",
5+
"scripts": {
6+
"clean": "ionic-app-scripts clean",
7+
"build": "ionic-app-scripts build",
8+
"lint": "ionic-app-scripts lint",
9+
"ionic:build": "ionic-app-scripts build",
10+
"ionic:serve": "ionic-app-scripts serve"
11+
},
12+
"config": {
13+
"ionic_webpack": "./config/webpack.config.js"
14+
},
15+
"dependencies": {
16+
"@angular/common": "4.4.3",
17+
"@angular/compiler": "4.4.3",
18+
"@angular/compiler-cli": "4.4.3",
19+
"@angular/core": "4.4.3",
20+
"@angular/forms": "4.4.3",
21+
"@angular/http": "4.4.3",
22+
"@angular/platform-browser": "4.4.3",
23+
"@angular/platform-browser-dynamic": "4.4.3",
24+
"@ionic-native/core": "4.3.0",
25+
"@ionic-native/splash-screen": "4.3.0",
26+
"@ionic-native/status-bar": "4.3.0",
27+
"@ionic/storage": "2.0.1",
28+
"cordova-android": "^6.2.3",
29+
"cordova-ios": "^4.4.0",
30+
"cordova-plugin-device": "^1.1.4",
31+
"cordova-plugin-splashscreen": "^4.0.3",
32+
"cordova-plugin-statusbar": "^2.2.2",
33+
"cordova-plugin-whitelist": "^1.3.1",
34+
"cordova-sqlite-storage": "^2.0.4",
35+
"ionic-angular": "3.7.0",
36+
"ionic-plugin-keyboard": "^2.2.1",
37+
"ionicons": "3.0.0",
38+
"rxjs": "5.4.3",
39+
"sw-toolbox": "3.6.0",
40+
"typeorm": "0.1.0-alpha.49",
41+
"zone.js": "0.8.17"
42+
},
43+
"devDependencies": {
44+
"@ionic/app-scripts": "^3.0.0",
45+
"@types/node": "^8.0.31",
46+
"typescript": "2.3.4"
47+
},
48+
"description": "An Ionic project",
49+
"cordova": {
50+
"plugins": {
51+
"cordova-plugin-device": {},
52+
"cordova-plugin-splashscreen": {},
53+
"cordova-plugin-statusbar": {},
54+
"cordova-plugin-whitelist": {},
55+
"ionic-plugin-keyboard": {},
56+
"cordova-sqlite-storage": {}
57+
},
58+
"platforms": [
59+
"android",
60+
"ios"
61+
]
62+
}
63+
}
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes

resources/icon.png.md5

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3f1bbdf1aefcb5ce7b60770ce907c68f

resources/ios/icon/icon-1024.png

22 KB

resources/ios/icon/icon-40.png

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/icon-50.png

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/icon-60.png

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/icon-72.png

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/icon-76.png

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/icon-small.png

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/[email protected]

0 Bytes

resources/ios/icon/icon.png

0 Bytes

resources/ios/icon/[email protected]

0 Bytes
0 Bytes

resources/ios/splash/Default-667h.png

0 Bytes

resources/ios/splash/Default-736h.png

0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes
0 Bytes

resources/splash.png.md5

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0dcf1df8c92c1ece4382d3357d9f8562

screenshot.png

31.5 KB

src/app/app.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { StatusBar } from '@ionic-native/status-bar';
66

77
import { MyApp } from './app.component';
88
import { HomePage } from '../pages/home/home';
9+
import { DatabaseProvider } from '../providers/database';
910

1011
@NgModule({
1112
declarations: [
@@ -24,7 +25,8 @@ import { HomePage } from '../pages/home/home';
2425
providers: [
2526
StatusBar,
2627
SplashScreen,
27-
{provide: ErrorHandler, useClass: IonicErrorHandler}
28+
{provide: ErrorHandler, useClass: IonicErrorHandler},
29+
DatabaseProvider
2830
]
2931
})
3032
export class AppModule {}

src/entities/author.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {Entity, Column, PrimaryGeneratedColumn, OneToMany} from "typeorm";
2+
import {Post} from "./post";
3+
4+
@Entity('author')
5+
export class Author {
6+
7+
@PrimaryGeneratedColumn()
8+
id: number;
9+
10+
@Column()
11+
name: string;
12+
13+
@Column({nullable: true})
14+
birthdate: string;
15+
16+
@OneToMany(type => Post, post => post.author)
17+
posts: Post[];
18+
}

src/entities/category.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2+
3+
@Entity('category')
4+
export class Category {
5+
6+
@PrimaryGeneratedColumn()
7+
id: number;
8+
9+
@Column()
10+
name: string;
11+
12+
}

src/entities/post.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {Entity, PrimaryGeneratedColumn, Column, ManyToMany, JoinTable, ManyToOne} from "typeorm";
2+
import {Category} from "./category";
3+
import {Author} from "./author"
4+
5+
@Entity('post')
6+
export class Post {
7+
8+
@PrimaryGeneratedColumn()
9+
id: number;
10+
11+
@Column()
12+
title: string;
13+
14+
@Column("text")
15+
text: string;
16+
17+
@ManyToMany(type => Category, {
18+
cascadeInsert: true
19+
})
20+
@JoinTable()
21+
categories: Category[];
22+
23+
@ManyToOne(type => Author, author => author.posts, {
24+
cascadeInsert: true
25+
})
26+
author: Author;
27+
}

src/pages/home/home.html

+17-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
<ion-header>
22
<ion-navbar>
33
<ion-title>
4-
Ionic Blank
4+
TypeORM Demo
55
</ion-title>
66
</ion-navbar>
77
</ion-header>
88

9-
<ion-content padding>
10-
The world is your oyster.
11-
<p>
12-
If you get lost, the <a href="http://ionicframework.com/docs/v2">docs</a> will be your guide.
13-
</p>
9+
<ion-content>
10+
<ion-list>
11+
<ion-item>
12+
<ion-spinner *ngIf="!savedPost" item-start></ion-spinner>
13+
<ion-icon *ngIf="savedPost" name="checkmark" item-start color="secondary"></ion-icon>
14+
<span>Post saved</span>
15+
</ion-item>
16+
<ion-item-divider color="primary">Loaded Post</ion-item-divider>
17+
<ion-item *ngIf="!loadedPost">Post not loaded</ion-item>
18+
<ion-item-group *ngIf="loadedPost">
19+
<ion-item>ID: {{loadedPost?.id}}</ion-item>
20+
<ion-item>Title: {{loadedPost?.title}}</ion-item>
21+
<ion-item>Author: {{loadedPost?.author.name}}</ion-item>
22+
<ion-item>Categories: {{getCategories()}}</ion-item>
23+
</ion-item-group>
24+
</ion-list>
1425
</ion-content>

src/pages/home/home.ts

+54-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,66 @@
11
import { Component } from '@angular/core';
2-
import { NavController } from 'ionic-angular';
2+
import { Platform, NavController } from 'ionic-angular';
3+
4+
import { DatabaseProvider } from '../../providers/database';
5+
6+
import { Author } from '../../entities/author';
7+
import { Category } from '../../entities/category';
8+
import { Post } from '../../entities/post';
39

410
@Component({
511
selector: 'page-home',
612
templateUrl: 'home.html'
713
})
814
export class HomePage {
15+
private savedPost: boolean = false;
16+
private loadedPost: Post = null;
17+
18+
constructor(public navCtrl: NavController, private platform: Platform, private database: DatabaseProvider) { }
19+
20+
ionViewDidLoad() {
21+
this.runDemo();
22+
}
23+
24+
async runDemo() {
25+
await this.platform.ready();
26+
const category1 = new Category();
27+
category1.name = "TypeScript";
28+
29+
const category2 = new Category();
30+
category2.name = "Programming";
31+
32+
const author = new Author();
33+
author.name = "Person";
34+
35+
const post = new Post();
36+
post.title = "Control flow based type analysis";
37+
post.text = `TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.`;
38+
post.categories = [category1, category2];
39+
post.author = author;
40+
41+
const connection = await this.database.getConnection();
42+
const postRepository = connection.getRepository(Post);
43+
await postRepository.save(post);
44+
45+
console.log("Post has been saved");
46+
this.savedPost = true;
47+
48+
const loadedPost = await postRepository.createQueryBuilder('post')
49+
.innerJoinAndSelect('post.author', 'author')
50+
.innerJoinAndSelect('post.categories', 'categories')
51+
.where('post.id = :id', {id: post.id})
52+
.getOne();
53+
54+
console.log("Post has been loaded: ", loadedPost);
55+
this.loadedPost = loadedPost;
56+
}
957

10-
constructor(public navCtrl: NavController) {
58+
getCategories() {
59+
if(this.loadedPost) {
60+
return this.loadedPost.categories.map(cat => cat.name).join(",");
61+
}
1162

63+
return '';
1264
}
1365

1466
}

0 commit comments

Comments
 (0)