Cleanup of code
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -132,13 +132,10 @@ export class OrderAPI extends BaseSQLDataSource {
|
||||
}
|
||||
|
||||
async getOrderRowFieldMapping(): Promise<any> {
|
||||
// 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;
|
||||
|
||||
@@ -16,9 +16,10 @@ export class ProductAPI extends SQLDataSource {
|
||||
super(config);
|
||||
}
|
||||
|
||||
async getMaterial(printId: number): Promise<any> {
|
||||
// SELECT materialid, material, (price / 100::float) as price FROM "product-materials"
|
||||
}
|
||||
// async getMaterial(printId: number): Promise<any> {
|
||||
// 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<ProductBlacklist> {
|
||||
if (!row.blacklisting) {
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user