2021-04-29 09:53:59 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
import { createConnection } from 'typeorm';
|
2021-08-29 16:45:46 +00:00
|
|
|
|
|
|
|
import { AuthToken } from './auth/auth-token.entity';
|
2021-05-19 19:09:15 +00:00
|
|
|
import { Author } from './authors/author.entity';
|
2021-04-29 09:53:59 +00:00
|
|
|
import { Group } from './groups/group.entity';
|
|
|
|
import { HistoryEntry } from './history/history-entry.entity';
|
|
|
|
import { MediaUpload } from './media/media-upload.entity';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { Note } from './notes/note.entity';
|
2021-04-29 09:53:59 +00:00
|
|
|
import { Tag } from './notes/tag.entity';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { NoteGroupPermission } from './permissions/note-group-permission.entity';
|
|
|
|
import { NoteUserPermission } from './permissions/note-user-permission.entity';
|
|
|
|
import { Edit } from './revisions/edit.entity';
|
|
|
|
import { Revision } from './revisions/revision.entity';
|
2021-04-29 09:53:59 +00:00
|
|
|
import { Identity } from './users/identity.entity';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { Session } from './users/session.entity';
|
|
|
|
import { User } from './users/user.entity';
|
2021-04-29 09:53:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function creates and populates a sqlite db for manual testing
|
|
|
|
*/
|
|
|
|
createConnection({
|
|
|
|
type: 'sqlite',
|
|
|
|
database: './hedgedoc.sqlite',
|
|
|
|
entities: [
|
|
|
|
User,
|
|
|
|
Note,
|
|
|
|
Revision,
|
2021-05-31 19:46:41 +00:00
|
|
|
Edit,
|
2021-04-29 09:53:59 +00:00
|
|
|
NoteGroupPermission,
|
|
|
|
NoteUserPermission,
|
|
|
|
Group,
|
|
|
|
HistoryEntry,
|
|
|
|
MediaUpload,
|
|
|
|
Tag,
|
|
|
|
AuthToken,
|
|
|
|
Identity,
|
2021-05-19 19:09:15 +00:00
|
|
|
Author,
|
|
|
|
Session,
|
2021-04-29 09:53:59 +00:00
|
|
|
],
|
|
|
|
synchronize: true,
|
|
|
|
logging: false,
|
2021-05-19 19:09:15 +00:00
|
|
|
dropSchema: true,
|
2021-04-29 09:53:59 +00:00
|
|
|
})
|
|
|
|
.then(async (connection) => {
|
2021-05-19 19:09:15 +00:00
|
|
|
const users = [];
|
|
|
|
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(
|
|
|
|
'This is a test note',
|
|
|
|
'This is a test note',
|
|
|
|
);
|
2021-05-31 19:46:41 +00:00
|
|
|
const edit = Edit.create(author, 1, 42);
|
|
|
|
revision.edits = [edit];
|
2021-05-19 19:09:15 +00:00
|
|
|
notes[i].revisions = Promise.all([revision]);
|
|
|
|
notes[i].userPermissions = [];
|
|
|
|
notes[i].groupPermissions = [];
|
|
|
|
user.ownedNotes = [notes[i]];
|
2021-05-31 19:46:41 +00:00
|
|
|
await connection.manager.save([notes[i], user, revision, edit, author]);
|
2021-05-19 19:09:15 +00:00
|
|
|
}
|
2021-04-29 09:53:59 +00:00
|
|
|
const foundUser = await connection.manager.findOne(User);
|
2021-05-02 16:35:38 +00:00
|
|
|
if (!foundUser) {
|
|
|
|
throw new Error('Could not find freshly seeded user. Aborting.');
|
|
|
|
}
|
2021-04-29 09:53:59 +00:00
|
|
|
const foundNote = await connection.manager.findOne(Note);
|
2021-05-02 16:35:38 +00:00
|
|
|
if (!foundNote) {
|
|
|
|
throw new Error('Could not find freshly seeded note. Aborting.');
|
|
|
|
}
|
|
|
|
if (!foundNote.alias) {
|
|
|
|
throw new Error('Could not find alias of freshly seeded note. Aborting.');
|
|
|
|
}
|
2021-04-29 09:53:59 +00:00
|
|
|
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));
|