-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathpinata.js
More file actions
100 lines (84 loc) · 2.54 KB
/
pinata.js
File metadata and controls
100 lines (84 loc) · 2.54 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
100
import debug from 'debug'
const log = debug('pinata:pinToPinata')
/**
* @typedef {import('../../../api/src/utils/db-types').definitions} definitions
* @typedef {Pick<definitions['pin'], 'id'|'status'|'content_cid'|'service'|'updated_at'>} Pin
* @typedef {import('@supabase/postgrest-js').PostgrestQueryBuilder<Pin>} PinQuery
*/
/**
* Sends pin requests to Pinata.
*
* @param {{
* db: import('../../../api/src/utils/db-client').DBClient
* pinata: import('../lib/pinata').Pinata
* }} config
*/
export async function pinToPinata({ db, pinata }) {
if (!log.enabled) {
console.log('ℹ️ Enable logging by setting DEBUG=pinata:pinToPinata')
}
const { count, error: countError } = await db.client
.from('pin')
.select('*', { count: 'exact', head: true })
.eq('service', 'Pinata')
.neq('status', 'Pinned')
.neq('status', 'PinError')
.range(0, 1)
if (countError) {
throw Object.assign(new Error(), countError)
}
log(`🎯 Updating ${count ?? 0} pin statuses`)
let offset = 0
const limit = 1000
while (true) {
/** @type {PinQuery} */
const query = db.client.from('pin')
const { data: pins, error } = await query
.select('id,status,content_cid,service')
.eq('service', 'Pinata')
.neq('status', 'Pinned')
.neq('status', 'PinError')
.range(offset, offset + limit - 1)
if (error) {
throw error
}
if (!pins) {
throw new Error('no pins found')
}
if (!pins.length) {
break
}
/** @type {Pin[]} */
const updatedPins = []
for (const pin of pins) {
try {
const pinataOptions = {} // TODO: add origins
await pinata.pinByHash(pin.content_cid, { pinataOptions })
log(
`📌 ${pin.content_cid} submitted to Pinata! ${pins.indexOf(pin)}/${
pins.length
}`
)
updatedPins.push({
...pin,
status: 'Pinned', // FIXME: not really pinned, queued
updated_at: new Date().toISOString(),
})
} catch (err) {
log(`💥 failed to pin ${pin.content_cid}`, err)
}
}
if (updatedPins.length) {
const { error: updateError } = await db.client
.from('pin')
.upsert(updatedPins, { count: 'exact', returning: 'minimal' })
if (updateError) {
throw Object.assign(new Error(), updateError)
}
}
log(`🗂 ${pins.length} processed, ${updatedPins.length} updated`)
log(`ℹ️ ${offset + pins.length} of ${count ?? 0} processed in total`)
offset += limit
}
log('✅ Done')
}