Seed: Generate multiple notes and authorships

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-05-19 21:09:15 +02:00
parent f6d430c23f
commit 6abcb686ca
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -5,6 +5,8 @@
*/ */
import { createConnection } from 'typeorm'; import { createConnection } from 'typeorm';
import { Author } from './authors/author.entity';
import { Session } from './users/session.entity';
import { User } from './users/user.entity'; import { User } from './users/user.entity';
import { Note } from './notes/note.entity'; import { Note } from './notes/note.entity';
import { Revision } from './revisions/revision.entity'; import { Revision } from './revisions/revision.entity';
@ -37,22 +39,45 @@ createConnection({
Tag, Tag,
AuthToken, AuthToken,
Identity, Identity,
Author,
Session,
], ],
synchronize: true, synchronize: true,
logging: false, logging: false,
dropSchema: true,
}) })
.then(async (connection) => { .then(async (connection) => {
const user = User.create('hardcoded', 'Test User'); const users = [];
const note = Note.create(undefined, 'test'); users.push(User.create('hardcoded', 'Test User 1'));
users.push(User.create('hardcoded_2', 'Test User 2'));
users.push(User.create('hardcoded_3', 'Test User 3'));
const notes: Note[] = [];
notes.push(Note.create(undefined, 'test'));
notes.push(Note.create(undefined, 'test2'));
notes.push(Note.create(undefined, 'test3'));
for (let i = 0; i < 3; i++) {
const author = connection.manager.create(Author, Author.create(1));
const user = connection.manager.create(User, users[i]);
author.user = user;
const revision = Revision.create( const revision = Revision.create(
'This is a test note', 'This is a test note',
'This is a test note', 'This is a test note',
); );
note.revisions = Promise.all([revision]); const authorship = Authorship.create(author, 1, 42);
note.userPermissions = []; revision.authorships = [authorship];
note.groupPermissions = []; notes[i].revisions = Promise.all([revision]);
user.ownedNotes = [note]; notes[i].userPermissions = [];
await connection.manager.save([user, note, revision]); notes[i].groupPermissions = [];
user.ownedNotes = [notes[i]];
await connection.manager.save([
notes[i],
user,
revision,
authorship,
author,
]);
}
const foundUser = await connection.manager.findOne(User); const foundUser = await connection.manager.findOne(User);
if (!foundUser) { if (!foundUser) {
throw new Error('Could not find freshly seeded user. Aborting.'); throw new Error('Could not find freshly seeded user. Aborting.');