Skip to content

Commit

Permalink
v0.15.0
Browse files Browse the repository at this point in the history
update to core v15
  • Loading branch information
Elschnagoo committed Dec 2, 2021
1 parent e3b47cd commit cc8e9bc
Show file tree
Hide file tree
Showing 11 changed files with 572 additions and 270 deletions.
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grandlinex/bundle-postgresql",
"version": "0.14.0",
"version": "0.15.0",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -25,35 +25,35 @@
},
"license": "BSD-3-Clause",
"dependencies": {
"@grandlinex/core": "^0.14.0",
"@grandlinex/core": "^0.15.0",
"pg": "^8.7.1"
},
"devDependencies": {
"@types/jest": "^27.0.2",
"@types/jest": "^27.0.3",
"@types/jsonwebtoken": "^8.5.6",
"@types/node": "^16.11.7",
"@types/node": "^16.11.11",
"@types/pg": "^8.6.1",
"@typescript-eslint/eslint-plugin": "^4.31.2",
"@typescript-eslint/parser": "^4.31.2",
"@typescript-eslint/eslint-plugin": "^5.5.0",
"@typescript-eslint/parser": "^5.5.0",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"eslint-config-airbnb": "^19.0.0",
"eslint-config-airbnb-typescript": "^14.0.1",
"eslint": "^8.3.0",
"eslint-config-airbnb": "^19.0.2",
"eslint-config-airbnb-typescript": "^16.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-jest": "^25.2.4",
"eslint-plugin-jest": "^25.3.0",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.27.0",
"eslint-plugin-react": "^7.27.1",
"eslint-plugin-react-hooks": "^4.3.0",
"jest": "^27.3.1",
"jest": "^27.4.3",
"jest-junit": "^13.0.0",
"prettier": "^2.4.1",
"prettier": "^2.5.0",
"ts-jest": "^27.0.7",
"ts-loader": "^9.2.6",
"ts-node": "^10.4.0",
"typedoc": "^0.22.9",
"typescript": "^4.4.4"
"typedoc": "^0.22.10",
"typescript": "^4.5.2"
},
"repository": {
"type": "git",
Expand Down
165 changes: 77 additions & 88 deletions src/class/PGCon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,24 @@ import {
ConfigType,
CoreDBCon,
CoreEntity,
EntityConfig,
getColumnMeta,
ICoreKernelModule,
IDataBase,
} from '@grandlinex/core';
import { RawQuery } from '@grandlinex/core/dist/lib';

import { Client, QueryResult } from 'pg';
import {
buildSearchQ,
mappingWithDataType,
objToTable,
rowToObj,
tableToObj,
} from '../util';

type PGDBType = Client;

function buildSearchQ<E>(
search: { [P in keyof E]?: E[P] },
param: any[],
searchQ: string
) {
let temp = searchQ;
const keys: (keyof E)[] = Object.keys(search) as (keyof E)[];
if (keys.length > 0) {
const filter: string[] = [];
let count = 1;
for (const key of keys) {
if (search[key] !== undefined) {
filter.push(`${key} = $${count++}`);
param.push(search[key]);
}
}
if (filter.length > 0) {
temp = ` WHERE ${filter.join(' AND ')}`;
}
}
return temp;
}
export default abstract class PGCon
extends CoreDBCon<PGDBType, QueryResult | null>
implements IDataBase<PGDBType, QueryResult | null>
Expand All @@ -49,93 +36,80 @@ export default abstract class PGCon
}

async createEntity<E extends CoreEntity>(
className: string,
config: EntityConfig<E>,
entity: E
): Promise<E | null> {
const clone: any = entity;
const keys = Object.keys(entity);
const param: any[] = [];
const vals: string[] = [];
const newKeys: string[] = [];
let index = 1;
keys.forEach((key) => {
if (key === 'e_id' && clone[key] === null) {
return;
}
newKeys.push(key);
param.push(clone[key]);
vals.push(`$${index}`);
index++;
});
const [keys, values, params] = objToTable(entity);

const result = await this.execScripts([
{
exec: `INSERT INTO ${this.schemaName}.${className}(${newKeys.join(
exec: `INSERT INTO ${this.schemaName}.${config.className}(${keys.join(
', '
)}) VALUES (${vals.join(', ')}) returning e_id`,
param,
)})
VALUES (${values.join(', ')})
returning e_id`,
param: params,
},
]);
clone.e_id = result[0]?.rows[0]?.e_id;
return clone;
}

async updateEntity<E extends CoreEntity>(
className: string,
config: EntityConfig<E>,
entity: E
): Promise<E | null> {
if (entity.e_id) {
const clone: any = entity;
const keys = Object.keys(entity);
const param: any[] = [];
const vals: string[] = [];
let index = 1;
keys.forEach((key) => {
if (key === 'e_id') {
return;
}
param.push(clone[key]);
vals.push(`${key}=$${index}`);
index++;
});
const [, values, params] = objToTable(entity, true);
const result = await this.execScripts([
{
exec: `UPDATE ${this.schemaName}.${className} SET ${vals.join(
', '
)} WHERE e_id=${entity.e_id};`,
param,
exec: `UPDATE ${this.schemaName}.${config.className}
SET ${values.join(', ')}
WHERE e_id = ${entity.e_id};`,
param: params,
},
]);

return result[0] ? clone : null;
return result[0] ? entity : null;
}
return null;
}

async getEntityById<E extends CoreEntity>(
className: string,
config: EntityConfig<E>,
id: number
): Promise<E | null> {
const query = await this.execScripts([
{
exec: `SELECT * FROM ${this.schemaName}.${className} WHERE e_id=${id};`,
exec: `SELECT *
FROM ${this.schemaName}.${config.className}
WHERE e_id = ${id};`,
param: [],
},
]);
return query[0]?.rows[0];

const res = query[0]?.rows[0];
if (res) {
return rowToObj<E>(config, res);
}
return null;
}

async deleteEntityById(className: string, id: number): Promise<boolean> {
const query = await this.execScripts([
{
exec: `DELETE FROM ${this.schemaName}.${className} WHERE e_id=${id};`,
exec: `DELETE
FROM ${this.schemaName}.${className}
WHERE e_id = ${id};`,
param: [],
},
]);
return query[0] !== null;
}

async findEntity<E extends CoreEntity>(
className: string,
config: EntityConfig<E>,
search: { [P in keyof E]?: E[P] | undefined }
): Promise<E | null> {
let searchQ = '';
Expand All @@ -145,31 +119,48 @@ export default abstract class PGCon

const query = await this.execScripts([
{
exec: `SELECT * FROM ${this.schemaName}.${className}${searchQ};`,
exec: `SELECT *
FROM ${this.schemaName}.${config.className} ${searchQ};`,
param,
},
]);
return query[0]?.rows[0] || null;

const res = query[0]?.rows[0];
if (res) {
return rowToObj<E>(config, res);
}
return null;
}

async getEntityList<E extends CoreEntity>(
className: string,
search: {
config: EntityConfig<E>,
limit?: number,
search?: {
[P in keyof E]: E[P];
}
): Promise<E[]> {
if (limit === 0) {
return [];
}
let searchQ = '';
const range = limit ? ` LIMIT ${limit}` : '';
const param: any[] = [];
if (search) {
searchQ = buildSearchQ<E>(search, param, searchQ);
}
const query = await this.execScripts([
{
exec: `SELECT * FROM ${this.schemaName}.${className}${searchQ};`,
exec: `SELECT *
FROM ${this.schemaName}.${config.className} ${searchQ}${range};`,
param,
},
]);
return query[0]?.rows || [];

const res = query[0]?.rows;
if (res) {
return tableToObj<E>(config, res);
}
return [];
}

async initEntity<E extends CoreEntity>(
Expand All @@ -178,9 +169,10 @@ export default abstract class PGCon
): Promise<boolean> {
await this.execScripts([
{
exec: `CREATE TABLE ${
this.schemaName
}.${className}(${this.transformEntityKeys<E>(entity)});`,
exec: `CREATE TABLE ${this.schemaName}.${className}
(
${this.transformEntityKeys<E>(entity)}
);`,
param: [],
},
]);
Expand All @@ -192,11 +184,13 @@ export default abstract class PGCon
const out: string[] = [];

keys.forEach((key) => {
if (key === 'e_id') {
const meta = getColumnMeta(entity, key);
if (meta?.dataType) {
mappingWithDataType(meta, out, key, this.schemaName);
} else if (key === 'e_id') {
out.push(`e_id SERIAL PRIMARY KEY`);
} else {
const type = typeof entity[key];
const dat = entity[key] as any;
switch (type) {
case 'bigint':
case 'number':
Expand All @@ -205,11 +199,6 @@ export default abstract class PGCon
case 'string':
out.push(`${key} TEXT`);
break;
case 'object':
if (dat instanceof Date) {
out.push(`${key} TEXT`);
}
break;
default:
break;
}
Expand All @@ -223,9 +212,9 @@ export default abstract class PGCon
try {
const query = await this.execScripts([
{
exec: `DELETE
FROM ${this.schemaName}.config
WHERE c_key = $1;`,
exec: `DELETE
FROM ${this.schemaName}.config
WHERE c_key = $1;`,
param: [key],
},
]);
Expand Down Expand Up @@ -308,8 +297,8 @@ export default abstract class PGCon
const query = await this.execScripts([
{
exec: `SELECT *
FROM ${this.schemaName}.config
WHERE c_key = '${key}'`,
FROM ${this.schemaName}.config
WHERE c_key = '${key}'`,
param: [],
},
]);
Expand All @@ -320,7 +309,7 @@ export default abstract class PGCon
const query = await this.execScripts([
{
exec: `INSERT INTO ${this.schemaName}.config (c_key, c_value)
VALUES ('${key}', '${value}');`,
VALUES ('${key}', '${value}');`,
param: [],
},
]);
Expand All @@ -334,8 +323,8 @@ export default abstract class PGCon
const query = await this.execScripts([
{
exec: `SELECT *
FROM ${this.schemaName}.config
WHERE c_key = '${key}'`,
FROM ${this.schemaName}.config
WHERE c_key = '${key}'`,
param: [],
},
]);
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PGCon from './class/PGCon';

// eslint-disable-next-line import/prefer-default-export
export * from './util';
export { PGCon };
export default PGCon;
22 changes: 22 additions & 0 deletions src/util/buildSearchQ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default function buildSearchQ<E>(
search: { [P in keyof E]?: E[P] },
param: any[],
searchQ: string
) {
let temp = searchQ;
const keys: (keyof E)[] = Object.keys(search) as (keyof E)[];
if (keys.length > 0) {
const filter: string[] = [];
let count = 1;
for (const key of keys) {
if (search[key] !== undefined) {
filter.push(`${key} = $${count++}`);
param.push(search[key]);
}
}
if (filter.length > 0) {
temp = ` WHERE ${filter.join(' AND ')}`;
}
}
return temp;
}
Loading

0 comments on commit cc8e9bc

Please sign in to comment.