UNPKG

dynamic-record

Version:

A bare minimum Javascript implementation of the Active Record pattern

125 lines (124 loc) 5.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createCollection = exports.DynamicCollection = exports.createSchemaInstance = exports.DynamicSchema = exports.createInstance = exports.DynamicModel = exports.DynamicRecord = exports.createConnection = void 0; const mongodb_1 = require("mongodb"); const mysql = require("mysql2/promise"); /** * Create a connection object that can be used by DynamicRecord instances. * * @method createConnection * @param {string} url Full connection URL for the database in question * @return {object} - Return the created connection object */ function createConnection(url) { const databaseURIRegex = /^(?<protocol>.+?):\/\/(?:(?<username>.+?)(?::(?<password>.+))?@)?(?<host>.+?)(?::(?<port>\d+?))?(?:\/(?<database>.+?))?(?:\?(?<options>.+?))?$/; const regexResult = url.match(databaseURIRegex); switch (regexResult.groups.protocol) { case "mongodb": case "mongodb+srv": { const client = new mongodb_1.MongoClient(url, { maxPoolSize: 10 }); const connection = client.connect(); return { type: "mongodb", interface: connection.then((client) => { const db = client.db(); return { db, client }; }) }; } case "mysql": { const connection = mysql.createPool({ host: regexResult.groups.host, port: parseInt(regexResult.groups.port), user: regexResult.groups.username, password: regexResult.groups.password, database: regexResult.groups.database }); return { type: "mysql", interface: Promise.resolve(connection) }; } default: throw new Error("URL protocol provided is not supported"); } } exports.createConnection = createConnection; var DynamicRecord_1 = require("./DynamicRecord"); Object.defineProperty(exports, "DynamicRecord", { enumerable: true, get: function () { return DynamicRecord_1.DynamicRecord; } }); var DynamicRecord_2 = require("./DynamicRecord"); Object.defineProperty(exports, "DynamicModel", { enumerable: true, get: function () { return DynamicRecord_2.Model; } }); const DynamicRecord_3 = require("./mongodb/DynamicRecord"); const DynamicRecord_4 = require("./mysql/DynamicRecord"); /** * Create an instance of DynamicRecord that uses established connection. * * @method createInstance * @param {object} connection Connection object created with `createConnection()` * @param {string} tableSlug Identifier for the table to use with this instance * @return {DynamicRecord} - Return the created DynamicRecord instance */ function createInstance(connection, tableSlug) { if (connection.type === "mongodb") { return new DynamicRecord_3.DynamicRecord({ tableSlug, connection }); } else if (connection.type === "mysql") { return new DynamicRecord_4.DynamicRecord({ tableSlug, connection }); } else { throw new Error("Connection object provided is not valid"); } } exports.createInstance = createInstance; var DynamicSchema_1 = require("./DynamicSchema"); Object.defineProperty(exports, "DynamicSchema", { enumerable: true, get: function () { return DynamicSchema_1.DynamicSchema; } }); const DynamicSchema_2 = require("./mongodb/DynamicSchema"); const DynamicSchema_3 = require("./mysql/DynamicSchema"); /** * Create an instance of DynamicSchema that uses established connection. * * @method createSchemaInstance * @param {object} connection Connection object created with `createConnection()` * @return {DynamicSchema} - Return the created DynamicSchema instance */ function createSchemaInstance(connection) { if (connection.type === "mongodb") { return new DynamicSchema_2.DynamicSchema({ connection }); } else if (connection.type === "mysql") { return new DynamicSchema_3.DynamicSchema({ connection }); } else { throw new Error("Connection object provided is not valid"); } } exports.createSchemaInstance = createSchemaInstance; var DynamicCollection_1 = require("./DynamicCollection"); Object.defineProperty(exports, "DynamicCollection", { enumerable: true, get: function () { return DynamicCollection_1.DynamicCollection; } }); const DynamicCollection_2 = require("./mongodb/DynamicCollection"); const DynamicCollection_3 = require("./mysql/DynamicCollection"); /** * Create an instance of DynamicCollection that uses established connection. * * @method createCollection * @param {object} connection Connection object created with `createConnection()` * @param {object} Model The Model constructor to use for this collection * @return {DynamicCollection} - Return the created DynamicCollection instance */ function createCollection(connection, Model, ...data) { if (connection.type === "mongodb") { return new DynamicCollection_2.DynamicCollection(Model, ...data); } else if (connection.type === "mysql") { return new DynamicCollection_3.DynamicCollection(Model, ...data); } else { throw new Error("Connection object provided is not valid"); } } exports.createCollection = createCollection;