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 David Mehren
parent 5a91662865
commit 46d03571c1
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

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