Use new APIs in seed.ts

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-04-17 21:22:51 +02:00
parent 2f1e6628a1
commit 43d3f14322

View file

@ -3,7 +3,7 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { createConnection } from 'typeorm';
import { DataSource } from 'typeorm';
import { AuthToken } from './auth/auth-token.entity';
import { Author } from './authors/author.entity';
@ -26,7 +26,7 @@ import { hashPassword } from './utils/password';
/**
* This function creates and populates a sqlite db for manual testing
*/
createConnection({
const dataSource = new DataSource({
type: 'sqlite',
database: './hedgedoc.sqlite',
entities: [
@ -49,8 +49,9 @@ createConnection({
synchronize: true,
logging: false,
dropSchema: true,
})
.then(async (connection) => {
});
dataSource.initialize().then(async () => {
const password = 'test_password';
const users = [];
users.push(User.create('hardcoded', 'Test User 1'));
@ -62,12 +63,12 @@ createConnection({
notes.push(Note.create(null, 'test3') as Note);
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 author = await dataSource.manager.save(dataSource.manager.create(Author, Author.create(1))) as Author;
const user = await dataSource.manager.save(dataSource.manager.create(User, users[i])) as User;
const identity = Identity.create(user, ProviderType.LOCAL, false);
identity.passwordHash = await hashPassword(password);
connection.manager.create(Identity, identity);
author.user = Promise.resolve(user);
dataSource.manager.create(Identity, identity);
author.user = (dataSource.manager.save(user) as Promise<User>);
const revision = Revision.create(
'This is a test note',
'This is a test note',
@ -79,7 +80,7 @@ createConnection({
notes[i].userPermissions = Promise.resolve([]);
notes[i].groupPermissions = Promise.resolve([]);
user.ownedNotes = Promise.resolve([notes[i]]);
await connection.manager.save([
await dataSource.manager.save([
notes[i],
user,
revision,
@ -88,11 +89,11 @@ createConnection({
identity,
]);
}
const foundUsers = await connection.manager.find(User);
const foundUsers = await dataSource.manager.find(User);
if (!foundUsers) {
throw new Error('Could not find freshly seeded users. Aborting.');
}
const foundNotes = await connection.manager.find(Note, {
const foundNotes = await dataSource.manager.find(Note, {
relations: ['aliases'],
});
if (!foundNotes) {
@ -116,7 +117,7 @@ createConnection({
for (const user of foundUsers) {
for (const note of foundNotes) {
const historyEntry = HistoryEntry.create(user, note);
await connection.manager.save(historyEntry);
await dataSource.manager.save(historyEntry);
console.log(
`Created HistoryEntry for user '${user.username}' and note '${
(await note.aliases)[0].name ?? ''