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