-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
106 lines (89 loc) · 3.58 KB
/
test.js
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
import chainquery from "./src/utils/chainquery.js";
import { Hono } from "hono";
import { serve } from '@hono/node-server';
// Base query skeleton
const baseQuery = {
table: "claim c",
fields: [
'c.id',
'c.title',
'c.name',
'c.claim_id',
'c.thumbnail_url AS thumbnail',
'c.description',
'c.language',
'c.release_time',
'COALESCE(c.audio_duration, c.duration) AS duration',
'c.frame_height AS height',
'c.frame_width AS width',
'c.license',
'c.is_nsfw AS nsfw',
'c.fee',
'c.source_name AS file_name',
"SUBSTRING_INDEX(COALESCE(c.content_type, c.source_media_type), '/', 1) AS media_type",
"SUBSTRING_INDEX(COALESCE(c.content_type, c.source_media_type), '/', -1) AS mime_type",
'c.effective_amount / 1e8 AS effective_amount',
'(SELECT COUNT(*) AS reposts FROM claim WHERE type = "claimreference" AND claim_reference = c.claim_id) AS reposts',
`(SELECT GROUP_CONCAT(tag SEPARATOR ', ') AS tags FROM claim_tag ct LEFT JOIN tag t ON t.id = ct.tag_id WHERE claim_id = c.claim_id
) AS tags`,
'c.claim_reference AS reposted_claim_id',
'c.author',
'p.title AS channel_title',
'p.name AS channel_name',
'p.claim_id AS channel_id',
'COALESCE(p.effective_amount / 1e8,1) AS certificate_amount',
'c.value_as_json AS value',
'c.modified_at',
`CASE
WHEN c.bid_state = 'Controlling' THEN 0
WHEN c.bid_state = 'Accepted' THEN 1
WHEN c.bid_state = 'Active' THEN 2
WHEN c.bid_state = 'Spent' THEN 3
WHEN c.bid_state = 'Expired' THEN 4
END AS bid_state`,
`CASE
WHEN c.type = 'channel' THEN 0
WHEN c.type = 'stream' THEN 1
WHEN c.type = 'claimreference' THEN 2
WHEN c.type = 'claimlist' THEN 3
END AS type`
].map(field=> field.replaceAll(' ', '').replaceAll('\n', ' ')).join(','),
joins: [
"LEFT JOIN claim p ON p.claim_id = c.publisher_id",
// "LEFT JOIN claim_tag ct ON ct.claim_id = c.claim_id",
// "LEFT JOIN tag t ON t.id = ct.tag_id"
].join(" ")
}
function generateQuery(params={}) {
const limit = params.limit || 5000;
const where = [];
if (params.claim_id) where.push(`c.claim_id IN (${params.claim_id.map(id=>`'${id}'`).join(',')})`);
if (params.channel_id) where.push(`c.publisher_id IN (${params.channel_id.map(id=>`'${id}'`).join(',')})`);
// where.push(`t.tag IS NOT NULL`);
return `SELECT ${baseQuery.fields} FROM ${baseQuery.table} ${baseQuery.joins} ${where.length ? `WHERE ${where.join(' AND ')}` : ""} ORDER BY c.id DESC LIMIT ${limit}`;
}
// const app = new Hono();
// app.get('/', async (c) => {
// const params = {};
// Object.keys(c.req.queries()).map(function(key) {
// params[key] = c.req.queries()[key][0].split(',');
// })
// // console.log(params);
// const query = generateQuery(params);
// // console.log(query);
// const resp = await chainquery(query);
// return c.json({ ...resp, results: resp.data ? resp.data.length : 0, limit: params.limit || 5000})
// });
// serve(app);
// const query = `
// SELECT
// ct.*, t.tag
// FROM claim_tag ct
// LEFT JOIN tag t ON t.id = ct.tag_id
// LIMIT 20
// `;
const query = generateQuery();
const resp = await chainquery(query);
// console.log({ ...resp, results: resp.data ? resp.data.length : 0, limit: 5000});
console.log("Query: " + query.replaceAll('\n', ' '));
console.log("done");