decentraland-server
Advanced tools
Comparing version 1.1.1 to 1.2.0
import { Postgres } from './postgres'; | ||
export * from './SQL'; | ||
export * from './postgres'; | ||
@@ -4,0 +3,0 @@ export declare const clients: { |
@@ -7,5 +7,4 @@ "use strict"; | ||
var postgres_1 = require("./postgres"); | ||
__export(require("./SQL")); | ||
__export(require("./postgres")); | ||
exports.clients = { postgres: postgres_1.postgres }; | ||
//# sourceMappingURL=index.js.map |
@@ -5,2 +5,3 @@ import * as cli from './cli'; | ||
export { Model } from './Model'; | ||
export * from './sql'; | ||
export { cli, db, server }; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -11,2 +14,3 @@ var cli = require("./cli"); | ||
exports.Model = Model_1.Model; | ||
__export(require("./sql")); | ||
//# sourceMappingURL=index.js.map |
import { clients } from './db'; | ||
import { Postgres } from './db/postgres'; | ||
import { QueryPart } from './db/types'; | ||
export declare class Model { | ||
export declare class Model<T> { | ||
static tableName: string; | ||
static columnNames: string[]; | ||
static primaryKey: string; | ||
static db: Postgres; | ||
static setDb(clientName?: keyof typeof clients): void; | ||
static find(conditions?: QueryPart, orderBy?: QueryPart, extra?: string): Promise<any>; | ||
static findOne(primaryKeyOrCond: string | number | QueryPart, orderBy?: QueryPart): Promise<any>; | ||
static find<U>(conditions?: QueryPart, orderBy?: QueryPart, extra?: string): Promise<U[]>; | ||
static findOne<U>(primaryKeyOrCond: string | number | QueryPart, orderBy?: QueryPart): Promise<U>; | ||
static count(conditions: QueryPart, extra?: string): Promise<number>; | ||
static query(queryString: any, values: any): Promise<any[]>; | ||
static insert<T>(row: T): Promise<T>; | ||
static query<U>(queryString: any, values?: any[]): Promise<U[]>; | ||
static insert<U>(row: U): Promise<U>; | ||
static update(changes: QueryPart, conditions: QueryPart): Promise<any>; | ||
static delete(conditions: QueryPart): Promise<any>; | ||
static isIncomplete(attributes: any): boolean; | ||
attributes: any; | ||
constructor(attributes: any); | ||
retreive(): Promise<any>; | ||
insert(): Promise<any>; | ||
attributes: T; | ||
tableName: string; | ||
constructor(attributes: T); | ||
getConstructor(): typeof Model; | ||
retreive(): Promise<T>; | ||
insert(): Promise<T>; | ||
update(conditions: any): Promise<any>; | ||
delete(conditions: any): Promise<any>; | ||
isEmpty(): boolean; | ||
isIncomplete(): any; | ||
get(key?: string): any; | ||
getIn(keyPath: string[]): any; | ||
getIn(keyPath: string[]): T; | ||
set(key: string, value: any): this; | ||
setIn(keyPath: string[], value: any): this; | ||
} |
@@ -41,3 +41,4 @@ "use strict"; | ||
function Model(attributes) { | ||
this.attributes = attributes || {}; | ||
this.tableName = this.getConstructor().tableName; | ||
this.attributes = attributes; | ||
} | ||
@@ -89,3 +90,3 @@ Model.setDb = function (clientName) { | ||
switch (_a.label) { | ||
case 0: return [4, this.db.insert(this.tableName, pick(row, this.columnNames), this.primaryKey)]; | ||
case 0: return [4, this.db.insert(this.tableName, row, this.primaryKey)]; | ||
case 1: | ||
@@ -100,3 +101,3 @@ insertion = _a.sent(); | ||
Model.update = function (changes, conditions) { | ||
return this.db.update(this.tableName, pick(changes, this.columnNames), conditions); | ||
return this.db.update(this.tableName, changes, conditions); | ||
}; | ||
@@ -106,4 +107,4 @@ Model.delete = function (conditions) { | ||
}; | ||
Model.isIncomplete = function (attributes) { | ||
return this.columnNames.some(function (column) { return !attributes.hasOwnProperty(column); }); | ||
Model.prototype.getConstructor = function () { | ||
return this.constructor; | ||
}; | ||
@@ -116,3 +117,3 @@ Model.prototype.retreive = function () { | ||
case 0: | ||
Constructor = this.constructor.prototype; | ||
Constructor = this.getConstructor(); | ||
primaryKey = this.attributes[Constructor.primaryKey]; | ||
@@ -129,42 +130,21 @@ _a = this; | ||
Model.prototype.insert = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.constructor.prototype.insert(this.attributes)]; | ||
case 1: return [2, _a.sent()]; | ||
} | ||
}); | ||
}); | ||
return this.getConstructor().insert(this.attributes); | ||
}; | ||
Model.prototype.update = function (conditions) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var primaryKey, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!conditions) { | ||
primaryKey = this.constructor.prototype.primaryKey; | ||
conditions = (_a = {}, _a[primaryKey] = this.attributes[primaryKey], _a); | ||
} | ||
return [4, this.constructor.prototype.update(this.attributes, conditions)]; | ||
case 1: return [2, _b.sent()]; | ||
} | ||
}); | ||
}); | ||
var Constructor = this.getConstructor(); | ||
if (!conditions) { | ||
var primaryKey = Constructor.primaryKey; | ||
conditions = (_a = {}, _a[primaryKey] = this.attributes[primaryKey], _a); | ||
} | ||
return Constructor.update(this.attributes, conditions); | ||
var _a; | ||
}; | ||
Model.prototype.delete = function (conditions) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var primaryKey, _a; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!conditions) { | ||
primaryKey = this.constructor.prototype.primaryKey; | ||
conditions = (_a = {}, _a[primaryKey] = this.attributes[primaryKey], _a); | ||
} | ||
return [4, this.constructor.prototype.delete(conditions)]; | ||
case 1: return [2, _b.sent()]; | ||
} | ||
}); | ||
}); | ||
var Constructor = this.getConstructor(); | ||
if (!conditions) { | ||
var primaryKey = Constructor.primaryKey; | ||
conditions = (_a = {}, _a[primaryKey] = this.attributes[primaryKey], _a); | ||
} | ||
return Constructor.delete(conditions); | ||
var _a; | ||
}; | ||
@@ -174,5 +154,2 @@ Model.prototype.isEmpty = function () { | ||
}; | ||
Model.prototype.isIncomplete = function () { | ||
return this.constructor.prototype.isIncomplete(this.attributes); | ||
}; | ||
Model.prototype.get = function (key) { | ||
@@ -212,3 +189,2 @@ return key ? this.attributes[key] : this.attributes; | ||
Model.tableName = null; | ||
Model.columnNames = []; | ||
Model.primaryKey = 'id'; | ||
@@ -219,12 +195,2 @@ Model.db = db_1.clients.postgres; | ||
exports.Model = Model; | ||
function pick(obj, keys) { | ||
var result = {}; | ||
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) { | ||
var key = keys_1[_i]; | ||
if (obj.hasOwnProperty(key)) { | ||
result[key] = obj[key]; | ||
} | ||
} | ||
return result; | ||
} | ||
//# sourceMappingURL=Model.js.map |
{ | ||
"name": "decentraland-server", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Set of common functionality accross Decentraland projects servers.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/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
29
56525
810