-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheatsheet.cheatmd
More file actions
87 lines (69 loc) · 1.59 KB
/
cheatsheet.cheatmd
File metadata and controls
87 lines (69 loc) · 1.59 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
# Cheatsheet
> #### Transactions Required {: .info}
>
> References to large objects are only valid for the duration of a
> transaction. In practice, all operations on large objects need to be in
> an `Repo.transaction/1` or `Repo.transact/1` call.
>
> Any large object value will be closed automatically at the end of the
> transaction.
## Operating on objects as a whole
{: .col-2}
### Streaming API
#### Writing local file to an object
```elixir
stream = File.stream!("/tmp/bigfile.dat")
{:ok, object_id} =
Repo.import_large_object(stream)
```
#### Reading object to local file
```elixir
stream = File.stream!("/tmp/bigfile.dat")
:ok =
Repo.export_large_object(object_id, into: stream)
```
### Buffered API
#### Writing data to an object
```elixir
large_binary = "This is a large binary."
{:ok, object_id} =
Repo.import_large_object(large_binary)
```
#### Reading data from an object
```elixir
{:ok, data} =
Repo.export_large_object(object_id)
```
## Granular access to objects
{: .col-2}
### Create a new object
```elixir
{:ok, object_id} =
Repo.create_large_object(mode: :write)
```
### Open an existing object
```elixir
# For reading
{:ok, object} =
Repo.open_large_object(object_id)
# For writing
{:ok, object} =
Repo.open_large_object(object_id, mode: :write)
```
### Read from object
```elixir
# Read 1024 bytes
{:ok, data} =
PgLargeObjects.LargeObject.read(object, 1024)
```
### Write to object
```elixir
binary = "Some data to store."
:ok =
PgLargeObjects.LargeObject.write(object, binary)
```
### Get object size
```elixir
{:ok, size} =
PgLargeObjects.LargeObject.size(object)
```