DynamicSchema

Constructor

new DynamicSchema()

(DEPRECATED - Use createSchemaInstance() instead)

Create an new DynamicSchema instance


Properties

tableName: string

The name of the table.

tableSlug: string

The slug of the table.

definition: object

The table's column definitions.

required: array

Label of required fields of this schema. Array of strings.

description: string

Description of the schema. Not used for anything internally.

jsonSchema: object

The underlying JSON Schema definition of the schema


Methods

createTable → Promise

Create a new table with the given schema. Schema must adhere to the JSON Schema definition set out in https://json-schema.org/

Each property corresponds to each column in the database. A few custom attributes to each property can be included for use by DynamicSchema to generate columns for special behaviour.

These properties are:

  • isIndex: Whether the column is an index field
  • isUnique: Whether the column is an unique field
  • isAutoIncrement: Whether the column is an auto-incrementing integer

Parameters:

  • schema: object
  • schema.$id: string - ID of the table, must be unique
  • schema.title: string - Defaults to `schema.$id`
  • schema.properties: object - The column definitions of the table

Returns:

Promise - Return promise of the instance containing the new table

dropTable → Promise

Drop the table from the database.

Returns:

Promise - Return promise of empty DynamicSchema instance

renameTable → Promise

Rename the table.

Parameters:

  • newSlug: string
  • newName: string - Defaults to newSlug

Returns:

Promise - Return promise of DynamicSchema instance

addIndex → Promise

Add an index to the table's schema.

Parameters:

  • options: object
  • options.name: string - The name of the column to be used as index
  • options.unique: boolean - Whether the index is unique or not
  • options.autoInrement: boolean - Whether it is an auto-incrementing index or not. If true, `options.unique` is automatically set to true

Returns:

Promise - Return promise of DynamicSchema instance

removeIndex → Promise

Remove an index to the table's schema

Parameters:

  • columnName: string - The name of the index to remove

Returns:

Promise - Return promise of DynamicSchema instance

read → Promise

Read the schema definition from the database.

Parameters:

  • tableSlug: string - The name of the table schema to retrieve

Returns:

Promise - Return promise of DynamicSchema instance

define → Promise

Define the table's columns. Passed object must adhere to properties attribute of JSON Schema's definition.

Optional required parameters define any of the columns as a required field.

Note that this function replaces any existing definition on the table. If you want to edit individual columns, you should use other functions instead.

Parameters:

  • definition: object - Definition of the table columns
  • required: Array - Array of column names that are required fields

Returns:

Promise - Return promise of DynamicSchema instance

addColumn → Promise

Add a single column to the table's schema definition. If the column name is already in use, this will return a rejected Promise.

Parameters:

  • name: string - The name of the column to add
  • type: string - Type of the column to add
  • description: string - Description of the column to add

Returns:

Promise - Return promise of DynamicSchema instance

addColumns → Promise

Add multiple columns to the table's schema definition. If any of the given columns already exist, this will return a rejected Promise. None of the changes will be made.

Parameters:

  • definitions: object - Object of objects containing new columns definitions

Returns:

Promise - Return promise of DynamicSchema instance

renameColumn → Promise

Rename a single column in the table's schema definition.

Parameters:

  • name: string - The name of the column to rename
  • newName: string - The new name of the target column

Returns:

Promise - Return promise of DynamicSchema instance

changeColumnType → Promise

Change the type of a single column in the table's schema definition.

Parameters:

  • name: string - The name of the column to change type
  • newType: string - The new type of the target column

Returns:

Promise - Return promise of DynamicSchema instance

removeColumn → Promise

Remove a single column from the table's schema definition.

Parameters:

  • name: string - The name of the column to remove

Returns:

Promise - Return promise of DynamicSchema instance