@epip/crud
Advanced tools
Comparing version 1.0.27 to 1.0.28
@@ -1,14 +0,36 @@ | ||
import { to } from 'await-to-js'; | ||
import { Repository } from 'typeorm'; | ||
import { IResponse } from '../types'; | ||
import { to } from "await-to-js"; | ||
import { DeepPartial, Repository } from "typeorm"; | ||
import { IResponse, IResponseAll } from "../types"; | ||
export class BaseService<T> { | ||
constructor(private repo: Repository<T>) {} | ||
export class BaseService<T> { | ||
constructor(private repo:Repository<T>){} | ||
public async getAll(): Promise<IResponse<T>> { | ||
const [err,results] = await to(this.repo.find({})); | ||
if(err){ | ||
public async getAll(): Promise<IResponseAll<T>> { | ||
const [err, results] = await to(this.repo.find({})); | ||
if (err) { | ||
return { | ||
status: 1, | ||
status: 500, | ||
message: err.message, | ||
} as IResponseAll<T>; | ||
} | ||
return { | ||
status: 200, | ||
message: "ok", | ||
results, | ||
}; | ||
} | ||
public async get(id: number): Promise<IResponse<T>> { | ||
const [err, results] = await to( | ||
this.repo.findOne({ | ||
where: { | ||
id, | ||
}, | ||
}) | ||
); | ||
if (err) { | ||
return { | ||
status: 500, | ||
message: err.message, | ||
} as IResponse<T>; | ||
@@ -18,8 +40,105 @@ } | ||
return { | ||
status:200, | ||
message:"ok", | ||
results | ||
status: 200, | ||
message: "ok", | ||
results, | ||
}; | ||
} | ||
public async post(entity: T): Promise<IResponse<T>> { | ||
const [err, results] = await to(this.repo.save(entity)); | ||
if (err) { | ||
return { | ||
status: 500, | ||
message: err.message, | ||
} as IResponse<T>; | ||
} | ||
return { | ||
status: 200, | ||
message: "ok", | ||
results, | ||
}; | ||
} | ||
public async put(id: number, entity: T): Promise<IResponse<T>> { | ||
let [err, results] = await to(this.get(id)); | ||
if (err) { | ||
return { | ||
status: 500, | ||
message: err.message, | ||
} as IResponse<T>; | ||
} | ||
let data: T; | ||
if (results?.results) { | ||
data = results?.results; | ||
for (const key in entity) { | ||
if (Object.prototype.hasOwnProperty.call(data, key)) { | ||
const element = entity[key]; | ||
data[key]=entity[key] | ||
} | ||
} | ||
}else{ | ||
return { | ||
status: 404, | ||
message: "not found record", | ||
} as IResponse<T>; | ||
} | ||
let res; | ||
[err, res] = await to(this.repo.save(data)); | ||
if (err) { | ||
return { | ||
status: 500, | ||
message: err.message, | ||
} as IResponse<T>; | ||
} | ||
return { | ||
status: 200, | ||
message: "ok", | ||
results:res, | ||
}; | ||
} | ||
public async patch(id: number, entity: DeepPartial<T>): Promise<IResponse<DeepPartial<T>>> { | ||
let [err, results] = await to(this.get(id)); | ||
if (err) { | ||
return { | ||
status: 500, | ||
message: err.message, | ||
} as IResponse<T>; | ||
} | ||
let data: DeepPartial<T>; | ||
if (results?.results) { | ||
data = results?.results; | ||
for (const key in entity) { | ||
if (Object.prototype.hasOwnProperty.call(data, key)) { | ||
const element = entity[key]; | ||
data[key]=entity[key] | ||
} | ||
} | ||
}else{ | ||
return { | ||
status: 404, | ||
message: "not found record", | ||
} as IResponse<T>; | ||
} | ||
let res; | ||
[err, res] = await to(this.repo.save(data)); | ||
if (err) { | ||
return { | ||
status: 500, | ||
message: err.message, | ||
} as IResponse<T>; | ||
} | ||
return { | ||
status: 200, | ||
message: "ok", | ||
results:res, | ||
}; | ||
} | ||
} |
import { ApiProperty } from "@nestjs/swagger"; | ||
export class IResponseAll<T>{ | ||
@ApiProperty() | ||
status:number=0; | ||
@ApiProperty() | ||
message?:string; | ||
results?:Array<T>; | ||
} | ||
export class IResponse<T>{ | ||
@@ -12,3 +24,3 @@ | ||
results?:Array<T>; | ||
results?:T; | ||
} |
@@ -1,8 +0,12 @@ | ||
import { Repository } from 'typeorm'; | ||
import { IResponse } from '../types'; | ||
import { DeepPartial, Repository } from "typeorm"; | ||
import { IResponse, IResponseAll } from "../types"; | ||
export declare class BaseService<T> { | ||
private repo; | ||
constructor(repo: Repository<T>); | ||
getAll(): Promise<IResponse<T>>; | ||
getAll(): Promise<IResponseAll<T>>; | ||
get(id: number): Promise<IResponse<T>>; | ||
post(entity: T): Promise<IResponse<T>>; | ||
put(id: number, entity: T): Promise<IResponse<T>>; | ||
patch(id: number, entity: DeepPartial<T>): Promise<IResponse<DeepPartial<T>>>; | ||
} | ||
//# sourceMappingURL=base.service.d.ts.map |
@@ -55,3 +55,3 @@ "use strict"; | ||
return [2, { | ||
status: 1, | ||
status: 500, | ||
message: err.message, | ||
@@ -63,3 +63,3 @@ }]; | ||
message: "ok", | ||
results: results | ||
results: results, | ||
}]; | ||
@@ -70,2 +70,148 @@ } | ||
}; | ||
BaseService.prototype.get = function (id) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, err, results; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4, (0, await_to_js_1.to)(this.repo.findOne({ | ||
where: { | ||
id: id, | ||
}, | ||
}))]; | ||
case 1: | ||
_a = _b.sent(), err = _a[0], results = _a[1]; | ||
if (err) { | ||
return [2, { | ||
status: 500, | ||
message: err.message, | ||
}]; | ||
} | ||
return [2, { | ||
status: 200, | ||
message: "ok", | ||
results: results, | ||
}]; | ||
} | ||
}); | ||
}); | ||
}; | ||
BaseService.prototype.post = function (entity) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, err, results; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4, (0, await_to_js_1.to)(this.repo.save(entity))]; | ||
case 1: | ||
_a = _b.sent(), err = _a[0], results = _a[1]; | ||
if (err) { | ||
return [2, { | ||
status: 500, | ||
message: err.message, | ||
}]; | ||
} | ||
return [2, { | ||
status: 200, | ||
message: "ok", | ||
results: results, | ||
}]; | ||
} | ||
}); | ||
}); | ||
}; | ||
BaseService.prototype.put = function (id, entity) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, err, results, data, key, element, res; | ||
var _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4, (0, await_to_js_1.to)(this.get(id))]; | ||
case 1: | ||
_a = _c.sent(), err = _a[0], results = _a[1]; | ||
if (err) { | ||
return [2, { | ||
status: 500, | ||
message: err.message, | ||
}]; | ||
} | ||
if (results === null || results === void 0 ? void 0 : results.results) { | ||
data = results === null || results === void 0 ? void 0 : results.results; | ||
for (key in entity) { | ||
if (Object.prototype.hasOwnProperty.call(data, key)) { | ||
element = entity[key]; | ||
data[key] = entity[key]; | ||
} | ||
} | ||
} | ||
else { | ||
return [2, { | ||
status: 404, | ||
message: "not found record", | ||
}]; | ||
} | ||
return [4, (0, await_to_js_1.to)(this.repo.save(data))]; | ||
case 2: | ||
_b = _c.sent(), err = _b[0], res = _b[1]; | ||
if (err) { | ||
return [2, { | ||
status: 500, | ||
message: err.message, | ||
}]; | ||
} | ||
return [2, { | ||
status: 200, | ||
message: "ok", | ||
results: res, | ||
}]; | ||
} | ||
}); | ||
}); | ||
}; | ||
BaseService.prototype.patch = function (id, entity) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, err, results, data, key, element, res; | ||
var _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4, (0, await_to_js_1.to)(this.get(id))]; | ||
case 1: | ||
_a = _c.sent(), err = _a[0], results = _a[1]; | ||
if (err) { | ||
return [2, { | ||
status: 500, | ||
message: err.message, | ||
}]; | ||
} | ||
if (results === null || results === void 0 ? void 0 : results.results) { | ||
data = results === null || results === void 0 ? void 0 : results.results; | ||
for (key in entity) { | ||
if (Object.prototype.hasOwnProperty.call(data, key)) { | ||
element = entity[key]; | ||
data[key] = entity[key]; | ||
} | ||
} | ||
} | ||
else { | ||
return [2, { | ||
status: 404, | ||
message: "not found record", | ||
}]; | ||
} | ||
return [4, (0, await_to_js_1.to)(this.repo.save(data))]; | ||
case 2: | ||
_b = _c.sent(), err = _b[0], res = _b[1]; | ||
if (err) { | ||
return [2, { | ||
status: 500, | ||
message: err.message, | ||
}]; | ||
} | ||
return [2, { | ||
status: 200, | ||
message: "ok", | ||
results: res, | ||
}]; | ||
} | ||
}); | ||
}); | ||
}; | ||
return BaseService; | ||
@@ -72,0 +218,0 @@ }()); |
@@ -1,2 +0,2 @@ | ||
export declare class IResponse<T> { | ||
export declare class IResponseAll<T> { | ||
status: number; | ||
@@ -6,2 +6,7 @@ message?: string; | ||
} | ||
export declare class IResponse<T> { | ||
status: number; | ||
message?: string; | ||
results?: T; | ||
} | ||
//# sourceMappingURL=res.interface.d.ts.map |
@@ -12,4 +12,19 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IResponse = void 0; | ||
exports.IResponse = exports.IResponseAll = void 0; | ||
var swagger_1 = require("@nestjs/swagger"); | ||
var IResponseAll = (function () { | ||
function IResponseAll() { | ||
this.status = 0; | ||
} | ||
__decorate([ | ||
(0, swagger_1.ApiProperty)(), | ||
__metadata("design:type", Number) | ||
], IResponseAll.prototype, "status", void 0); | ||
__decorate([ | ||
(0, swagger_1.ApiProperty)(), | ||
__metadata("design:type", String) | ||
], IResponseAll.prototype, "message", void 0); | ||
return IResponseAll; | ||
}()); | ||
exports.IResponseAll = IResponseAll; | ||
var IResponse = (function () { | ||
@@ -16,0 +31,0 @@ function IResponse() { |
{ | ||
"name": "@epip/crud", | ||
"version": "1.0.27", | ||
"version": "1.0.28", | ||
"description": "", | ||
@@ -17,3 +17,4 @@ "main": "index.js", | ||
"scripts": { | ||
"build": "tsc -p tsconfig.json" | ||
"build": "tsc -p tsconfig.json", | ||
"publish": "tsc -p tsconfig.json && npm version patch && npm publish --access public" | ||
}, | ||
@@ -20,0 +21,0 @@ "repository": { |
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
660
140961
52