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,80 +49,81 @@ createConnection({
synchronize: true, synchronize: true,
logging: false, logging: false,
dropSchema: true, dropSchema: true,
}) });
.then(async (connection) => {
const password = 'test_password';
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(null, 'test') as Note);
notes.push(Note.create(null, 'test2') as Note);
notes.push(Note.create(null, 'test3') as Note);
for (let i = 0; i < 3; i++) { dataSource.initialize().then(async () => {
const author = connection.manager.create(Author, Author.create(1)); const password = 'test_password';
const user = connection.manager.create(User, users[i]); const users = [];
const identity = Identity.create(user, ProviderType.LOCAL, false); users.push(User.create('hardcoded', 'Test User 1'));
identity.passwordHash = await hashPassword(password); users.push(User.create('hardcoded_2', 'Test User 2'));
connection.manager.create(Identity, identity); users.push(User.create('hardcoded_3', 'Test User 3'));
author.user = Promise.resolve(user); const notes: Note[] = [];
const revision = Revision.create( notes.push(Note.create(null, 'test') as Note);
'This is a test note', notes.push(Note.create(null, 'test2') as Note);
'This is a test note', notes.push(Note.create(null, 'test3') as Note);
notes[i],
) as Revision; for (let i = 0; i < 3; i++) {
const edit = Edit.create(author, 1, 42) as Edit; const author = await dataSource.manager.save(dataSource.manager.create(Author, Author.create(1))) as Author;
revision.edits = Promise.resolve([edit]); const user = await dataSource.manager.save(dataSource.manager.create(User, users[i])) as User;
notes[i].revisions = Promise.all([revision]); const identity = Identity.create(user, ProviderType.LOCAL, false);
notes[i].userPermissions = Promise.resolve([]); identity.passwordHash = await hashPassword(password);
notes[i].groupPermissions = Promise.resolve([]); dataSource.manager.create(Identity, identity);
user.ownedNotes = Promise.resolve([notes[i]]); author.user = (dataSource.manager.save(user) as Promise<User>);
await connection.manager.save([ const revision = Revision.create(
notes[i], 'This is a test note',
user, 'This is a test note',
revision, notes[i],
edit, ) as Revision;
author, const edit = Edit.create(author, 1, 42) as Edit;
identity, revision.edits = Promise.resolve([edit]);
]); notes[i].revisions = Promise.all([revision]);
} notes[i].userPermissions = Promise.resolve([]);
const foundUsers = await connection.manager.find(User); notes[i].groupPermissions = Promise.resolve([]);
if (!foundUsers) { user.ownedNotes = Promise.resolve([notes[i]]);
throw new Error('Could not find freshly seeded users. Aborting.'); await dataSource.manager.save([
} notes[i],
const foundNotes = await connection.manager.find(Note, { user,
relations: ['aliases'], revision,
}); edit,
if (!foundNotes) { author,
throw new Error('Could not find freshly seeded notes. Aborting.'); identity,
} ]);
for (const note of foundNotes) { }
if (!(await note.aliases)[0]) { const foundUsers = await dataSource.manager.find(User);
throw new Error( if (!foundUsers) {
'Could not find alias of freshly seeded notes. Aborting.', throw new Error('Could not find freshly seeded users. Aborting.');
); }
} const foundNotes = await dataSource.manager.find(Note, {
} relations: ['aliases'],
for (const user of foundUsers) { });
console.log( if (!foundNotes) {
`Created User '${user.username}' with password '${password}'`, throw new Error('Could not find freshly seeded notes. Aborting.');
}
for (const note of foundNotes) {
if (!(await note.aliases)[0]) {
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 '${(await note.aliases)[0].name ?? ''}'`);
}
for (const user of foundUsers) {
for (const note of foundNotes) { for (const note of foundNotes) {
console.log(`Created Note '${(await note.aliases)[0].name ?? ''}'`); const historyEntry = HistoryEntry.create(user, note);
await dataSource.manager.save(historyEntry);
console.log(
`Created HistoryEntry for user '${user.username}' and note '${
(await note.aliases)[0].name ?? ''
}'`,
);
} }
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 '${
(await note.aliases)[0].name ?? ''
}'`,
);
}
}
})
.catch((error) => console.log(error)); .catch((error) => console.log(error));