fix: update seed.ts

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-08-31 13:39:51 +02:00 committed by Yannick Bungers
parent 28be215aad
commit 53f5713905

View file

@ -10,6 +10,7 @@ import { Author } from './authors/author.entity';
import { Group } from './groups/group.entity';
import { HistoryEntry } from './history/history-entry.entity';
import { Identity } from './identity/identity.entity';
import { ProviderType } from './identity/provider-type.enum';
import { MediaUpload } from './media/media-upload.entity';
import { Note } from './notes/note.entity';
import { Tag } from './notes/tag.entity';
@ -19,6 +20,7 @@ import { Edit } from './revisions/edit.entity';
import { Revision } from './revisions/revision.entity';
import { Session } from './users/session.entity';
import { User } from './users/user.entity';
import { hashPassword } from './utils/password';
/**
* This function creates and populates a sqlite db for manual testing
@ -47,6 +49,7 @@ createConnection({
dropSchema: true,
})
.then(async (connection) => {
const password = 'test_password';
const users = [];
users.push(User.create('hardcoded', 'Test User 1'));
users.push(User.create('hardcoded_2', 'Test User 2'));
@ -59,6 +62,9 @@ createConnection({
for (let i = 0; i < 3; i++) {
const author = connection.manager.create(Author, Author.create(1));
const user = connection.manager.create(User, users[i]);
const identity = Identity.create(user, ProviderType.LOCAL);
identity.passwordHash = await hashPassword(password);
connection.manager.create(Identity, identity);
author.user = user;
const revision = Revision.create(
'This is a test note',
@ -70,23 +76,48 @@ createConnection({
notes[i].userPermissions = [];
notes[i].groupPermissions = [];
user.ownedNotes = [notes[i]];
await connection.manager.save([notes[i], user, revision, edit, author]);
await connection.manager.save([
notes[i],
user,
revision,
edit,
author,
identity,
]);
}
const foundUser = await connection.manager.findOne(User);
if (!foundUser) {
throw new Error('Could not find freshly seeded user. Aborting.');
const foundUsers = await connection.manager.find(User);
if (!foundUsers) {
throw new Error('Could not find freshly seeded users. Aborting.');
}
const foundNote = await connection.manager.findOne(Note);
if (!foundNote) {
throw new Error('Could not find freshly seeded note. Aborting.');
const foundNotes = await connection.manager.find(Note);
if (!foundNotes) {
throw new Error('Could not find freshly seeded notes. Aborting.');
}
if (!foundNote.alias) {
throw new Error('Could not find alias of freshly seeded note. Aborting.');
for (const note of foundNotes) {
if (!note.alias) {
throw new Error(
'Could not find alias of freshly seeded notes. Aborting.',
);
}
}
for (const user of foundUsers) {
console.log(
`Created User '${user.userName}' with password '${password}'`,
);
}
for (const note of foundNotes) {
console.log(`Created Note '${note.alias ?? ''}'`);
}
for (const user of foundUsers) {
for (const note of foundNotes) {
const historyEntry = HistoryEntry.create(user, note);
await connection.manager.save(historyEntry);
console.log(
`Created HistoryEntry for user '${user.userName}' and note '${
note.alias ?? ''
}'`,
);
}
}
const historyEntry = HistoryEntry.create(foundUser, foundNote);
await connection.manager.save(historyEntry);
console.log(`Created User '${foundUser.userName}'`);
console.log(`Created Note '${foundNote.alias}'`);
console.log(`Created HistoryEntry`);
})
.catch((error) => console.log(error));