@spinajs/orm
Advanced tools
Comparing version 1.0.7 to 1.0.10
import { Container } from "@spinajs/di"; | ||
import { QueryMethod, WhereBoolean, WhereOperators } from "./enums"; | ||
import { IColumnsBuilder, ICompilerOutput, ILimitBuilder, IOrderByBuilder, IQueryBuilder, IQueryLimit, ISelectQueryBuilder, ISort, IWhereBuilder, OrmDriver } from "./interfaces"; | ||
import { IColumnsBuilder, ICompilerOutput, ILimitBuilder, IOrderByBuilder, IQueryBuilder, IQueryLimit, ISelectQueryBuilder, ISort, IWhereBuilder, QueryContext } from "./interfaces"; | ||
import { ColumnStatement, IQueryStatement } from "./statements"; | ||
import { WhereFunction } from "./types"; | ||
import { OrmDriver } from "./driver"; | ||
export declare class QueryBuilder implements IQueryBuilder { | ||
@@ -15,2 +16,3 @@ protected _method: QueryMethod; | ||
protected _nonSelect: boolean; | ||
protected _queryContext: QueryContext; | ||
constructor(container: Container, driver: OrmDriver, model?: Constructor<any>); | ||
@@ -22,3 +24,3 @@ get Table(): string; | ||
toDB(): ICompilerOutput; | ||
then(resolve: (rows: any[]) => void, reject: (err: Error) => void): void; | ||
then(resolve: (rows: any[]) => void, reject: (err: Error) => void): Promise<void>; | ||
setTable(table: string, alias?: string): this; | ||
@@ -107,3 +109,3 @@ from(table: string, alias?: string): this; | ||
toDB(): ICompilerOutput; | ||
then(resolve: (rows: any[]) => void, reject: (err: Error) => void): void; | ||
then(resolve: (rows: any[]) => void, reject: (err: Error) => void): Promise<void>; | ||
} | ||
@@ -110,0 +112,0 @@ export interface DeleteQueryBuilder extends IWhereBuilder, ILimitBuilder { |
@@ -28,2 +28,3 @@ "use strict"; | ||
const statements_1 = require("./statements"); | ||
const driver_1 = require("./driver"); | ||
function isWhereOperator(val) { | ||
@@ -60,3 +61,3 @@ return util_1.isString(val) && Object.values(enums_1.WhereOperators).includes(val.toLowerCase()); | ||
const compiled = this.toDB(); | ||
this._driver.execute(compiled.expression, compiled.bindings).then((result) => { | ||
return this._driver.execute(compiled.expression, compiled.bindings, this._queryContext).then((result) => { | ||
if (this._model && !this._nonSelect) { | ||
@@ -87,3 +88,3 @@ resolve(result.map(r => { | ||
di_1.Inject(di_1.Container), | ||
__metadata("design:paramtypes", [di_1.Container, interfaces_1.OrmDriver, Object]) | ||
__metadata("design:paramtypes", [di_1.Container, driver_1.OrmDriver, Object]) | ||
], QueryBuilder); | ||
@@ -364,2 +365,3 @@ exports.QueryBuilder = QueryBuilder; | ||
this._nonSelect = false; | ||
this._queryContext = interfaces_1.QueryContext.Select; | ||
} | ||
@@ -402,3 +404,3 @@ get IsDistinct() { | ||
then(resolve, reject) { | ||
super.then((result) => { | ||
return super.then((result) => { | ||
if (this._first) { | ||
@@ -409,3 +411,3 @@ if (this._fail && result.length === 0) { | ||
else { | ||
resolve(result[0]); | ||
resolve(result ? result[0] : null); | ||
} | ||
@@ -435,2 +437,3 @@ } | ||
}; | ||
this._queryContext = interfaces_1.QueryContext.Delete; | ||
} | ||
@@ -460,2 +463,3 @@ get Truncate() { | ||
this._statements = []; | ||
this._queryContext = interfaces_1.QueryContext.Update; | ||
} | ||
@@ -488,2 +492,3 @@ get Value() { | ||
this._values = []; | ||
this._queryContext = interfaces_1.QueryContext.Insert; | ||
} | ||
@@ -599,2 +604,3 @@ get Values() { | ||
this.setTable(name); | ||
this._queryContext = interfaces_1.QueryContext.Schema; | ||
} | ||
@@ -635,3 +641,3 @@ get Columns() { | ||
di_1.Inject(di_1.Container), | ||
__metadata("design:paramtypes", [di_1.Container, interfaces_1.OrmDriver]) | ||
__metadata("design:paramtypes", [di_1.Container, driver_1.OrmDriver]) | ||
], SchemaQueryBuilder); | ||
@@ -638,0 +644,0 @@ exports.SchemaQueryBuilder = SchemaQueryBuilder; |
@@ -0,3 +1,7 @@ | ||
import { IModelDescrtiptor } from "./interfaces"; | ||
import "reflect-metadata"; | ||
export declare const MODEL_DESCTRIPTION_SYMBOL: unique symbol; | ||
export declare const MIGRATION_DESCRIPTION_SYMBOL: unique symbol; | ||
export declare function extractDecoratorDescriptor(callback: (model: IModelDescrtiptor, target: any, propertyKey: symbol | string, indexOrDescriptor: number | PropertyDescriptor) => void, base?: boolean): any; | ||
export declare function Migration(connection: string): (target: any) => void; | ||
export declare function Connection(name: string): any; | ||
@@ -4,0 +8,0 @@ export declare function Model(tableName: string): any; |
@@ -5,3 +5,4 @@ "use strict"; | ||
exports.MODEL_DESCTRIPTION_SYMBOL = Symbol.for("MODEL_DESCRIPTOR"); | ||
function _model(callback, base = false) { | ||
exports.MIGRATION_DESCRIPTION_SYMBOL = Symbol.for("MIGRATION_DESCRIPTOR"); | ||
function extractDecoratorDescriptor(callback, base = false) { | ||
return (target, propertyKey, indexOrDescriptor) => { | ||
@@ -17,3 +18,3 @@ let metadata = null; | ||
metadata = { | ||
Columns: null, | ||
Columns: [], | ||
Connection: null, | ||
@@ -45,4 +46,18 @@ PrimaryKey: "", | ||
} | ||
exports.extractDecoratorDescriptor = extractDecoratorDescriptor; | ||
function Migration(connection) { | ||
return (target) => { | ||
let metadata = target[exports.MIGRATION_DESCRIPTION_SYMBOL]; | ||
if (!metadata) { | ||
metadata = { | ||
Connection: "" | ||
}; | ||
target[exports.MIGRATION_DESCRIPTION_SYMBOL] = metadata; | ||
} | ||
metadata.Connection = connection; | ||
}; | ||
} | ||
exports.Migration = Migration; | ||
function Connection(name) { | ||
return _model((model) => { | ||
return extractDecoratorDescriptor((model) => { | ||
model.Connection = name; | ||
@@ -53,3 +68,3 @@ }, true); | ||
function Model(tableName) { | ||
return _model((model) => { | ||
return extractDecoratorDescriptor((model) => { | ||
model.TableName = tableName; | ||
@@ -60,3 +75,3 @@ }, true); | ||
function CreatedAt() { | ||
return _model((model, target, propertyKey) => { | ||
return extractDecoratorDescriptor((model, target, propertyKey) => { | ||
const type = Reflect.getMetadata('design:type', target, propertyKey); | ||
@@ -71,3 +86,3 @@ if (type.name !== "Date") { | ||
function UpdatedAt() { | ||
return _model((model, target, propertyKey) => { | ||
return extractDecoratorDescriptor((model, target, propertyKey) => { | ||
const type = Reflect.getMetadata('design:type', target, propertyKey); | ||
@@ -82,3 +97,3 @@ if (type.name !== "Date") { | ||
function SoftDelete() { | ||
return _model((model, target, propertyKey) => { | ||
return extractDecoratorDescriptor((model, target, propertyKey) => { | ||
const type = Reflect.getMetadata('design:type', target, propertyKey); | ||
@@ -93,3 +108,3 @@ if (type.name !== "Date") { | ||
function Archived() { | ||
return _model((model, target, propertyKey) => { | ||
return extractDecoratorDescriptor((model, target, propertyKey) => { | ||
const type = Reflect.getMetadata('design:type', target, propertyKey); | ||
@@ -104,3 +119,3 @@ if (type.name !== "Date") { | ||
function Primary() { | ||
return _model((model, _target, propertyKey) => { | ||
return extractDecoratorDescriptor((model, _target, propertyKey) => { | ||
model.PrimaryKey = propertyKey; | ||
@@ -107,0 +122,0 @@ }); |
"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; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const di_1 = require("@spinajs/di"); | ||
class ModelHydrator { | ||
} | ||
exports.ModelHydrator = ModelHydrator; | ||
let PropertyHydrator = class PropertyHydrator extends ModelHydrator { | ||
class PropertyHydrator extends ModelHydrator { | ||
hydrate(target, values) { | ||
@@ -23,18 +16,12 @@ const descriptor = target.ModelDescriptor; | ||
const column = (_a = descriptor.Columns) === null || _a === void 0 ? void 0 : _a.find(c => c.Name === k); | ||
target[k] = column ? column.Converter.fromDB(values[k]) : values[k]; | ||
target[k] = column.Converter ? column.Converter.fromDB(values[k]) : values[k]; | ||
}); | ||
} | ||
}; | ||
PropertyHydrator = __decorate([ | ||
di_1.Injectable(ModelHydrator) | ||
], PropertyHydrator); | ||
} | ||
exports.PropertyHydrator = PropertyHydrator; | ||
let JoinHydrator = class JoinHydrator extends ModelHydrator { | ||
class JoinHydrator extends ModelHydrator { | ||
hydrate(_target, _values) { | ||
} | ||
}; | ||
JoinHydrator = __decorate([ | ||
di_1.Injectable(ModelHydrator) | ||
], JoinHydrator); | ||
} | ||
exports.JoinHydrator = JoinHydrator; | ||
//# sourceMappingURL=hydrators.js.map |
@@ -10,1 +10,2 @@ export * from "./interfaces"; | ||
export * from "./hydrators"; | ||
export * from "./driver"; |
@@ -14,2 +14,3 @@ "use strict"; | ||
__export(require("./hydrators")); | ||
__export(require("./driver")); | ||
//# sourceMappingURL=index.js.map |
@@ -5,3 +5,10 @@ import { RawQuery } from './builders'; | ||
import { WhereFunction } from './types'; | ||
import { ResolveStrategy, IContainer } from '@spinajs/di'; | ||
import { OrmDriver } from './driver'; | ||
export declare enum QueryContext { | ||
Insert = 0, | ||
Select = 1, | ||
Update = 2, | ||
Delete = 3, | ||
Schema = 4 | ||
} | ||
export interface IDriverOptions { | ||
@@ -18,12 +25,4 @@ Database?: string; | ||
} | ||
export declare abstract class OrmDriver extends ResolveStrategy { | ||
Options: IDriverOptions; | ||
Container: IContainer; | ||
constructor(container: IContainer, options: IDriverOptions); | ||
abstract execute(stmt: string | object, params?: any[]): Promise<any[] | any>; | ||
abstract ping(): Promise<void>; | ||
abstract connect(): Promise<void>; | ||
abstract disconnect(): void; | ||
abstract tableInfo(name: string, schema?: string): Promise<IColumnDescriptor[]>; | ||
abstract resolve(container: IContainer): void; | ||
export interface IMigrationDescriptor { | ||
Connection: string; | ||
} | ||
@@ -64,2 +63,6 @@ export interface IModelDescrtiptor { | ||
} | ||
export declare abstract class OrmMigration { | ||
abstract up(connection: OrmDriver): Promise<void>; | ||
abstract down(connection: OrmDriver): Promise<void>; | ||
} | ||
export interface IModelArchivedDescriptor { | ||
@@ -66,0 +69,0 @@ ArchivedAt: string; |
"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; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const di_1 = require("@spinajs/di"); | ||
class OrmDriver extends di_1.ResolveStrategy { | ||
constructor(container, options) { | ||
super(); | ||
this.Options = options; | ||
this.Container = container; | ||
} | ||
} | ||
exports.OrmDriver = OrmDriver; | ||
class SelectQueryCompiler { | ||
} | ||
var QueryContext; | ||
(function (QueryContext) { | ||
QueryContext[QueryContext["Insert"] = 0] = "Insert"; | ||
QueryContext[QueryContext["Select"] = 1] = "Select"; | ||
QueryContext[QueryContext["Update"] = 2] = "Update"; | ||
QueryContext[QueryContext["Delete"] = 3] = "Delete"; | ||
QueryContext[QueryContext["Schema"] = 4] = "Schema"; | ||
})(QueryContext = exports.QueryContext || (exports.QueryContext = {})); | ||
let OrmMigration = class OrmMigration { | ||
}; | ||
OrmMigration = __decorate([ | ||
di_1.NewInstance() | ||
], OrmMigration); | ||
exports.OrmMigration = OrmMigration; | ||
let SelectQueryCompiler = class SelectQueryCompiler { | ||
}; | ||
SelectQueryCompiler = __decorate([ | ||
di_1.NewInstance() | ||
], SelectQueryCompiler); | ||
exports.SelectQueryCompiler = SelectQueryCompiler; | ||
class DeleteQueryCompiler { | ||
} | ||
let DeleteQueryCompiler = class DeleteQueryCompiler { | ||
}; | ||
DeleteQueryCompiler = __decorate([ | ||
di_1.NewInstance() | ||
], DeleteQueryCompiler); | ||
exports.DeleteQueryCompiler = DeleteQueryCompiler; | ||
class UpdateQueryCompiler { | ||
} | ||
let UpdateQueryCompiler = class UpdateQueryCompiler { | ||
}; | ||
UpdateQueryCompiler = __decorate([ | ||
di_1.NewInstance() | ||
], UpdateQueryCompiler); | ||
exports.UpdateQueryCompiler = UpdateQueryCompiler; | ||
class InsertQueryCompiler { | ||
} | ||
let InsertQueryCompiler = class InsertQueryCompiler { | ||
}; | ||
InsertQueryCompiler = __decorate([ | ||
di_1.NewInstance() | ||
], InsertQueryCompiler); | ||
exports.InsertQueryCompiler = InsertQueryCompiler; | ||
class TableQueryCompiler { | ||
} | ||
let TableQueryCompiler = class TableQueryCompiler { | ||
}; | ||
TableQueryCompiler = __decorate([ | ||
di_1.NewInstance() | ||
], TableQueryCompiler); | ||
exports.TableQueryCompiler = TableQueryCompiler; | ||
let ColumnQueryCompiler = class ColumnQueryCompiler { | ||
}; | ||
ColumnQueryCompiler = __decorate([ | ||
di_1.NewInstance() | ||
], ColumnQueryCompiler); | ||
exports.ColumnQueryCompiler = ColumnQueryCompiler; | ||
//# sourceMappingURL=interfaces.js.map |
@@ -5,2 +5,3 @@ import { IModelDescrtiptor } from "./interfaces"; | ||
import { WhereOperators } from './enums'; | ||
export declare function extractModelDescriptor(target: any): IModelDescrtiptor; | ||
export declare abstract class ModelBase<T> { | ||
@@ -24,3 +25,3 @@ get ModelDescriptor(): IModelDescrtiptor; | ||
save(): Promise<void>; | ||
abstract fresh(): Promise<T>; | ||
fresh(): Promise<T>; | ||
protected defaults(): void; | ||
@@ -27,0 +28,0 @@ } |
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -8,5 +15,30 @@ const decorators_1 = require("./decorators"); | ||
const hydrators_1 = require("./hydrators"); | ||
const _ = __importStar(require("lodash")); | ||
function extractModelDescriptor(target) { | ||
const descriptor = {}; | ||
_reduce(target); | ||
return descriptor; | ||
function _reduce(t) { | ||
if (!t) { | ||
return; | ||
} | ||
if (t[decorators_1.MODEL_DESCTRIPTION_SYMBOL]) { | ||
_.mergeWith(descriptor, t[decorators_1.MODEL_DESCTRIPTION_SYMBOL], (a, b) => { | ||
if (!a) { | ||
return b; | ||
} | ||
if (Array.isArray(a)) { | ||
return a.concat(b); | ||
} | ||
return a; | ||
}); | ||
} | ||
_reduce(t.prototype); | ||
_reduce(t.__proto__); | ||
} | ||
} | ||
exports.extractModelDescriptor = extractModelDescriptor; | ||
class ModelBase { | ||
get ModelDescriptor() { | ||
return this.constructor[decorators_1.MODEL_DESCTRIPTION_SYMBOL]; | ||
return extractModelDescriptor(this.constructor); | ||
} | ||
@@ -81,2 +113,5 @@ get PrimaryKeyName() { | ||
} | ||
async fresh() { | ||
return this.constructor.find(this.PrimaryKeyValue); | ||
} | ||
defaults() { | ||
@@ -83,0 +118,0 @@ var _a; |
@@ -1,12 +0,16 @@ | ||
import { AsyncResolveStrategy, IContainer } from "@spinajs/di"; | ||
import { AsyncResolveStrategy, Container, IContainer } from "@spinajs/di"; | ||
import { ClassInfo } from "@spinajs/reflection"; | ||
import { OrmDriver } from "./interfaces"; | ||
import { OrmMigration } from "./interfaces"; | ||
import { ModelBase } from "./model"; | ||
import { OrmDriver } from './driver'; | ||
export declare class Orm extends AsyncResolveStrategy { | ||
Models: Array<ClassInfo<ModelBase<any>>>; | ||
Migrations: Array<ClassInfo<OrmMigration>>; | ||
Connections: Map<string, OrmDriver>; | ||
Container: IContainer; | ||
Container: Container; | ||
private Log; | ||
private Configuration; | ||
migrateUp(name?: string): Promise<void>; | ||
reloadTableInfo(): Promise<void>; | ||
resolveAsync(container: IContainer): Promise<void>; | ||
} |
@@ -29,2 +29,25 @@ "use strict"; | ||
} | ||
async migrateUp(name) { | ||
const migrations = name ? this.Migrations.filter(m => m.name === name) : this.Migrations; | ||
for (const m of migrations) { | ||
const md = m.type[decorators_1.MIGRATION_DESCRIPTION_SYMBOL]; | ||
const cn = this.Connections.get(md.Connection); | ||
const migration = this.Container.resolve(m.type, [cn]); | ||
await migration.up(cn); | ||
} | ||
} | ||
async reloadTableInfo() { | ||
for (const m of this.Models) { | ||
const descriptor = model_1.extractModelDescriptor(m.type); | ||
if (descriptor) { | ||
const connection = this.Connections.get(descriptor.Connection); | ||
if (connection) { | ||
const columns = await connection.tableInfo(descriptor.TableName, connection.Options.Database); | ||
if (columns) { | ||
m.type[decorators_1.MODEL_DESCTRIPTION_SYMBOL].Columns = lodash_1.default.uniqBy(descriptor.Columns.concat(columns), "Name"); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
async resolveAsync(container) { | ||
@@ -42,3 +65,3 @@ const connections = this.Configuration.get("db.connections", []); | ||
await Promise.all(Array.from(this.Connections.values()).map((d) => { | ||
d.connect(); | ||
return d.connect(); | ||
})); | ||
@@ -49,10 +72,4 @@ for (const m of this.Models) { | ||
} | ||
const descriptor = m.type[decorators_1.MODEL_DESCTRIPTION_SYMBOL]; | ||
if (descriptor) { | ||
const connection = this.Connections.get(descriptor.Connection); | ||
if (connection) { | ||
descriptor.Columns = await connection.tableInfo(descriptor.TableName, connection.Options.Database); | ||
} | ||
} | ||
} | ||
await this.reloadTableInfo(); | ||
} | ||
@@ -69,2 +86,10 @@ catch (err) { | ||
__decorate([ | ||
reflection_1.ListFromFiles("/**/*.{ts,js}", "system.dirs.migrations"), | ||
__metadata("design:type", Array) | ||
], Orm.prototype, "Migrations", void 0); | ||
__decorate([ | ||
di_2.Autoinject(), | ||
__metadata("design:type", di_1.Container) | ||
], Orm.prototype, "Container", void 0); | ||
__decorate([ | ||
log_1.Logger({ module: "ORM" }), | ||
@@ -71,0 +96,0 @@ __metadata("design:type", Object) |
{ | ||
"name": "@spinajs/orm", | ||
"version": "1.0.7", | ||
"version": "1.0.10", | ||
"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
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
152832
42
2060
0