Skip to content

Commit

Permalink
Create collections specific for ddo versions (#225)
Browse files Browse the repository at this point in the history
* Created collections for diff ddo versions.

* Created ddo collections dinamically.

* tweak.

* Changed location.

* tweak for dir location.

* Added log.

* tweak.

* removed log.

* Renamed the collections.

* removed v4 folder.

* fix review.

* refactor readJsonMappings.

* modified return statement.
  • Loading branch information
mariacarmina authored Jan 30, 2024
1 parent be0cde4 commit 3bb25bf
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 13 deletions.
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 @@ export function getSchema(version: string = CURRENT_VERSION): string {
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
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)
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
53 changes: 46 additions & 7 deletions src/components/database/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
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(): TypesenseCollectionCreateSchema[] {
const jsonDocuments: TypesenseCollectionCreateSchema[] = []
const pathToSchemaDir: string = '../../../schemas'
const currentModulePath = fileURLToPath(import.meta.url)

try {
const currentDirectory = dirname(currentModulePath)
const schemaFilePath = resolve(currentDirectory, pathToSchemaDir)
const jsonFiles = fs
.readdirSync(schemaFilePath)
.filter((file) => path.extname(file) === '.json')

if (jsonFiles.length === 0) {
DATABASE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`No JSON files found in the schemas directory ${schemaFilePath}.`,
true
)
return []
} else {
jsonFiles.forEach((file) => {
// 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)
})
return jsonDocuments
}
} catch (error) {
DATABASE_LOGGER.log(
LOG_LEVELS_STR.LEVEL_ERROR,
`JSON mappings could not be loaded in database.
Error: ${error}`,
true
)
}
return []
}

export type Schema = TypesenseCollectionCreateSchema
export type Schemas = {
Expand All @@ -8,14 +52,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

0 comments on commit 3bb25bf

Please sign in to comment.