INSERTにはinsertを使うサンプル

This commit is contained in:
mei23 2020-10-04 11:57:57 +09:00
parent ea7504f564
commit 6d22032b61
No known key found for this signature in database
GPG Key ID: DD8628500D3E4B23
7 changed files with 23 additions and 23 deletions

View File

@ -18,9 +18,9 @@ export async function fetchMeta(noCache = false): Promise<Meta> {
cache = meta;
return meta;
} else {
const saved = await transactionalEntityManager.save(Meta, {
const saved = await transactionalEntityManager.insert(Meta, {
id: 'x'
}) as Meta;
}).then(x => transactionalEntityManager.findOneOrFail(Meta, x.identifiers[0]));
cache = saved;
return saved;

View File

@ -145,7 +145,7 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<Us
try {
// Start transaction
await getConnection().transaction(async transactionalEntityManager => {
user = await transactionalEntityManager.save(new User({
user = await transactionalEntityManager.insert(User, {
id: genId(),
avatarId: null,
bannerId: null,
@ -163,9 +163,9 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<Us
tags,
isBot,
isCat: (person as any).isCat === true
})) as IRemoteUser;
}).then(x => transactionalEntityManager.findOneOrFail(User, x.identifiers[0])) as IRemoteUser;
await transactionalEntityManager.save(new UserProfile({
await transactionalEntityManager.insert(UserProfile, {
userId: user.id,
description: person.summary ? htmlToMfm(person.summary, person.tag) : null,
url: getOneApHrefNullable(person.url),
@ -173,13 +173,13 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise<Us
birthday: bday ? bday[0] : null,
location: person['vcard:Address'] || null,
userHost: host
}));
});
await transactionalEntityManager.save(new UserPublickey({
await transactionalEntityManager.insert(UserPublickey, {
userId: user.id,
keyId: person.publicKey.id,
keyPem: person.publicKey.publicKeyPem
}));
});
});
} catch (e) {
// duplicate key error

View File

@ -67,7 +67,7 @@ export async function signup(username: User['username'], password: UserProfile['
if (exist) throw new Error(' the username is already used');
account = await transactionalEntityManager.save(new User({
account = await transactionalEntityManager.insert(User,{
id: genId(),
createdAt: new Date(),
username: username,
@ -77,24 +77,24 @@ export async function signup(username: User['username'], password: UserProfile['
isAdmin: (await Users.count({
host: null,
})) === 0,
}));
}).then(x => transactionalEntityManager.findOneOrFail(User, x.identifiers[0]));
await transactionalEntityManager.save(new UserKeypair({
await transactionalEntityManager.insert(UserKeypair, {
publicKey: keyPair[0],
privateKey: keyPair[1],
userId: account.id
}));
});
await transactionalEntityManager.save(new UserProfile({
await transactionalEntityManager.insert(UserProfile, {
userId: account.id,
autoAcceptFollowed: true,
password: hash,
}));
});
await transactionalEntityManager.save(new UsedUsername({
await transactionalEntityManager.insert(UsedUsername, {
createdAt: new Date(),
username: username.toLowerCase(),
}));
});
});
usersChart.update(account, true);

View File

@ -691,7 +691,7 @@ export default define(meta, async (ps, me) => {
if (meta) {
await transactionalEntityManager.update(Meta, meta.id, set);
} else {
await transactionalEntityManager.save(Meta, set);
await transactionalEntityManager.insert(Meta, set);
}
});

View File

@ -55,14 +55,14 @@ export default define(meta, async (ps, user) => {
}
}
const channel = await Channels.save({
const channel = await Channels.insert({
id: genId(),
createdAt: new Date(),
userId: user.id,
name: ps.name,
description: ps.description || null,
bannerId: banner ? banner.id : null,
} as Channel);
}).then(x => Channels.findOneOrFail(x.identifiers[0]));
return await Channels.pack(channel, user);
});

View File

@ -305,12 +305,12 @@ export default abstract class Chart<T extends Record<string, any>> {
if (currentLog != null) return currentLog;
// 新規ログ挿入
log = await this.repository.save({
log = await this.repository.insert({
group: group,
span: span,
date: date,
...Chart.convertObjectToFlattenColumns(data)
});
}).then(x => this.repository.findOneOrFail(x.identifiers[0]));
logger.info(`${this.name + (group ? `:${group}` : '')} (${span}): New commit created`);

View File

@ -33,13 +33,13 @@ export default async (user: User, note: Note, reaction?: string) => {
}
// Create reaction
const inserted = await NoteReactions.save({
const inserted = await NoteReactions.insert({
id: genId(),
createdAt: new Date(),
noteId: note.id,
userId: user.id,
reaction
});
}).then(x => NoteReactions.findOneOrFail(x.identifiers[0]));
// Increment reactions count
const sql = `jsonb_set("reactions", '{${reaction}}', (COALESCE("reactions"->>'${reaction}', '0')::int + 1)::text::jsonb)`;