Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,366 changes: 680 additions & 686 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"license": "ISC",
"dependencies": {
"@apollo/client": "^3.7.16",
"@terminusdb/terminusdb-client": "^10.0.30",
"@terminusdb/terminusdb-client": "^10.0.31",
"@types/swagger-ui-express": "^4.1.3",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
Expand Down
33 changes: 18 additions & 15 deletions src/api/addContextMiddle.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { NextFunction, Request, Response } from "express"


const logger = {
error : function (message:string){
import {Logger} from './core/typeDef'
// I have to add a logger strategy
const logger:Logger = {
error : function (...message:string[]){
const dd = new Date()
console.log('ERROR', dd.toISOString(), message)
const messageStr = Array.isArray(message) ? message.join(" ") : ''
console.log('ERROR', dd.toISOString(), messageStr)
},
debug : function (message:string){
debug : function (...message:string[]){
const dd = new Date()
console.log('DEBUG', dd.toISOString(), message)
const messageStr = Array.isArray(message) ? message.join(" ") : ''
console.log('DEBUG', dd.toISOString(), messageStr)
},
info : function (message:string){
info : function (...message:string[]){
const dd = new Date()
console.log('INFO', dd.toISOString(), message)
const messageStr = Array.isArray(message) ? message.join(" ") : ''
console.log('INFO', dd.toISOString(), messageStr)
},
warning : function (message:string){
warn : function (...message:string[]){
const dd = new Date()
console.log('WARNING', dd.toISOString(), message)
const messageStr = Array.isArray(message) ? message.join(" ") : ''
console.log('WARNING', dd.toISOString(), messageStr)
}
}

export const addContextMiddle = function (req:Request, res:Response, next:NextFunction) {
var dd = new Date()
console.log("addContextMiddle",dd.toISOString())
var dd = new Date()
if(req.url.startsWith("/api-docs")){
return next();
}
Expand All @@ -34,10 +37,10 @@ export const addContextMiddle = function (req:Request, res:Response, next:NextFu
const base64Url = auth.split('Basic')[1]
const basicDecode = atob(base64Url)
const basicArr = basicDecode.split(':')
req.context = {user : basicArr[0], password : basicArr[1]}//, logger }
req.context = {user : basicArr[0], password : basicArr[1],logger:logger}
//to be review
}catch(err:any){
console.log(err.message)
logger.error("addContextMiddle", err.message)
return res.status(401).send("Unauthorized")
}
next()
Expand Down
56 changes: 15 additions & 41 deletions src/api/core/ChangeRequestDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,50 @@ import { Request } from "express"
import * as typeDef from "./typeDef"
import {ApiError} from "./ApiError"
import * as IndexClient from './IndexClient'
import * as settings from './settings'

import dbSchema from '../../change_request_schema.json'
import { doc } from "@terminusdb/terminusdb-client/dist/typescript/lib/woql"
//import {v4 as uuidv4} from 'uuid';
const { v4: uuidv4 } = require('uuid');

const endpoint :string = process.env.SERVER_ENDPOINT || "http://127.0.0.1:6363"
const key = process.env.USER_KEY || "root"
const CROrg = process.env.CR_TEAM_NAME || "terminusCR"
const user = process.env.USER_NAME || "admin"


const logger = {
error : function (message:string){
const dd = new Date()
console.log('ERROR', dd.toISOString(), message)
},
debug : function (message:string){
const dd = new Date()
console.log('DEBUG', dd.toISOString(), message)
},
info : function (message:string){
const dd = new Date()
console.log('INFO', dd.toISOString(), message)
},
warning : function (message:string){
const dd = new Date()
console.log('WARNING', dd.toISOString(), message)
}
}
//const endpoint :string = process.env.SERVER_ENDPOINT || "http://127.0.0.1:6363"
//const key = process.env.USER_KEY || "root"
//const CROrg = process.env.CR_TEAM_NAME || "terminusCR"
//const user = process.env.USER_NAME || "admin"
class ChangeRequestDB {
client: WOQLClient;
accessControl: AccessControl;
request : Request
user : string | undefined
password : string | undefined
orgName : string | undefined
dbName : string | undefined
changeRequestDbName: string | undefined
logger : Object
logger : typeDef.Logger
endStatus:any = { Assigned: true, Error: true }
availableStatus:any = { Assigned: true, Error: true, Progress: true }
errorMessage:any = {'api:UnknownDatabase':true,'api:NoUniqueIdForOrganizationName':true}

constructor(req : Request) {
this.request = req
this.password = req.context.password
this.user = req.context.user
// user that has admin privileges for the terminuscms team
this.client = new TerminusClient.WOQLClient(endpoint, { key: key, user: user })
this.client.organization(CROrg)
this.client = new TerminusClient.WOQLClient(settings.endpoint, { key:settings.key, user: settings.user })
this.client.organization(settings.CROrg)
this.orgName = req.params.org
this.dbName = req.params.db
this.logger = logger//req.context.logger
this.logger = req.context.logger
// every dataproduct has the related change_request database
this.changeRequestDbName = `${TerminusClient.UTILS.encodeURISegment(this.orgName)}__${this.dbName}__CR`
this.accessControl = new AccessControl(endpoint, { key: key, user: user })
this.client.db(this.changeRequestDbName)
}

// check if the user is authorized to see this change request db
// maybe I can add a capability at database level
// admin create the db
// and add the capability

async getChangeRequests (changeId: String | false = false, as_list = true) {
const params : DocParamsGet = { type: 'ChangeRequest', as_list: as_list}
if (changeId) {
Expand All @@ -77,21 +58,14 @@ class ChangeRequestDB {
return result
} catch (err:any) {
// if there is no change request we return an empty array
if (typeof err.data === 'object' && err.data['api:error'] && err.data['api:error']['@type'] === 'api:UnknownDatabase') {
if (typeof err.data === 'object' && err.data['api:error']
&& this.errorMessage[err.data['api:error']['@type']]) {
return []
}
throw err
}
}

async createTeam(){
try{
await this.accessControl.createOrganization(CROrg)
}catch(err){
// the team already exists
}
}

async createCRDatabase(){
try {
if(this.changeRequestDbName){
Expand All @@ -106,7 +80,7 @@ class ChangeRequestDB {
} catch (err:any) {

// the database already exists
//this.logger.debug(`the change request db already exists ${err.message}`)
this.logger.debug(`the change request db already exists ${err.message}`)
}
}

Expand Down Expand Up @@ -162,7 +136,7 @@ class ChangeRequestDB {

// I'm using the current user to connect with the database that is going to be modified
connectWithCurrentUser () {
const tmpClient = new TerminusClient.WOQLClient(endpoint, { user: this.user, organization: this.orgName })
const tmpClient = new TerminusClient.WOQLClient(settings.endpoint, { user: this.user, organization: this.orgName })
// I remove the basic authentication type header
tmpClient.localAuth({ user: this.user, type: "basic", key: this.password || '' })
tmpClient.db(this.dbName)
Expand Down
36 changes: 5 additions & 31 deletions src/api/core/GraphqlTableConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ApolloClient, InMemoryCache, gql, concat,HttpLink,ApolloLink } from '@apollo/client/core';
import { Request } from "express"
import TerminusClient from '@terminusdb/terminusdb-client'
import { AdvancedSearchField } from './typeDef'
import { AdvancedSearchField,Logger } from './typeDef'
const endpoint = process.env.SERVER_ENDPOINT || ""
const key = process.env.USER_KEY || ""
const user = process.env.USER_NAME || ""
Expand Down Expand Up @@ -137,25 +137,6 @@ const advancedSearchMatchType : Record<AdvancedKeyType,AdvancedSearchField> = {
}
}

const logger = {
error : function (message:string){
const dd = new Date()
console.log('ERROR', dd.toISOString(), message)
},
debug : function (message:string){
const dd = new Date()
console.log('DEBUG', dd.toISOString(), message)
},
info : function (message:string){
const dd = new Date()
console.log('INFO', dd.toISOString(), message)
},
warning : function (message:string){
const dd = new Date()
console.log('WARNING', dd.toISOString(), message)
}
}

const query = gql(`{__schema
{ types{...FullType}
directives{name description locations args{...InputValue}}}}
Expand All @@ -173,7 +154,7 @@ class GraphqlTableConfig {
password : string | undefined
orgName : string | undefined
dbName : string | undefined
logger : typeof logger
logger : Logger

constructor(req : Request) {
this.request = req
Expand All @@ -182,7 +163,7 @@ class GraphqlTableConfig {
// user that has admin privileges for the terminuscms team
this.orgName = req.params.org
this.dbName = req.params.db
this.logger = logger//req.context.logger
this.logger = req.context.logger
// every dataproduct has the related change_request database
}

Expand Down Expand Up @@ -219,7 +200,6 @@ class GraphqlTableConfig {
const result = await client.query({ query })
const schemaDoc = await woqlClient.getDocument({ graph_type: 'schema', as_list: true })
const tableConfig = this.formatSchema(result, schemaDoc)
//this.logger.debug(JSON.stringify(tableConfig, null, 4))
return tableConfig
}

Expand All @@ -243,10 +223,6 @@ class GraphqlTableConfig {

advSearchFilterFormatter (typesObj:any, fieldKey:string, fieldTypeValue:AdvancedKeyType, isList:boolean) {
try{
if(fieldKey==="homeworld"){
console.log("stop")

}
let advField:AdvancedSearchField = advancedSearchMatchType[fieldTypeValue] || {}
if (advField !== undefined) {
advField = JSON.parse(JSON.stringify(advField))
Expand All @@ -260,7 +236,7 @@ class GraphqlTableConfig {
advField.typevalue = fieldTypeValue
return advField
}catch(err:any){
console.log("advSearchFilterFormatter",fieldKey,fieldTypeValue)
this.logger.error("advSearchFilterFormatter",fieldKey,fieldTypeValue)
throw err
}
}
Expand All @@ -282,7 +258,6 @@ class GraphqlTableConfig {

formatSchema(result:any, schemaDoc:any) {
const types: GraphQLSchemaType[] = result.data.__schema.types
//console.log(types)
const typesObj01:any = types[0]
const name = typesObj01.name
const typesObj:ObjectMap = {}
Expand All @@ -291,7 +266,6 @@ class GraphqlTableConfig {
const itemMatch = schemaDoc.find((doc:any) => doc['@id'] === name ||
`${doc['@id']}_Filter` === name || `${doc['@id']}_Ordering` === name)
if (itemMatch) {
console.log("TYPE NAME",name , kind)
const ordering = itemMatch['@metadata'] && itemMatch['@metadata'].order_by ? { order_by: itemMatch['@metadata'].order_by } : {}
typesObj[name] = { type: kind, ...ordering }
// to be review
Expand Down Expand Up @@ -481,7 +455,7 @@ class GraphqlTableConfig {
objQuery[key] = {
query: `
query ${key}Query($offset: Int, $limit: Int ${filter} ${order} ){
${key}(offset: $offset, limit: $limit ${filterVar} ${orderVar}){
${key}(include_children:false, offset: $offset, limit: $limit ${filterVar} ${orderVar}){
_id
${fieldKeysArr.join(' \n ')}
}
Expand Down
14 changes: 0 additions & 14 deletions src/api/core/Person.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/api/core/index.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/api/core/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const endpoint :string = process.env.SERVER_ENDPOINT || "http://127.0.0.1:6363"
export const key = process.env.USER_KEY || "root"
export const CROrg = process.env.CR_TEAM_NAME || "terminusCR"
export const user = process.env.USER_NAME || "admin"

1 change: 1 addition & 0 deletions src/api/core/typeDef.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

export type ChangeReqStatus = "Submitted" | "Open" | "Rejected" | "Merged"
export type Logger = {error:Function, debug:Function, info:Function, warn:Function}
export type MessageObj = {"@type" : "Message",
"text":string,
"timestamp":number,
Expand Down
2 changes: 1 addition & 1 deletion src/api/custom.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare namespace Express {
export interface Request {
context: {user:string ,password:string}
context: {user:string ,password:string,logger:any}
}
}
8 changes: 4 additions & 4 deletions src/api/paths/api/changes/{org}/{db}/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import ChangeRequestDB from "../../../../../core/ChangeRequestDB";

export const GET: Operation = async(req:Request, res:Response) =>{
try{
var dd = new Date()
console.log("ChangeRequestDB",dd.toISOString())
//var dd = new Date()
//console.log("ChangeRequestDB",dd.toISOString())
const changeR = new ChangeRequestDB(req)
const result = await changeR.getChangeRequests()
var dd1 = new Date()
console.log("ChangeRequestDB",dd1.toISOString())
//var dd1 = new Date()
//console.log("ChangeRequestDB",dd1.toISOString())
res.status(200).json(result);
}catch(err:any){
console.log(err.message)
Expand Down
21 changes: 19 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import bodyParser from "body-parser";
import cors from "cors";
import {addContextMiddle} from "./api/addContextMiddle"
import { createProxyMiddleware } from 'http-proxy-middleware'

import TerminusClient , {WOQLClient,WOQL, AccessControl} from "@terminusdb/terminusdb-client"
declare namespace Express {
export interface Request {
context?: object
Expand Down Expand Up @@ -66,7 +66,24 @@ app.use(
);

app.use(addContextMiddle)
app.listen(3035);
app.listen(3035,function(){
// when we start the server we check if the terminusCR team already exists
// if it does not exists we'll create it
const endpoint :string = process.env.SERVER_ENDPOINT || "http://127.0.0.1:6363"
const key = process.env.USER_KEY || "root"
const CROrg = process.env.CR_TEAM_NAME || "terminusCR"
const user = process.env.USER_NAME || "admin"
const accessControl = new AccessControl(endpoint, { key: key, user: user })

accessControl.createOrganization(CROrg).then(result=>{
console.log("The Change Request team has been created")
}).catch((err:any)=>{
if (typeof err.data === 'object' && err.data['api:error']
&& err.data['api:error']['@type'] === "api:NoUniqueIdForOrganizationName") {
console.log("The Change Request team already exists")
}
})
});

console.log("App running on port http://localhost:3035");
console.log(
Expand Down