UNPKG

@knorm/knorm

Version:

A JavaScript ORM written using ES6 classes

173 lines (172 loc) 6.56 kB
/** * Creates and configures ORMs. */ export class Knorm { /** * Creates a new {@link Knorm} (ORM) instance. Each instance has it's own set * of classes and configuration, which enables having multiple {@link Knorm} * instances in a single application. * * @param {object} [config] The ORM's configuration. * @param {function} [config.fieldToColumn] A function to convert all * field-names to column names, for example * [snakeCase](https://lodash.com/docs/4.17.10#snakeCase). * @param {boolean} [config.debug] Whether or not to enable debug mode. See * [the debugging guide](/guides/debugging) for more info. * */ constructor(config?: { fieldToColumn?: Function; debug?: boolean; } | undefined); /** * The {@link Knorm} instance's model registry. Stores models added via * {@link Knorm#addModel}. * * @type {object} */ models: object; /** * The {@link Knorm} instance's plugin registry. Stores plugins added via * {@link Knorm#use}. * * @type {object} */ plugins: object; /** * The {@link Knorm} instance's config. Stores config passed in via the * constructor. * * @type {object} */ config: object; /** * Loads a plugin into the ORM. * * @param {object|function} plugin The plugin to load. If passed as a * function, the function is called with the ORM instance for initialisation. * Note that if an object is passed, it should have `name` and `init` * properties. * @param {string} plugin.name The name of the plugin. This allows later * accessing the plugin via the ORM's `plugins` object. Note that for * functions, the plugin's `name` is the function's name. * @param {function} plugin.init The function called to initialise the plugin. * * @throws {KnormError} if the plugin provided is not a function or is an * object without an `init` method. * @throws {KnormError} if the plugin has no `name` property. * @throws {KnormError} if the plugin has already been added. * * @returns {Knorm} the ORM instance. */ use(plugin: object | Function): Knorm; /** * Adds a model to a {@link Knorm} instance. * * @param {Model} Model The model to add to the ORM. Note that models are * automatically added to a {@link Knorm} instance when they are configured, * therefore you will only need to call this method to add models that are not * configured. See {@link /guides/models.html#model-registry} for more info. * * @throws {KnormError} if the model passed does not inherit the ORM's * {@link Model} instance. This prevents one from (perhaps accidentally) * adding a model from ORM instance X to ORM instance Y. * @throws {KnormError} if the model has already been added. * * @returns {Knorm} The same {@link Knorm} instance to allow chaining. */ addModel(Model: Model): Knorm; /** * Creates a clone of an existing {@link Knorm} instance, copying all the * models and plugins loaded into the original orm into it. * * @returns {Knorm} The newly cloned {@link Knorm} instance. */ clone(): Knorm; /** * Updates the {@link Transaction} class used in the {@link Knorm} instance. * This ensures all references to the new class are updated accordingly. * * @param {Transaction} Transaction The new class. This could be a class that * extends the current {@link Knorm#Transaction} class or an entirely new * class. * * @returns {Knorm} The same {@link Knorm} instance to allow chaining. */ updateTransaction(Transaction: Transaction): Knorm; /** * The {@link Knorm} instance's {@link Transaction} class. */ Transaction: Transaction | undefined; /** * Updates the {@link Model} class used in the {@link Knorm} instance. This * ensures all references to the new class are updated accordingly. * * @param {Model} Model The new class. This could be a class that extends the * current {@link Knorm#Model} class or an entirely new class. * * @returns {Knorm} The same {@link Knorm} instance to allow chaining. */ updateModel(Model: Model): Knorm; /** * The {@link Knorm} instance's {@link Model} class. */ Model: Model | undefined; /** * Updates the {@link Field} class used in the {@link Knorm} instance. This * ensures all references to the new class are updated accordingly. * * @param {Field} Field The new class. This could be a class that extends the * current {@link Knorm#Field} class or an entirely new class. * * @returns {Knorm} The same {@link Knorm} instance to allow chaining. */ updateField(Field: Field): Knorm; /** * The {@link Knorm} instance's {@link Field} class. */ Field: Field | undefined; /** * Updates the {@link Query} class used in the {@link Knorm} instance. This * ensures all references to the new class are updated accordingly. * * @param {Query} Query The new class. This could be a class that extends the * current {@link Knorm#Query} class or an entirely new class. * * @returns {Knorm} The same {@link Knorm} instance to allow chaining. */ updateQuery(Query: Query): Knorm; /** * The {@link Knorm} instance's {@link Query} class. */ Query: Query | undefined; /** * Updates the {@link Connection} class used in the {@link Knorm} instance. * This ensures all references to the new class are updated accordingly. * * @param {Connection} Connection The new class. This could be a class that * extends the current {@link Knorm#Connection} class or an entirely new * class. * * @returns {Knorm} The same {@link Knorm} instance to allow chaining. */ updateConnection(Connection: Connection): Knorm; /** * The {@link Knorm} instance's {@link Connection} class. */ Connection: Connection | undefined; } export namespace Knorm { export { Connection }; export { Model }; export { Query }; export { Field }; export { Transaction }; export { KnormError }; } import { Model } from "./Model"; import { Transaction } from "./Transaction"; import { Field } from "./Field"; import { Query } from "./Query"; import { Connection } from "./Connection"; import { KnormError } from "./KnormError";