Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create collections specific for ddo versions #225

Merged
merged 19 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions schemas/op_ddo_v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "op_ddo_v4.1.0",
"enable_nested_fields": true,
"fields": [{ "name": ".*", "type": "auto", "optional": true }]
}
5 changes: 5 additions & 0 deletions schemas/op_ddo_v3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "op_ddo_v4.3.0",
"enable_nested_fields": true,
"fields": [{ "name": ".*", "type": "auto", "optional": true }]
}
5 changes: 5 additions & 0 deletions schemas/op_ddo_v5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "op_ddo_v4.5.0",
"enable_nested_fields": true,
"fields": [{ "name": ".*", "type": "auto", "optional": true }]
}
2 changes: 1 addition & 1 deletion src/components/core/utils/validateDdoHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
CORE_LOGGER.logMessage(`Can't find schema ${version}`, true)
return
}
const path = `../../../../schemas/v4/${version}.ttl`
const path = `../../../../schemas/${version}.ttl`
// Use fileURLToPath to convert the URL to a file path
const currentModulePath = fileURLToPath(import.meta.url)

Expand Down Expand Up @@ -58,7 +58,7 @@
}

function isIsoFormat(dateString: string): boolean {
const isoDateRegex = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z)?$/

Check warning on line 61 in src/components/core/utils/validateDdoHandler.ts

View workflow job for this annotation

GitHub Actions / lint

Unsafe Regular Expression
return isoDateRegex.test(dateString)
}

Expand Down Expand Up @@ -117,7 +117,7 @@
const filename = new URL(schemaFilePath, import.meta.url)
const dataset = rdfDataset.dataset()
try {
const contents = await readFile(filename, { encoding: 'utf8' })

Check warning on line 120 in src/components/core/utils/validateDdoHandler.ts

View workflow job for this annotation

GitHub Actions / lint

Found readFile from package "node:fs/promises" with non literal argument at index 0
const parser = new Parser()
const quads = parser.parse(contents)
quads.forEach((quad: Quad) => {
Expand Down
10 changes: 5 additions & 5 deletions src/components/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ export class Database {
configureCustomDBTransport(this, DATABASE_LOGGER)
}

this.ddo = await new DdoDatabase(config, schemas.ddoSchemas)
this.nonce = await new NonceDatabase(config, schemas.nonceSchemas)
this.indexer = await new IndexerDatabase(config, schemas.indexerSchemas)
this.logs = await new LogDatabase(config, schemas.logSchemas)
this.order = await new OrderDatabase(config, schemas.orderSchema)
this.ddo = await new DdoDatabase(this.config, schemas.ddoSchemas)
mariacarmina marked this conversation as resolved.
Show resolved Hide resolved
this.nonce = await new NonceDatabase(this.config, schemas.nonceSchemas)
this.indexer = await new IndexerDatabase(this.config, schemas.indexerSchemas)
this.logs = await new LogDatabase(this.config, schemas.logSchemas)
this.order = await new OrderDatabase(this.config, schemas.orderSchema)
return this
})() as unknown as Database
}
Expand Down
51 changes: 44 additions & 7 deletions src/components/database/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
import { TypesenseCollectionCreateSchema } from '../../@types/index.js'
import fs from 'fs'
import path, { dirname, resolve } from 'path'
import { fileURLToPath } from 'url'
import { DATABASE_LOGGER } from '../../utils/logging/common.js'
import { LOG_LEVELS_STR } from '../../utils/logging/Logger.js'

export function readJsonSchemas(): any[] {
const jsonDocuments: any[] = []
mariacarmina marked this conversation as resolved.
Show resolved Hide resolved
const pathToSchemaDir: string = '../../../schemas'
const currentModulePath = fileURLToPath(import.meta.url)

// Use dirname to get the directory name
const currentDirectory = dirname(currentModulePath)
const schemaFilePath = resolve(currentDirectory, pathToSchemaDir)
const jsonFiles = fs
mariacarmina marked this conversation as resolved.
Show resolved Hide resolved
.readdirSync(schemaFilePath)
.filter((file) => path.extname(file) === '.json')

if (!jsonFiles || jsonFiles === undefined) {
mariacarmina marked this conversation as resolved.
Show resolved Hide resolved
DATABASE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`JSON mappings could not be read. Possibly the JSON format mappings for Typesense DB are missing or invalid.`,
true
)
return []
}
jsonFiles.forEach((file) => {
try {
// eslint-disable-next-line security/detect-non-literal-fs-filename
const fileData = fs.readFileSync(path.join(schemaFilePath, file), 'utf-8')
const jsonFile = JSON.parse(fileData.toString())
jsonDocuments.push(jsonFile)
} catch (err) {
DATABASE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`Error loading DDO schema from ${path.join(pathToSchemaDir, file)}: ${err}`,
true
)
}
})
return jsonDocuments
}

export type Schema = TypesenseCollectionCreateSchema
export type Schemas = {
Expand All @@ -8,14 +50,9 @@ export type Schemas = {
logSchemas: Schema
orderSchema: Schema
}
const ddoSchemas = readJsonSchemas()
export const schemas: Schemas = {
ddoSchemas: [
{
name: 'ddo_v0.1',
enable_nested_fields: true,
fields: [{ name: '.*', type: 'auto', optional: true }] // optional: true for turning the full version DDO into short one for states DEPRECATED and REVOKED
}
],
ddoSchemas,
nonceSchemas: {
name: 'nonce',
enable_nested_fields: true,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ let dbconn = null
if (config.dbConfig?.url) {
// once we create a database instance, we check the environment and possibly add the DB transport
// after that, all loggers will eventually have it too (if in production/staging environments)
// it creates dinamically DDO schemas
dbconn = await new Database(config.dbConfig)
} else {
config.hasIndexer = false
Expand Down
Loading