( ctx: Mocha.Context, dialect: D, )
| 601 | export type JSONTestContext = Awaited<ReturnType<typeof initJSONTest>> |
| 602 | |
| 603 | export async function initJSONTest<D extends DialectDescriptor>( |
| 604 | ctx: Mocha.Context, |
| 605 | dialect: D, |
| 606 | ) { |
| 607 | const testContext = await initTest(ctx, dialect) |
| 608 | |
| 609 | let db = testContext.db.withTables<{ |
| 610 | person_metadata: { |
| 611 | person_id: number |
| 612 | website: JSONColumnType<{ url: string }> |
| 613 | nicknames: JSONColumnType<string[]> |
| 614 | profile: JSONColumnType<{ |
| 615 | auth: { |
| 616 | roles: string[] |
| 617 | last_login?: { device: string } |
| 618 | is_verified: SqlBool |
| 619 | login_count: number |
| 620 | } |
| 621 | avatar: string | null |
| 622 | tags: string[] |
| 623 | }> |
| 624 | experience: JSONColumnType< |
| 625 | { |
| 626 | establishment: string |
| 627 | }[] |
| 628 | > |
| 629 | schedule: JSONColumnType<{ name: string; time: string }[][][]> |
| 630 | } |
| 631 | }>() |
| 632 | |
| 633 | if (dialect.sqlSpec === 'sqlite') { |
| 634 | db = db.withPlugin(new ParseJSONResultsPlugin()) |
| 635 | } |
| 636 | |
| 637 | const jsonColumnDataType = resolveJSONColumnDataType(dialect) |
| 638 | const notNull = (cb: ColumnDefinitionBuilder) => cb.notNull() |
| 639 | |
| 640 | await db.schema |
| 641 | .createTable('person_metadata') |
| 642 | .addColumn('person_id', 'integer', (cb) => |
| 643 | cb.primaryKey().references('person.id'), |
| 644 | ) |
| 645 | .addColumn('website', jsonColumnDataType, notNull) |
| 646 | .addColumn('nicknames', jsonColumnDataType, notNull) |
| 647 | .addColumn('profile', jsonColumnDataType, notNull) |
| 648 | .addColumn('experience', jsonColumnDataType, notNull) |
| 649 | .addColumn('schedule', jsonColumnDataType, notNull) |
| 650 | .execute() |
| 651 | |
| 652 | return { ...testContext, db } |
| 653 | } |
| 654 | |
| 655 | export function resolveJSONColumnDataType( |
| 656 | dialect: DialectDescriptor, |
no test coverage detected
searching dependent graphs…