refactor: adapt for typeorm 0.3

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-04-17 22:00:04 +02:00
parent 43d3f14322
commit 06c39ea759
5 changed files with 89 additions and 76 deletions

View file

@ -51,79 +51,85 @@ const dataSource = new DataSource({
dropSchema: true,
});
dataSource.initialize().then(async () => {
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);
dataSource
.initialize()
.then(async () => {
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++) {
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);
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',
notes[i],
) as Revision;
const edit = Edit.create(author, 1, 42) as Edit;
revision.edits = Promise.resolve([edit]);
notes[i].revisions = Promise.all([revision]);
notes[i].userPermissions = Promise.resolve([]);
notes[i].groupPermissions = Promise.resolve([]);
user.ownedNotes = Promise.resolve([notes[i]]);
await dataSource.manager.save([
notes[i],
user,
revision,
edit,
author,
identity,
]);
}
const foundUsers = await dataSource.manager.find(User);
if (!foundUsers) {
throw new Error('Could not find freshly seeded users. Aborting.');
}
const foundNotes = await dataSource.manager.find(Note, {
relations: ['aliases'],
});
if (!foundNotes) {
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 (let i = 0; i < 3; 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);
dataSource.manager.create(Identity, identity);
author.user = dataSource.manager.save(user);
const revision = Revision.create(
'This is a test note',
'This is a test note',
notes[i],
) as Revision;
const edit = Edit.create(author, 1, 42) as Edit;
revision.edits = Promise.resolve([edit]);
notes[i].revisions = Promise.all([revision]);
notes[i].userPermissions = Promise.resolve([]);
notes[i].groupPermissions = Promise.resolve([]);
user.ownedNotes = Promise.resolve([notes[i]]);
await dataSource.manager.save([
notes[i],
user,
revision,
edit,
author,
identity,
]);
}
const foundUsers = await dataSource.manager.find(User);
if (!foundUsers) {
throw new Error('Could not find freshly seeded users. Aborting.');
}
const foundNotes = await dataSource.manager.find(Note, {
relations: ['aliases'],
});
if (!foundNotes) {
throw new Error('Could not find 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) {
const historyEntry = HistoryEntry.create(user, note);
await dataSource.manager.save(historyEntry);
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 HistoryEntry for user '${user.username}' and note '${
(await note.aliases)[0].name ?? ''
}'`,
`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) {
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 ?? ''
}'`,
);
}
}
})
.catch((error) => console.log(error));