12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package db
- import (
- "fmt"
- "github.com/gshopify/service-wrapper/model"
- "gshopper.com/gshopify/products/cache"
- "gshopper.com/gshopify/products/graphql/generated"
- "time"
- )
- const defaultCurrency = "USD"
- var (
- productKey = func(ln model.LanguageCode, currency generated.CurrencyCode, clause string, args ...any) *cache.SqlKey {
- return cache.NewSQLKey(
- "product",
- time.Minute,
- []string{
- "t.id as id",
- "anyLast(handle) as handle", "anyLast(type) as type", "anyLast(scope) as scope", "anyLast(tags) as tags", "anyLast(vendor) as vendor",
- "anyLast(gift) as gift", "anyLast(collections) as collections", "anyLast(images) as images",
- "anyLast(product_variant.inventory_management) as inventory_management",
- "anyLast(product_variant.inventory_policy) as inventory_policy",
- "anyLast(product_variant.options) as options",
- "anyLast(inventory_item.requires_shipping) as requires_shipping",
- "anyLast(inventory_item.tracked) as tracked",
- "anyLast(inventory_level.available) as available",
- "if(tracked, if(available > 0, true, if(inventory_policy == 'continue', true, false)), true) as for_sale",
- "anyLast(created_at) as created_at", "anyLast(updated_at) as updated_at", "anyLast(published_at) as published_at", "anyLast(deleted_at) as deleted_at",
- ln.SqlFieldSelection("title", "", "anyLast"),
- ln.SqlFieldSelection("description", "", "anyLast"),
- fmt.Sprintf("min(product_variant.price['%s']) as price_min", currency),
- fmt.Sprintf("max(product_variant.price['%s']) as price_max", currency),
- fmt.Sprintf("min(product_variant.compare_at_price['%s']) as compare_at_price_min", currency),
- fmt.Sprintf("max(product_variant.compare_at_price['%s']) as compare_at_price_max", currency),
- },
- clause, args...)
- }
- productCollectionKey = func(ln model.LanguageCode, clause string, args ...any) *cache.SqlKey {
- return cache.NewSQLKey(
- "product_collection",
- 3*time.Minute,
- []string{
- "id", "handle", "scope", "sort_order", "image", "template_suffix",
- "created_at", "updated_at", "published_at", "deleted_at",
- ln.SqlFieldSelection("title", "", ""),
- ln.SqlFieldSelection("description", "", ""),
- },
- clause,
- args...,
- )
- }
- productOptionKey = func(ln model.LanguageCode, clause string, args ...any) *cache.SqlKey {
- return cache.NewSQLKey(
- "product_option",
- time.Minute,
- []string{
- "id", "position", "created_at", "updated_at", "published_at", "deleted_at",
- ln.SqlFieldSelection("name", "", ""),
- ln.SqlArraySelection("values"),
- "values[tupleElement(opt, 2)] as value",
- },
- clause, args...)
- }
- productVariantKey = func(ln model.LanguageCode, currency generated.CurrencyCode, clause string, args ...any) *cache.SqlKey {
- return cache.NewSQLKey(
- "product_variant",
- time.Minute,
- []string{
- "anyLast(t.id) as id",
- "anyLast(t.product_id) as product_id",
- "anyLast(t.inventory_item_id) as inventory_item_id",
- "anyLast(t.position) as position",
- "anyLast(t.image) as image",
- "anyLast(t.inventory_management) as inventory_management",
- "anyLast(t.inventory_policy) as inventory_policy",
- "anyLast(t.options) as options",
- "anyLast(t.grams) as grams",
- "anyLast(t.weight) as weight",
- "anyLast(t.weight_unit) as weight_unit",
- "anyLast(t.created_at) as created_at",
- "anyLast(t.updated_at) as updated_at",
- "anyLast(t.published_at) as published_at",
- "anyLast(t.deleted_at) as deleted_at",
- "sum(inventory_level.available) as available",
- "anyLast(inventory_item.sku) as sku",
- "anyLast(inventory_item.barcode) as barcode",
- "anyLast(inventory_item.requires_shipping) as requires_shipping",
- "anyLast(inventory_item.tracked) as tracked",
- "if(tracked, if(available > 0, true, if(inventory_policy == 'continue', true, false)), true) as for_sale",
- ln.SqlFieldSelection("title", "product", "anyLast"),
- fmt.Sprintf("any(t.price['%s']) as price", currency),
- fmt.Sprintf("any(t.compare_at_price['%s']) as compare_at_price", currency),
- },
- clause, args...)
- }
- )
|