feat: import-dir script

This commit is contained in:
diced 2022-10-23 10:08:21 -07:00
parent a90130e8bf
commit 2bd5352fc5
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
5 changed files with 53 additions and 2 deletions

View file

@ -29,6 +29,7 @@ const { rm } = require('fs/promises');
'src/lib/datasources/Swift.ts',
'src/lib/datasource.ts',
'src/scripts/read-config.ts',
'src/scripts/import-dir.ts',
],
format: 'cjs',
resolveExtensions: ['.ts', '.js'],

View file

@ -17,7 +17,8 @@
"docker:run": "docker-compose up -d",
"docker:down": "docker-compose down",
"docker:build-dev": "docker-compose --file docker-compose.dev.yml up --build",
"scripts:read-config": "npm-run-all build:server && node dist/scripts/read-config"
"scripts:read-config": "npm-run-all build:server && node dist/scripts/read-config",
"scripts:import-dir": "npm-run-all build:server && node dist/scripts/import-dir"
},
"dependencies": {
"@dicedtomato/mantine-data-grid": "0.0.23",

View file

@ -364,7 +364,7 @@ export default function Layout({ children, props }) {
<Popover.Dropdown p={4} mr='md' sx={{ minWidth: '200px' }}>
<Stack spacing={2}>
<Menu.Label>
{user.username}{' '}
{user.username} ({user.id}){' '}
{user.administrator && user.username !== 'administrator' ? '(Administrator)' : ''}
</Menu.Label>
<MenuItemLink icon={<SettingsIcon />} href='/dashboard/manage'>

View file

@ -13,6 +13,7 @@ export interface User {
superAdmin: boolean;
oauth: boolean;
oauthProvider: 'github' | 'discord';
id: number;
}
export const userState = atom({

48
src/scripts/import-dir.ts Normal file
View file

@ -0,0 +1,48 @@
import datasource from '../lib/datasource';
import { readdir, stat } from 'fs/promises';
import config from '../lib/config';
import { migrations } from '../server/util';
import { PrismaClient } from '@prisma/client';
import { guess } from '../lib/mimes';
async function main() {
const directory = process.argv[2];
if (!directory) {
console.error('no directory specified');
process.exit(1);
}
const files = await readdir(directory);
const userId = Number(process.argv[3] ?? '1');
if (isNaN(userId)) {
console.error('invalid user id');
process.exit(1);
}
const data = [];
for (let i = 0; i !== files.length; ++i) {
const mime = await guess(files[i].split('.').pop());
data.push({
file: files[i],
mimetype: mime,
userId,
});
console.log(`Imported ${files[i]} (${mime} mimetype) to user ${userId}`);
}
process.env.DATABASE_URL = config.core.database_url;
await migrations();
const prisma = new PrismaClient();
console.log('Starting transaction to database..');
prisma.image.createMany({
data,
});
console.log('Finished transaction to database.');
}
main();