-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketchpad.ts
143 lines (108 loc) · 3.14 KB
/
sketchpad.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import Model, {
DB,
PrimaryGeneratedColumn,
Generated,
PrimaryColumn,
Column,
BelongsTo,
HasMany,
CreateDateColumn,
} from "@memoria/model";
import { SQLAdapter } from "@memoria/adapters"; // NOTE: this has to come AFTER @memoria/model import
class CustomSQLAdapter extends SQLAdapter {
static logging = false;
}
class Photo extends Model {
static Adapter = CustomSQLAdapter;
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
href: string;
@Column("bool")
is_public: boolean;
@Column("int")
user_id: number;
@BelongsTo((type) => User, (user) => user.photos)
user: User;
}
class User extends Model {
static Adapter = CustomSQLAdapter;
@PrimaryGeneratedColumn("increment")
id: number;
@Column()
first_name: string;
@Column()
last_name: string;
@CreateDateColumn()
created_at: string;
@Column("int")
@Generated()
points: number;
@HasMany((type) => Photo, (photo) => photo.user)
photos: Photo[];
}
let a = new User();
console.log("new User() output:");
console.table(a);
async function main() {
try {
await DB.resetRecords();
// console.log(Schema.Schemas[1].relations);
let user = await User.insert({ first_name: "Izel", last_name: "Nakri" });
console.log("User insert:");
console.table(user.toObject());
let updatedUser = await User.update({ id: user.id, first_name: "Moris" });
console.log("User update:");
console.table(updatedUser.toJSON());
let savedUser = await User.update({ id: user.id, first_name: "Mo" });
console.log("User save:");
console.table(savedUser);
let foundUser = await User.find(user.id);
console.log("foundUser:");
console.table(foundUser);
console.log("User.count:", await User.count());
let deletedUser = await User.delete(user);
console.log("deletedUser:");
console.table(deletedUser);
console.log("User.count:", await User.count());
let insertedUsers = await User.insertAll([
{ first_name: "Yukihiro", last_name: "Matsumoto" },
{ first_name: "Yehuda", last_name: "Katz" },
]);
console.log("insertedUsers:");
console.table(insertedUsers);
console.log("User.count:", await User.count());
let foundUsers = await User.findAll();
console.log("foundUsers:");
console.table(foundUsers);
let changedUsers = await User.updateAll(
foundUsers.map((foundUser) => {
foundUser.first_name = "Changed";
return foundUser;
})
);
console.log("changedUsers:");
console.table(changedUsers);
let savedUsers = await User.saveAll(
foundUsers.map((foundUser) => {
foundUser.first_name = "Changed2";
return foundUser;
})
);
console.log("savedUsers:");
console.table(savedUsers);
console.log("User.count:", await User.count());
let allDeletedUsers = await User.deleteAll(changedUsers);
console.log("allDeletedUsers:");
console.table(allDeletedUsers);
console.log("User.count:", await User.count());
} catch (error) {
console.log("error:");
console.log(error);
}
console.log("-------------- THE END ---------------");
}
main();
// TODO: Relationships