@spinajs/orm
Advanced tools
Comparing version 1.0.12 to 1.0.14
import { QueryContext } from './interfaces'; | ||
import { ResolveStrategy, IContainer } from "@spinajs/di"; | ||
import { IDriverOptions, IColumnDescriptor } from "."; | ||
import { UpdateQueryBuilder, SelectQueryBuilder, DeleteQueryBuilder, InsertQueryBuilder, SchemaQueryBuilder } from "./builders"; | ||
import { UpdateQueryBuilder, SelectQueryBuilder, DeleteQueryBuilder, InsertQueryBuilder, SchemaQueryBuilder, QueryBuilder } from "./builders"; | ||
import { Log } from '@spinajs/log'; | ||
export declare type TransactionCallback = (driver: OrmDriver) => Promise<any>; | ||
export declare abstract class OrmDriver extends ResolveStrategy { | ||
Options: IDriverOptions; | ||
Container: IContainer; | ||
protected Log: Log; | ||
constructor(container: IContainer, options: IDriverOptions); | ||
abstract execute(stmt: string | object, params: any[], context: QueryContext): Promise<any[] | any>; | ||
execute(stmt: string | object, params: any[], _context: QueryContext): Promise<any[] | any>; | ||
abstract ping(): Promise<boolean>; | ||
@@ -20,2 +23,3 @@ abstract connect(): Promise<OrmDriver>; | ||
schema(): SchemaQueryBuilder; | ||
abstract transaction(queryOrCallback?: QueryBuilder[] | TransactionCallback): Promise<void>; | ||
} |
"use strict"; | ||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; | ||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); | ||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; | ||
return c > 3 && r && Object.defineProperty(target, key, r), r; | ||
}; | ||
var __metadata = (this && this.__metadata) || function (k, v) { | ||
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -6,2 +15,3 @@ const di_1 = require("@spinajs/di"); | ||
const hydrators_1 = require("./hydrators"); | ||
const log_1 = require("@spinajs/log"); | ||
class OrmDriver extends di_1.ResolveStrategy { | ||
@@ -13,2 +23,9 @@ constructor(container, options) { | ||
} | ||
execute(stmt, params, _context) { | ||
var _a; | ||
if ((_a = this.Options.Debug) === null || _a === void 0 ? void 0 : _a.Queries) { | ||
this.Log.trace("[ QUERY ] raw query: {0} , bindings: {1}", stmt, params); | ||
} | ||
return undefined; | ||
} | ||
resolve(container) { | ||
@@ -34,3 +51,7 @@ container.register(hydrators_1.PropertyHydrator).as(hydrators_1.ModelHydrator); | ||
} | ||
__decorate([ | ||
log_1.Logger({ module: "ORM" }), | ||
__metadata("design:type", Object) | ||
], OrmDriver.prototype, "Log", void 0); | ||
exports.OrmDriver = OrmDriver; | ||
//# sourceMappingURL=driver.js.map |
@@ -11,4 +11,9 @@ import { RawQuery } from './builders'; | ||
Delete = 3, | ||
Schema = 4 | ||
Schema = 4, | ||
Transaction = 5 | ||
} | ||
export declare enum MigrationTransactionMode { | ||
None = 0, | ||
PerMigration = 1 | ||
} | ||
export interface IDriverOptions { | ||
@@ -24,3 +29,11 @@ Database?: string; | ||
Name: string; | ||
MigrationTable: string; | ||
Migration?: { | ||
Table?: string; | ||
Transaction?: { | ||
Mode?: MigrationTransactionMode; | ||
}; | ||
}; | ||
Debug?: { | ||
Queries?: boolean; | ||
}; | ||
} | ||
@@ -27,0 +40,0 @@ export interface IMigrationDescriptor { |
@@ -17,3 +17,9 @@ "use strict"; | ||
QueryContext[QueryContext["Schema"] = 4] = "Schema"; | ||
QueryContext[QueryContext["Transaction"] = 5] = "Transaction"; | ||
})(QueryContext = exports.QueryContext || (exports.QueryContext = {})); | ||
var MigrationTransactionMode; | ||
(function (MigrationTransactionMode) { | ||
MigrationTransactionMode[MigrationTransactionMode["None"] = 0] = "None"; | ||
MigrationTransactionMode[MigrationTransactionMode["PerMigration"] = 1] = "PerMigration"; | ||
})(MigrationTransactionMode = exports.MigrationTransactionMode || (exports.MigrationTransactionMode = {})); | ||
let OrmMigration = class OrmMigration { | ||
@@ -20,0 +26,0 @@ }; |
@@ -15,3 +15,3 @@ import { Configuration } from '@spinajs/configuration'; | ||
protected Configuration: Configuration; | ||
migrateUp(name?: string): Promise<void>; | ||
migrateUp(name?: string): Promise<boolean>; | ||
reloadTableInfo(): Promise<void>; | ||
@@ -18,0 +18,0 @@ resolveAsync(): Promise<void>; |
@@ -21,2 +21,3 @@ "use strict"; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
const interfaces_1 = require("./interfaces"); | ||
const model_1 = require("./model"); | ||
@@ -32,2 +33,3 @@ const decorators_1 = require("./decorators"); | ||
async migrateUp(name) { | ||
var _a, _b, _c, _d; | ||
const migrations = name ? this.Migrations.filter(m => m.name === name) : this.Migrations; | ||
@@ -37,7 +39,4 @@ for (const m of migrations) { | ||
const cn = this.Connections.get(md.Connection); | ||
const migrationTableName = cn.Options.MigrationTable || MIGRATION_TABLE_NAME; | ||
const migrationTable = await cn.tableInfo(migrationTableName); | ||
if (!migrationTable) { | ||
await _buildSchemaTable(cn, migrationTableName); | ||
} | ||
const migrationTableName = (_b = (_a = cn.Options.Migration) === null || _a === void 0 ? void 0 : _a.Table, (_b !== null && _b !== void 0 ? _b : MIGRATION_TABLE_NAME)); | ||
await _ensureMigrationTable(cn, migrationTableName); | ||
const exists = await cn.select().from(migrationTableName).where({ Migration: m.name }).first(); | ||
@@ -48,9 +47,17 @@ if (exists) { | ||
} | ||
const migration = this.Container.resolve(m.type, [cn]); | ||
try { | ||
await migration.up(cn); | ||
await cn.insert().into(migrationTableName).values({ | ||
Migration: m.name, | ||
CreatedAt: new Date() | ||
}); | ||
const migration = this.Container.resolve(m.type, [cn]); | ||
const trFunction = async (driver) => { | ||
await migration.up(driver); | ||
await driver.insert().into(migrationTableName).values({ | ||
Migration: m.name, | ||
CreatedAt: new Date() | ||
}); | ||
}; | ||
if (((_d = (_c = cn.Options.Migration) === null || _c === void 0 ? void 0 : _c.Transaction) === null || _d === void 0 ? void 0 : _d.Mode) === interfaces_1.MigrationTransactionMode.PerMigration) { | ||
await cn.transaction(trFunction); | ||
} | ||
else { | ||
await trFunction(cn); | ||
} | ||
this.Log.info(`Migration ${m.name} from file ${m.file} applied succesyfull`); | ||
@@ -60,10 +67,15 @@ } | ||
this.Log.error(ex, `Cannot execute migration ${m.name} from file ${m.file}`); | ||
return false; | ||
} | ||
} | ||
async function _buildSchemaTable(connection, mTableName) { | ||
await connection.schema().createTable(mTableName, (table) => { | ||
table.int("Id").autoIncrement().primaryKey(); | ||
table.string("Migration").notNull(); | ||
table.dateTime("CreatedAt").notNull(); | ||
}); | ||
return true; | ||
async function _ensureMigrationTable(connection, tableName) { | ||
const migrationTable = await connection.tableInfo(tableName); | ||
if (!migrationTable) { | ||
await connection.schema().createTable(tableName, (table) => { | ||
table.int("Id").autoIncrement().primaryKey(); | ||
table.string("Migration").notNull(); | ||
table.dateTime("CreatedAt").notNull(); | ||
}); | ||
} | ||
} | ||
@@ -86,3 +98,7 @@ } | ||
async resolveAsync() { | ||
const migrateOnStartup = this.Configuration.get("db.migrateOnStartup", false); | ||
await this.createConnections(); | ||
if (migrateOnStartup) { | ||
await this.migrateUp(); | ||
} | ||
await this.reloadTableInfo(); | ||
@@ -89,0 +105,0 @@ this.applyModelMixins(); |
{ | ||
"name": "@spinajs/orm", | ||
"version": "1.0.12", | ||
"version": "1.0.14", | ||
"description": "SpinaJS Orm module", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
160457
2167