Skip to content

Commit ffa9633

Browse files
persist todos with TypeORM
1 parent ea833ee commit ffa9633

File tree

7 files changed

+62
-79
lines changed

7 files changed

+62
-79
lines changed

app/components/App.js

-32
This file was deleted.

app/components/App.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Vue from 'nativescript-vue'
22
import TaskItem from './TaskItem'
3-
import Todo from "~/models/Todo";
3+
import Todo from "../models/Todo";
44

55
class App extends Vue {
66
surprise: boolean
77
newtask: string
88
todos: Array<Todo>
9+
refreshTodos: () => void
910
}
1011
export default Vue.extend<App>({
1112
data() {
@@ -18,10 +19,21 @@ export default Vue.extend<App>({
1819
]
1920
};
2021
},
22+
mounted() {
23+
this.refreshTodos()
24+
},
2125
methods: {
26+
refreshTodos() {
27+
Todo.find().then((todos) => {
28+
console.log(todos)
29+
this.todos = todos
30+
}).catch(console.error)
31+
},
32+
2233
addTodo() {
23-
this.todos.push(new Todo(this.newtask, false))
24-
console.log(this.todos)
34+
const todo = new Todo(this.newtask, false);
35+
todo.save().then(() => this.refreshTodos())
36+
.catch(console.error)
2537
}
2638
},
2739
template: `
@@ -32,7 +44,7 @@ export default Vue.extend<App>({
3244
<Button @tap="addTodo" text="ADD TASK"></Button>
3345
<ListView id="todolist" for="todo in todos">
3446
<v-template>
35-
<TaskItem :todo="todo"></TaskItem>
47+
<TaskItem :todo="todo" @requestRefresh="refreshTodos"></TaskItem>
3648
</v-template>
3749
</ListView>
3850
</StackLayout>

app/components/TaskItem.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ class TaskItem extends Vue {
88
export default Vue.extend<TaskItem>({
99
props: ['todo'],
1010
methods: {
11-
created() {
12-
console.log(this.todo)
11+
async removeSelf() {
12+
if (this.todo.id) {
13+
await Todo.delete({id: this.todo.id})
14+
this.$emit('requestRefresh')
15+
}
1316
}
1417
},
1518
template: `
1619
<StackLayout class="task-item" orientation="horizontal" xmlns="http://schemas.nativescript.org/tns.xsd">
17-
<Label class="task-item-task" :text="todo.task"></Label>
20+
<Label @tap="removeSelf" class="task-item-task" :text="todo.task"></Label>
1821
</StackLayout>
1922
`
2023
})

app/main.js

-22
This file was deleted.

app/main.ts

+28-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@ import Vue from 'nativescript-vue'
22
import App from './components/App'
33
import 'nativescript-sqlite'
44

5-
import {createConnection} from "typeorm/browser";
6-
7-
createConnection({
8-
database: 'test.db',
9-
type: 'nativescript'
10-
}).then((connection) => {
11-
console.log("Connection Created")
12-
}).catch((err: Error) => console.log(err))
5+
import {createConnection, getManager} from "typeorm/browser";
6+
7+
import Todo from '~/models/Todo';
8+
9+
(async () => {
10+
try {
11+
const connection = await createConnection({
12+
database: 'test.db',
13+
type: 'nativescript',
14+
entities: [
15+
Todo
16+
],
17+
logging: true
18+
})
19+
20+
console.log("Connection Created")
21+
22+
await connection.synchronize(false)
23+
24+
console.log("Synchronized")
25+
26+
27+
} catch (err) {
28+
console.error(err)
29+
}
30+
})();
31+
32+
1333

1434
Vue.config.silent = false
1535

app/models/Todo.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm/browser";
1+
import {BaseEntity, Entity, PrimaryGeneratedColumn, Column} from "typeorm/browser";
22

33
@Entity()
4-
export default class Todo {
5-
@PrimaryGeneratedColumn('increment')
4+
export default class Todo extends BaseEntity {
5+
@PrimaryGeneratedColumn()
66
id?: number;
77

88
@Column()
99
task: string;
1010

1111
@Column()
1212
done: boolean;
13-
constructor(task, done) {
14-
this.task = task
15-
this.done = done
13+
14+
constructor(task: string, done: boolean) {
15+
super();
16+
this.task = task;
17+
this.done = done;
1618
}
1719

1820
}

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)