diff --git a/src/datasources/BaseSQLDataSource.ts b/src/datasources/BaseSQLDataSource.ts index e1b579b..2d45bd5 100644 --- a/src/datasources/BaseSQLDataSource.ts +++ b/src/datasources/BaseSQLDataSource.ts @@ -2,6 +2,38 @@ import { SQLDataSource } from 'datasource-sql'; const { InMemoryLRUCache } = require('apollo-server-caching'); import crypto from 'crypto'; +const MINUTE = 60; + +export class RawBuilder { + _cache: any; + query: any; + constructor(knex, cache, query) { + this._cache = cache; + this.query = knex.raw(query); + } + + async cache(ttl: number) { + const cacheKey = crypto + .createHash('sha1') + .update(this.query.toString()) + .digest('base64'); + + return this._cache.get(cacheKey).then((entry) => { + if (entry) { + return Promise.resolve(JSON.parse(entry)); + } + + return this.query.then((rows) => { + if (rows) { + this._cache.set(cacheKey, JSON.stringify(rows), { ttl }); + } + + return Promise.resolve(rows); + }); + }); + } +} + export class BaseSQLDataSource extends SQLDataSource { cache: any; constructor(config) { @@ -13,24 +45,7 @@ export class BaseSQLDataSource extends SQLDataSource { return date.toISOString().split('T')[0]; } - async cacheQuery(ttl = 5, query) { - const cacheKey = crypto - .createHash('sha1') - .update(query.toString()) - .digest('base64'); - - return this.cache.get(cacheKey).then((entry) => { - if (entry) { - return Promise.resolve(JSON.parse(entry)); - } - - return query.then((rows) => { - if (rows) { - this.cache.set(cacheKey, JSON.stringify(rows), { ttl }); - } - - return Promise.resolve(rows); - }); - }); + cachedRaw(query: string): RawBuilder { + return new RawBuilder(this.knex, this.cache, query); } } diff --git a/src/datasources/category-api.ts b/src/datasources/category-api.ts index fff2793..87a48b5 100644 --- a/src/datasources/category-api.ts +++ b/src/datasources/category-api.ts @@ -59,17 +59,16 @@ WHERE product_category.product_id = ? } /** - * category keywords + * TODO: should we have these? + * category keywords and texts * -SELECT category_keyword.category_id, keywords.value -FROM category_keyword -JOIN keywords ON category_keyword.keyword_id = keywords.id; + SELECT category_keyword.category_id, keywords.value + FROM category_keyword + JOIN keywords ON category_keyword.keyword_id = keywords.id; - FROM categorydata cd - JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id - - -* - */ + FROM categorydata cd + JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id + * + */ } diff --git a/src/datasources/order-api.ts b/src/datasources/order-api.ts index 3de0728..94bd25b 100644 --- a/src/datasources/order-api.ts +++ b/src/datasources/order-api.ts @@ -132,13 +132,10 @@ export class OrderAPI extends BaseSQLDataSource { } async getOrderRowFieldMapping(): Promise { - // const fields = await this.knex.select('*').from('order-row_fields'); - const fieldsRes = await this.cacheQuery( - MINUTE * 10, - this.knex - .raw('SELECT * from "order-row_fields"') - .then((data) => data.rows), - ); + // const fields = await this.knex.select('*').from('order-row_fields'); doesn't work becuase of string case... + const fieldsRes = await this.cachedRaw('SELECT * from "order-row_fields"') + .cache(MINUTE * 60) + .then((data) => data.rows); return fieldsRes.reduce((obj, item) => { obj[item.fieldid] = item.name; diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index bbbd7b6..128a94f 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -16,9 +16,10 @@ export class ProductAPI extends SQLDataSource { super(config); } - async getMaterial(printId: number): Promise { - // SELECT materialid, material, (price / 100::float) as price FROM "product-materials" - } + // async getMaterial(printId: number): Promise { + // TODO: discuss if this is still needed. Anneli wants to remove the choice of standard, premium and self-adhesive + // SELECT materialid, material, (price / 100::float) as price FROM "product-materials" + // } getBlacklisting(row: any): Array { if (!row.blacklisting) { diff --git a/src/schema.ts b/src/schema.ts index 5060631..f3b9883 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -8,6 +8,9 @@ const typeDefs = gql` type Query { orders(filter: FilterInput!): OrderResult order(id: ID!): Order + """ + Will be replaced with a ProductsResult type similar to OrdersResult but we need to change in the similar-images code then + """ products(limit: Int, visible: Boolean, browsable: Boolean): [Product] product(id: Int!): Product categories: [Category]