UNPKG

sqlite3orm

Version:

ORM for sqlite3 and TypeScript/JavaScript

186 lines 5.55 kB
"use strict"; // import * as core from './core'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Schema = void 0; exports.schema = schema; const utils_1 = require("../utils"); const Table_1 = require("./Table"); /** * A singleton holding the database schema definitions * * @export * @class Schema */ class Schema { /** * The one and only Schema instance * * @static */ static schema; mapNameToTable; _dateInMilliSeconds; get dateInMilliSeconds() { return this._dateInMilliSeconds == undefined ? false : this._dateInMilliSeconds; } set dateInMilliSeconds(val) { this._dateInMilliSeconds = val; } /** * Creates an instance of Schema. */ constructor() { if (!Schema.schema) { // initialize the 'singleton' Schema.schema = this; this.mapNameToTable = new Map(); } return Schema.schema; } /** * lookup table definition for given table name * * @param name - The name of the table * @returns The table definition or undefined */ hasTable(name) { return this.mapNameToTable.get((0, utils_1.qualifiySchemaIdentifier)(name)); } /** * get a table definition * * @param name - The name of the table * @returns The table definition */ getTable(name) { const table = this.mapNameToTable.get((0, utils_1.qualifiySchemaIdentifier)(name)); if (!table) { throw new Error(`table '${name}' not registered yet`); } return table; } /** * add a table definition * * @param table - The table definition * @returns The table definition */ getOrAddTable(name, opts) { const qname = (0, utils_1.qualifiySchemaIdentifier)(name); let table = this.mapNameToTable.get(qname); if (!table) { table = new Table_1.Table(name); this.mapNameToTable.set(qname, table); if (opts.withoutRowId != undefined) { table.withoutRowId = opts.withoutRowId; } if (opts.autoIncrement != undefined) { table.autoIncrement = opts.autoIncrement; } } else { if (opts.withoutRowId != undefined) { if (table.isWithoutRowIdDefined && opts.withoutRowId != table.withoutRowId) { throw new Error(`conflicting withoutRowId settings: new: ${opts.withoutRowId}, old ${table.withoutRowId}`); } table.withoutRowId = opts.withoutRowId; } if (opts.autoIncrement != undefined) { if (table.isAutoIncrementDefined && opts.autoIncrement != table.autoIncrement) { throw new Error(`conflicting autoIncrement settings: new: ${opts.autoIncrement}, old ${table.autoIncrement}`); } table.autoIncrement = opts.autoIncrement; } } return table; } /** * delete a table definition * * @param table - The table definition */ deleteTable(name) { this.mapNameToTable.delete((0, utils_1.qualifiySchemaIdentifier)(name)); } /** * get array of table definitions * * @returns The table definitions */ getAllTables() { return Array.from(this.mapNameToTable.values()); } /** * create a table in the database * * @param sqldb - The db connection * @param name - The name of the table * @returns A promise */ createTable(sqldb, name, force) { const table = this.getTable(name); return sqldb.exec(table.getCreateTableStatement(force)); } /** * drop a table from the database * * @param sqldb - The db connection * @param name - The name of the table * @returns A promise */ dropTable(sqldb, name) { const table = this.getTable(name); return sqldb.exec(table.getDropTableStatement()); } /** * add a column/field to a database table * * @param sqldb - The db connection * @param tableName - The name of the table * @param colName - The name of the column * @returns A promise */ alterTableAddColumn(sqldb, tableName, colName) { const table = this.getTable(tableName); return sqldb.exec(table.getAlterTableAddColumnStatement(colName)); } /** * create an index in the database * * @param sqldb - The db connection * @param tableName - The name of the table * @param idxName - The name of the index * @param [unique] - create unique index * @returns A promise */ createIndex(sqldb, tableName, idxName, unique) { const table = this.getTable(tableName); return sqldb.exec(table.getCreateIndexStatement(idxName, unique)); } /** * drop a table from the database * * @param sqldb - The db connection * @param tableName - The name of the table * @param idxName - The name of the index * @returns A promise */ dropIndex(sqldb, tableName, idxName) { const table = this.getTable(tableName); return sqldb.exec(table.getDropIndexStatement(idxName)); } } exports.Schema = Schema; /** * get the Schema singleton * * @export * @returns {Schema} */ function schema() { if (!Schema.schema) { new Schema(); } return Schema.schema; } //# sourceMappingURL=Schema.js.map