SeedCompany / cord-api-v3

Bible translation project management API
MIT License
18 stars 4 forks source link

[EdgeDB] Seed script execution v2 #3130

Closed CarsonF closed 5 months ago

CarsonF commented 5 months ago

Scripts still work the same as before. Driving this change is the need to disable access policies while seeding 4985f3959b3ca97029cdab4f6970443b78a2e9a3. Seeds do not have a user configured so #3126 fails to apply seeds as they are now assumed to executed as an "anonymous user".

TS seed files

While I was here I added support for seeds to be TS files.

These TS files should export a default function which will be executed in the appropriate order.

They can return or yield one or more query strings or query builder queries. They will be executed in order, and the results printed, just like the edgeql files.

// dbschema/seeds/777.something.ts
import { SeedFn } from '~/core/edgedb/seeds.run';

export default (async function* ({ e }) {

  yield 'select 1';

  yield e.select(2);

} satisfies SeedFn);

Queries can also be executed explicitly for result manipulation.

export default (async function ({ e, db, print }) {

  const root = await e
    .assert_exists(e.select(e.RootUser).assert_single())
    .run(db);

  print(`Root User: ${root.id}`);

} satisfies SeedFn);