@heyhru/business-dms-saved-sql 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +124 -0
- package/dist/index.mjs +94 -0
- package/dist/saved-sql.model.d.ts +18 -0
- package/dist/saved-sql.model.d.ts.map +1 -0
- package/dist/saved-sql.service.d.ts +12 -0
- package/dist/saved-sql.service.d.ts.map +1 -0
- package/dist/saved-sql.sql.d.ts +4 -0
- package/dist/saved-sql.sql.d.ts.map +1 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @heyhru/business-dms-saved-sql
|
|
2
|
+
|
|
3
|
+
DMS saved SQL domain logic: service, model, sql.
|
|
4
|
+
|
|
5
|
+
## API
|
|
6
|
+
|
|
7
|
+
### Route handlers (Fastify)
|
|
8
|
+
|
|
9
|
+
The following functions are route-facing handlers with signature `(req: FastifyRequest, reply: FastifyReply)`. Register them directly on a Fastify instance.
|
|
10
|
+
|
|
11
|
+
| Function | Route action | Description |
|
|
12
|
+
| ---------------- | ------------------------- | ------------------------------------------------------------------------------------ |
|
|
13
|
+
| `savedSqlList` | `POST /saved-sqls/list` | List saved SQLs for the authenticated user |
|
|
14
|
+
| `savedSqlCreate` | `POST /saved-sqls/create` | Create a saved SQL (`name`, `sqlText` required; `dataSourceId`, `database` optional) |
|
|
15
|
+
| `savedSqlUpdate` | `POST /saved-sqls/update` | Update `name` or `sqlText` by `id` |
|
|
16
|
+
| `savedSqlDelete` | `POST /saved-sqls/delete` | Delete a saved SQL by `id` |
|
|
17
|
+
|
|
18
|
+
All endpoints are user-scoped: operations are restricted to the authenticated user's own records.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
savedSqlCreate: () => savedSqlCreate,
|
|
24
|
+
savedSqlDelete: () => savedSqlDelete,
|
|
25
|
+
savedSqlList: () => savedSqlList,
|
|
26
|
+
savedSqlUpdate: () => savedSqlUpdate
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/saved-sql.model.ts
|
|
31
|
+
var import_server_plugin_pg = require("@heyhru/server-plugin-pg");
|
|
32
|
+
|
|
33
|
+
// src/saved-sql.sql.ts
|
|
34
|
+
var LIST_BY_USER = `
|
|
35
|
+
SELECT id, name, sql_text, data_source_id, database, created_at, updated_at
|
|
36
|
+
FROM saved_sqls
|
|
37
|
+
WHERE user_id = ?
|
|
38
|
+
ORDER BY updated_at DESC`;
|
|
39
|
+
var CREATE = `
|
|
40
|
+
INSERT INTO saved_sqls (user_id, name, sql_text, data_source_id, database)
|
|
41
|
+
VALUES (?, ?, ?, ?, ?)
|
|
42
|
+
RETURNING id, name, sql_text, data_source_id, database, created_at, updated_at`;
|
|
43
|
+
var DELETE = `
|
|
44
|
+
DELETE FROM saved_sqls
|
|
45
|
+
WHERE id = ? AND user_id = ?`;
|
|
46
|
+
|
|
47
|
+
// src/saved-sql.model.ts
|
|
48
|
+
function listSavedSqlsByUser(userId) {
|
|
49
|
+
return (0, import_server_plugin_pg.getPgDb)().query(LIST_BY_USER, [userId]);
|
|
50
|
+
}
|
|
51
|
+
function createSavedSqlRow(opts) {
|
|
52
|
+
return (0, import_server_plugin_pg.getPgDb)().queryOne(CREATE, [
|
|
53
|
+
opts.userId,
|
|
54
|
+
opts.name,
|
|
55
|
+
opts.sqlText,
|
|
56
|
+
opts.dataSourceId,
|
|
57
|
+
opts.database
|
|
58
|
+
]);
|
|
59
|
+
}
|
|
60
|
+
async function updateSavedSqlRow(id, userId, data) {
|
|
61
|
+
const fields = [];
|
|
62
|
+
const values = [];
|
|
63
|
+
if (data.name !== void 0) {
|
|
64
|
+
fields.push("name = ?");
|
|
65
|
+
values.push(data.name);
|
|
66
|
+
}
|
|
67
|
+
if (data.sqlText !== void 0) {
|
|
68
|
+
fields.push("sql_text = ?");
|
|
69
|
+
values.push(data.sqlText);
|
|
70
|
+
}
|
|
71
|
+
if (!fields.length) throw new Error("No fields to update");
|
|
72
|
+
fields.push("updated_at = NOW()");
|
|
73
|
+
values.push(id, userId);
|
|
74
|
+
return (0, import_server_plugin_pg.getPgDb)().queryOne(
|
|
75
|
+
`UPDATE saved_sqls SET ${fields.join(", ")} WHERE id = ? AND user_id = ? RETURNING id, name, sql_text, data_source_id, database, created_at, updated_at`,
|
|
76
|
+
values
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
function deleteSavedSqlRow(id, userId) {
|
|
80
|
+
return (0, import_server_plugin_pg.getPgDb)().run(DELETE, [id, userId]);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/saved-sql.service.ts
|
|
84
|
+
async function savedSqlList(req, reply) {
|
|
85
|
+
return reply.send(await listSavedSqlsByUser(req.user.id));
|
|
86
|
+
}
|
|
87
|
+
async function savedSqlCreate(req, reply) {
|
|
88
|
+
const { name, sqlText, dataSourceId, database } = req.body ?? {};
|
|
89
|
+
if (!name || !sqlText) {
|
|
90
|
+
return reply.code(400).send({ error: "name and sqlText are required" });
|
|
91
|
+
}
|
|
92
|
+
const row = await createSavedSqlRow({
|
|
93
|
+
userId: req.user.id,
|
|
94
|
+
name,
|
|
95
|
+
sqlText,
|
|
96
|
+
dataSourceId: dataSourceId ?? null,
|
|
97
|
+
database: database ?? null
|
|
98
|
+
});
|
|
99
|
+
return reply.code(201).send(row);
|
|
100
|
+
}
|
|
101
|
+
async function savedSqlUpdate(req, reply) {
|
|
102
|
+
const { id, name, sqlText } = req.body ?? {};
|
|
103
|
+
if (!id) return reply.code(400).send({ error: "id is required" });
|
|
104
|
+
try {
|
|
105
|
+
const row = await updateSavedSqlRow(id, req.user.id, { name, sqlText });
|
|
106
|
+
if (!row) return reply.code(404).send({ error: "Not found" });
|
|
107
|
+
return reply.send(row);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
return reply.code(400).send({ error: err instanceof Error ? err.message : String(err) });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async function savedSqlDelete(req, reply) {
|
|
113
|
+
const { id } = req.body ?? {};
|
|
114
|
+
if (!id) return reply.code(400).send({ error: "id is required" });
|
|
115
|
+
await deleteSavedSqlRow(id, req.user.id);
|
|
116
|
+
return reply.code(204).send();
|
|
117
|
+
}
|
|
118
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
+
0 && (module.exports = {
|
|
120
|
+
savedSqlCreate,
|
|
121
|
+
savedSqlDelete,
|
|
122
|
+
savedSqlList,
|
|
123
|
+
savedSqlUpdate
|
|
124
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/saved-sql.model.ts
|
|
2
|
+
import { getPgDb } from "@heyhru/server-plugin-pg";
|
|
3
|
+
|
|
4
|
+
// src/saved-sql.sql.ts
|
|
5
|
+
var LIST_BY_USER = `
|
|
6
|
+
SELECT id, name, sql_text, data_source_id, database, created_at, updated_at
|
|
7
|
+
FROM saved_sqls
|
|
8
|
+
WHERE user_id = ?
|
|
9
|
+
ORDER BY updated_at DESC`;
|
|
10
|
+
var CREATE = `
|
|
11
|
+
INSERT INTO saved_sqls (user_id, name, sql_text, data_source_id, database)
|
|
12
|
+
VALUES (?, ?, ?, ?, ?)
|
|
13
|
+
RETURNING id, name, sql_text, data_source_id, database, created_at, updated_at`;
|
|
14
|
+
var DELETE = `
|
|
15
|
+
DELETE FROM saved_sqls
|
|
16
|
+
WHERE id = ? AND user_id = ?`;
|
|
17
|
+
|
|
18
|
+
// src/saved-sql.model.ts
|
|
19
|
+
function listSavedSqlsByUser(userId) {
|
|
20
|
+
return getPgDb().query(LIST_BY_USER, [userId]);
|
|
21
|
+
}
|
|
22
|
+
function createSavedSqlRow(opts) {
|
|
23
|
+
return getPgDb().queryOne(CREATE, [
|
|
24
|
+
opts.userId,
|
|
25
|
+
opts.name,
|
|
26
|
+
opts.sqlText,
|
|
27
|
+
opts.dataSourceId,
|
|
28
|
+
opts.database
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
async function updateSavedSqlRow(id, userId, data) {
|
|
32
|
+
const fields = [];
|
|
33
|
+
const values = [];
|
|
34
|
+
if (data.name !== void 0) {
|
|
35
|
+
fields.push("name = ?");
|
|
36
|
+
values.push(data.name);
|
|
37
|
+
}
|
|
38
|
+
if (data.sqlText !== void 0) {
|
|
39
|
+
fields.push("sql_text = ?");
|
|
40
|
+
values.push(data.sqlText);
|
|
41
|
+
}
|
|
42
|
+
if (!fields.length) throw new Error("No fields to update");
|
|
43
|
+
fields.push("updated_at = NOW()");
|
|
44
|
+
values.push(id, userId);
|
|
45
|
+
return getPgDb().queryOne(
|
|
46
|
+
`UPDATE saved_sqls SET ${fields.join(", ")} WHERE id = ? AND user_id = ? RETURNING id, name, sql_text, data_source_id, database, created_at, updated_at`,
|
|
47
|
+
values
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
function deleteSavedSqlRow(id, userId) {
|
|
51
|
+
return getPgDb().run(DELETE, [id, userId]);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/saved-sql.service.ts
|
|
55
|
+
async function savedSqlList(req, reply) {
|
|
56
|
+
return reply.send(await listSavedSqlsByUser(req.user.id));
|
|
57
|
+
}
|
|
58
|
+
async function savedSqlCreate(req, reply) {
|
|
59
|
+
const { name, sqlText, dataSourceId, database } = req.body ?? {};
|
|
60
|
+
if (!name || !sqlText) {
|
|
61
|
+
return reply.code(400).send({ error: "name and sqlText are required" });
|
|
62
|
+
}
|
|
63
|
+
const row = await createSavedSqlRow({
|
|
64
|
+
userId: req.user.id,
|
|
65
|
+
name,
|
|
66
|
+
sqlText,
|
|
67
|
+
dataSourceId: dataSourceId ?? null,
|
|
68
|
+
database: database ?? null
|
|
69
|
+
});
|
|
70
|
+
return reply.code(201).send(row);
|
|
71
|
+
}
|
|
72
|
+
async function savedSqlUpdate(req, reply) {
|
|
73
|
+
const { id, name, sqlText } = req.body ?? {};
|
|
74
|
+
if (!id) return reply.code(400).send({ error: "id is required" });
|
|
75
|
+
try {
|
|
76
|
+
const row = await updateSavedSqlRow(id, req.user.id, { name, sqlText });
|
|
77
|
+
if (!row) return reply.code(404).send({ error: "Not found" });
|
|
78
|
+
return reply.send(row);
|
|
79
|
+
} catch (err) {
|
|
80
|
+
return reply.code(400).send({ error: err instanceof Error ? err.message : String(err) });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function savedSqlDelete(req, reply) {
|
|
84
|
+
const { id } = req.body ?? {};
|
|
85
|
+
if (!id) return reply.code(400).send({ error: "id is required" });
|
|
86
|
+
await deleteSavedSqlRow(id, req.user.id);
|
|
87
|
+
return reply.code(204).send();
|
|
88
|
+
}
|
|
89
|
+
export {
|
|
90
|
+
savedSqlCreate,
|
|
91
|
+
savedSqlDelete,
|
|
92
|
+
savedSqlList,
|
|
93
|
+
savedSqlUpdate
|
|
94
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare function listSavedSqlsByUser(userId: string): Promise<Record<string, unknown>[]>;
|
|
2
|
+
interface CreateSavedSqlOpts {
|
|
3
|
+
userId: string;
|
|
4
|
+
name: string;
|
|
5
|
+
sqlText: string;
|
|
6
|
+
dataSourceId: string | null;
|
|
7
|
+
database: string | null;
|
|
8
|
+
}
|
|
9
|
+
export declare function createSavedSqlRow(opts: CreateSavedSqlOpts): Promise<Record<string, unknown> | undefined>;
|
|
10
|
+
export declare function updateSavedSqlRow(id: string, userId: string, data: {
|
|
11
|
+
name?: string;
|
|
12
|
+
sqlText?: string;
|
|
13
|
+
}): Promise<Record<string, unknown> | undefined>;
|
|
14
|
+
export declare function deleteSavedSqlRow(id: string, userId: string): Promise<{
|
|
15
|
+
changes: number;
|
|
16
|
+
}>;
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=saved-sql.model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"saved-sql.model.d.ts","sourceRoot":"","sources":["../src/saved-sql.model.ts"],"names":[],"mappings":"AAGA,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,sCAEjD;AAED,UAAU,kBAAkB;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,kBAAkB,gDAQzD;AAED,wBAAsB,iBAAiB,CACrC,EAAE,EAAE,MAAM,EACV,MAAM,EAAE,MAAM,EACd,IAAI,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,gDAmB1C;AAED,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;;GAE3D"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type FastifyRequest, type FastifyReply } from "fastify";
|
|
2
|
+
interface AuthenticatedRequest extends FastifyRequest {
|
|
3
|
+
user: {
|
|
4
|
+
id: string;
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export declare function savedSqlList(req: AuthenticatedRequest, reply: FastifyReply): Promise<never>;
|
|
8
|
+
export declare function savedSqlCreate(req: AuthenticatedRequest, reply: FastifyReply): Promise<never>;
|
|
9
|
+
export declare function savedSqlUpdate(req: AuthenticatedRequest, reply: FastifyReply): Promise<never>;
|
|
10
|
+
export declare function savedSqlDelete(req: AuthenticatedRequest, reply: FastifyReply): Promise<never>;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=saved-sql.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"saved-sql.service.d.ts","sourceRoot":"","sources":["../src/saved-sql.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAQjE,UAAU,oBAAqB,SAAQ,cAAc;IACnD,IAAI,EAAE;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;CACtB;AAED,wBAAsB,YAAY,CAAC,GAAG,EAAE,oBAAoB,EAAE,KAAK,EAAE,YAAY,kBAEhF;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,oBAAoB,EAAE,KAAK,EAAE,YAAY,kBAkBlF;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,oBAAoB,EAAE,KAAK,EAAE,YAAY,kBAclF;AAED,wBAAsB,cAAc,CAAC,GAAG,EAAE,oBAAoB,EAAE,KAAK,EAAE,YAAY,kBAKlF"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const LIST_BY_USER = "\nSELECT id, name, sql_text, data_source_id, database, created_at, updated_at\nFROM saved_sqls\nWHERE user_id = ?\nORDER BY updated_at DESC";
|
|
2
|
+
export declare const CREATE = "\nINSERT INTO saved_sqls (user_id, name, sql_text, data_source_id, database)\nVALUES (?, ?, ?, ?, ?)\nRETURNING id, name, sql_text, data_source_id, database, created_at, updated_at";
|
|
3
|
+
export declare const DELETE = "\nDELETE FROM saved_sqls\nWHERE id = ? AND user_id = ?";
|
|
4
|
+
//# sourceMappingURL=saved-sql.sql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"saved-sql.sql.d.ts","sourceRoot":"","sources":["../src/saved-sql.sql.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,gJAIA,CAAC;AAE1B,eAAO,MAAM,MAAM,yLAG4D,CAAC;AAEhF,eAAO,MAAM,MAAM,2DAEU,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@heyhru/business-dms-saved-sql",
|
|
3
|
+
"publishConfig": {
|
|
4
|
+
"access": "public"
|
|
5
|
+
},
|
|
6
|
+
"version": "0.4.0",
|
|
7
|
+
"description": "DMS saved SQL domain logic: service, model, sql",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.mjs",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup && tsc --emitDeclarationOnly",
|
|
23
|
+
"dev": "tsup --watch",
|
|
24
|
+
"lint": "eslint src",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"clean": "rm -rf dist"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@heyhru/server-plugin-pg": "0.4.0",
|
|
30
|
+
"fastify": "^5.8.4"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsup": "^8.5.1",
|
|
34
|
+
"typescript": "^6.0.2",
|
|
35
|
+
"vitest": "^4.1.2"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "48f00e1f0b1ca5d0789c0df9384a12f2c8e847c4"
|
|
38
|
+
}
|