decentraland-server
Advanced tools
Comparing version 1.5.0 to 1.6.0
import { Postgres } from './postgres'; | ||
export * from './postgres'; | ||
export * from './types'; | ||
export declare const clients: { | ||
postgres: Postgres; | ||
}; |
import * as pg from 'pg'; | ||
import { Column, OrderClause, QueryArgument, QueryPart } from './types'; | ||
import { Column, OrderClause, QueryArgument, QueryPart, OnConflict } from './types'; | ||
export declare class Postgres { | ||
@@ -12,4 +12,4 @@ client: pg.Client; | ||
selectOne(tableName: string, conditions?: QueryPart, orderBy?: QueryPart): Promise<any>; | ||
_query(method: string, tableName: string, conditions?: QueryPart, orderBy?: QueryPart, extra?: string): Promise<any[]>; | ||
insert(tableName: string, changes: QueryPart, primaryKey?: string): Promise<any>; | ||
private _query(method, tableName, conditions?, orderBy?, extra?); | ||
insert(tableName: string, changes: QueryPart, primaryKey?: string, onConflict?: OnConflict): Promise<any>; | ||
update(tableName: string, changes: QueryPart, conditions: QueryPart): Promise<any>; | ||
@@ -27,2 +27,3 @@ delete(tableName: string, conditions: QueryPart): Promise<any>; | ||
truncate(tableName: string): Promise<pg.QueryResult>; | ||
toOnConflictUpsert({target, changes}: OnConflict, indexStart?: number): string; | ||
toColumnFields(columns: Column): string[]; | ||
@@ -29,0 +30,0 @@ toAssignmentFields(columns: Column, start?: number): string[]; |
@@ -103,6 +103,7 @@ "use strict"; | ||
}; | ||
Postgres.prototype.insert = function (tableName, changes, primaryKey) { | ||
Postgres.prototype.insert = function (tableName, changes, primaryKey, onConflict) { | ||
if (primaryKey === void 0) { primaryKey = 'id'; } | ||
if (onConflict === void 0) { onConflict = { target: [], changes: {} }; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var values; | ||
var values, conflictValues; | ||
return __generator(this, function (_a) { | ||
@@ -114,6 +115,5 @@ switch (_a.label) { | ||
} | ||
changes.created_at = changes.created_at || new Date(); | ||
changes.updated_at = changes.updated_at || new Date(); | ||
values = Object.values(changes); | ||
return [4, this.client.query("INSERT INTO " + tableName + "(\n " + this.toColumnFields(changes) + "\n ) VALUES(\n " + this.toValuePlaceholders(changes) + "\n ) RETURNING " + primaryKey, values)]; | ||
conflictValues = Object.values(onConflict.changes); | ||
return [4, this.client.query("INSERT INTO " + tableName + "(\n " + this.toColumnFields(changes) + "\n ) VALUES(\n " + this.toValuePlaceholders(changes) + "\n )\n " + this.toOnConflictUpsert(onConflict, values.length) + "\n RETURNING " + primaryKey, values.concat(conflictValues))]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -136,3 +136,2 @@ } | ||
} | ||
changes.updated_at = changes.updated_at || new Date(); | ||
changeValues = Object.values(changes); | ||
@@ -168,3 +167,3 @@ conditionValues = Object.values(conditions); | ||
_c.label = 2; | ||
case 2: return [4, this.client.query("CREATE TABLE IF NOT EXISTS \"" + tableName + "\" (\n " + rows + "\n \"created_at\" timestamp NOT NULL,\n \"updated_at\" timestamp,\n PRIMARY KEY (\"" + primaryKey + "\")\n );")]; | ||
case 2: return [4, this.client.query("CREATE TABLE IF NOT EXISTS \"" + tableName + "\" (\n " + rows + "\n PRIMARY KEY (\"" + primaryKey + "\")\n );")]; | ||
case 3: | ||
@@ -201,2 +200,9 @@ _c.sent(); | ||
}; | ||
Postgres.prototype.toOnConflictUpsert = function (_a, indexStart) { | ||
var target = _a.target, changes = _a.changes; | ||
if (indexStart === void 0) { indexStart = 0; } | ||
return target.length > 0 | ||
? "ON CONFLICT (" + target + ") DO\n UPDATE SET " + this.toAssignmentFields(changes, indexStart) | ||
: 'ON CONFLICT DO NOTHING'; | ||
}; | ||
Postgres.prototype.toColumnFields = function (columns) { | ||
@@ -203,0 +209,0 @@ var columnNames = Object.keys(columns); |
@@ -14,1 +14,6 @@ export interface QueryArgument { | ||
} | ||
export interface OnConflict<U extends QueryPart = any> { | ||
target: (keyof U)[]; | ||
changes: QueryPart; | ||
} | ||
export declare type PrimaryKey = string | number; |
import { clients } from './db'; | ||
import { Postgres } from './db/postgres'; | ||
import { QueryPart } from './db/types'; | ||
import { PrimaryKey, QueryPart } from './db/types'; | ||
export declare class Model<T> { | ||
static tableName: string; | ||
static primaryKey: string; | ||
static withTimestamps: boolean; | ||
static db: Postgres; | ||
static setDb(clientName?: keyof typeof clients): void; | ||
static find<U = any>(conditions?: QueryPart, orderBy?: QueryPart, extra?: string): Promise<U[]>; | ||
static findOne<U = any>(primaryKeyOrCond: string | number | QueryPart, orderBy?: QueryPart): Promise<U>; | ||
static count(conditions: QueryPart, extra?: string): Promise<number>; | ||
static find<U extends QueryPart = any>(conditions?: Partial<U>, orderBy?: Partial<U>, extra?: string): Promise<U[]>; | ||
static findOne<U = any, P extends QueryPart = any>(primaryKey: PrimaryKey, orderBy?: Partial<P>): any; | ||
static findOne<U extends QueryPart = any, P extends QueryPart = any>(conditions: Partial<U>, orderBy?: Partial<P>): any; | ||
static count<U extends QueryPart = any>(conditions: Partial<U>, extra?: string): Promise<number>; | ||
static query<U = any>(queryString: any, values?: any[]): Promise<U[]>; | ||
static insert<U = any>(row: U): Promise<U>; | ||
static update(changes: QueryPart, conditions: QueryPart): Promise<any>; | ||
static delete(conditions: QueryPart): Promise<any>; | ||
static insert<U extends QueryPart = any>(row: U): Promise<U>; | ||
static upsert<U extends QueryPart = any>(row: U, target: (keyof U)[], changes?: Partial<U>): Promise<U>; | ||
private static _insert<U>(row, onConflict?); | ||
static update<U extends QueryPart = any, P extends QueryPart = any>(changes: Partial<U>, conditions: Partial<P>): Promise<any>; | ||
static delete<U extends QueryPart = any>(conditions: Partial<U>): Promise<any>; | ||
attributes: T; | ||
@@ -24,2 +28,3 @@ tableName: string; | ||
insert(): Promise<T>; | ||
upsert<K extends keyof T>(target: K[], changes?: Partial<T>): Promise<T>; | ||
update(conditions?: Partial<T>): Promise<any>; | ||
@@ -26,0 +31,0 @@ delete(conditions?: Partial<T>): Promise<any>; |
@@ -88,6 +88,36 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var insertion; | ||
return __generator(this, function (_a) { | ||
return [2, this._insert(row)]; | ||
}); | ||
}); | ||
}; | ||
Model.upsert = function (row, target, changes) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var onConflict; | ||
return __generator(this, function (_a) { | ||
onConflict = { target: target, changes: changes }; | ||
return [2, this._insert(row, onConflict)]; | ||
}); | ||
}); | ||
}; | ||
Model._insert = function (row, onConflict) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var createdAt, updatedAt, insertion; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4, this.db.insert(this.tableName, row, this.primaryKey)]; | ||
case 0: | ||
createdAt = new Date(); | ||
updatedAt = new Date(); | ||
if (onConflict) { | ||
onConflict.changes = onConflict.changes || row; | ||
} | ||
if (this.withTimestamps) { | ||
row.created_at = row.created_at || createdAt; | ||
row.updated_at = row.updated_at || updatedAt; | ||
if (onConflict) { | ||
onConflict.changes.updated_at = | ||
onConflict.changes.updated_at || updatedAt; | ||
} | ||
} | ||
return [4, this.db.insert(this.tableName, row, this.primaryKey, onConflict)]; | ||
case 1: | ||
@@ -102,2 +132,5 @@ insertion = _a.sent(); | ||
Model.update = function (changes, conditions) { | ||
if (this.withTimestamps) { | ||
changes.updated_at = changes.updated_at || new Date(); | ||
} | ||
return this.db.update(this.tableName, changes, conditions); | ||
@@ -136,2 +169,5 @@ }; | ||
}; | ||
Model.prototype.upsert = function (target, changes) { | ||
return this.getConstructor().upsert(this.attributes, target, changes); | ||
}; | ||
Model.prototype.update = function (conditions) { | ||
@@ -185,2 +221,3 @@ var query = conditions ? conditions : this.getDefaultQuery(); | ||
Model.primaryKey = 'id'; | ||
Model.withTimestamps = true; | ||
Model.db = db_1.clients.postgres; | ||
@@ -187,0 +224,0 @@ return Model; |
{ | ||
"name": "decentraland-server", | ||
"version": "1.5.0", | ||
"version": "1.6.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
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
60775
866