Files
coins/bin/insert.ts
T

181 lines
4.5 KiB
TypeScript

import { NumistaType } from '@/types/numista';
import { db } from './db';
const bannedKeys = ['issuing_entity'];
const omit = (data: object, keys: string[] = []) =>
Object.fromEntries(
Object.entries(data).filter(
([key, value]) => !keys.includes(key) && (typeof value === 'number' || typeof value === 'boolean' || !!value)
)
);
const exists = (table: string, primaryKey: string, value: unknown) => {
const stmt = `SELECT * FROM ${table} WHERE ${primaryKey}=${typeof value === 'string' ? `'${value}'` : value}`;
return !!db.prepare(stmt).get();
};
const inserter = (table: string, data: Record<string, unknown>, primaryKey?: string) => {
if (primaryKey && exists(table, primaryKey, data[primaryKey])) {
console.error(`${table} ${data[primaryKey]} already exists`);
return;
}
const entries = Object.entries(data).filter(([key]) => !bannedKeys.includes(key));
const stmt = `INSERT INTO ${table} (${entries.map(([key]) => key).join(', ')}) VALUES (${entries
.map(([, value]) => {
if (Array.isArray(value)) {
return `'${value.join(', ').replaceAll("'", "''")}'`;
}
if (typeof value === 'string') {
return `'${value.replaceAll("'", "''")}'`;
}
return value;
})
.join(', ')});`;
console.log(stmt);
db.exec(stmt);
};
export const handleInsertType = (type: NumistaType) => {
if (type.issuer) {
inserter('Issuer', omit(type.issuer), 'code');
}
if (type.value) {
if (type.value.currency) {
inserter('Currency', omit(type.value.currency), 'id');
}
inserter(
'Value',
omit(
{
...type.value,
currency_id: type.value.currency?.id,
id: type.id
},
['currency']
),
'id'
);
}
if (type.demonetization) {
inserter('Demonetization', omit({ ...type.demonetization, id: type.id }), 'id');
}
if (type.composition) {
inserter('Composition', omit({ ...type.composition, id: type.id }), 'id');
}
if (type.technique) {
inserter('Technique', omit({ ...type.technique, id: type.id }), 'id');
}
if (type.obverse) {
inserter('Obverse', omit({ ...type.obverse, id: type.id }), 'id');
}
if (type.reverse) {
inserter('Reverse', omit({ ...type.reverse, id: type.id }), 'id');
}
if (type.edge) {
inserter('Edge', omit({ ...type.edge, id: type.id }), 'id');
}
if (type.watermark) {
inserter('Watermark', omit({ ...type.watermark, id: type.id }), 'id');
}
inserter(
'NumistaType',
omit(
{
...type,
issuer_code: type.issuer?.code,
count: type.count || 1,
...(type.value && { value_id: type.id }),
...(type.demonetization && { demonetization_id: type.id }),
...(type.composition && { composition_id: type.id }),
...(type.technique && { technique_id: type.id }),
...(type.obverse && { obverse_id: type.id }),
...(type.reverse && { reverse_id: type.id }),
...(type.edge && { edge_id: type.id }),
...(type.watermark && { watermark_id: type.id })
},
[
'issuer',
'value',
'ruler',
'obverse',
'reverse',
'edge',
'watermark',
'tags',
'references',
'mints',
'printers',
'demonetization',
'composition',
'technique',
'related_types'
]
),
'id'
);
if (type.ruler?.length) {
for (const ruler of type.ruler) {
if (ruler.group) {
inserter('RulerGroup', omit(ruler.group), 'id');
}
inserter(
'Ruler',
omit(
{
...ruler,
group_id: ruler.group?.id
},
['group']
),
'id'
);
try {
inserter('TypeRuler', {
type_id: type.id,
ruler_id: ruler.id
});
} catch {
//ok
}
}
}
};
export const handleUpdateTypeCount = (type: NumistaType) => {
if (!exists('NumistaType', 'id', type.id)) {
console.error(`id ${type.id} does not exist`);
return;
}
const stmt = `UPDATE NumistaType SET count = ${type.count} WHERE id = ${type.id}`;
console.log(stmt);
db.exec(stmt);
};
export const handleRelated = (type: NumistaType) => {
if (type.related_types?.length) {
for (const related_type of type.related_types) {
if (exists('NumistaType', 'id', related_type.id)) {
inserter(
'TypeRelated',
omit({
type1_id: type.id,
type2_id: related_type.id
})
);
}
}
}
};