- update to AdonisJS 6

This commit is contained in:
Kaimbacher 2024-03-14 20:25:27 +01:00
parent f828ca4491
commit cb51a4136f
167 changed files with 21485 additions and 21212 deletions

View file

@ -1,44 +1,34 @@
import { BaseCommand, flags } from '@adonisjs/core/build/standalone';
// import Logger from '@ioc:Adonis/Core/Logger';
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces.js';
import { create } from 'xmlbuilder2';
import Dataset from 'App/Models/Dataset';
import XmlModel from 'App/Library/XmlModel';
import Dataset from '#app/Models/Dataset';
import XmlModel from '#app/Library/XmlModel';
import { readFileSync } from 'fs';
import { transform } from 'saxon-js';
import SaxonJS from 'saxon-js';
import { Client } from '@opensearch-project/opensearch';
import { getDomain } from 'App/Utils/utility-functions';
import { getDomain } from '#app/Utils/utility-functions';
import { BaseCommand, flags } from '@adonisjs/core/ace';
import { CommandOptions } from '@adonisjs/core/types/ace';
const opensearchNode = process.env.OPENSEARCH_HOST || 'localhost';
const client = new Client({ node: `http://${opensearchNode}` }); // replace with your OpenSearch endpoint
export default class IndexDatasets extends BaseCommand {
public static commandName = 'index:datasets';
public static description = 'Index datasets based on publish_id';
static commandName = 'index:datasets';
static description = 'Index datasets based on publish_id';
@flags.number({ alias: 'p' })
public publish_id: number;
public static settings = {
/**
* Set the following value to true, if you want to load the application
* before running the command. Don't forget to call `node ace generate:manifest`
* afterwards.
*/
static options: CommandOptions = {
loadApp: true,
/**
* Set the following value to true, if you want this command to keep running until
* you manually decide to exit the process. Don't forget to call
* `node ace generate:manifest` afterwards.
*/
stayAlive: false,
staysAlive: false,
};
public async run() {
async run() {
this.logger.info('Hello world!');
// const { default: Dataset } = await import('App/Models/Dataset');
const datasets = await this.getDatasets();
const { default: Dataset } = await import('#app/Models/Dataset');
const datasets = await Dataset.query().where('server_state', 'published').exec(); //this.getDatasets();
const proc = readFileSync('public/assets2/solr.sef.json');
const index_name = 'tethys-records';
@ -50,13 +40,15 @@ export default class IndexDatasets extends BaseCommand {
}
}
private async getDatasets(): Promise<Dataset[]> {
const query = Dataset.query().preload('xmlCache').where('server_state', 'published');
if (this.publish_id) {
query.where('publish_id', this.publish_id);
}
return await query;
}
// private async getDatasets(): Promise<any[]> {
// // const { default: Dataset } = await import('#app/Models/Dataset');
// // const Dataset = (await import('#app/Models/Dataset')).default
// const query = Dataset.query().where('server_state', 'published');
// if (this.publish_id) {
// query.where('publish_id', this.publish_id);
// }
// return await query;
// }
private async indexDocument(dataset: Dataset, index_name: string, proc: Buffer): Promise<void> {
try {
@ -75,14 +67,14 @@ export default class IndexDatasets extends BaseCommand {
}
}
private async getJsonString(dataset, proc) {
private async getJsonString(dataset: Dataset, proc: Buffer) {
let xml = create({ version: '1.0', encoding: 'UTF-8', standalone: true }, '<root></root>');
const datasetNode = xml.root().ele('Dataset');
await this.createXmlRecord(dataset, datasetNode);
const xmlString = xml.end({ prettyPrint: false });
try {
const result = await transform({
const result = await SaxonJS.transform({
stylesheetText: proc,
destination: 'serialized',
sourceText: xmlString,

View file

@ -1,8 +1,9 @@
import { BaseCommand } from '@adonisjs/core/build/standalone';
import crypto from 'crypto';
import fs from 'fs';
// import Config from '@ioc:Adonis/Core/Config';
import Logger from '@ioc:Adonis/Core/Logger';
import logger from '@adonisjs/core/services/logger';
import { BaseCommand } from "@adonisjs/core/ace";
import { CommandOptions } from "@adonisjs/core/types/ace";
export default class ValidateChecksum extends BaseCommand {
/**
@ -14,26 +15,14 @@ export default class ValidateChecksum extends BaseCommand {
* Command description is displayed in the "help" output
*/
public static description = '';
public static settings = {
/**
* Set the following value to true, if you want to load the application
* before running the command. Don't forget to call `node ace generate:manifest`
* afterwards.
*/
loadApp: true,
/**
* Set the following value to true, if you want this command to keep running until
* you manually decide to exit the process. Don't forget to call
* `node ace generate:manifest` afterwards.
*/
stayAlive: false,
};
static options: CommandOptions = {
loadApp: true,
staysAlive: false,
};
public async run() {
// this.logger.info('Hello world!')
const { default: File } = await import('App/Models/File');
const { default: File } = await import('#app/Models/File');
// const { default: HashValue } = await (await (import ('App/Models/HashValue')));
// query all published files from database:
@ -55,21 +44,21 @@ export default class ValidateChecksum extends BaseCommand {
calculatedMd5FileHash = await this.checksumFile(filePath, 'md5');
} catch (exception) {
// this.logger.error(exception.message);
Logger.error(exception.message);
logger.error(exception.message);
continue;
}
if (hashValue['md5'] === calculatedMd5FileHash) {
Logger.info(`File id ${file.id} OK: stored md5 checksum: ${calculatedMd5FileHash}, same control md5 checksum: ${hashValue['md5']}`);
logger.info(`File id ${file.id} OK: stored md5 checksum: ${calculatedMd5FileHash}, same control md5 checksum: ${hashValue['md5']}`);
} else {
Logger.error(
logger.error(
`File id ${file.id}: stored md5 checksum: ${calculatedMd5FileHash}, control md5 checksum: ${hashValue['md5']}`,
);
}
}
}
private async checksumFile(path, hashName = 'md5'): Promise<string> {
private async checksumFile(path: string, hashName = 'md5'): Promise<string> {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(hashName);
const stream = fs.createReadStream(path);

View file

@ -1,19 +0,0 @@
import { listDirectoryFiles } from '@adonisjs/core/build/standalone';
import Application from '@ioc:Adonis/Core/Application';
/*
|--------------------------------------------------------------------------
| Exporting an array of commands
|--------------------------------------------------------------------------
|
| Instead of manually exporting each file from this directory, we use the
| helper `listDirectoryFiles` to recursively collect and export an array
| of filenames.
|
| Couple of things to note:
|
| 1. The file path must be relative from the project root and not this directory.
| 2. We must ignore this file to avoid getting into an infinite loop
|
*/
export default listDirectoryFiles(__dirname, Application.appRoot, ['./commands/index']);

13
commands/index_test.ts Normal file
View file

@ -0,0 +1,13 @@
import { BaseCommand } from '@adonisjs/core/ace'
import type { CommandOptions } from '@adonisjs/core/types/ace'
export default class IndexTest extends BaseCommand {
static commandName = 'index:test'
static description = ''
static options: CommandOptions = {}
async run() {
this.logger.info('Hello world from "IndexTest"')
}
}