Cleanup of code
This commit is contained in:
@@ -2,6 +2,38 @@ import { SQLDataSource } from 'datasource-sql';
|
|||||||
const { InMemoryLRUCache } = require('apollo-server-caching');
|
const { InMemoryLRUCache } = require('apollo-server-caching');
|
||||||
import crypto from 'crypto';
|
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 {
|
export class BaseSQLDataSource extends SQLDataSource {
|
||||||
cache: any;
|
cache: any;
|
||||||
constructor(config) {
|
constructor(config) {
|
||||||
@@ -13,24 +45,7 @@ export class BaseSQLDataSource extends SQLDataSource {
|
|||||||
return date.toISOString().split('T')[0];
|
return date.toISOString().split('T')[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
async cacheQuery(ttl = 5, query) {
|
cachedRaw(query: string): RawBuilder {
|
||||||
const cacheKey = crypto
|
return new RawBuilder(this.knex, this.cache, query);
|
||||||
.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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
SELECT category_keyword.category_id, keywords.value
|
||||||
FROM category_keyword
|
FROM category_keyword
|
||||||
JOIN keywords ON category_keyword.keyword_id = keywords.id;
|
JOIN keywords ON category_keyword.keyword_id = keywords.id;
|
||||||
|
|
||||||
|
|
||||||
FROM categorydata cd
|
FROM categorydata cd
|
||||||
JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id
|
JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id
|
||||||
|
*
|
||||||
|
*/
|
||||||
*
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -132,13 +132,10 @@ export class OrderAPI extends BaseSQLDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getOrderRowFieldMapping(): Promise<any> {
|
async getOrderRowFieldMapping(): Promise<any> {
|
||||||
// const fields = await this.knex.select('*').from('order-row_fields');
|
// const fields = await this.knex.select('*').from('order-row_fields'); doesn't work becuase of string case...
|
||||||
const fieldsRes = await this.cacheQuery(
|
const fieldsRes = await this.cachedRaw('SELECT * from "order-row_fields"')
|
||||||
MINUTE * 10,
|
.cache(MINUTE * 60)
|
||||||
this.knex
|
.then((data) => data.rows);
|
||||||
.raw('SELECT * from "order-row_fields"')
|
|
||||||
.then((data) => data.rows),
|
|
||||||
);
|
|
||||||
|
|
||||||
return fieldsRes.reduce((obj, item) => {
|
return fieldsRes.reduce((obj, item) => {
|
||||||
obj[item.fieldid] = item.name;
|
obj[item.fieldid] = item.name;
|
||||||
|
|||||||
@@ -16,9 +16,10 @@ export class ProductAPI extends SQLDataSource {
|
|||||||
super(config);
|
super(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMaterial(printId: number): Promise<any> {
|
// async getMaterial(printId: number): Promise<any> {
|
||||||
// SELECT materialid, material, (price / 100::float) as price FROM "product-materials"
|
// 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> {
|
getBlacklisting(row: any): Array<ProductBlacklist> {
|
||||||
if (!row.blacklisting) {
|
if (!row.blacklisting) {
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ const typeDefs = gql`
|
|||||||
type Query {
|
type Query {
|
||||||
orders(filter: FilterInput!): OrderResult
|
orders(filter: FilterInput!): OrderResult
|
||||||
order(id: ID!): Order
|
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]
|
products(limit: Int, visible: Boolean, browsable: Boolean): [Product]
|
||||||
product(id: Int!): Product
|
product(id: Int!): Product
|
||||||
categories: [Category]
|
categories: [Category]
|
||||||
|
|||||||
Reference in New Issue
Block a user