94 lines
2.5 KiB
TypeScript
94 lines
2.5 KiB
TypeScript
import sqlite3 from 'sqlite3';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
|
|
// Read JSON files
|
|
const users = JSON.parse(fs.readFileSync('./seed/user.json', 'utf-8'));
|
|
const apiKeys = JSON.parse(fs.readFileSync('./seed/apiKey.json', 'utf-8'));
|
|
const colors = JSON.parse(fs.readFileSync('./seed/color.json', 'utf-8'));
|
|
const categories = JSON.parse(fs.readFileSync('./seed/category.json', 'utf-8'));
|
|
|
|
// Connect to SQLite
|
|
const db = new sqlite3.Database('/home/ryan/Notes/lifetracker/lifetracker.db');
|
|
|
|
// Helper function to insert data
|
|
const insertData = (table, columns, values) => {
|
|
const placeholders = columns.map(() => '?').join(', ');
|
|
const sql = `INSERT INTO ${table} (${columns.join(', ')}) VALUES (${placeholders})`;
|
|
db.run(sql, values, (err) => {
|
|
if (err) {
|
|
console.error(`Error inserting into ${table}:`, err.message);
|
|
}
|
|
});
|
|
};
|
|
|
|
// Insert users
|
|
users.forEach(user => {
|
|
insertData(
|
|
'user',
|
|
['id', 'name', 'email', 'emailVerified', 'image', 'password', 'role'],
|
|
[
|
|
user.id,
|
|
user.name,
|
|
user.email,
|
|
user.emailVerified || null,
|
|
user.image || null,
|
|
user.password,
|
|
user.role || 'user'
|
|
]
|
|
);
|
|
});
|
|
|
|
// Insert API keys
|
|
apiKeys.forEach(apiKey => {
|
|
insertData(
|
|
'apiKey',
|
|
['id', 'keyHash', 'keyId', 'name', 'userId', 'createdAt'],
|
|
[
|
|
apiKey.id,
|
|
apiKey.keyHash,
|
|
apiKey.keyId,
|
|
apiKey.name,
|
|
apiKey.userId,
|
|
apiKey.createdAt || new Date().toISOString()
|
|
]
|
|
);
|
|
});
|
|
categories.forEach(category => {
|
|
insertData(
|
|
'category',
|
|
['id', 'code', 'colorId', 'createdAt', 'description', 'name', 'parentId', 'userId'],
|
|
[
|
|
category.id,
|
|
category.code,
|
|
category.colorId || null,
|
|
category.createdAt || new Date().toISOString(),
|
|
category.description || null,
|
|
category.name,
|
|
category.parentId || null,
|
|
category.userId
|
|
]
|
|
);
|
|
});
|
|
|
|
colors.forEach(color => {
|
|
insertData(
|
|
'color',
|
|
['id', 'name', 'hexcode', 'inverse', 'userId', 'createdAt'],
|
|
[
|
|
color.id,
|
|
color.name,
|
|
color.hexcode,
|
|
color.inverse || null,
|
|
color.userId,
|
|
color.createdAt || new Date().toISOString()
|
|
]
|
|
);
|
|
});
|
|
|
|
// Close the connection
|
|
db.close(() => {
|
|
console.log('Database population completed!');
|
|
});
|