Comparing version 1.0.3 to 1.0.4
@@ -1,1 +0,10 @@ | ||
export declare const groupBy: () => void; | ||
type Calc = {}; | ||
type GroupByQuery = { | ||
filed: string; | ||
distint?: boolean; | ||
calc?: Calc; | ||
order?: string; | ||
by?: string; | ||
}; | ||
export declare const groupBy: (groupQuery: GroupByQuery) => void; | ||
export {}; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.groupBy = void 0; | ||
const groupBy = () => { | ||
const lodash_1 = __importDefault(require("lodash")); | ||
const groupBy = (groupQuery) => { | ||
let fields = []; | ||
if (groupQuery.distint) { | ||
fields.push(`DISTINCT(${groupQuery.filed}}) AS ${groupQuery.filed}`); | ||
} | ||
else { | ||
fields.push(groupQuery.filed); | ||
} | ||
if (groupQuery.calc) { | ||
if (lodash_1.default.isArray(groupQuery.calc)) { | ||
} | ||
else { | ||
} | ||
} | ||
else { | ||
fields.push(`COUNT(${groupQuery.filed} AS total) `); | ||
} | ||
// const SQL = `G` | ||
}; | ||
exports.groupBy = groupBy; |
import type { QueryResult } from 'pg'; | ||
export declare abstract class BaseQuery { | ||
/** | ||
* Get Database connection form provider | ||
*/ | ||
protected db(): import("pg").ClientBase; | ||
/** | ||
* Exec | ||
*/ | ||
sql(sql: string, params?: (string | number | boolean)[]): Promise<QueryResult>; | ||
} |
@@ -6,5 +6,11 @@ "use strict"; | ||
class BaseQuery { | ||
/** | ||
* Get Database connection form provider | ||
*/ | ||
db() { | ||
return (0, Util_1.getDB)(); | ||
} | ||
/** | ||
* Exec | ||
*/ | ||
sql(sql, params) { | ||
@@ -11,0 +17,0 @@ return this.db().query(sql, params); |
import type { TObject, Static } from '@sinclair/typebox'; | ||
import type { QuerySchema, WhereCondition, WhereItem } from './types'; | ||
import { BaseQuery } from './BaseQuery'; | ||
type TableOptions = { | ||
import { BaseView } from './BaseView'; | ||
export declare class BaseTable<T extends TObject> extends BaseView<T> { | ||
/** | ||
* Table 所在的 Schema 默认为 'public' | ||
*/ | ||
schema?: string; | ||
/** | ||
* KEY 标识字段,默认为 "id" | ||
*/ | ||
key?: string; | ||
/** | ||
* The Table's Default Sorting Rule / Order Field | ||
*/ | ||
default_order?: string; | ||
/** | ||
* The Table's Default Sorting Rule / By Method | ||
*/ | ||
default_by?: 'asc' | 'desc'; | ||
/** | ||
* 1. PageSize , if not specified , use DEFAULT_PAGESIZE | ||
* 2. DEFAULT_PAGESIZE can use setup to specified default : 10 | ||
*/ | ||
default_pagesize?: number; | ||
/** | ||
* 默认查询过滤,比如 {field:'disabled',value:0,operation:'<>'} | ||
* 设置后,通过 query / all 拼装的 sql 都会带上 AND disabled <> 0 | ||
*/ | ||
globalQueryCondition: WhereItem[]; | ||
}; | ||
export declare class BaseTable<T extends TObject> extends BaseQuery { | ||
private _table; | ||
private _CONFIG; | ||
private _QUERY_CACHE; | ||
constructor(tableName: string, schema: T, options?: TableOptions); | ||
private _query; | ||
private toWhereCondition; | ||
/** | ||
* @see WhereCondition | ||
* Use a WhereCondition Query Data | ||
*/ | ||
queryByCondition(query?: WhereCondition): void; | ||
/** | ||
* @see QuerySchema | ||
* Use a QuerySchema Query Data | ||
*/ | ||
query(query?: QuerySchema): Promise<Static<T>[]>; | ||
/** | ||
* @see QuerySchema | ||
* Use a QuerySchema Query Data With Page | ||
* this will return a object with {total:number,list:any[]} | ||
* | ||
*/ | ||
queryPager(query?: QuerySchema): Promise<{ | ||
total: number; | ||
list: Static<T>[]; | ||
}>; | ||
all(): Promise<Static<T>[]>; | ||
getById(id: number | string): Promise<Static<T>>; | ||
/** | ||
* check row data while insert or update | ||
@@ -70,2 +13,1 @@ * and auto warp the data | ||
} | ||
export {}; |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -18,114 +9,6 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const base_1 = require("../base"); | ||
const BaseQuery_1 = require("./BaseQuery"); | ||
const QueryPagition_1 = require("./QueryPagition"); | ||
const QueryWhere_1 = require("./QueryWhere"); | ||
const BaseView_1 = require("./BaseView"); | ||
const QueryBuilder_1 = require("./QueryBuilder"); | ||
const DEFAULT_SCHEMA = 'public'; | ||
class BaseTable extends BaseQuery_1.BaseQuery { | ||
constructor(tableName, schema, options) { | ||
super(); | ||
this._CONFIG = { | ||
key: 'id', | ||
order: 'id', | ||
by: 'desc', | ||
query_fields: '*', | ||
FIELD_MAP: new Map(), | ||
}; | ||
this._QUERY_CACHE = new Map(); | ||
let fields = []; | ||
this._CONFIG.FIELD_MAP = new Map(); | ||
lodash_1.default.keys(schema.properties).map(field => { | ||
let properties = schema.properties[field]; | ||
this._CONFIG.FIELD_MAP.set(field, properties); | ||
if (properties.column) { | ||
fields.push(`${properties.column} as ${field}`); | ||
} | ||
if (properties.ignore === false) { | ||
return; | ||
} | ||
else { | ||
fields.push(field); | ||
} | ||
}); | ||
this._table = DEFAULT_SCHEMA + '.' + tableName; | ||
this._CONFIG.query_fields = fields.join(','); | ||
if (options == null) | ||
return; | ||
if (options.schema) | ||
this._table = options.schema + '.' + tableName; | ||
if (options.key) { | ||
this._CONFIG.key = options.key; | ||
this._CONFIG.order = options.key; | ||
} | ||
if (options.default_order) | ||
this._CONFIG.order = options.default_order; | ||
if (options.default_by) | ||
this._CONFIG.by = options.default_by; | ||
} | ||
_query(WHERE, PARAM = [], ORDER_BY = '', LIMIT = '') { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const SQL_QUERY = ` | ||
SELECT ${this._CONFIG.query_fields} | ||
FROM ${this._table} | ||
${WHERE} ${ORDER_BY} ${LIMIT}`; | ||
const queryResult = yield this.sql(SQL_QUERY, PARAM); | ||
return queryResult.rows; | ||
}); | ||
} | ||
toWhereCondition(query) { | ||
} | ||
class BaseTable extends BaseView_1.BaseView { | ||
/** | ||
* @see WhereCondition | ||
* Use a WhereCondition Query Data | ||
*/ | ||
queryByCondition(query) { | ||
const WHERE = (0, QueryWhere_1.whereByCondition)(query); | ||
} | ||
/** | ||
* @see QuerySchema | ||
* Use a QuerySchema Query Data | ||
*/ | ||
query(query) { | ||
const { FIELD_MAP, order, by } = this._CONFIG; | ||
const [WHERE, PARAM] = (0, QueryBuilder_1.whereByQuery)(query, FIELD_MAP, this._QUERY_CACHE); | ||
const [ORDER_BY, LIMIT] = (0, QueryPagition_1.orderByLimit)(FIELD_MAP, query, order, by); | ||
return this._query(WHERE, PARAM, ORDER_BY, LIMIT); | ||
} | ||
/** | ||
* @see QuerySchema | ||
* Use a QuerySchema Query Data With Page | ||
* this will return a object with {total:number,list:any[]} | ||
* | ||
*/ | ||
queryPager(query) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let total = 0; | ||
const { key, order, by, FIELD_MAP } = this._CONFIG; | ||
const [WHERE, PARAM] = (0, QueryBuilder_1.whereByQuery)(query, FIELD_MAP, this._QUERY_CACHE); | ||
if (lodash_1.default.has(query, 'total_') && lodash_1.default.isNumber(query.total_)) { | ||
total = query.total_; | ||
} | ||
else { | ||
const SQL_COUNT = `SELECT COUNT(${key}) AS total FROM ${this._table} ${WHERE}`; | ||
const countResult = yield this.sql(SQL_COUNT, PARAM); | ||
if (countResult.rowCount != 1) { | ||
return { | ||
total: 0, | ||
list: [], | ||
}; | ||
} | ||
total = parseInt(countResult.rows[0].total); | ||
} | ||
const [ORDER_BY, LIMIT] = (0, QueryPagition_1.orderByLimit)(FIELD_MAP, query, order, by); | ||
const list = yield this._query(WHERE, PARAM, ORDER_BY, LIMIT); | ||
return { total, list }; | ||
}); | ||
} | ||
all() { | ||
return (0, base_1.selectAll)(this.db(), this._table, this._CONFIG.query_fields); | ||
} | ||
getById(id) { | ||
return (0, base_1.selectById)(this.db(), this._table, id, [], this._CONFIG.key); | ||
} | ||
/** | ||
* check row data while insert or update | ||
@@ -137,7 +20,15 @@ * and auto warp the data | ||
this._CONFIG.FIELD_MAP.forEach((schema, key) => { | ||
let field = schema.column || key; | ||
if (lodash_1.default.has(obj, key)) { | ||
clone[field] = obj[key]; | ||
} | ||
let type = (0, QueryBuilder_1.getFieldType)(schema); | ||
let field = schema.column || key; | ||
if (type == 'date') { | ||
if (schema.isCreate && isAdd) { | ||
clone[field] = new Date(); | ||
if (schema.isCreate) { | ||
if (isAdd) { | ||
clone[field] = new Date(); | ||
} | ||
else { | ||
lodash_1.default.unset(clone, field); | ||
} | ||
return; | ||
@@ -150,5 +41,2 @@ } | ||
} | ||
if (lodash_1.default.has(obj, key)) { | ||
clone[field] = obj[key]; | ||
} | ||
}); | ||
@@ -166,7 +54,5 @@ return clone; | ||
let entity = this.checkEntity(object, true); | ||
// console.log(entity) | ||
return (0, base_1.insert)(this.db(), this._table, entity); | ||
// return null; | ||
} | ||
} | ||
exports.BaseTable = BaseTable; |
import type { QuerySchema } from './types'; | ||
export declare const orderByLimit: (fieldSet: Map<string, any>, query?: QuerySchema, default_order?: string, default_by?: string) => [string, string]; | ||
export declare const DEFAULT_PAGE_SIZE = 10; | ||
export declare const orderByLimit: (fieldSet: Map<string, any>, query?: QuerySchema, pageSize?: number, default_order?: string, default_by?: string) => [string, string]; |
@@ -6,5 +6,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.orderByLimit = void 0; | ||
exports.orderByLimit = exports.DEFAULT_PAGE_SIZE = void 0; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
const DEFAULT_PAGE_SIZE = 10; | ||
exports.DEFAULT_PAGE_SIZE = 10; | ||
const BY_SET = new Set(['asc', 'desc']); | ||
@@ -30,10 +30,10 @@ const orderBy = (fieldSet, query, default_order = 'id', default_by = 'desc') => { | ||
}; | ||
const limit = (query) => { | ||
const limit = (query, pageSize = exports.DEFAULT_PAGE_SIZE) => { | ||
let start = lodash_1.default.has(query, 'start_') ? query.start_ : 0; | ||
let count = lodash_1.default.has(query, 'count_') ? query.count_ : DEFAULT_PAGE_SIZE; | ||
let count = lodash_1.default.has(query, 'count_') ? query.count_ : pageSize; | ||
return `LIMIT ${count} OFFSET ${start}`; | ||
}; | ||
const orderByLimit = (fieldSet, query, default_order = 'id', default_by = 'desc') => [ | ||
orderBy(fieldSet, query, default_order, default_by), limit(query) | ||
const orderByLimit = (fieldSet, query, pageSize = exports.DEFAULT_PAGE_SIZE, default_order = 'id', default_by = 'desc') => [ | ||
orderBy(fieldSet, query, default_order, default_by), limit(query, pageSize) | ||
]; | ||
exports.orderByLimit = orderByLimit; |
@@ -30,4 +30,4 @@ import type { SchemaOptions } from '@sinclair/typebox'; | ||
}; | ||
export type QuerySuffix = 'Min' | 'Mint' | 'Max' | 'Maxt' | 'MinH' | 'MinD' | 'MinM' | 'MaxH' | 'MaxD' | 'MaxM' | 'Like' | 'Likel' | 'Liker'; | ||
export declare const SUFFIX: QuerySuffix[]; | ||
export type MagicSuffix = 'Min' | 'Mint' | 'Max' | 'Maxt' | 'MinH' | 'MinD' | 'MinM' | 'MaxH' | 'MaxD' | 'MaxM' | 'Like' | 'Likel' | 'Liker'; | ||
export declare const SUFFIX: MagicSuffix[]; | ||
export type QuerySchema = { | ||
@@ -34,0 +34,0 @@ /** |
@@ -35,7 +35,1 @@ "use strict"; | ||
}; | ||
// const BasePageQuery = Type.Object({ | ||
// start_: Type.Optional(Type.Number()), | ||
// count_: Type.Optional(Type.Number()), | ||
// order_: Type.Optional(Type.String()), | ||
// by_: Type.Optional(Type.Number()), | ||
// }) |
export { BaseTable } from './domain/BaseTable'; | ||
export { BaseView } from './domain/BaseView'; | ||
export { setup, UType } from './domain/Util'; | ||
export type { Static } from '@sinclair/typebox'; | ||
export * from './domain/types'; |
@@ -17,5 +17,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.UType = exports.setup = exports.BaseTable = void 0; | ||
exports.UType = exports.setup = exports.BaseView = exports.BaseTable = void 0; | ||
var BaseTable_1 = require("./domain/BaseTable"); | ||
Object.defineProperty(exports, "BaseTable", { enumerable: true, get: function () { return BaseTable_1.BaseTable; } }); | ||
var BaseView_1 = require("./domain/BaseView"); | ||
Object.defineProperty(exports, "BaseView", { enumerable: true, get: function () { return BaseView_1.BaseView; } }); | ||
var Util_1 = require("./domain/Util"); | ||
@@ -22,0 +24,0 @@ Object.defineProperty(exports, "setup", { enumerable: true, get: function () { return Util_1.setup; } }); |
@@ -20,4 +20,4 @@ "use strict"; | ||
// contact | character varying(64) | | | | ||
// create_time | timestamp(0) without time zone | | | CURRENT_TIMESTAMP | ||
// product | character varying(64) | | | | ||
// create_time | timestamp(0) without time zone | | | CURRENT_TIMESTAMP | ||
// product | character varying(64) | | | | ||
// Indexes: | ||
@@ -27,4 +27,5 @@ // "feedback_pkey" PRIMARY KEY, btree (id) | ||
// console.log(pg.database) | ||
const u = yield pg.query(`SELECT * FROM pg_tables WHERE schemaname = $1 AND tablename = $2 `, [schema, table]); | ||
// const u = await pg.query(`SELECT * FROM pg_tables WHERE tablename = $1 `, [table]); | ||
// const u = await pg.query(`SELECT * FROM pg_tables WHERE schemaname = $1 AND tablename = $2 `, [schema, table]); | ||
// const u = await pg.query('\\dt $1', [table]); | ||
// pg.emit() | ||
// const u = await pg.query(` | ||
@@ -36,3 +37,48 @@ // select a.attnum,a.attname,concat_ws('',t.typname,SUBSTRING(format_type(a.atttypid,a.atttypmod) | ||
// `); | ||
console.log(u.rows); | ||
// console.log(u.rows) | ||
// pg.addListener('log', function () { | ||
// console.log(arguments) | ||
// }) | ||
// pg.on('drain', function () { | ||
// console.log('drain', arguments) | ||
// }) | ||
// pg.on('error', function () { | ||
// console.log('error', arguments) | ||
// }) | ||
// pg.on('notice', function () { | ||
// console.log('notice', arguments) | ||
// }) | ||
// pg.on('notification', function () { | ||
// console.log('notification', arguments) | ||
// }) | ||
// pg.on('end', function () { | ||
// console.log('end', arguments) | ||
// }) | ||
// pg.on('error',(err)=>{ | ||
// console.error(err) | ||
// }) | ||
pg.on('notification', (msg) => { | ||
console.log(msg); | ||
// console.log(msg.channel, msg.payload) // foo | ||
// console.log() // bar! | ||
}); | ||
yield pg.query('LISTEN foo'); | ||
yield pg.query(`NOTIFY foo, 'bar!'`); | ||
// console.log(q) | ||
// q.submit() | ||
// console.log(u) | ||
// copyFrom(queryText: string): stream.Writable; | ||
// copyTo(queryText: string): stream.Readable; | ||
// pauseDrain(): void; | ||
// resumeDrain(): void; | ||
// escapeIdentifier(str: string): string; | ||
// escapeLiteral(str: string): string; | ||
// on(event: 'drain', listener: () => void): this; | ||
// on(event: 'error', listener: (err: Error) => void): this; | ||
// on(event: 'notice', listener: (notice: NoticeMessage) => void): this; | ||
// on(event: 'notification', listener: (message: Notification) => void): this; | ||
// // tslint:disable-next-line unified-signatures | ||
// on(event: 'end', listener: () => void): this; | ||
// pg.emit('log','fdasfda') | ||
// console.log(pg.eventNames()) | ||
}); | ||
@@ -39,0 +85,0 @@ exports.build = build; |
{ | ||
"name": "eha", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Eha For ServerLess", | ||
"author": "ada87" | ||
} |
110920
89
2736