Skip to content

Commit ee2851c

Browse files
committed
Set ghost agent as the current actor for seeds
1 parent 80a9c87 commit ee2851c

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

dbschema/seeds/000.system-agents.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const agents = [
66
{ name: 'External Mailing Group', roles: ['Leadership'] },
77
];
88

9-
export default (async function ({ runAndPrint }) {
9+
export default (async function ({ runAndPrint, e, db, actorId }) {
1010
await runAndPrint(
1111
`
1212
with
@@ -24,4 +24,12 @@ export default (async function ({ runAndPrint }) {
2424
`,
2525
{ agentsJson: agents },
2626
);
27+
28+
const getGhost = e.assert_exists(
29+
e.select(e.SystemAgent, () => ({
30+
filter_single: { name: 'Ghost' },
31+
})),
32+
);
33+
const ghost = await getGhost.run(db);
34+
actorId.next(ghost.id);
2735
} satisfies SeedFn);

src/core/edgedb/edgedb.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { TransactionContext } from './transaction.context';
4040
...connectConfig,
4141
});
4242

43-
Object.assign(client, { options: options.currentAsLazyRef });
43+
options.attachToClient(client);
4444

4545
if (config.databaseEngine === 'edgedb') {
4646
await registerCustomScalarCodecs(client, codecs);

src/core/edgedb/options.context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable, OnModuleDestroy } from '@nestjs/common';
22
import { AsyncLocalStorage } from 'async_hooks';
3+
import type { Client } from 'edgedb';
34
import {
45
BehaviorSubject,
56
combineLatest,
@@ -90,6 +91,9 @@ export class OptionsContext
9091
get currentAsLazyRef() {
9192
return lazyRef(() => this.current);
9293
}
94+
attachToClient(client: Client) {
95+
Object.assign(client, { options: this.currentAsLazyRef });
96+
}
9397

9498
onModuleDestroy() {
9599
this.disable();

src/core/edgedb/seeds.run.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { many, MaybeAsync } from '@seedcompany/common';
22
import { BaseContext, Command, runExit } from 'clipanion';
33
import { Client, createClient, Executor } from 'edgedb';
4+
import type { QueryArgs } from 'edgedb/dist/ifaces';
45
import { glob } from 'glob';
56
import fs from 'node:fs/promises';
7+
import { BehaviorSubject } from 'rxjs';
68
import { inspect } from 'util';
9+
import { ID } from '~/common';
10+
import { Options } from './options';
11+
import { OptionsContext, OptionsFn } from './options.context';
712
import { e } from './reexports';
813

914
type Query = string | QBQuery;
@@ -17,17 +22,30 @@ export type SeedFn = (
1722

1823
interface SeedParams {
1924
e: typeof e;
20-
runAndPrint: (query: Query) => Promise<void>;
25+
runAndPrint: (query: Query, args?: QueryArgs) => Promise<void>;
2126
db: Client;
2227
print: (something: unknown) => void;
2328
context: BaseContext;
29+
actorId: BehaviorSubject<ID | undefined>;
2430
}
2531

26-
const db = createClient().withConfig({
27-
// eslint-disable-next-line @typescript-eslint/naming-convention
28-
allow_user_specified_id: true,
29-
// eslint-disable-next-line @typescript-eslint/naming-convention
30-
apply_access_policies: false,
32+
const optionsContext = new OptionsContext(
33+
new Options().withConfig({
34+
// eslint-disable-next-line @typescript-eslint/naming-convention
35+
allow_user_specified_id: true,
36+
// eslint-disable-next-line @typescript-eslint/naming-convention
37+
apply_access_policies: false,
38+
}),
39+
);
40+
const db = createClient();
41+
optionsContext.attachToClient(db);
42+
43+
const actor = new BehaviorSubject<ID | undefined>(undefined);
44+
const actorOptions = new BehaviorSubject<OptionsFn>((opts) => opts);
45+
actor.subscribe((actor) => {
46+
actorOptions.next((options) =>
47+
actor ? options.withGlobals({ currentActorId: actor }) : options,
48+
);
3149
});
3250

3351
class SeedCommand extends Command {
@@ -43,11 +61,11 @@ class SeedCommand extends Command {
4361
}
4462
};
4563

46-
const runAndPrint = async (query: Query) => {
64+
const runAndPrint = async (query: Query, args?: QueryArgs) => {
4765
try {
4866
const rows =
4967
typeof query === 'string'
50-
? await db.query(query)
68+
? await db.query(query, args)
5169
: await query.run(db);
5270
printResult(rows);
5371
} catch (e) {
@@ -61,6 +79,7 @@ class SeedCommand extends Command {
6179
print: printResult,
6280
runAndPrint,
6381
e,
82+
actorId: actor,
6483
};
6584

6685
for (const file of files) {
@@ -89,7 +108,9 @@ class SeedCommand extends Command {
89108

90109
async validateAndExecute() {
91110
try {
92-
return await super.validateAndExecute();
111+
return await optionsContext.usingOptions(actorOptions, async () => {
112+
return await super.validateAndExecute();
113+
});
93114
} finally {
94115
await db.close();
95116
}

0 commit comments

Comments
 (0)