@mrxsys/mrx-core 2.3.3-canary-20250514-a17e993
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/CHANGELOG.md +45 -0
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/dist/chunk-0pft81dh.js +2 -0
- package/dist/chunk-6sa6shm6.js +2 -0
- package/dist/chunk-7map0v4a.js +2 -0
- package/dist/chunk-dppgkz92.js +2 -0
- package/dist/chunk-kn1300vm.js +2 -0
- package/dist/chunk-nntk6mwx.js +2 -0
- package/dist/chunk-rr6e20n5.js +2 -0
- package/dist/chunk-s3xqwymw.js +2 -0
- package/dist/chunk-t411e41y.js +2 -0
- package/dist/chunk-wggj590m.js +2 -0
- package/dist/chunk-xcz2fxzt.js +2 -0
- package/dist/chunk-yeyzc9n2.js +2 -0
- package/dist/database/enums/databaseKeyError.d.ts +56 -0
- package/dist/database/enums/index.d.ts +2 -0
- package/dist/database/enums/index.js +2 -0
- package/dist/database/enums/mssqlErrorCode.d.ts +25 -0
- package/dist/database/events/index.d.ts +2 -0
- package/dist/database/events/index.js +2 -0
- package/dist/database/events/mssqlEvent.d.ts +3 -0
- package/dist/database/events/tableEvent.d.ts +6 -0
- package/dist/database/index.d.ts +5 -0
- package/dist/database/index.js +2 -0
- package/dist/database/mssql.d.ts +163 -0
- package/dist/database/table.d.ts +58 -0
- package/dist/database/types/index.d.ts +2 -0
- package/dist/database/types/index.js +1 -0
- package/dist/database/types/mssqlDatabaseOption.d.ts +92 -0
- package/dist/database/types/mssqlEventLog.d.ts +29 -0
- package/dist/elysia/advancedSearch.d.ts +460 -0
- package/dist/elysia/crud.d.ts +609 -0
- package/dist/elysia/dynamicDatabaseSelector.d.ts +352 -0
- package/dist/elysia/enums/elysiaKeyError.d.ts +16 -0
- package/dist/elysia/enums/httpStatusCode.d.ts +241 -0
- package/dist/elysia/enums/index.d.ts +2 -0
- package/dist/elysia/enums/index.js +2 -0
- package/dist/elysia/error.d.ts +50 -0
- package/dist/elysia/index.d.ts +7 -0
- package/dist/elysia/index.js +2 -0
- package/dist/elysia/jwt.d.ts +46 -0
- package/dist/elysia/microservice.d.ts +102 -0
- package/dist/elysia/ratelimit.d.ts +76 -0
- package/dist/elysia/schemas/index.d.ts +2 -0
- package/dist/elysia/schemas/index.js +2 -0
- package/dist/elysia/schemas/info.d.ts +9 -0
- package/dist/elysia/schemas/ping.d.ts +3 -0
- package/dist/elysia/types/crudOptions.d.ts +126 -0
- package/dist/elysia/types/crudRoutes.d.ts +2 -0
- package/dist/elysia/types/dynamicDatabaseSelectorPluginOptions.d.ts +30 -0
- package/dist/elysia/types/index.d.ts +5 -0
- package/dist/elysia/types/index.js +1 -0
- package/dist/elysia/types/jwtOptions.d.ts +92 -0
- package/dist/elysia/types/rateLimitOptions.d.ts +44 -0
- package/dist/error/coreError.d.ts +109 -0
- package/dist/error/index.d.ts +1 -0
- package/dist/error/index.js +2 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +2 -0
- package/dist/mailer/enums/index.d.ts +1 -0
- package/dist/mailer/enums/index.js +2 -0
- package/dist/mailer/enums/mailerKeyError.d.ts +12 -0
- package/dist/mailer/index.d.ts +1 -0
- package/dist/mailer/index.js +2 -0
- package/dist/mailer/smtp.d.ts +62 -0
- package/dist/mailer/types/index.d.ts +3 -0
- package/dist/mailer/types/index.js +1 -0
- package/dist/mailer/types/smtpCredentials.d.ts +10 -0
- package/dist/mailer/types/smtpOptions.d.ts +26 -0
- package/dist/mailer/types/smtpPoolOptions.d.ts +7 -0
- package/dist/repository/index.d.ts +1 -0
- package/dist/repository/index.js +2 -0
- package/dist/repository/repository.d.ts +381 -0
- package/dist/repository/types/advancedSearch.d.ts +47 -0
- package/dist/repository/types/index.d.ts +8 -0
- package/dist/repository/types/index.js +1 -0
- package/dist/repository/types/orderBy.d.ts +21 -0
- package/dist/repository/types/queryOptions.d.ts +33 -0
- package/dist/repository/types/queryOptionsExtendPagination.d.ts +21 -0
- package/dist/repository/types/queryOptionsExtendStream.d.ts +16 -0
- package/dist/repository/types/selectedFields.d.ts +16 -0
- package/dist/repository/types/transaction.d.ts +10 -0
- package/dist/repository/types/whereClause.d.ts +15 -0
- package/dist/store/index.d.ts +1 -0
- package/dist/store/index.js +2 -0
- package/dist/store/redis.d.ts +6 -0
- package/dist/utils/enums/index.d.ts +1 -0
- package/dist/utils/enums/index.js +2 -0
- package/dist/utils/enums/utilKeyError.d.ts +8 -0
- package/dist/utils/env.d.ts +10 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/isIsoDateString.d.ts +1 -0
- package/dist/utils/stream.d.ts +11 -0
- package/dist/utils/types/index.d.ts +1 -0
- package/dist/utils/types/index.js +1 -0
- package/dist/utils/types/streamWithAsyncIterable.d.ts +26 -0
- package/package.json +119 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
|
|
2
|
+
## v2.3.3-canary-20250514-a17e993
|
|
3
|
+
|
|
4
|
+
[compare changes](https://github.com/MRX-Systems/MRX-Core/compare/v2.3.3-canary-20250514-ce5e64c...v2.3.3-canary-20250514-a17e993)
|
|
5
|
+
|
|
6
|
+
### 🔧 Fixes
|
|
7
|
+
|
|
8
|
+
- **🔧:** [Remove t.ts] ([253a76bd](https://github.com/MRX-Systems/MRX-Core/commit/253a76bd))
|
|
9
|
+
|
|
10
|
+
### 🧹 Refactors
|
|
11
|
+
|
|
12
|
+
- **🧹:** [remove unused color enum exports] ([9daea580](https://github.com/MRX-Systems/MRX-Core/commit/9daea580))
|
|
13
|
+
- **🧹:** [Refactor file structure, export, comments] ([f7e58962](https://github.com/MRX-Systems/MRX-Core/commit/f7e58962))
|
|
14
|
+
- **🧹:** [rename package and update metadata] ([a4a081ce](https://github.com/MRX-Systems/MRX-Core/commit/a4a081ce))
|
|
15
|
+
|
|
16
|
+
### 📖 Documentation
|
|
17
|
+
|
|
18
|
+
- **📖:** [enhance HTTP status code documentation and structure] ([c6e65b86](https://github.com/MRX-Systems/MRX-Core/commit/c6e65b86))
|
|
19
|
+
- **📖:** [update WhereClause interface documentation] ([a8a40f58](https://github.com/MRX-Systems/MRX-Core/commit/a8a40f58))
|
|
20
|
+
- **📖:** [enhance Transaction type documentation] ([bc19f4c8](https://github.com/MRX-Systems/MRX-Core/commit/bc19f4c8))
|
|
21
|
+
- **📖:** [update async iterable interface documentation] ([f42d90b1](https://github.com/MRX-Systems/MRX-Core/commit/f42d90b1))
|
|
22
|
+
- **📖:** [improve documentation for QueryOptionsExtendPagination] ([304258f5](https://github.com/MRX-Systems/MRX-Core/commit/304258f5))
|
|
23
|
+
- **📖:** [clarify throwIfNoResult and transaction properties] ([3fa97eaa](https://github.com/MRX-Systems/MRX-Core/commit/3fa97eaa))
|
|
24
|
+
- **📖:** [update OrderBy type documentation for clarity] ([4b6366e6](https://github.com/MRX-Systems/MRX-Core/commit/4b6366e6))
|
|
25
|
+
- **📖:** [add remarks for MssqlEventLog interface clarity] ([84ffca77](https://github.com/MRX-Systems/MRX-Core/commit/84ffca77))
|
|
26
|
+
- **📖:** [Add detailed documentation for plugins] ([f2019ea8](https://github.com/MRX-Systems/MRX-Core/commit/f2019ea8))
|
|
27
|
+
|
|
28
|
+
### 📦 Build
|
|
29
|
+
|
|
30
|
+
- **📦:** [update @types/bun dependency version] ([94db9e9a](https://github.com/MRX-Systems/MRX-Core/commit/94db9e9a))
|
|
31
|
+
- **📦:** [update docs script in package.json] ## Refactoring - Updated the "docs" script in package.json to include the tsconfig file. ([6727b000](https://github.com/MRX-Systems/MRX-Core/commit/6727b000))
|
|
32
|
+
|
|
33
|
+
### 🌊 Types
|
|
34
|
+
|
|
35
|
+
- **🌊:** [refine advanced search type definitions and examples] ([f1d9a5fb](https://github.com/MRX-Systems/MRX-Core/commit/f1d9a5fb))
|
|
36
|
+
|
|
37
|
+
### 🦉 Chore
|
|
38
|
+
|
|
39
|
+
- **🦉:** V2.3.3-canary-20250514-764de87 ([79956e3c](https://github.com/MRX-Systems/MRX-Core/commit/79956e3c))
|
|
40
|
+
|
|
41
|
+
### ❤️ Contributors
|
|
42
|
+
|
|
43
|
+
- Ruby <necrelox@proton.me>
|
|
44
|
+
- Github-actions <necrelox@proton.me>
|
|
45
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 MRX Systems
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# 📦 Core
|
|
2
|
+
|
|
3
|
+
> ONLY FOR BUN
|
|
4
|
+
|
|
5
|
+
## 📌 Table of contents
|
|
6
|
+
|
|
7
|
+
- [📦 Core](#-core)
|
|
8
|
+
- [📌 Table of contents](#-table-of-contents)
|
|
9
|
+
- [⚖️ License](#-license)
|
|
10
|
+
- [📧 Contact](#-contact)
|
|
11
|
+
|
|
12
|
+
## ⚖️ License
|
|
13
|
+
|
|
14
|
+
Distributed under the MIT License. See LICENSE for more information.
|
|
15
|
+
|
|
16
|
+
## 📧 Contact
|
|
17
|
+
|
|
18
|
+
Mail - [dev@mrxsys.com](mailto:dev@mrxsys.com)
|
|
19
|
+
|
|
20
|
+
[Project link](https://github.com/MRX-Systems/MRX-Core)
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var{randomUUIDv7:g}=globalThis.Bun;class j extends Error{cause;_uuid=g();_date=new Date;_key;_httpStatusCode;constructor(f){super(f?.message);super.name="CoreError",this.cause=f?.cause,this._key=f?.key||"",this._httpStatusCode=f?.httpStatusCode||500}get uuid(){return this._uuid}get date(){return this._date}get key(){return this._key}get httpStatusCode(){return this._httpStatusCode}}
|
|
2
|
+
export{j as _};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var x=(g)=>typeof g==="string"&&new Date(g).toString()!=="Invalid Date";var A=(g)=>{let h={[Symbol.asyncIterator]:()=>({next(){return new Promise((i,q)=>{let w=()=>{g.off("data",k),g.off("end",D),g.off("error",I)},k=(S)=>{w(),i({value:S,done:!1})},D=()=>{w(),i({value:void 0,done:!0})},I=(S)=>{w(),q(S)};g.on("data",k),g.on("end",D),g.on("error",I)})}})};return g[Symbol.asyncIterator]=h[Symbol.asyncIterator],g};
|
|
2
|
+
export{x as Y,A as Z};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var f={mssqlConnectionError:"core.error.mssql.database.connection_error",mssqlDisconnectError:"core.error.mssql.database.disconnect_error",mssqlNotConnected:"core.error.mssql.database.not_connected",mssqlNoResult:"core.error.mssql.database.no_result",mssqlQueryError:"core.error.mssql.database.query_error",mssqlDatabaseAccessDenied:"core.error.mssql.database.access_denied",mssqlDatabaseAuthorizationFailed:"core.error.mssql.database.authorization_failed",mssqlDatabaseSyntaxError:"core.error.mssql.database.syntax_error",mssqlDatabaseColumnNotFound:"core.error.mssql.database.column_not_found",mssqlTableNotFound:"core.error.mssql.database.table_not_found",mssqlDatabaseAmbiguousColumn:"core.error.mssql.database.ambiguous_column",mssqlDatabaseDuplicateKey:"core.error.mssql.database.duplicate_key",mssqlDatabaseForeignKeyViolation:"core.error.mssql.database.foreign_key_violation",mssqlDatabaseUniqueConstraintViolation:"core.error.mssql.database.unique_constraint_violation",mssqlDatabaseDeadlockDetected:"core.error.mssql.database.deadlock_detected",mssqlDatabaseResourceLocked:"core.error.mssql.database.resource_locked",mssqlDatabaseTransactionAborted:"core.error.mssql.database.transaction_aborted",mssqlDatabaseInsufficientMemory:"core.error.mssql.database.insufficient_memory",mssqlDatabaseInsufficientStorage:"core.error.mssql.database.insufficient_storage",mssqlDatabaseQueryTimeout:"core.error.mssql.database.query_timeout",mssqlDatabaseTransactionLogFull:"core.error.mssql.database.transaction_log_full",mssqlDatabaseDataTooLarge:"core.error.mssql.database.data_too_large",mssqlDatabasePermissionDenied:"core.error.mssql.database.permission_denied",mssqlDatabaseIdentityInsertNotAllowed:"core.error.mssql.database.identity_insert_not_allowed",mssqlDatabaseCannotUpdateIdentityColumn:"core.error.mssql.database.cannot_update_identity_column"};var v={0:f.mssqlQueryError,4060:f.mssqlDatabaseAccessDenied,18452:f.mssqlDatabaseAuthorizationFailed,18456:f.mssqlDatabaseAuthorizationFailed,102:f.mssqlDatabaseSyntaxError,207:f.mssqlDatabaseColumnNotFound,208:f.mssqlTableNotFound,209:f.mssqlDatabaseAmbiguousColumn,2627:f.mssqlDatabaseDuplicateKey,547:f.mssqlDatabaseForeignKeyViolation,2601:f.mssqlDatabaseUniqueConstraintViolation,1205:f.mssqlDatabaseDeadlockDetected,1222:f.mssqlDatabaseResourceLocked,3928:f.mssqlDatabaseTransactionAborted,701:f.mssqlDatabaseInsufficientMemory,1105:f.mssqlDatabaseInsufficientStorage,8645:f.mssqlDatabaseQueryTimeout,9002:f.mssqlDatabaseTransactionLogFull,8152:f.mssqlDatabaseDataTooLarge,229:f.mssqlDatabasePermissionDenied,544:f.mssqlDatabaseIdentityInsertNotAllowed,8102:f.mssqlDatabaseCannotUpdateIdentityColumn};
|
|
2
|
+
export{f as $,v as aa};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function l(x,z){return z in x}function a(x){return Array.isArray(x)}function e(x){return typeof x==="bigint"}function Q(x){return typeof x==="boolean"}function xx(x){return x instanceof globalThis.Date}function W(x){return typeof x==="number"}function q(x){return typeof x==="object"&&x!==null}function Ex(x){return x instanceof globalThis.RegExp}function B(x){return typeof x==="string"}function qx(x){return x instanceof globalThis.Uint8Array}function wx(x){return x===void 0}class Y extends Error{constructor(x){super(x)}}var G=Symbol.for("TypeBox.Transform"),Z=Symbol.for("TypeBox.Readonly"),J=Symbol.for("TypeBox.Optional"),_=Symbol.for("TypeBox.Hint"),w=Symbol.for("TypeBox.Kind");function Jx(x){return q(x)&&x[J]==="Optional"}function $(x){return E(x,"Any")}function D(x){return E(x,"Argument")}function F(x){return E(x,"Array")}function P(x){return E(x,"AsyncIterator")}function V(x){return E(x,"BigInt")}function h(x){return E(x,"Boolean")}function C(x){return E(x,"Computed")}function H(x){return E(x,"Constructor")}function M(x){return E(x,"Date")}function T(x){return E(x,"Function")}function N(x){return E(x,"Integer")}function S(x){return E(x,"Intersect")}function c(x){return E(x,"Iterator")}function E(x,z){return q(x)&&w in x&&x[w]===z}function j(x){return E(x,"Literal")}function L(x){return E(x,"MappedKey")}function b(x){return E(x,"MappedResult")}function k(x){return E(x,"Never")}function g(x){return E(x,"Not")}function p(x){return E(x,"Null")}function R(x){return E(x,"Number")}function U(x){return E(x,"Object")}function A(x){return E(x,"Promise")}function o(x){return E(x,"Record")}function m(x){return E(x,"Ref")}function y(x){return E(x,"RegExp")}function r(x){return E(x,"String")}function t(x){return E(x,"Symbol")}function O(x){return E(x,"TemplateLiteral")}function f(x){return E(x,"This")}function Qx(x){return q(x)&&G in x}function K(x){return E(x,"Tuple")}function d(x){return E(x,"Undefined")}function i(x){return E(x,"Union")}function n(x){return E(x,"Uint8Array")}function I(x){return E(x,"Unknown")}function s(x){return E(x,"Unsafe")}function v(x){return E(x,"Void")}function u(x){return q(x)&&w in x&&B(x[w])}function Wx(x){return $(x)||D(x)||F(x)||h(x)||V(x)||P(x)||C(x)||H(x)||M(x)||T(x)||N(x)||S(x)||c(x)||j(x)||L(x)||b(x)||k(x)||g(x)||p(x)||R(x)||U(x)||A(x)||o(x)||m(x)||y(x)||r(x)||t(x)||O(x)||f(x)||K(x)||d(x)||i(x)||n(x)||I(x)||s(x)||v(x)||u(x)}
|
|
2
|
+
export{l as q,a as r,e as s,Q as t,xx as u,W as v,q as w,Ex as x,B as y,qx as z,wx as A,Y as B,G as C,Z as D,J as E,_ as F,w as G,Jx as H,F as I,S as J,b as K,k as L,U as M,o as N,Qx as O,K as P,d as Q,i as R,u as S,Wx as T};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var f={jwtSignError:"core.error.elysia.jwt_sign_error",jwtSecretNotFound:"core.error.elysia.jwt_secret_not_found",rateLimitExceeded:"core.error.elysia.rate_limit_exceeded",dynamicDatabaseKeyNotFound:"core.error.elysia.dynamic_database_key_not_found",needAdvancedSearch:"core.error.elysia.need_advanced_search"};var m={ok:200,created:201,accepted:202,nonAuthoritativeInformation:203,noContent:204,resetContent:205,partialContent:206,multiStatus:207,alreadyReported:208,imUsed:226,multipleChoices:300,movedPermanently:301,found:302,seeOther:303,notModified:304,useProxy:305,temporaryRedirect:307,permanentRedirect:308,badRequest:400,unauthorized:401,paymentRequired:402,forbidden:403,notFound:404,methodNotAllowed:405,notAcceptable:406,proxyAuthenticationRequired:407,requestTimeout:408,conflict:409,gone:410,lengthRequired:411,preconditionFailed:412,payloadTooLarge:413,uriTooLong:414,unsupportedMediaType:415,rangeNotSatisfiable:416,expectationFailed:417,imATeapot:418,misdirectedRequest:421,unprocessableEntity:422,locked:423,failedDependency:424,tooEarly:425,upgradeRequired:426,preconditionRequired:428,tooManyRequests:429,requestHeaderFieldsTooLarge:431,unavailableForLegalReasons:451,internalServerError:500,notImplemented:501,badGateway:502,serviceUnavailable:503,gatewayTimeout:504,httpVersionNotSupported:505,variantAlsoNegotiates:506,insufficientStorage:507,loopDetected:508,notExtended:510,networkAuthenticationRequired:511};
|
|
2
|
+
export{f as da,m as ea};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{Y as _,Z as A}from"./chunk-7map0v4a.js";import{_ as U}from"./chunk-6sa6shm6.js";import{$ as X,aa as Z}from"./chunk-dppgkz92.js";import{PassThrough as x}from"stream";var $={$eq:(j,z,G)=>j.where(z,G),$neq:(j,z,G)=>j.whereNot(z,G),$lt:(j,z,G)=>j.where(z,"<",G),$lte:(j,z,G)=>j.where(z,"<=",G),$gt:(j,z,G)=>j.where(z,">",G),$gte:(j,z,G)=>j.where(z,">=",G),$in:(j,z,G)=>j.whereIn(z,G),$nin:(j,z,G)=>j.whereNotIn(z,G),$between:(j,z,G)=>j.whereBetween(z,G),$nbetween:(j,z,G)=>j.whereNotBetween(z,G),$like:(j,z,G)=>{let H=`%${G}%`;if(_(G))return j.whereRaw(`CONVERT(VARCHAR, ${z}, 23) LIKE ?`,[H]);return j.whereLike(z,H)},$nlike:(j,z,G)=>{let H=`%${G}%`;if(_(G))return j.whereRaw(`CONVERT(VARCHAR, ${z}, 23) NOT LIKE ?`,[H]);return j.whereRaw(`${z} NOT LIKE ?`,[H])},$isNull:(j,z,G)=>G?j.whereNull(z):j.whereNotNull(z)};class R{_knex;_table;constructor(j,z){this._knex=j,this._table=z}findStream(j){let z=this._knex(this._table.name).select(j?.selectedFields??"*");if(j?.advancedSearch)this._applySearch(z,j.advancedSearch);let G=[j?.orderBy?.[0]||this._table.primaryKey[0],j?.orderBy?.[1]||"asc"];z.orderBy(G[0],G[1]);let H=z.stream(),L=new x({objectMode:!0,...j?.transform&&{transform:j.transform}});return H.on("error",(M)=>{let J=M?.number||0;L.emit("error",new U({key:Z[J]??X.mssqlQueryError,message:"An error occurred while streaming the query results.",cause:{query:z.toSQL().sql,error:M}}))}),H.pipe(L),A(L)}async find(j){let z=this._knex(this._table.name).select(j?.selectedFields??"*");if(j?.advancedSearch)this._applySearch(z,j.advancedSearch);let G=[j?.orderBy?.[0]||this._table.primaryKey[0],j?.orderBy?.[1]||"asc"],H=j?.limit||100,L=j?.offset||0;return z.orderBy(G[0],G[1]).limit(H).offset(L),this._executeQuery(z,j?.throwIfNoResult)}async findOne(j){let z=this._knex(this._table.name).select(j?.selectedFields??"*");if(j?.advancedSearch)this._applySearch(z,j.advancedSearch);let G=[j?.orderBy?.[0]||this._table.primaryKey[0],j?.orderBy?.[1]||"asc"];return z.orderBy(G[0],G[1]),this._executeQuery(z,j?.throwIfNoResult).then((H)=>H[0])}async count(j){let z=this._knex(this._table.name).count({count:"*"});if(j?.advancedSearch)this._applySearch(z,j.advancedSearch);return this._executeQuery(z,j?.throwIfNoResult).then((G)=>G[0].count)}async insert(j,z){let G=this._knex(this._table.name).insert(j).returning(z?.selectedFields??"*");return this._executeQuery(G)}async update(j,z){let G=this._knex(this._table.name).update(j).returning(z?.selectedFields??"*");if(z?.advancedSearch)this._applySearch(G,z.advancedSearch);return this._executeQuery(G)}async delete(j){let z=this._knex(this._table.name).delete().returning(j?.selectedFields??"*");if(j?.advancedSearch)this._applySearch(z,j.advancedSearch);return this._executeQuery(z)}_applySearch(j,z){let G=(H,L)=>{for(let[M,J]of Object.entries(L))if(this._isComplexQuery(J)){let Q=J;for(let[O,W]of Object.entries(Q))if(O in $){let Y=$[O];Y(H,M,W)}}else if(M==="$q"&&(typeof J==="string"||typeof J==="number")){for(let Q of this._table.fields)if(J)H.orWhere(Q,"like",`%${J}%`)}else if(M==="$q"&&typeof J==="object"&&"selectedFields"in J){let{selectedFields:Q,value:O}=J,W=typeof O==="number",Y=W?"=":"like",F=W?O:`%${O}%`;Q.forEach((P)=>{H.orWhere(P,Y,F)})}else{if(typeof J==="object"&&Object.keys(J).length===0)continue;H.where(M,J)}};if(Array.isArray(z))z.reduce((H,L)=>H.orWhere((M)=>this._applySearch(M,L)),j);else G(j,z)}_handleError(j,z){if(j instanceof U)throw j;let G=j?.number||0;throw new U({key:Z[G]??X.mssqlQueryError,message:"An error occurred while executing the query.",cause:{query:z.toSQL().sql,error:j}})}_isComplexQuery(j){let z=new Set(["$eq","$neq","$lt","$lte","$gt","$gte","$in","$nin","$between","$nbetween","$like","$nlike","$isNull"]);return Boolean(j&&typeof j==="object"&&!Array.isArray(j)&&Object.keys(j).some((G)=>z.has(G)))}async _executeQuery(j,z=!1){try{let G=await j;if(z&&G.length===0)throw new U({key:X.mssqlNoResult,message:"No records found matching the specified query options.",cause:{query:j.toSQL().sql}});return G}catch(G){return this._handleError(G,j)}}}
|
|
2
|
+
export{$ as W,R as X};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{X}from"./chunk-t411e41y.js";import{_ as O}from"./chunk-6sa6shm6.js";import{$ as J}from"./chunk-dppgkz92.js";import{ba as W,ca as P}from"./chunk-yeyzc9n2.js";import $ from"events";class V extends ${_databaseName;_fields=[];_name;_primaryKey;constructor(w,F,z,H){super();this._databaseName=w,this._name=F,this._fields=z,this._primaryKey=H}get databaseName(){return this._databaseName}get name(){return this._name}get fields(){return this._fields}get primaryKey(){return this._primaryKey}}import{EventEmitter as A}from"events";import B from"knex";class D extends A{_isConnected=!1;_databaseName;_tables=new Map;_repositories=new Map;_db;_pulse;constructor(w){super();this._databaseName=w.databaseName,this._pulse=w.pulse??!1,this._db=B({client:"mssql",debug:w.debug??!0,acquireConnectionTimeout:w.connectionTimeout??20000,log:{debug:(F)=>{if(w.debug){let z=this._extractTablesFromSqlQuery(F.sql),H={database:w.databaseName,tables:z,queryUuid:F.__knexQueryUid,method:F.method,sql:F.sql,bindings:F.bindings};this.emit(W.log,H)}}},connection:{database:w.databaseName,host:w.host,port:w.port,user:w.user,password:w.password,options:{encrypt:w.encrypt??!0},bigNumberStrings:!1},pool:{min:w.poolMin??2,max:w.poolMax??10}})}async connect(){try{if(await this._introspectDatabase(),this._isConnected=!0,this._pulse)this._addEventKnex()}catch(w){throw new O({key:J.mssqlConnectionError,message:`Failed to connect to the database: "${this._databaseName}".`,cause:w})}}async disconnect(){if(!this._isConnected)throw new O({key:J.mssqlNotConnected,message:`Database "${this._databaseName}" is not connected.`});try{await this._db.destroy(),this._isConnected=!1}catch(w){throw new O({key:J.mssqlDisconnectError,message:`Failed to disconnect from the database: "${this._databaseName}".`,cause:w})}}getRepository(w,F){if(!this._isConnected)throw new O({key:J.mssqlNotConnected,message:`Database "${this._databaseName}" is not connected.`});if(!this._tables.has(w))throw new O({key:J.mssqlTableNotFound,message:`Table not found: "${w}".`,cause:{table:w}});let z=this._repositories.get(w);if(F){let H=this._tables.get(w);if(z&&z instanceof F)return z;return z=new F(this._db,H),this._repositories.set(w,z),z}return this._repositories.get(w)}getTable(w){if(!this._isConnected)throw new O({key:J.mssqlNotConnected,message:`Database "${this._databaseName}" is not connected.`});if(!this._tables.has(w))throw new O({key:J.mssqlTableNotFound,message:`Table not found: "${w}".`,cause:{table:w}});return this._tables.get(w)}get databaseName(){return this._databaseName}get tables(){return this._tables}get repositories(){return this._repositories}get isConnected(){return this._isConnected}get db(){if(!this._isConnected)throw new O({key:J.mssqlNotConnected,message:`Database "${this._databaseName}" is not connected.`});return this._db}async _introspectDatabase(){(await this._db.from({c:"information_schema.columns"}).select("c.table_name as tableName").select(this._db.raw("STRING_AGG(c.column_name, ',') AS fields")).select("pk.primaryKeyColumn as primaryKeyColumn").select("pk.primaryKeyType as primaryKeyType").leftJoin(this._db.from({tc:"information_schema.table_constraints"}).join({kcu:"information_schema.key_column_usage"},"tc.constraint_name","kcu.constraint_name").join({col:"information_schema.columns"},(z)=>{z.on("col.table_name","=","kcu.table_name").andOn("col.column_name","=","kcu.column_name")}).where("tc.constraint_type","PRIMARY KEY").groupBy("tc.table_name","kcu.column_name","col.data_type").select("tc.table_name as tableName").select("kcu.column_name as primaryKeyColumn").select("col.data_type as primaryKeyType").as("pk"),"c.table_name","pk.tableName").groupBy("c.table_name","pk.primaryKeyColumn","pk.primaryKeyType")).reduce((z,{tableName:H,fields:Q,primaryKeyColumn:Y,primaryKeyType:Z})=>{let _=Z==="int"?"NUMBER":"STRING";return z.set(H,{fields:Q.split(","),primaryKey:[Y,_]}),z},new Map).forEach((z,H)=>{let Q=new V(this._databaseName,H,z.fields,z.primaryKey);this._tables.set(H,Q),this._repositories.set(H,new X(this._db,Q))})}_extractTablesFromSqlQuery(w){let F=/(?:update|insert\s+into|delete\s+from|from|join|with)\s+\[?([^\]\s]+)\]?/gi,z=[...w.matchAll(F)].map((H)=>H[1]);return Array.from(new Set(z))}_handleQueryResponse(w,F){let z=this._extractTablesFromSqlQuery(F.sql),H=this._tables.get(z[0]);switch(F.method){case"select":H?.emit(P.selected,w);break;case"insert":H?.emit(P.created,w);break;case"update":H?.emit(P.updated,w);break;case"delete":H?.emit(P.deleted,w);break;default:break}}_handleQueryError(w,F){this.emit("query:error",w,F)}_addEventKnex(){this._db.on("query-error",this._handleQueryError.bind(this)),this._db.on("query-response",this._handleQueryResponse.bind(this))}}
|
|
2
|
+
export{V as U,D as V};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{t as e}from"elysia";var o=e.Object({message:e.String({description:"Message",example:"Microservice Information"}),content:e.Object({author:e.String({description:"Author",example:"Ruby"}),name:e.String({description:"Name",example:"API"}),version:e.String({description:"Version",example:"1.0.0"}),description:e.String({description:"Description",example:"Is a microservice that provides a RESTful API for the application."})})});import{t as i}from"elysia";var s=i.Object({message:i.String({description:"Message",example:"pong"})});
|
|
2
|
+
export{o as fa,s as ga};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database error key is a list of errors in the database context.
|
|
3
|
+
* Each property represents a specific MSSQL error scenario.
|
|
4
|
+
*/
|
|
5
|
+
export declare const databaseKeyError: {
|
|
6
|
+
/** Error when failing to connect to MSSQL database. */
|
|
7
|
+
readonly mssqlConnectionError: "core.error.mssql.database.connection_error";
|
|
8
|
+
/** Error when disconnecting from MSSQL database. */
|
|
9
|
+
readonly mssqlDisconnectError: "core.error.mssql.database.disconnect_error";
|
|
10
|
+
/** Error when MSSQL database is not connected. */
|
|
11
|
+
readonly mssqlNotConnected: "core.error.mssql.database.not_connected";
|
|
12
|
+
/** Error when no result is returned from MSSQL query. */
|
|
13
|
+
readonly mssqlNoResult: "core.error.mssql.database.no_result";
|
|
14
|
+
/** Error when a query fails in MSSQL database. */
|
|
15
|
+
readonly mssqlQueryError: "core.error.mssql.database.query_error";
|
|
16
|
+
/** Error when access to MSSQL database is denied. */
|
|
17
|
+
readonly mssqlDatabaseAccessDenied: "core.error.mssql.database.access_denied";
|
|
18
|
+
/** Error when MSSQL database authorization fails. */
|
|
19
|
+
readonly mssqlDatabaseAuthorizationFailed: "core.error.mssql.database.authorization_failed";
|
|
20
|
+
/** Error when there is a syntax error in MSSQL database. */
|
|
21
|
+
readonly mssqlDatabaseSyntaxError: "core.error.mssql.database.syntax_error";
|
|
22
|
+
/** Error when a column is not found in MSSQL database. */
|
|
23
|
+
readonly mssqlDatabaseColumnNotFound: "core.error.mssql.database.column_not_found";
|
|
24
|
+
/** Error when a table is not found in MSSQL database. */
|
|
25
|
+
readonly mssqlTableNotFound: "core.error.mssql.database.table_not_found";
|
|
26
|
+
/** Error when there is an ambiguous column in MSSQL database. */
|
|
27
|
+
readonly mssqlDatabaseAmbiguousColumn: "core.error.mssql.database.ambiguous_column";
|
|
28
|
+
/** Error when there is a duplicate key in MSSQL database. */
|
|
29
|
+
readonly mssqlDatabaseDuplicateKey: "core.error.mssql.database.duplicate_key";
|
|
30
|
+
/** Error when there is a foreign key violation in MSSQL database. */
|
|
31
|
+
readonly mssqlDatabaseForeignKeyViolation: "core.error.mssql.database.foreign_key_violation";
|
|
32
|
+
/** Error when a unique constraint is violated in MSSQL database. */
|
|
33
|
+
readonly mssqlDatabaseUniqueConstraintViolation: "core.error.mssql.database.unique_constraint_violation";
|
|
34
|
+
/** Error when a deadlock is detected in MSSQL database. */
|
|
35
|
+
readonly mssqlDatabaseDeadlockDetected: "core.error.mssql.database.deadlock_detected";
|
|
36
|
+
/** Error when a resource is locked in MSSQL database. */
|
|
37
|
+
readonly mssqlDatabaseResourceLocked: "core.error.mssql.database.resource_locked";
|
|
38
|
+
/** Error when a transaction is aborted in MSSQL database. */
|
|
39
|
+
readonly mssqlDatabaseTransactionAborted: "core.error.mssql.database.transaction_aborted";
|
|
40
|
+
/** Error when there is insufficient memory in MSSQL database. */
|
|
41
|
+
readonly mssqlDatabaseInsufficientMemory: "core.error.mssql.database.insufficient_memory";
|
|
42
|
+
/** Error when there is insufficient storage in MSSQL database. */
|
|
43
|
+
readonly mssqlDatabaseInsufficientStorage: "core.error.mssql.database.insufficient_storage";
|
|
44
|
+
/** Error when a query times out in MSSQL database. */
|
|
45
|
+
readonly mssqlDatabaseQueryTimeout: "core.error.mssql.database.query_timeout";
|
|
46
|
+
/** Error when the transaction log is full in MSSQL database. */
|
|
47
|
+
readonly mssqlDatabaseTransactionLogFull: "core.error.mssql.database.transaction_log_full";
|
|
48
|
+
/** Error when data is too large for MSSQL database. */
|
|
49
|
+
readonly mssqlDatabaseDataTooLarge: "core.error.mssql.database.data_too_large";
|
|
50
|
+
/** Error when permission is denied in MSSQL database. */
|
|
51
|
+
readonly mssqlDatabasePermissionDenied: "core.error.mssql.database.permission_denied";
|
|
52
|
+
/** Error when identity insert is not allowed in MSSQL database. */
|
|
53
|
+
readonly mssqlDatabaseIdentityInsertNotAllowed: "core.error.mssql.database.identity_insert_not_allowed";
|
|
54
|
+
/** Error when updating identity column is not allowed in MSSQL database. */
|
|
55
|
+
readonly mssqlDatabaseCannotUpdateIdentityColumn: "core.error.mssql.database.cannot_update_identity_column";
|
|
56
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** MSSQL error codes with their corresponding error key. */
|
|
2
|
+
export declare const mssqlErrorCode: {
|
|
3
|
+
readonly 0: "core.error.mssql.database.query_error";
|
|
4
|
+
readonly 4060: "core.error.mssql.database.access_denied";
|
|
5
|
+
readonly 18452: "core.error.mssql.database.authorization_failed";
|
|
6
|
+
readonly 18456: "core.error.mssql.database.authorization_failed";
|
|
7
|
+
readonly 102: "core.error.mssql.database.syntax_error";
|
|
8
|
+
readonly 207: "core.error.mssql.database.column_not_found";
|
|
9
|
+
readonly 208: "core.error.mssql.database.table_not_found";
|
|
10
|
+
readonly 209: "core.error.mssql.database.ambiguous_column";
|
|
11
|
+
readonly 2627: "core.error.mssql.database.duplicate_key";
|
|
12
|
+
readonly 547: "core.error.mssql.database.foreign_key_violation";
|
|
13
|
+
readonly 2601: "core.error.mssql.database.unique_constraint_violation";
|
|
14
|
+
readonly 1205: "core.error.mssql.database.deadlock_detected";
|
|
15
|
+
readonly 1222: "core.error.mssql.database.resource_locked";
|
|
16
|
+
readonly 3928: "core.error.mssql.database.transaction_aborted";
|
|
17
|
+
readonly 701: "core.error.mssql.database.insufficient_memory";
|
|
18
|
+
readonly 1105: "core.error.mssql.database.insufficient_storage";
|
|
19
|
+
readonly 8645: "core.error.mssql.database.query_timeout";
|
|
20
|
+
readonly 9002: "core.error.mssql.database.transaction_log_full";
|
|
21
|
+
readonly 8152: "core.error.mssql.database.data_too_large";
|
|
22
|
+
readonly 229: "core.error.mssql.database.permission_denied";
|
|
23
|
+
readonly 544: "core.error.mssql.database.identity_insert_not_allowed";
|
|
24
|
+
readonly 8102: "core.error.mssql.database.cannot_update_identity_column";
|
|
25
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import"./enums/index.js";import"./events/index.js";import{U as s,V as a}from"../chunk-wggj590m.js";import"../chunk-t411e41y.js";import"../chunk-7map0v4a.js";import"../chunk-6sa6shm6.js";import{$ as f,aa as m}from"../chunk-dppgkz92.js";import{ba as x,ca as y}from"../chunk-yeyzc9n2.js";import"../chunk-rr6e20n5.js";export{y as tableEvent,x as mssqlEvent,m as mssqlErrorCode,f as databaseKeyError,s as Table,a as MSSQL};
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
import { type Knex } from 'knex';
|
|
3
|
+
import { Repository } from '../repository/repository';
|
|
4
|
+
import { Table } from './table';
|
|
5
|
+
import type { MSSQLDatabaseOptions } from './types/mssqlDatabaseOption';
|
|
6
|
+
/**
|
|
7
|
+
* Manages the connection with an MSSQL database.
|
|
8
|
+
* The class extends {@link EventEmitter}.
|
|
9
|
+
*
|
|
10
|
+
* - Emits events for logging and table operations.
|
|
11
|
+
* - Automatically generates repositories and tables for CRUD operations.
|
|
12
|
+
* - Allows custom repository implementations per table.
|
|
13
|
+
*
|
|
14
|
+
* Example usage:
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const mssql = new MSSQL(options);
|
|
17
|
+
* await mssql.connect();
|
|
18
|
+
* const users = await mssql.getRepository('users').find();
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare class MSSQL extends EventEmitter {
|
|
22
|
+
/**
|
|
23
|
+
* Indicates whether the database is connected.
|
|
24
|
+
*/
|
|
25
|
+
private _isConnected;
|
|
26
|
+
/**
|
|
27
|
+
* The name of the database.
|
|
28
|
+
*/
|
|
29
|
+
private readonly _databaseName;
|
|
30
|
+
/**
|
|
31
|
+
* A map of tables in the database.
|
|
32
|
+
* @see {@link Table}
|
|
33
|
+
*/
|
|
34
|
+
private readonly _tables;
|
|
35
|
+
/**
|
|
36
|
+
* A map of repositories for each table.
|
|
37
|
+
* @see {@link Repository}
|
|
38
|
+
*/
|
|
39
|
+
private readonly _repositories;
|
|
40
|
+
/**
|
|
41
|
+
* The Knex instance for the database connection.
|
|
42
|
+
* @see {@link Knex}
|
|
43
|
+
* @see https://knexjs.org/
|
|
44
|
+
*/
|
|
45
|
+
private readonly _db;
|
|
46
|
+
/**
|
|
47
|
+
* Indicates whether to add basic event listeners for all tables.
|
|
48
|
+
* @default false
|
|
49
|
+
*/
|
|
50
|
+
private readonly _pulse;
|
|
51
|
+
/**
|
|
52
|
+
* Create a new instance of `MSSQL` with the specified options.
|
|
53
|
+
* @param options - The configuration options for the MSSQL database connection.
|
|
54
|
+
* @see {@link MSSQLDatabaseOptions}
|
|
55
|
+
*/
|
|
56
|
+
constructor(options: MSSQLDatabaseOptions);
|
|
57
|
+
/**
|
|
58
|
+
* Establishes a connection to the MSSQL database.
|
|
59
|
+
*
|
|
60
|
+
* If the connection is successful, introspection is performed to retrieve information about tables, columns(fields), and primary keys.
|
|
61
|
+
* This information is used to create instances of {@link Table} and {@link Repository} for each table in the database.
|
|
62
|
+
*
|
|
63
|
+
* @throws ({@link CoreError}) Thrown if an error occurs during the connection process. ({@link databaseKeyError.mssqlConnectionError})
|
|
64
|
+
*/
|
|
65
|
+
connect(): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Closes the connection to the MSSQL database.
|
|
68
|
+
*
|
|
69
|
+
* @throws ({@link CoreError}) Thrown if the database is not connected. ({@link databaseKeyError.mssqlNotConnected})
|
|
70
|
+
* @throws ({@link CoreError}) Thrown if an error occurs during the disconnection process. ({@link databaseKeyError.mssqlDisconnectError})
|
|
71
|
+
*/
|
|
72
|
+
disconnect(): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Retrieves a repository for a specific table.
|
|
75
|
+
*
|
|
76
|
+
* @param tableName - The name of the table to retrieve the repository for.
|
|
77
|
+
* @param customRepository - Optional custom repository class to use for the table. The class must extend {@link Repository}.
|
|
78
|
+
*
|
|
79
|
+
* @throws ({@link CoreError}) Thrown if the database is not connected. ({@link databaseKeyError.mssqlNotConnected})
|
|
80
|
+
* @throws ({@link CoreError}) Thrown if the specified table is not found. ({@link databaseKeyError.mssqlTableNotFound})
|
|
81
|
+
*
|
|
82
|
+
* @template TModel - The type of the model for the repository.
|
|
83
|
+
* @template TRepo - The repository to retrieve extends {@link Repository}.
|
|
84
|
+
*
|
|
85
|
+
* @returns The {@link Repository} for the specified table.
|
|
86
|
+
*/
|
|
87
|
+
getRepository<TModel, TRepo extends Repository<TModel>>(tableName: string, customRepository: new (knex: Knex, table: Table) => TRepo): TRepo;
|
|
88
|
+
getRepository<TModel = unknown>(tableName: string): Repository<TModel>;
|
|
89
|
+
/**
|
|
90
|
+
* Retrieves a table by name.
|
|
91
|
+
*
|
|
92
|
+
* @param tableName - The name of the table to retrieve.
|
|
93
|
+
*
|
|
94
|
+
* @throws ({@link CoreError}) Thrown if the database is not connected. ({@link databaseKeyError.mssqlNotConnected})
|
|
95
|
+
* @throws ({@link CoreError}) Thrown if the specified table is not found. ({@link databaseKeyError.mssqlTableNotFound})
|
|
96
|
+
*
|
|
97
|
+
* @returns The {@link Table} object for the specified table.
|
|
98
|
+
*/
|
|
99
|
+
getTable(tableName: string): Table;
|
|
100
|
+
/**
|
|
101
|
+
* Retrieves the name of the database.
|
|
102
|
+
*
|
|
103
|
+
* @returns The name of the database.
|
|
104
|
+
*/
|
|
105
|
+
get databaseName(): string;
|
|
106
|
+
/**
|
|
107
|
+
* Retrieves the map of tables in the database.
|
|
108
|
+
*
|
|
109
|
+
* @returns A map of table names to {@link Table} instances.
|
|
110
|
+
*/
|
|
111
|
+
get tables(): Map<string, Table>;
|
|
112
|
+
/**
|
|
113
|
+
* Retrieves the map of repositories for each table.
|
|
114
|
+
*
|
|
115
|
+
* @returns A map of table names to {@link Repository} instances.
|
|
116
|
+
*/
|
|
117
|
+
get repositories(): Map<string, Repository>;
|
|
118
|
+
/**
|
|
119
|
+
* Indicates whether the database is connected.
|
|
120
|
+
*
|
|
121
|
+
* @returns `true` if the database is connected, `false` otherwise.
|
|
122
|
+
*/
|
|
123
|
+
get isConnected(): boolean;
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves the Knex instance for the database connection.
|
|
126
|
+
*
|
|
127
|
+
* @throws ({@link CoreError}) Thrown if the database is not connected. ({@link databaseKeyError.mssqlNotConnected})
|
|
128
|
+
*
|
|
129
|
+
* @returns The {@link Knex} instance for the database connection.
|
|
130
|
+
*/
|
|
131
|
+
get db(): Knex;
|
|
132
|
+
/**
|
|
133
|
+
* Introspects the database to retrieve information about tables, columns(fields), and primary keys.
|
|
134
|
+
* Creates instances of {@link Table} and {@link Repository} for each table in the database.
|
|
135
|
+
*/
|
|
136
|
+
private _introspectDatabase;
|
|
137
|
+
/**
|
|
138
|
+
* Extracts table names from a SQL query.
|
|
139
|
+
*
|
|
140
|
+
* @param sql - The SQL query string to extract table names from.
|
|
141
|
+
*
|
|
142
|
+
* @returns An array of table names found in the SQL query.
|
|
143
|
+
*/
|
|
144
|
+
private _extractTablesFromSqlQuery;
|
|
145
|
+
/**
|
|
146
|
+
* Handles the response from a query and emits the appropriate event based on the method.
|
|
147
|
+
*
|
|
148
|
+
* @param response - The response from the query.
|
|
149
|
+
* @param obj - The query object containing the method and SQL query string.
|
|
150
|
+
*/
|
|
151
|
+
private _handleQueryResponse;
|
|
152
|
+
/**
|
|
153
|
+
* Handles an error from a query and emits the `query:error` event.
|
|
154
|
+
*
|
|
155
|
+
* @param error - The error that occurred during the query.
|
|
156
|
+
* @param query - The query object containing the method and SQL query string.
|
|
157
|
+
*/
|
|
158
|
+
private _handleQueryError;
|
|
159
|
+
/**
|
|
160
|
+
* Adds event listeners to the Knex instance to handle query responses and errors.
|
|
161
|
+
*/
|
|
162
|
+
private _addEventKnex;
|
|
163
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a database table and provides access to its metadata (name, fields, primary key).
|
|
4
|
+
*
|
|
5
|
+
* - Emits events for table operations (e.g., SELECTED, CREATED, UPDATED, DELETED).
|
|
6
|
+
* - Used for query construction and event handling in the database layer.
|
|
7
|
+
*
|
|
8
|
+
* Example:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const table = new Table('db', 'users', ['id', 'name'], ['id', 'NUMBER']);
|
|
11
|
+
* table.on('SELECTED', data => console.log(data));
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare class Table extends EventEmitter {
|
|
15
|
+
/**
|
|
16
|
+
* The name of the database that this table belongs to.
|
|
17
|
+
*/
|
|
18
|
+
private readonly _databaseName;
|
|
19
|
+
/**
|
|
20
|
+
* The fields (columns) of the table.
|
|
21
|
+
*/
|
|
22
|
+
private readonly _fields;
|
|
23
|
+
/**
|
|
24
|
+
* The name of the table.
|
|
25
|
+
*/
|
|
26
|
+
private readonly _name;
|
|
27
|
+
/**
|
|
28
|
+
* The primary key of the table, represented as a tuple of field name and type.
|
|
29
|
+
* The type can be either 'NUMBER' or 'STRING'.
|
|
30
|
+
*/
|
|
31
|
+
private readonly _primaryKey;
|
|
32
|
+
/**
|
|
33
|
+
* Creates an instance of the Table class.
|
|
34
|
+
*
|
|
35
|
+
* @param databaseName - The name of the database that this table belongs to.
|
|
36
|
+
* @param tableName - The name of the table.
|
|
37
|
+
* @param fields - The fields (columns) of the table.
|
|
38
|
+
* @param primaryKey - The primary key of the table, represented as a tuple of field name and type.
|
|
39
|
+
*/
|
|
40
|
+
constructor(databaseName: string, tableName: string, fields: string[], primaryKey: [string, 'NUMBER' | 'STRING']);
|
|
41
|
+
/**
|
|
42
|
+
* Gets the name of the database that this table belongs to.
|
|
43
|
+
*/
|
|
44
|
+
get databaseName(): string;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the name of the table.
|
|
47
|
+
*/
|
|
48
|
+
get name(): string;
|
|
49
|
+
/**
|
|
50
|
+
* Gets the fields (columns) of the table.
|
|
51
|
+
*/
|
|
52
|
+
get fields(): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Gets the primary key of the table, represented as a tuple of field name and type.
|
|
55
|
+
* The type can be either 'NUMBER' or 'STRING'.
|
|
56
|
+
*/
|
|
57
|
+
get primaryKey(): [string, 'NUMBER' | 'STRING'];
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// @bun
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options to configure the MSSQL database connection.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* const options: MSSQLDatabaseOptions = {
|
|
7
|
+
* databaseName: 'my_database',
|
|
8
|
+
* host: 'localhost',
|
|
9
|
+
* port: 1433,
|
|
10
|
+
* user: 'sa',
|
|
11
|
+
* password: 'Password123',
|
|
12
|
+
* encrypt: true,
|
|
13
|
+
* poolMin: 5,
|
|
14
|
+
* poolMax: 20,
|
|
15
|
+
* debug: true,
|
|
16
|
+
* pulse: true
|
|
17
|
+
* };
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export interface MSSQLDatabaseOptions {
|
|
21
|
+
/** The name of the database to connect to. */
|
|
22
|
+
databaseName: string;
|
|
23
|
+
/** The host of the database server. */
|
|
24
|
+
host: string;
|
|
25
|
+
/** The port number for the database connection. */
|
|
26
|
+
port: number;
|
|
27
|
+
/** The username for authenticating with the database. */
|
|
28
|
+
user: string;
|
|
29
|
+
/** The password for authenticating with the database. */
|
|
30
|
+
password: string;
|
|
31
|
+
/**
|
|
32
|
+
* Whether to encrypt the database connection.
|
|
33
|
+
* When enabled, communication between the client and server will be encrypted,
|
|
34
|
+
* providing increased security but potentially slight performance impact.
|
|
35
|
+
*
|
|
36
|
+
* @defaultValue true
|
|
37
|
+
*/
|
|
38
|
+
encrypt?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Minimum number of connections in the connection pool.
|
|
41
|
+
* This represents the minimum number of connections that will be maintained in the pool,
|
|
42
|
+
* even when there is no activity. Higher values can reduce connection latency
|
|
43
|
+
* but increase resource usage.
|
|
44
|
+
*
|
|
45
|
+
* @defaultValue 2
|
|
46
|
+
*/
|
|
47
|
+
poolMin?: number;
|
|
48
|
+
/**
|
|
49
|
+
* Maximum number of connections in the connection pool.
|
|
50
|
+
* This limits how many concurrent connections can be established to the database.
|
|
51
|
+
* Setting this too low might cause connection timeouts during high load,
|
|
52
|
+
* while setting it too high might overload the database server.
|
|
53
|
+
*
|
|
54
|
+
* @defaultValue 10
|
|
55
|
+
*/
|
|
56
|
+
poolMax?: number;
|
|
57
|
+
/**
|
|
58
|
+
* The timeout in milliseconds for acquiring a connection.
|
|
59
|
+
* If a connection cannot be acquired within this timeframe, an error will be thrown.
|
|
60
|
+
*
|
|
61
|
+
* @defaultValue 20000
|
|
62
|
+
*/
|
|
63
|
+
connectionTimeout?: number;
|
|
64
|
+
/**
|
|
65
|
+
* Enables debug mode for logging database operations.
|
|
66
|
+
* When enabled, all SQL queries will be logged with their parameters,
|
|
67
|
+
* which is valuable for development but may impact performance.
|
|
68
|
+
*
|
|
69
|
+
* @defaultValue false
|
|
70
|
+
*/
|
|
71
|
+
debug?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* If set to true, adds basic event listeners for all tables.
|
|
74
|
+
* This automatically sets up event listeners to track database operations
|
|
75
|
+
* (select, create, update, delete) for all tables, allowing for easy monitoring
|
|
76
|
+
* and event-driven programming.
|
|
77
|
+
*
|
|
78
|
+
* [TODO] - Change this section when working on the AND-169 ticket.
|
|
79
|
+
*
|
|
80
|
+
* @defaultValue false
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const mssql = new MSSQL(options);
|
|
84
|
+
* await mssql.connect();
|
|
85
|
+
* // SELECTED, CREATED, UPDATED, DELETED
|
|
86
|
+
* mssql.table('table_name').on(EVENT_TABLE.SELECTED, (data: unknown) => {
|
|
87
|
+
* console.log(data); // data is the response from the query
|
|
88
|
+
* });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
pulse?: boolean;
|
|
92
|
+
}
|