1
1
import { many , MaybeAsync } from '@seedcompany/common' ;
2
2
import { BaseContext , Command , runExit } from 'clipanion' ;
3
3
import { Client , createClient , Executor } from 'edgedb' ;
4
+ import type { QueryArgs } from 'edgedb/dist/ifaces' ;
4
5
import { glob } from 'glob' ;
5
6
import fs from 'node:fs/promises' ;
7
+ import { BehaviorSubject } from 'rxjs' ;
6
8
import { inspect } from 'util' ;
9
+ import { ID } from '~/common' ;
10
+ import { Options } from './options' ;
11
+ import { OptionsContext , OptionsFn } from './options.context' ;
7
12
import { e } from './reexports' ;
8
13
9
14
type Query = string | QBQuery ;
@@ -17,17 +22,30 @@ export type SeedFn = (
17
22
18
23
interface SeedParams {
19
24
e : typeof e ;
20
- runAndPrint : ( query : Query ) => Promise < void > ;
25
+ runAndPrint : ( query : Query , args ?: QueryArgs ) => Promise < void > ;
21
26
db : Client ;
22
27
print : ( something : unknown ) => void ;
23
28
context : BaseContext ;
29
+ actorId : BehaviorSubject < ID | undefined > ;
24
30
}
25
31
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
+ ) ;
31
49
} ) ;
32
50
33
51
class SeedCommand extends Command {
@@ -43,11 +61,11 @@ class SeedCommand extends Command {
43
61
}
44
62
} ;
45
63
46
- const runAndPrint = async ( query : Query ) => {
64
+ const runAndPrint = async ( query : Query , args ?: QueryArgs ) => {
47
65
try {
48
66
const rows =
49
67
typeof query === 'string'
50
- ? await db . query ( query )
68
+ ? await db . query ( query , args )
51
69
: await query . run ( db ) ;
52
70
printResult ( rows ) ;
53
71
} catch ( e ) {
@@ -61,6 +79,7 @@ class SeedCommand extends Command {
61
79
print : printResult ,
62
80
runAndPrint,
63
81
e,
82
+ actorId : actor ,
64
83
} ;
65
84
66
85
for ( const file of files ) {
@@ -89,7 +108,9 @@ class SeedCommand extends Command {
89
108
90
109
async validateAndExecute ( ) {
91
110
try {
92
- return await super . validateAndExecute ( ) ;
111
+ return await optionsContext . usingOptions ( actorOptions , async ( ) => {
112
+ return await super . validateAndExecute ( ) ;
113
+ } ) ;
93
114
} finally {
94
115
await db . close ( ) ;
95
116
}
0 commit comments