You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.3 KiB
JavaScript
99 lines
2.3 KiB
JavaScript
5 years ago
|
const pg = require('../pg')
|
||
|
const joinjs = require('join-js').default;
|
||
|
const debug = require('debug')('sos:db:user')
|
||
|
const mappings = require('../mappings')
|
||
|
|
||
|
const sharp = require('sharp')
|
||
|
|
||
|
const item = module.exports = {}
|
||
|
|
||
|
item.findById = async (item_uuid) => {
|
||
|
const query = {
|
||
|
text: 'select * from v_item where item_uuid = $1',
|
||
|
values: [
|
||
|
item_uuid
|
||
|
]
|
||
|
}
|
||
|
|
||
|
debug(query);
|
||
|
|
||
|
const {rows} = await pg.query(query)
|
||
|
return joinjs.map(rows, mappings, 'itemMap', 'item_')[0];
|
||
|
}
|
||
|
|
||
|
item.findBySlug = async (item_slug) => {
|
||
|
const query = {
|
||
|
text: 'select * from v_item where item_urlslug = $1',
|
||
|
values: [
|
||
|
item_slug
|
||
|
]
|
||
|
}
|
||
|
|
||
|
debug(query);
|
||
|
|
||
|
const {rows} = await pg.query(query)
|
||
|
return joinjs.map(rows, mappings, 'itemMap', 'item_')[0];
|
||
|
}
|
||
|
|
||
|
item.create = async (name, urlslug, description, price_cents, published) => {
|
||
|
const query = {
|
||
|
text: 'select * from public.create_item($1::text, $2::citext, $2::text, $4::integer, $5::boolean)',
|
||
|
values: [
|
||
|
name,
|
||
|
urlslug,
|
||
|
description,
|
||
|
price_cents,
|
||
|
published
|
||
|
]
|
||
|
}
|
||
|
|
||
|
debug(query)
|
||
|
|
||
|
const {rows} = await pg.query(query)
|
||
|
return joinjs.map(rows, mappings, 'itemMap', 'item_')
|
||
|
}
|
||
|
|
||
|
item.addImage = async (item_uuid, image_buffer, uploader_uuid) => {
|
||
|
|
||
|
// Default param chain: output as png
|
||
|
const source = sharp(image_buffer).png()
|
||
|
|
||
|
// All output images clone the source param chain, and then
|
||
|
// resize before creating a buffer
|
||
|
const [image, thumb] = await Promise.all([
|
||
|
source.clone().toBuffer(),
|
||
|
source.clone().resize({width: 300}).toBuffer()
|
||
|
])
|
||
|
|
||
|
const query = {
|
||
|
text: 'select * from public.add_image_to_item($1, $2, $3, $4, $5)',
|
||
|
values: [
|
||
|
item_uuid,
|
||
|
image,
|
||
|
thumb,
|
||
|
'image/png',
|
||
|
uploader_uuid
|
||
|
]
|
||
|
}
|
||
|
|
||
|
const {rows} = await pg.query(query)
|
||
|
return joinjs.map(rows, mappings, 'itemMap', 'item_')
|
||
|
}
|
||
|
|
||
|
const queries = {
|
||
|
'large': 'select * from get_image_large($1)',
|
||
|
'thumb': 'select * from get_image_thumb($1)'
|
||
|
}
|
||
|
|
||
|
item.getImage = async (image_uuid, size) => {
|
||
|
if(!profileSizeQueries[size])
|
||
|
throw new Error(`Cannot get unknown image size: "${size}"`)
|
||
|
|
||
|
const query = {
|
||
|
text: profileSizeQueries[size],
|
||
|
values: [image_uuid]
|
||
|
}
|
||
|
|
||
|
const {rows} = await pg.query(query)
|
||
|
return joinjs.map(rows, relationMaps, 'bareImageMap', 'image_')[0];
|
||
|
}
|