initial commit
This commit is contained in:
42
src/scripts/create-test-user-with-theme.ts
Normal file
42
src/scripts/create-test-user-with-theme.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { getDataSource, User } from '../lib/database';
|
||||
|
||||
async function createTestUser() {
|
||||
try {
|
||||
// Initialize database connection
|
||||
const dataSource = await getDataSource();
|
||||
const userRepository = dataSource.getRepository(User);
|
||||
|
||||
// Check if test user already exists
|
||||
const existingUser = await userRepository.findOne({
|
||||
where: { username: 'admin' }
|
||||
});
|
||||
|
||||
if (existingUser) {
|
||||
console.log('Test user already exists');
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a new user
|
||||
const user = new User();
|
||||
user.username = 'admin';
|
||||
user.password = 'password';
|
||||
user.theme = 'system'; // Default to system theme
|
||||
|
||||
// Hash the password
|
||||
await user.hashPassword();
|
||||
|
||||
// Save the user
|
||||
await userRepository.save(user);
|
||||
|
||||
console.log('Test user created successfully');
|
||||
} catch (error) {
|
||||
console.error('Error creating test user:', error);
|
||||
} finally {
|
||||
// Close the connection
|
||||
const dataSource = await getDataSource();
|
||||
await dataSource.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the function
|
||||
createTestUser();
|
||||
37
src/scripts/create-test-user.ts
Normal file
37
src/scripts/create-test-user.ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { getDataSource, User } from '../lib/database';
|
||||
|
||||
async function createTestUser() {
|
||||
try {
|
||||
console.log('Connecting to database...');
|
||||
const dataSource = await getDataSource();
|
||||
|
||||
console.log('Checking if test user exists...');
|
||||
const userRepository = dataSource.getRepository(User);
|
||||
const existingUser = await userRepository.findOne({ where: { username: 'admin' } });
|
||||
|
||||
if (existingUser) {
|
||||
console.log('Test user already exists.');
|
||||
await userRepository.delete({ id: existingUser.id });
|
||||
console.log('Deleted existing test user.');
|
||||
}
|
||||
|
||||
console.log('Creating test user...');
|
||||
const user = new User();
|
||||
user.username = 'admin';
|
||||
user.password = 'password';
|
||||
|
||||
// Hash the password
|
||||
await user.hashPassword();
|
||||
|
||||
// Save the user
|
||||
await userRepository.save(user);
|
||||
|
||||
console.log('Test user created successfully.');
|
||||
} catch (error) {
|
||||
console.error('Error creating test user:', error);
|
||||
} finally {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
createTestUser();
|
||||
91
src/scripts/reset-database.ts
Normal file
91
src/scripts/reset-database.ts
Normal file
@ -0,0 +1,91 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { getDataSource, User, Post } from '../lib/database';
|
||||
|
||||
async function resetDatabase() {
|
||||
try {
|
||||
console.log('Resetting database...');
|
||||
|
||||
// Path to the SQLite database file
|
||||
const dbPath = path.join(process.cwd(), 'data', 'database.sqlite');
|
||||
|
||||
// Check if the database file exists
|
||||
if (fs.existsSync(dbPath)) {
|
||||
console.log('Deleting existing database file...');
|
||||
// Delete the database file
|
||||
fs.unlinkSync(dbPath);
|
||||
console.log('Database file deleted.');
|
||||
}
|
||||
|
||||
// Initialize a new database connection
|
||||
// This will create a new database file with the updated schema
|
||||
console.log('Initializing new database...');
|
||||
const dataSource = await getDataSource();
|
||||
|
||||
// Create a test user
|
||||
console.log('Creating test user...');
|
||||
const userRepository = dataSource.getRepository(User);
|
||||
|
||||
const user = new User();
|
||||
user.username = 'admin';
|
||||
user.password = 'password';
|
||||
user.theme = 'system'; // Default to system theme
|
||||
|
||||
// Hash the password
|
||||
await user.hashPassword();
|
||||
|
||||
// Save the user
|
||||
await userRepository.save(user);
|
||||
|
||||
console.log('Test user created successfully.');
|
||||
console.log('Username: admin');
|
||||
console.log('Password: password');
|
||||
|
||||
// Create a sample post
|
||||
console.log('Creating sample post...');
|
||||
const postRepository = dataSource.getRepository(Post);
|
||||
|
||||
const post = new Post();
|
||||
post.title = 'Welcome to KantanCMS';
|
||||
post.content = JSON.stringify({
|
||||
time: new Date().getTime(),
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Welcome to KantanCMS',
|
||||
level: 2
|
||||
}
|
||||
},
|
||||
{
|
||||
type: 'paragraph',
|
||||
data: {
|
||||
text: 'This is a sample post created by the database reset script. You can edit or delete this post from the admin panel.'
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
post.user = user;
|
||||
|
||||
await postRepository.save(post);
|
||||
|
||||
console.log('Sample post created successfully.');
|
||||
console.log('Database reset complete!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error resetting database:', error);
|
||||
} finally {
|
||||
// Close the connection
|
||||
try {
|
||||
const dataSource = await getDataSource();
|
||||
if (dataSource.isInitialized) {
|
||||
await dataSource.destroy();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error closing database connection:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run the function
|
||||
resetDatabase();
|
||||
Reference in New Issue
Block a user