Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

SchemaModule

Provides methods for building database schema.

class SchemaModule {
constructor(executor: QueryExecutor);
alterTable(table: string): AlterTableBuilder;
createIndex(indexName: string): CreateIndexBuilder;
createSchema(schema: string): CreateSchemaBuilder;
createTable<TB extends string>(table: TB): CreateTableBuilder<TB, never>;
createType(typeName: string): CreateTypeBuilder;
createView(viewName: string): CreateViewBuilder;
dropIndex(indexName: string): DropIndexBuilder;
dropSchema(schema: string): DropSchemaBuilder;
dropTable(table: string): DropTableBuilder;
dropType(typeName: string): DropTypeBuilder;
dropView(viewName: string): DropViewBuilder;
withoutPlugins(): SchemaModule;
withPlugin(plugin: KyselyPlugin): SchemaModule;
withSchema(schema: string): SchemaModule;
}

§Constructors

§
new SchemaModule(executor: QueryExecutor)
[src]

§Methods

§
alterTable(table: string): AlterTableBuilder
[src]

Alter a table.

Examples

await db.schema
  .alterTable('person')
  .alterColumn('first_name', (ac) => ac.setDataType('text'))
  .execute()
§
createIndex(indexName: string): CreateIndexBuilder
[src]

Create a new index.

Examples

await db.schema
  .createIndex('person_full_name_unique_index')
  .on('person')
  .columns(['first_name', 'last_name'])
  .execute()
§
createSchema(schema: string): CreateSchemaBuilder
[src]

Create a new schema.

Examples

await db.schema
  .createSchema('some_schema')
  .execute()
§
createTable<TB extends string>(table: TB): CreateTableBuilder<TB, never>
[src]

Create a new table.

Examples

This example creates a new table with columns id, first_name, last_name and gender:

await db.schema
  .createTable('person')
  .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
  .addColumn('first_name', 'varchar', col => col.notNull())
  .addColumn('last_name', 'varchar', col => col.notNull())
  .addColumn('gender', 'varchar')
  .execute()

This example creates a table with a foreign key. Not all database engines support column-level foreign key constraint definitions. For example if you are using MySQL 5.X see the next example after this one.

await db.schema
  .createTable('pet')
  .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
  .addColumn('owner_id', 'integer', col => col
    .references('person.id')
    .onDelete('cascade')
  )
  .execute()

This example adds a foreign key constraint for a columns just like the previous example, but using a table-level statement. On MySQL 5.X you need to define foreign key constraints like this:

await db.schema
  .createTable('pet')
  .addColumn('id', 'integer', col => col.primaryKey().autoIncrement())
  .addColumn('owner_id', 'integer')
  .addForeignKeyConstraint(
    'pet_owner_id_foreign', ['owner_id'], 'person', ['id'],
    (constraint) => constraint.onDelete('cascade')
  )
  .execute()
§
createType(typeName: string): CreateTypeBuilder
[src]

Create a new type.

Only some dialects like PostgreSQL have user-defined types.

Examples

await db.schema
  .createType('species')
  .asEnum(['dog', 'cat', 'frog'])
  .execute()
§
createView(viewName: string): CreateViewBuilder
[src]

Create a new view.

Examples

await db.schema
  .createView('dogs')
  .orReplace()
  .as(db.selectFrom('pet').selectAll().where('species', '=', 'dog'))
  .execute()
§
dropIndex(indexName: string): DropIndexBuilder
[src]

Drop an index.

Examples

await db.schema
  .dropIndex('person_full_name_unique_index')
  .execute()
§
dropSchema(schema: string): DropSchemaBuilder
[src]

Drop a schema.

Examples

await db.schema
  .dropSchema('some_schema')
  .execute()
§
dropTable(table: string): DropTableBuilder
[src]

Drop a table.

Examples

await db.schema
  .dropTable('person')
  .execute()
§
dropType(typeName: string): DropTypeBuilder
[src]

Drop a type.

Only some dialects like PostgreSQL have user-defined types.

Examples

await db.schema
  .dropType('species')
  .ifExists()
  .execute()
§
dropView(viewName: string): DropViewBuilder
[src]

Drop a view.

Examples

await db.schema
  .dropView('dogs')
  .ifExists()
  .execute()
§
withoutPlugins(): SchemaModule
[src]

Returns a copy of this schema module without any plugins.

§
withPlugin(plugin: KyselyPlugin): SchemaModule
[src]

Returns a copy of this schema module with the given plugin installed.

§
withSchema(schema: string): SchemaModule
[src]

See {@link QueryCreator.withSchema}