-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathentry.swift
More file actions
99 lines (88 loc) · 2.42 KB
/
entry.swift
File metadata and controls
99 lines (88 loc) · 2.42 KB
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
import Foundation
import PowerSync
import PowerSyncStructuredQueries
import StructuredQueries
@Table("users")
struct User {
var id: String
var name: String
var birthday: Date?
}
@Table("posts")
struct Post {
var id: String
var description: String
@Column("user_id")
var userId: String
}
@Selection
struct JoinedResult {
let postDescription: String
let userName: String
}
@main
struct Main {
static func main() async throws {
// TODO, check if the schema can be shared in some way
let powersync = PowerSyncDatabase(
schema: Schema(
tables: [
Table(
name: "users",
columns: [
.text("name"),
.text("birthday")
]
),
Table(
name: "posts",
columns: [
.text("description"),
.text("user_id")
]
),
],
),
dbFilename: "tests.sqlite"
)
let testUserID = UUID().uuidString
try await User.insert {
($0.id, $0.name, $0.birthday)
} values: {
(testUserID, "Steven", Date())
}.execute(powersync)
try await User.insert {
User(
id: UUID().uuidString,
name: "Nevets"
)
}.execute(powersync)
let users = try await User.all.fetchAll(powersync)
print("The users are:")
for user in users {
print(user)
}
let posts = try await Post.all.fetchAll(powersync)
print("The posts are:")
for post in posts {
print(post)
}
try await Post.insert {
Post(
id: UUID().uuidString, description: "A Post", userId: testUserID
)
}.execute(powersync)
print("Joined posts are:")
let joinedPosts = try await Post.join(User.all) { $0.userId == $1.id }
.select {
JoinedResult.Columns(
postDescription: $0.description,
userName: $1.name
)
}
.fetchAll(powersync)
for joinedResult in joinedPosts {
print(joinedResult)
}
}
}