@devbro/neko-sql 0.1.38 → 0.1.40
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/dist/cjs/index.d.ts +7 -2
- package/dist/cjs/index.js +50 -29
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/databases/index.d.mts +1 -0
- package/dist/esm/databases/mysql/MysqlConnection.d.mts +1 -0
- package/dist/esm/databases/mysql/MysqlConnection.mjs +12 -9
- package/dist/esm/databases/mysql/MysqlConnection.mjs.map +1 -1
- package/dist/esm/databases/postgresql/PostgresqlConnection.d.mts +5 -2
- package/dist/esm/databases/postgresql/PostgresqlConnection.mjs +16 -11
- package/dist/esm/databases/postgresql/PostgresqlConnection.mjs.map +1 -1
- package/dist/esm/databases/postgresql/index.d.mts +1 -0
- package/dist/esm/databases/sqlite/SqliteConnection.d.mts +1 -0
- package/dist/esm/databases/sqlite/SqliteConnection.mjs +7 -3
- package/dist/esm/databases/sqlite/SqliteConnection.mjs.map +1 -1
- package/dist/esm/helper.d.mts +3 -0
- package/dist/esm/helper.mjs +19 -0
- package/dist/esm/helper.mjs.map +1 -0
- package/dist/esm/index.d.mts +1 -0
- package/package.json +3 -3
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
import { Connection as ConnectionAbs } from "../../Connection.mjs";
|
|
4
|
-
import mysql from "mysql2/promise";
|
|
5
4
|
import { Query } from "../../Query.mjs";
|
|
6
5
|
import { MysqlQueryGrammar } from "./MysqlQueryGrammar.mjs";
|
|
7
6
|
import { Schema } from "../../Schema.mjs";
|
|
8
7
|
import { MysqlSchemaGrammar } from "./MysqlSchemaGrammar.mjs";
|
|
9
8
|
import { EventManager } from "@devbro/neko-helper";
|
|
9
|
+
import { loadPackage } from "../../helper.mjs";
|
|
10
10
|
class MysqlConnection extends ConnectionAbs {
|
|
11
11
|
static {
|
|
12
12
|
__name(this, "MysqlConnection");
|
|
@@ -26,6 +26,7 @@ class MysqlConnection extends ConnectionAbs {
|
|
|
26
26
|
connection;
|
|
27
27
|
static pool;
|
|
28
28
|
static poolConfig;
|
|
29
|
+
static mysql;
|
|
29
30
|
static defaults = {
|
|
30
31
|
port: 3306,
|
|
31
32
|
connectionLimit: 20,
|
|
@@ -36,9 +37,12 @@ class MysqlConnection extends ConnectionAbs {
|
|
|
36
37
|
};
|
|
37
38
|
constructor(params) {
|
|
38
39
|
super();
|
|
40
|
+
if (!MysqlConnection.mysql) {
|
|
41
|
+
MysqlConnection.mysql = loadPackage("mysql2/promise");
|
|
42
|
+
}
|
|
39
43
|
if (!MysqlConnection.pool) {
|
|
40
44
|
MysqlConnection.poolConfig = { ...MysqlConnection.defaults, ...params };
|
|
41
|
-
MysqlConnection.pool = mysql.createPool(MysqlConnection.poolConfig);
|
|
45
|
+
MysqlConnection.pool = MysqlConnection.mysql.createPool(MysqlConnection.poolConfig);
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
48
|
async connect() {
|
|
@@ -137,19 +141,18 @@ class MysqlConnection extends ConnectionAbs {
|
|
|
137
141
|
}
|
|
138
142
|
async createDatabase(name) {
|
|
139
143
|
if (!this.isConnected()) {
|
|
140
|
-
const tempConn2 = await mysql.createConnection({
|
|
144
|
+
const tempConn2 = await MysqlConnection.mysql.createConnection({
|
|
141
145
|
host: MysqlConnection.poolConfig.host,
|
|
142
146
|
user: MysqlConnection.poolConfig.user,
|
|
143
147
|
password: MysqlConnection.poolConfig.password,
|
|
144
148
|
port: MysqlConnection.poolConfig.port
|
|
145
149
|
});
|
|
146
150
|
const safeName2 = this.validateAndEscapeIdentifier(name);
|
|
147
|
-
|
|
148
|
-
let [rows] = await tempConn2.query(`CREATE DATABASE ${safeName2}`);
|
|
151
|
+
await tempConn2.query(`CREATE DATABASE ${safeName2}`);
|
|
149
152
|
await tempConn2.end();
|
|
150
153
|
return;
|
|
151
154
|
}
|
|
152
|
-
const tempConn = await mysql.createConnection({
|
|
155
|
+
const tempConn = await MysqlConnection.mysql.createConnection({
|
|
153
156
|
host: MysqlConnection.poolConfig.host,
|
|
154
157
|
user: MysqlConnection.poolConfig.user,
|
|
155
158
|
password: MysqlConnection.poolConfig.password,
|
|
@@ -163,7 +166,7 @@ class MysqlConnection extends ConnectionAbs {
|
|
|
163
166
|
if (this.isConnected()) {
|
|
164
167
|
throw new Error("Cannot drop database while connected.");
|
|
165
168
|
}
|
|
166
|
-
const tempConn = await mysql.createConnection({
|
|
169
|
+
const tempConn = await MysqlConnection.mysql.createConnection({
|
|
167
170
|
host: MysqlConnection.poolConfig.host,
|
|
168
171
|
user: MysqlConnection.poolConfig.user,
|
|
169
172
|
password: MysqlConnection.poolConfig.password,
|
|
@@ -182,13 +185,13 @@ class MysqlConnection extends ConnectionAbs {
|
|
|
182
185
|
}
|
|
183
186
|
async existsDatabase(name) {
|
|
184
187
|
if (!this.isConnected()) {
|
|
185
|
-
const tempConn = await mysql.createConnection({
|
|
188
|
+
const tempConn = await MysqlConnection.mysql.createConnection({
|
|
186
189
|
host: MysqlConnection.poolConfig.host,
|
|
187
190
|
user: MysqlConnection.poolConfig.user,
|
|
188
191
|
password: MysqlConnection.poolConfig.password,
|
|
189
192
|
port: MysqlConnection.poolConfig.port
|
|
190
193
|
});
|
|
191
|
-
|
|
194
|
+
const [rows2] = await tempConn.query(
|
|
192
195
|
"SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?",
|
|
193
196
|
[name]
|
|
194
197
|
);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/databases/mysql/MysqlConnection.mts"],"sourcesContent":["import { connection_events, Connection as ConnectionAbs } from '../../Connection.mjs';\nimport mysql from 'mysql2/promise';\nimport { CompiledSql } from '../../types.mjs';\nimport { Query } from '../../Query.mjs';\nimport { MysqlQueryGrammar } from './MysqlQueryGrammar.mjs';\nimport { Schema } from '../../Schema.mjs';\nimport { MysqlSchemaGrammar } from './MysqlSchemaGrammar.mjs';\nimport { EventManager } from '@devbro/neko-helper';\n\nexport class MysqlConnection extends ConnectionAbs {\n private eventManager = new EventManager();\n\n on(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.on(event, listener);\n return this;\n }\n off(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.off(event, listener);\n return this;\n }\n emit(event: connection_events, ...args: any[]): Promise<boolean> {\n return this.eventManager.emit(event, ...args);\n }\n\n connection: mysql.PoolConnection | undefined;\n static pool: mysql.Pool;\n static poolConfig: mysql.PoolOptions;\n\n static defaults: mysql.PoolOptions = {\n port: 3306,\n connectionLimit: 20,\n waitForConnections: true,\n queueLimit: 0,\n enableKeepAlive: true,\n keepAliveInitialDelay: 0,\n };\n\n constructor(params: mysql.PoolOptions) {\n super();\n if (!MysqlConnection.pool) {\n MysqlConnection.poolConfig = { ...MysqlConnection.defaults, ...params };\n MysqlConnection.pool = mysql.createPool(MysqlConnection.poolConfig);\n }\n }\n async connect(): Promise<boolean> {\n this.eventManager.emit('connect').catch(() => {});\n this.connection = await MysqlConnection.pool.getConnection();\n return true;\n }\n async runQuery(sql: CompiledSql | string): Promise<any> {\n if (typeof sql === 'string') {\n sql = { sql: sql, bindings: [], parts: [sql] };\n }\n\n this.eventManager.emit('query', { sql: sql.sql, bindings: sql.bindings }).catch(() => {});\n\n if (!this.isConnected()) {\n await this.connect();\n }\n\n const result = await this.connection!.query(sql.sql, sql.bindings);\n return result[0];\n }\n\n async runCursor(sql: CompiledSql): Promise<any> {\n // MySQL doesn't have native cursor support like PostgreSQL\n // Return the full result set for now\n return this.runQuery(sql);\n }\n\n async disconnect(): Promise<boolean> {\n if (this.connection === undefined) {\n return true;\n }\n this.connection.release();\n this.connection = undefined;\n this.eventManager.emit('disconnect').catch(() => {});\n return true;\n }\n\n getQuery(): Query {\n return new Query(this, new MysqlQueryGrammar());\n }\n\n getSchema(): Schema {\n return new Schema(this, new MysqlSchemaGrammar());\n }\n\n getQueryGrammar(): MysqlQueryGrammar {\n return new MysqlQueryGrammar();\n }\n getSchemaGrammar(): MysqlSchemaGrammar {\n return new MysqlSchemaGrammar();\n }\n\n async beginTransaction(): Promise<void> {\n await this.runQuery({\n sql: 'BEGIN',\n bindings: [],\n parts: ['BEGIN'],\n });\n }\n\n async commit(): Promise<void> {\n await this.runQuery({ sql: 'COMMIT', bindings: [], parts: ['COMMIT'] });\n }\n\n async rollback(): Promise<void> {\n await this.runQuery({ sql: 'ROLLBACK', bindings: [], parts: ['ROLLBACK'] });\n }\n\n static destroy(): Promise<void> {\n return MysqlConnection.pool.end();\n }\n\n isConnected(): boolean {\n return this.connection !== undefined;\n }\n\n /**\n * Validates and escapes a MySQL identifier (database name, table name, etc.)\n * Uses a whitelist approach to ensure only safe characters are allowed\n */\n private validateAndEscapeIdentifier(name: string): string {\n // MySQL identifiers can contain: letters, digits, underscores, and dollar signs\n // They must start with a letter, underscore, or digit (in MySQL 5.7.8+)\n const validIdentifierPattern = /^[a-zA-Z0-9_$]+$/;\n\n if (!validIdentifierPattern.test(name)) {\n throw new Error(\n `Invalid identifier: \"${name}\". Identifiers must contain only letters, digits, underscores, and dollar signs.`\n );\n }\n\n // MySQL reserved keywords that should be quoted\n const reservedKeywords = new Set([\n 'database',\n 'table',\n 'user',\n 'order',\n 'group',\n 'select',\n 'insert',\n 'update',\n 'delete',\n 'from',\n 'where',\n 'index',\n 'key',\n ]);\n\n // Quote the identifier with backticks if it's a reserved keyword\n if (reservedKeywords.has(name.toLowerCase())) {\n // Escape any backticks in the name\n const escapedName = name.replace(/`/g, '``');\n return `\\`${escapedName}\\``;\n }\n\n return name;\n }\n\n async createDatabase(name: string): Promise<void> {\n if (!this.isConnected()) {\n const tempConn = await mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n\n const safeName = this.validateAndEscapeIdentifier(name);\n console.log(safeName);\n let [rows] = await tempConn.query(`CREATE DATABASE ${safeName}`);\n\n await tempConn.end();\n return;\n }\n\n const tempConn = await mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n const safeName = this.validateAndEscapeIdentifier(name);\n await tempConn.query(`CREATE DATABASE ${safeName}`);\n await tempConn.end();\n }\n\n async dropDatabase(name: string): Promise<void> {\n if (this.isConnected()) {\n throw new Error('Cannot drop database while connected.');\n }\n\n const tempConn = await mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n const safeName = this.validateAndEscapeIdentifier(name);\n await tempConn.query(`DROP DATABASE ${safeName}`);\n await tempConn.end();\n }\n\n async listDatabases(): Promise<string[]> {\n if (!this.isConnected()) {\n await this.connect();\n }\n const [rows] = await this.connection!.query<mysql.RowDataPacket[]>('SHOW DATABASES');\n return rows.map((row: any) => row.Database);\n }\n\n async existsDatabase(name: string): Promise<boolean> {\n if (!this.isConnected()) {\n const tempConn = await mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n\n let [rows] = await tempConn.query<mysql.RowDataPacket[]>(\n 'SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?',\n [name]\n );\n\n await tempConn.end();\n\n return rows.length > 0;\n }\n const [rows] = await this.connection!.query<mysql.RowDataPacket[]>(\n 'SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?',\n [name]\n );\n return rows.length > 0;\n }\n}\n"],"mappings":";;AAAA,SAA4B,cAAc,qBAAqB;AAC/D,OAAO,WAAW;AAElB,SAAS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;AAAA,MACA,CAAC,IAAI;AAAA,IACP;AACA,WAAO,KAAK,SAAS;AAAA,EACvB;AACF;","names":["tempConn","safeName","rows"]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/databases/mysql/MysqlConnection.mts"],"sourcesContent":["import { connection_events, Connection as ConnectionAbs } from '../../Connection.mjs';\nimport type mysql from 'mysql2/promise';\nimport { CompiledSql } from '../../types.mjs';\nimport { Query } from '../../Query.mjs';\nimport { MysqlQueryGrammar } from './MysqlQueryGrammar.mjs';\nimport { Schema } from '../../Schema.mjs';\nimport { MysqlSchemaGrammar } from './MysqlSchemaGrammar.mjs';\nimport { EventManager } from '@devbro/neko-helper';\nimport { loadPackage } from '../../helper.mjs';\n\nexport class MysqlConnection extends ConnectionAbs {\n private eventManager = new EventManager();\n\n on(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.on(event, listener);\n return this;\n }\n off(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.off(event, listener);\n return this;\n }\n emit(event: connection_events, ...args: any[]): Promise<boolean> {\n return this.eventManager.emit(event, ...args);\n }\n\n connection: mysql.PoolConnection | undefined;\n static pool: mysql.Pool;\n static poolConfig: mysql.PoolOptions;\n static mysql: typeof mysql;\n\n static defaults: mysql.PoolOptions = {\n port: 3306,\n connectionLimit: 20,\n waitForConnections: true,\n queueLimit: 0,\n enableKeepAlive: true,\n keepAliveInitialDelay: 0,\n };\n\n constructor(params: mysql.PoolOptions) {\n super();\n if (!MysqlConnection.mysql) {\n MysqlConnection.mysql = loadPackage('mysql2/promise') as typeof mysql;\n }\n if (!MysqlConnection.pool) {\n MysqlConnection.poolConfig = { ...MysqlConnection.defaults, ...params };\n MysqlConnection.pool = MysqlConnection.mysql.createPool(MysqlConnection.poolConfig);\n }\n }\n async connect(): Promise<boolean> {\n this.eventManager.emit('connect').catch(() => {});\n this.connection = await MysqlConnection.pool.getConnection();\n return true;\n }\n async runQuery(sql: CompiledSql | string): Promise<any> {\n if (typeof sql === 'string') {\n sql = { sql: sql, bindings: [], parts: [sql] };\n }\n\n this.eventManager.emit('query', { sql: sql.sql, bindings: sql.bindings }).catch(() => {});\n\n if (!this.isConnected()) {\n await this.connect();\n }\n\n const result = await this.connection!.query(sql.sql, sql.bindings);\n return result[0];\n }\n\n async runCursor(sql: CompiledSql): Promise<any> {\n // MySQL doesn't have native cursor support like PostgreSQL\n // Return the full result set for now\n return this.runQuery(sql);\n }\n\n async disconnect(): Promise<boolean> {\n if (this.connection === undefined) {\n return true;\n }\n this.connection.release();\n this.connection = undefined;\n this.eventManager.emit('disconnect').catch(() => {});\n return true;\n }\n\n getQuery(): Query {\n return new Query(this, new MysqlQueryGrammar());\n }\n\n getSchema(): Schema {\n return new Schema(this, new MysqlSchemaGrammar());\n }\n\n getQueryGrammar(): MysqlQueryGrammar {\n return new MysqlQueryGrammar();\n }\n getSchemaGrammar(): MysqlSchemaGrammar {\n return new MysqlSchemaGrammar();\n }\n\n async beginTransaction(): Promise<void> {\n await this.runQuery({\n sql: 'BEGIN',\n bindings: [],\n parts: ['BEGIN'],\n });\n }\n\n async commit(): Promise<void> {\n await this.runQuery({ sql: 'COMMIT', bindings: [], parts: ['COMMIT'] });\n }\n\n async rollback(): Promise<void> {\n await this.runQuery({ sql: 'ROLLBACK', bindings: [], parts: ['ROLLBACK'] });\n }\n\n static destroy(): Promise<void> {\n return MysqlConnection.pool.end();\n }\n\n isConnected(): boolean {\n return this.connection !== undefined;\n }\n\n /**\n * Validates and escapes a MySQL identifier (database name, table name, etc.)\n * Uses a whitelist approach to ensure only safe characters are allowed\n */\n private validateAndEscapeIdentifier(name: string): string {\n // MySQL identifiers can contain: letters, digits, underscores, and dollar signs\n // They must start with a letter, underscore, or digit (in MySQL 5.7.8+)\n const validIdentifierPattern = /^[a-zA-Z0-9_$]+$/;\n\n if (!validIdentifierPattern.test(name)) {\n throw new Error(\n `Invalid identifier: \"${name}\". Identifiers must contain only letters, digits, underscores, and dollar signs.`\n );\n }\n\n // MySQL reserved keywords that should be quoted\n const reservedKeywords = new Set([\n 'database',\n 'table',\n 'user',\n 'order',\n 'group',\n 'select',\n 'insert',\n 'update',\n 'delete',\n 'from',\n 'where',\n 'index',\n 'key',\n ]);\n\n // Quote the identifier with backticks if it's a reserved keyword\n if (reservedKeywords.has(name.toLowerCase())) {\n // Escape any backticks in the name\n const escapedName = name.replace(/`/g, '``');\n return `\\`${escapedName}\\``;\n }\n\n return name;\n }\n\n async createDatabase(name: string): Promise<void> {\n if (!this.isConnected()) {\n const tempConn = await MysqlConnection.mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n\n const safeName = this.validateAndEscapeIdentifier(name);\n await tempConn.query(`CREATE DATABASE ${safeName}`);\n\n await tempConn.end();\n return;\n }\n\n const tempConn = await MysqlConnection.mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n const safeName = this.validateAndEscapeIdentifier(name);\n await tempConn.query(`CREATE DATABASE ${safeName}`);\n await tempConn.end();\n }\n\n async dropDatabase(name: string): Promise<void> {\n if (this.isConnected()) {\n throw new Error('Cannot drop database while connected.');\n }\n\n const tempConn = await MysqlConnection.mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n const safeName = this.validateAndEscapeIdentifier(name);\n await tempConn.query(`DROP DATABASE ${safeName}`);\n await tempConn.end();\n }\n\n async listDatabases(): Promise<string[]> {\n if (!this.isConnected()) {\n await this.connect();\n }\n const [rows] = await this.connection!.query<mysql.RowDataPacket[]>('SHOW DATABASES');\n return rows.map((row: any) => row.Database);\n }\n\n async existsDatabase(name: string): Promise<boolean> {\n if (!this.isConnected()) {\n const tempConn = await MysqlConnection.mysql.createConnection({\n host: MysqlConnection.poolConfig.host,\n user: MysqlConnection.poolConfig.user,\n password: MysqlConnection.poolConfig.password,\n port: MysqlConnection.poolConfig.port,\n });\n\n const [rows] = await tempConn.query<mysql.RowDataPacket[]>(\n 'SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?',\n [name]\n );\n\n await tempConn.end();\n\n return rows.length > 0;\n }\n const [rows] = await this.connection!.query<mysql.RowDataPacket[]>(\n 'SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?',\n [name]\n );\n return rows.length > 0;\n }\n}\n"],"mappings":";;AAAA,SAA4B,cAAc,qBAAqB;AAG/D,SAAS,aAAa;AACtB,SAAS,yBAAyB;AAClC,SAAS,cAAc;AACvB,SAAS,0BAA0B;AACnC,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAErB,MAAM,wBAAwB,cAAc;AAAA,EAVnD,OAUmD;AAAA;AAAA;AAAA,EACzC,eAAe,IAAI,aAAa;AAAA,EAExC,GAAG,OAA0B,UAA0C;AACrE,SAAK,aAAa,GAAG,OAAO,QAAQ;AACpC,WAAO;AAAA,EACT;AAAA,EACA,IAAI,OAA0B,UAA0C;AACtE,SAAK,aAAa,IAAI,OAAO,QAAQ;AACrC,WAAO;AAAA,EACT;AAAA,EACA,KAAK,UAA6B,MAA+B;AAC/D,WAAO,KAAK,aAAa,KAAK,OAAO,GAAG,IAAI;AAAA,EAC9C;AAAA,EAEA;AAAA,EACA,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO;AAAA,EAEP,OAAO,WAA8B;AAAA,IACnC,MAAM;AAAA,IACN,iBAAiB;AAAA,IACjB,oBAAoB;AAAA,IACpB,YAAY;AAAA,IACZ,iBAAiB;AAAA,IACjB,uBAAuB;AAAA,EACzB;AAAA,EAEA,YAAY,QAA2B;AACrC,UAAM;AACN,QAAI,CAAC,gBAAgB,OAAO;AAC1B,sBAAgB,QAAQ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AK,WAAY,MAA6B,gBAAgB;AACnF,WAAO,KAAK,IAAI,CAAC,QAAa,IAAI,QAAQ;AAAA,EAC5C;AAAA,EAEA,MAAM,eAAe,MAAgC;AACnD,QAAI,CAAC,KAAK,YAAY,GAAG;AACvB,YAAM,WAAW,MAAM,gBAAgB,MAAM,iBAAiB;AAAA,QAC5D,MAAM,gBAAgB,WAAW;AAAA,QACjC,MAAM,gBAAgB,WAAW;AAAA,QACjC,UAAU,gBAAgB,WAAW;AAAA,QACrC,MAAM,gBAAgB,WAAW;AAAA,MACnC,CAAC;AAED,YAAM,CAACE,KAAI,IAAI,MAAM,SAAS;AAAA,QAC5B;AAAA,QACA,CAAC,IAAI;AAAA,MACP;AAEA,YAAM,SAAS,IAAI;AAEnB,aAAOA,MAAK,SAAS;AAAA,IACvB;AACA,UAAM,CAAC,IAAI,IAAI,MAAM,KAAK,WAAY;AAAA,MACpC;AAAA,MACA,CAAC,IAAI;AAAA,IACP;AACA,WAAO,KAAK,SAAS;AAAA,EACvB;AACF;","names":["tempConn","safeName","rows"]}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { b as Connection, c as connection_events, m as CompiledSql, d as Query, S as Schema } from '../../Blueprint-DjP_Sfrr.mjs';
|
|
2
|
-
import { PoolClient,
|
|
2
|
+
import pg, { PoolClient, PoolConfig } from 'pg';
|
|
3
3
|
import { PostgresqlQueryGrammar } from './PostgresqlQueryGrammar.mjs';
|
|
4
4
|
import { PostgresqlSchemaGrammar } from './PostgresqlSchemaGrammar.mjs';
|
|
5
|
+
import pg_cursor from 'pg-cursor';
|
|
5
6
|
import '@devbro/neko-helper';
|
|
6
7
|
|
|
7
8
|
declare class PostgresqlConnection extends Connection {
|
|
@@ -9,8 +10,10 @@ declare class PostgresqlConnection extends Connection {
|
|
|
9
10
|
on(event: connection_events, listener: (...args: any[]) => void): this;
|
|
10
11
|
off(event: connection_events, listener: (...args: any[]) => void): this;
|
|
11
12
|
emit(event: connection_events, ...args: any[]): Promise<boolean>;
|
|
13
|
+
static pg: typeof pg;
|
|
14
|
+
static pg_cursor: typeof pg_cursor;
|
|
12
15
|
connection: PoolClient | undefined;
|
|
13
|
-
static pool: Pool;
|
|
16
|
+
static pool: pg.Pool;
|
|
14
17
|
static defaults: PoolConfig;
|
|
15
18
|
constructor(params: PoolConfig);
|
|
16
19
|
connect(): Promise<boolean>;
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
import { Connection as ConnectionAbs } from "../../Connection.mjs";
|
|
4
|
-
import { Client } from "pg";
|
|
5
|
-
import { Pool } from "pg";
|
|
6
4
|
import { Query } from "../../Query.mjs";
|
|
7
5
|
import { PostgresqlQueryGrammar } from "./PostgresqlQueryGrammar.mjs";
|
|
8
6
|
import { Schema } from "../../Schema.mjs";
|
|
9
7
|
import { PostgresqlSchemaGrammar } from "./PostgresqlSchemaGrammar.mjs";
|
|
10
|
-
import Cursor from "pg-cursor";
|
|
11
8
|
import { EventManager } from "@devbro/neko-helper";
|
|
9
|
+
import { loadPackage } from "../../helper.mjs";
|
|
12
10
|
class PostgresqlConnection extends ConnectionAbs {
|
|
13
11
|
static {
|
|
14
12
|
__name(this, "PostgresqlConnection");
|
|
@@ -25,6 +23,8 @@ class PostgresqlConnection extends ConnectionAbs {
|
|
|
25
23
|
emit(event, ...args) {
|
|
26
24
|
return this.eventManager.emit(event, ...args);
|
|
27
25
|
}
|
|
26
|
+
static pg;
|
|
27
|
+
static pg_cursor;
|
|
28
28
|
connection;
|
|
29
29
|
static pool;
|
|
30
30
|
static defaults = {
|
|
@@ -39,8 +39,15 @@ class PostgresqlConnection extends ConnectionAbs {
|
|
|
39
39
|
};
|
|
40
40
|
constructor(params) {
|
|
41
41
|
super();
|
|
42
|
+
if (!PostgresqlConnection.pg) {
|
|
43
|
+
PostgresqlConnection.pg = loadPackage("pg");
|
|
44
|
+
PostgresqlConnection.pg_cursor = loadPackage("pg-cursor");
|
|
45
|
+
}
|
|
42
46
|
if (!PostgresqlConnection.pool) {
|
|
43
|
-
PostgresqlConnection.pool = new Pool({
|
|
47
|
+
PostgresqlConnection.pool = new PostgresqlConnection.pg.Pool({
|
|
48
|
+
...PostgresqlConnection.defaults,
|
|
49
|
+
...params
|
|
50
|
+
});
|
|
44
51
|
}
|
|
45
52
|
}
|
|
46
53
|
async connect() {
|
|
@@ -67,7 +74,7 @@ class PostgresqlConnection extends ConnectionAbs {
|
|
|
67
74
|
return result?.rows;
|
|
68
75
|
}
|
|
69
76
|
async runCursor(sql) {
|
|
70
|
-
return this.connection?.query(new
|
|
77
|
+
return this.connection?.query(new PostgresqlConnection.pg_cursor(sql.sql, sql.bindings));
|
|
71
78
|
}
|
|
72
79
|
async disconnect() {
|
|
73
80
|
if (this.connection === void 0) {
|
|
@@ -140,7 +147,7 @@ class PostgresqlConnection extends ConnectionAbs {
|
|
|
140
147
|
await this.runQuery(`CREATE DATABASE ${safeName2}`);
|
|
141
148
|
return;
|
|
142
149
|
}
|
|
143
|
-
const conn = new Client({
|
|
150
|
+
const conn = new PostgresqlConnection.pg.Client({
|
|
144
151
|
...PostgresqlConnection.pool.options,
|
|
145
152
|
database: "postgres"
|
|
146
153
|
});
|
|
@@ -155,7 +162,7 @@ class PostgresqlConnection extends ConnectionAbs {
|
|
|
155
162
|
await this.runQuery(`DROP DATABASE ${safeName2}`);
|
|
156
163
|
return;
|
|
157
164
|
}
|
|
158
|
-
const conn = new Client({
|
|
165
|
+
const conn = new PostgresqlConnection.pg.Client({
|
|
159
166
|
...PostgresqlConnection.pool.options,
|
|
160
167
|
database: "postgres"
|
|
161
168
|
// connect to default 'postgres' database to drop others
|
|
@@ -176,15 +183,13 @@ class PostgresqlConnection extends ConnectionAbs {
|
|
|
176
183
|
}
|
|
177
184
|
async existsDatabase(name) {
|
|
178
185
|
if (!this.isConnected()) {
|
|
179
|
-
const conn = new Client({
|
|
186
|
+
const conn = new PostgresqlConnection.pg.Client({
|
|
180
187
|
...PostgresqlConnection.pool.options,
|
|
181
188
|
database: "postgres"
|
|
182
189
|
});
|
|
183
190
|
await conn.connect();
|
|
184
191
|
const safeName = this.validateAndEscapeIdentifier(name);
|
|
185
|
-
const result2 = await conn.query("SELECT 1 FROM pg_database WHERE datname = $1", [
|
|
186
|
-
safeName
|
|
187
|
-
]);
|
|
192
|
+
const result2 = await conn.query("SELECT 1 FROM pg_database WHERE datname = $1", [safeName]);
|
|
188
193
|
await conn.end();
|
|
189
194
|
return result2.rows.length > 0;
|
|
190
195
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/databases/postgresql/PostgresqlConnection.mts"],"sourcesContent":["import { connection_events, Connection as ConnectionAbs } from '../../Connection.mjs';\nimport { Client, PoolClient, PoolConfig } from 'pg';\nimport { Pool } from 'pg';\nimport { CompiledSql } from '../../types.mjs';\nimport { Query } from '../../Query.mjs';\nimport { PostgresqlQueryGrammar } from './PostgresqlQueryGrammar.mjs';\nimport { Schema } from '../../Schema.mjs';\nimport { PostgresqlSchemaGrammar } from './PostgresqlSchemaGrammar.mjs';\nimport Cursor from 'pg-cursor';\nimport { EventManager } from '@devbro/neko-helper';\n\nexport class PostgresqlConnection extends ConnectionAbs {\n private eventManager = new EventManager();\n\n on(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.on(event, listener);\n return this;\n }\n off(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.off(event, listener);\n return this;\n }\n emit(event: connection_events, ...args: any[]): Promise<boolean> {\n return this.eventManager.emit(event, ...args);\n }\n\n connection: PoolClient | undefined;\n static pool: Pool;\n\n static defaults: PoolConfig = {\n port: 5432,\n ssl: false,\n max: 20,\n idleTimeoutMillis: 1, // wait X milli seconds before closing an idle/released connection\n connectionTimeoutMillis: 30000, // wait up to 30 seconds to obtain a new connection\n maxUses: 7500,\n };\n\n constructor(params: PoolConfig) {\n super();\n if (!PostgresqlConnection.pool) {\n PostgresqlConnection.pool = new Pool({ ...PostgresqlConnection.defaults, ...params });\n }\n }\n async connect(): Promise<boolean> {\n this.eventManager.emit('connect').catch(() => {});\n this.connection = await PostgresqlConnection.pool.connect();\n return true;\n }\n async runQuery(sql: CompiledSql | string): Promise<any> {\n if (typeof sql === 'string') {\n sql = { sql: sql, bindings: [], parts: [sql] };\n }\n let counter = 1;\n let sql2 = sql.sql;\n if (sql.parts && sql.parts.length > 0) {\n sql2 = sql.parts.map((v) => (v === '?' ? '$' + counter++ : v)).join(' ');\n }\n\n this.eventManager.emit('query', { sql: sql2, bindings: sql.bindings }).catch(() => {});\n\n if (!this.isConnected()) {\n await this.connect();\n }\n const result = await this.connection!.query(sql2, sql.bindings);\n return result?.rows;\n }\n\n async runCursor(sql: CompiledSql): Promise<any> {\n return this.connection?.query(new Cursor(sql.sql, sql.bindings));\n }\n\n async disconnect(): Promise<boolean> {\n if (this.connection === undefined) {\n return true;\n }\n await this.connection?.release();\n this.connection = undefined;\n this.eventManager.emit('disconnect').catch(() => {});\n return true;\n }\n\n getQuery(): Query {\n return new Query(this, new PostgresqlQueryGrammar());\n }\n\n getSchema(): Schema {\n return new Schema(this, new PostgresqlSchemaGrammar());\n }\n\n getQueryGrammar(): PostgresqlQueryGrammar {\n return new PostgresqlQueryGrammar();\n }\n getSchemaGrammar(): PostgresqlSchemaGrammar {\n return new PostgresqlSchemaGrammar();\n }\n\n async beginTransaction(): Promise<void> {\n await this.runQuery({ sql: 'BEGIN', bindings: [], parts: ['BEGIN'] });\n }\n\n async commit(): Promise<void> {\n await this.runQuery({ sql: 'COMMIT', bindings: [], parts: ['COMMIT'] });\n }\n\n async rollback(): Promise<void> {\n await this.runQuery({ sql: 'ROLLBACK', bindings: [], parts: ['ROLLBACK'] });\n }\n\n static destroy(): Promise<void> {\n return PostgresqlConnection.pool.end();\n }\n\n isConnected(): boolean {\n return this.connection !== undefined;\n }\n\n /**\n * Validates and escapes a PostgreSQL identifier (database name, table name, etc.)\n * Uses a whitelist approach to ensure only safe characters are allowed\n */\n private validateAndEscapeIdentifier(name: string): string {\n // PostgreSQL identifiers can contain: letters, digits, underscores, and dollar signs\n // They must start with a letter or underscore\n const validIdentifierPattern = /^[a-zA-Z_][a-zA-Z0-9_$]*$/;\n\n if (!validIdentifierPattern.test(name)) {\n throw new Error(\n `Invalid identifier: \"${name}\". Identifiers must start with a letter or underscore and contain only letters, digits, underscores, and dollar signs.`\n );\n }\n\n // PostgreSQL reserved keywords that should be quoted\n const reservedKeywords = new Set([\n 'user',\n 'table',\n 'database',\n 'order',\n 'group',\n 'select',\n 'insert',\n 'update',\n 'delete',\n ]);\n\n // Quote the identifier if it's a reserved keyword or contains uppercase letters\n if (reservedKeywords.has(name.toLowerCase()) || name !== name.toLowerCase()) {\n // Escape any double quotes in the name\n const escapedName = name.replace(/\"/g, '\"\"');\n return `\"${escapedName}\"`;\n }\n\n return name;\n }\n\n async createDatabase(name: string): Promise<void> {\n if (this.isConnected()) {\n const safeName = this.validateAndEscapeIdentifier(name);\n await this.runQuery(`CREATE DATABASE ${safeName}`);\n return;\n }\n\n const conn = new Client({\n ...PostgresqlConnection.pool.options,\n database: 'postgres',\n });\n await conn.connect();\n const safeName = this.validateAndEscapeIdentifier(name);\n await conn.query(`CREATE DATABASE ${safeName}`);\n await conn.end();\n }\n\n async dropDatabase(name: string): Promise<void> {\n if (this.isConnected()) {\n const safeName = this.validateAndEscapeIdentifier(name);\n await this.runQuery(`DROP DATABASE ${safeName}`);\n return;\n }\n\n const conn = new Client({\n ...PostgresqlConnection.pool.options,\n database: 'postgres', // connect to default 'postgres' database to drop others\n });\n await conn.connect();\n const safeName = this.validateAndEscapeIdentifier(name);\n await conn.query(`DROP DATABASE ${safeName}`);\n await conn.end();\n }\n\n async listDatabases(): Promise<string[]> {\n if (!this.isConnected()) {\n await this.connect();\n }\n const result = await this.connection!.query(\n 'SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname'\n );\n return result.rows.map((row: any) => row.datname);\n }\n\n async existsDatabase(name: string): Promise<boolean> {\n if (!this.isConnected()) {\n const conn = new Client({\n ...PostgresqlConnection.pool.options,\n database: 'postgres',\n });\n await conn.connect();\n const safeName = this.validateAndEscapeIdentifier(name);\n const result = await conn.query('SELECT 1 FROM pg_database WHERE datname = $1', [\n safeName,\n ]);\n await conn.end();\n return result.rows.length > 0;\n }\n\n const result = await this.connection!.query('SELECT 1 FROM pg_database WHERE datname = $1', [\n name,\n ]);\n return result.rows.length > 0;\n }\n}\n"],"mappings":";;AAAA,SAA4B,cAAc,qBAAqB;AAC/D,SAAS,cAAsC;AAC/C,SAAS,YAAY;AAErB,SAAS,aAAa;AACtB,SAAS,8BAA8B;AACvC,SAAS,cAAc;AACvB,SAAS,+BAA+B;AACxC,OAAO,YAAY;AACnB,SAAS,oBAAoB;AAEtB,MAAM,6BAA6B,cAAc;AAAA,EAXxD,OAWwD;AAAA;AAAA;AAAA,EAC9C,eAAe,IAAI,aAAa;AAAA,EAExC,GAAG,OAA0B,UAA0C;AACrE,SAAK,aAAa,GAAG,OAAO,QAAQ;AACpC,WAAO;AAAA,EACT;AAAA,EACA,IAAI,OAA0B,UAA0C;AACtE,SAAK,aAAa,IAAI,OAAO,QAAQ;AACrC,WAAO;AAAA,EACT;AAAA,EACA,KAAK,UAA6B,MAA+B;AAC/D,WAAO,KAAK,aAAa,KAAK,OAAO,GAAG,IAAI;AAAA,EAC9C;AAAA,EAEA;AAAA,EACA,OAAO;AAAA,EAEP,OAAO,WAAuB;AAAA,IAC5B,MAAM;AAAA,IACN,KAAK;AAAA,IACL,KAAK;AAAA,IACL,mBAAmB;AAAA;AAAA,IACnB,yBAAyB;AAAA;AAAA,IACzB,SAAS;AAAA,EACX;AAAA,EAEA,YAAY,QAAoB;AAC9B,UAAM;AACN,QAAI,CAAC,qBAAqB,MAAM;AAC9B,2BAAqB,OAAO,IAAI,KAAK,EAAE,GAAG,qBAAqB,UAAU,GAAG,OAAO,CAAC;AAAA,IACtF;AAAA,E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AI;AACtD,YAAMC,UAAS,MAAM,KAAK,MAAM,gDAAgD;AAAA,QAC9E;AAAA,MACF,CAAC;AACD,YAAM,KAAK,IAAI;AACf,aAAOA,QAAO,KAAK,SAAS;AAAA,IAChC;AAEA,UAAM,SAAS,MAAM,KAAK,WAAY,MAAM,gDAAgD;AAAA,MAC1F;AAAA,IACF,CAAC;AACD,WAAO,OAAO,KAAK,SAAS;AAAA,EAC9B;AACF;","names":["safeName","result"]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/databases/postgresql/PostgresqlConnection.mts"],"sourcesContent":["import { connection_events, Connection as ConnectionAbs } from '../../Connection.mjs';\nimport type { Client, PoolClient, PoolConfig } from 'pg';\nimport type pg from 'pg';\nimport { CompiledSql } from '../../types.mjs';\nimport { Query } from '../../Query.mjs';\nimport { PostgresqlQueryGrammar } from './PostgresqlQueryGrammar.mjs';\nimport { Schema } from '../../Schema.mjs';\nimport { PostgresqlSchemaGrammar } from './PostgresqlSchemaGrammar.mjs';\nimport type pg_cursor from 'pg-cursor';\nimport { EventManager } from '@devbro/neko-helper';\nimport { loadPackage } from '../../helper.mjs';\n\nexport class PostgresqlConnection extends ConnectionAbs {\n private eventManager = new EventManager();\n\n on(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.on(event, listener);\n return this;\n }\n off(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.off(event, listener);\n return this;\n }\n emit(event: connection_events, ...args: any[]): Promise<boolean> {\n return this.eventManager.emit(event, ...args);\n }\n\n static pg: typeof pg;\n static pg_cursor: typeof pg_cursor;\n connection: PoolClient | undefined;\n static pool: pg.Pool;\n\n static defaults: PoolConfig = {\n port: 5432,\n ssl: false,\n max: 20,\n idleTimeoutMillis: 1, // wait X milli seconds before closing an idle/released connection\n connectionTimeoutMillis: 30000, // wait up to 30 seconds to obtain a new connection\n maxUses: 7500,\n };\n\n constructor(params: PoolConfig) {\n super();\n if (!PostgresqlConnection.pg) {\n PostgresqlConnection.pg = loadPackage('pg');\n PostgresqlConnection.pg_cursor = loadPackage('pg-cursor');\n }\n if (!PostgresqlConnection.pool) {\n PostgresqlConnection.pool = new PostgresqlConnection.pg.Pool({\n ...PostgresqlConnection.defaults,\n ...params,\n });\n }\n }\n async connect(): Promise<boolean> {\n this.eventManager.emit('connect').catch(() => {});\n this.connection = await PostgresqlConnection.pool.connect();\n return true;\n }\n async runQuery(sql: CompiledSql | string): Promise<any> {\n if (typeof sql === 'string') {\n sql = { sql: sql, bindings: [], parts: [sql] };\n }\n let counter = 1;\n let sql2 = sql.sql;\n if (sql.parts && sql.parts.length > 0) {\n sql2 = sql.parts.map((v) => (v === '?' ? '$' + counter++ : v)).join(' ');\n }\n\n this.eventManager.emit('query', { sql: sql2, bindings: sql.bindings }).catch(() => {});\n\n if (!this.isConnected()) {\n await this.connect();\n }\n const result = await this.connection!.query(sql2, sql.bindings);\n return result?.rows;\n }\n\n async runCursor(sql: CompiledSql): Promise<any> {\n return this.connection?.query(new PostgresqlConnection.pg_cursor(sql.sql, sql.bindings));\n }\n\n async disconnect(): Promise<boolean> {\n if (this.connection === undefined) {\n return true;\n }\n await this.connection?.release();\n this.connection = undefined;\n this.eventManager.emit('disconnect').catch(() => {});\n return true;\n }\n\n getQuery(): Query {\n return new Query(this, new PostgresqlQueryGrammar());\n }\n\n getSchema(): Schema {\n return new Schema(this, new PostgresqlSchemaGrammar());\n }\n\n getQueryGrammar(): PostgresqlQueryGrammar {\n return new PostgresqlQueryGrammar();\n }\n getSchemaGrammar(): PostgresqlSchemaGrammar {\n return new PostgresqlSchemaGrammar();\n }\n\n async beginTransaction(): Promise<void> {\n await this.runQuery({ sql: 'BEGIN', bindings: [], parts: ['BEGIN'] });\n }\n\n async commit(): Promise<void> {\n await this.runQuery({ sql: 'COMMIT', bindings: [], parts: ['COMMIT'] });\n }\n\n async rollback(): Promise<void> {\n await this.runQuery({ sql: 'ROLLBACK', bindings: [], parts: ['ROLLBACK'] });\n }\n\n static destroy(): Promise<void> {\n return PostgresqlConnection.pool.end();\n }\n\n isConnected(): boolean {\n return this.connection !== undefined;\n }\n\n /**\n * Validates and escapes a PostgreSQL identifier (database name, table name, etc.)\n * Uses a whitelist approach to ensure only safe characters are allowed\n */\n private validateAndEscapeIdentifier(name: string): string {\n // PostgreSQL identifiers can contain: letters, digits, underscores, and dollar signs\n // They must start with a letter or underscore\n const validIdentifierPattern = /^[a-zA-Z_][a-zA-Z0-9_$]*$/;\n\n if (!validIdentifierPattern.test(name)) {\n throw new Error(\n `Invalid identifier: \"${name}\". Identifiers must start with a letter or underscore and contain only letters, digits, underscores, and dollar signs.`\n );\n }\n\n // PostgreSQL reserved keywords that should be quoted\n const reservedKeywords = new Set([\n 'user',\n 'table',\n 'database',\n 'order',\n 'group',\n 'select',\n 'insert',\n 'update',\n 'delete',\n ]);\n\n // Quote the identifier if it's a reserved keyword or contains uppercase letters\n if (reservedKeywords.has(name.toLowerCase()) || name !== name.toLowerCase()) {\n // Escape any double quotes in the name\n const escapedName = name.replace(/\"/g, '\"\"');\n return `\"${escapedName}\"`;\n }\n\n return name;\n }\n\n async createDatabase(name: string): Promise<void> {\n if (this.isConnected()) {\n const safeName = this.validateAndEscapeIdentifier(name);\n await this.runQuery(`CREATE DATABASE ${safeName}`);\n return;\n }\n\n const conn = new PostgresqlConnection.pg.Client({\n ...PostgresqlConnection.pool.options,\n database: 'postgres',\n });\n await conn.connect();\n const safeName = this.validateAndEscapeIdentifier(name);\n await conn.query(`CREATE DATABASE ${safeName}`);\n await conn.end();\n }\n\n async dropDatabase(name: string): Promise<void> {\n if (this.isConnected()) {\n const safeName = this.validateAndEscapeIdentifier(name);\n await this.runQuery(`DROP DATABASE ${safeName}`);\n return;\n }\n\n const conn = new PostgresqlConnection.pg.Client({\n ...PostgresqlConnection.pool.options,\n database: 'postgres', // connect to default 'postgres' database to drop others\n });\n await conn.connect();\n const safeName = this.validateAndEscapeIdentifier(name);\n await conn.query(`DROP DATABASE ${safeName}`);\n await conn.end();\n }\n\n async listDatabases(): Promise<string[]> {\n if (!this.isConnected()) {\n await this.connect();\n }\n const result = await this.connection!.query(\n 'SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname'\n );\n return result.rows.map((row: any) => row.datname);\n }\n\n async existsDatabase(name: string): Promise<boolean> {\n if (!this.isConnected()) {\n const conn = new PostgresqlConnection.pg.Client({\n ...PostgresqlConnection.pool.options,\n database: 'postgres',\n });\n await conn.connect();\n const safeName = this.validateAndEscapeIdentifier(name);\n const result = await conn.query('SELECT 1 FROM pg_database WHERE datname = $1', [safeName]);\n await conn.end();\n return result.rows.length > 0;\n }\n\n const result = await this.connection!.query('SELECT 1 FROM pg_database WHERE datname = $1', [\n name,\n ]);\n return result.rows.length > 0;\n }\n}\n"],"mappings":";;AAAA,SAA4B,cAAc,qBAAqB;AAI/D,SAAS,aAAa;AACtB,SAAS,8BAA8B;AACvC,SAAS,cAAc;AACvB,SAAS,+BAA+B;AAExC,SAAS,oBAAoB;AAC7B,SAAS,mBAAmB;AAErB,MAAM,6BAA6B,cAAc;AAAA,EAZxD,OAYwD;AAAA;AAAA;AAAA,EAC9C,eAAe,IAAI,aAAa;AAAA,EAExC,GAAG,OAA0B,UAA0C;AACrE,SAAK,aAAa,GAAG,OAAO,QAAQ;AACpC,WAAO;AAAA,EACT;AAAA,EACA,IAAI,OAA0B,UAA0C;AACtE,SAAK,aAAa,IAAI,OAAO,QAAQ;AACrC,WAAO;AAAA,EACT;AAAA,EACA,KAAK,UAA6B,MAA+B;AAC/D,WAAO,KAAK,aAAa,KAAK,OAAO,GAAG,IAAI;AAAA,EAC9C;AAAA,EAEA,OAAO;AAAA,EACP,OAAO;AAAA,EACP;AAAA,EACA,OAAO;AAAA,EAEP,OAAO,WAAuB;AAAA,IAC5B,MAAM;AAAA,IACN,KAAK;AAAA,IACL,KAAK;AAAA,IACL,mBAAmB;AAAA;AAAA,IACnB,yBAAyB;AAAA;AAAA,IACzB,SAAS;AAAA,EACX;AAAA,EAEA,YAAY,QAAoB;AA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K,WAAY;AAAA,MACpC;AAAA,IACF;AACA,WAAO,OAAO,KAAK,IAAI,CAAC,QAAa,IAAI,OAAO;AAAA,EAClD;AAAA,EAEA,MAAM,eAAe,MAAgC;AACnD,QAAI,CAAC,KAAK,YAAY,GAAG;AACvB,YAAM,OAAO,IAAI,qBAAqB,GAAG,OAAO;AAAA,QAC9C,GAAG,qBAAqB,KAAK;AAAA,QAC7B,UAAU;AAAA,MACZ,CAAC;AACD,YAAM,KAAK,QAAQ;AACnB,YAAM,WAAW,KAAK,4BAA4B,IAAI;AACtD,YAAMC,UAAS,MAAM,KAAK,MAAM,gDAAgD,CAAC,QAAQ,CAAC;AAC1F,YAAM,KAAK,IAAI;AACf,aAAOA,QAAO,KAAK,SAAS;AAAA,IAC9B;AAEA,UAAM,SAAS,MAAM,KAAK,WAAY,MAAM,gDAAgD;AAAA,MAC1F;AAAA,IACF,CAAC;AACD,WAAO,OAAO,KAAK,SAAS;AAAA,EAC9B;AACF;","names":["safeName","result"]}
|
|
@@ -35,6 +35,7 @@ declare class SqliteConnection extends Connection {
|
|
|
35
35
|
/** Default configuration values for SQLite connections */
|
|
36
36
|
static defaults: Partial<SqliteConfig>;
|
|
37
37
|
constructor(params: SqliteConfig);
|
|
38
|
+
static sqlite: typeof Database;
|
|
38
39
|
/**
|
|
39
40
|
* Establishes a connection to the SQLite database
|
|
40
41
|
* Creates or opens the database file specified in the configuration
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
import { Connection as ConnectionAbs } from "../../Connection.mjs";
|
|
4
|
-
import Database from "better-sqlite3";
|
|
5
4
|
import { Query } from "../../Query.mjs";
|
|
6
5
|
import { SqliteQueryGrammar } from "./SqliteQueryGrammar.mjs";
|
|
7
6
|
import { Schema } from "../../Schema.mjs";
|
|
8
7
|
import { SqliteSchemaGrammar } from "./SqliteSchemaGrammar.mjs";
|
|
9
8
|
import { EventManager } from "@devbro/neko-helper";
|
|
10
9
|
import * as fs from "fs";
|
|
10
|
+
import { loadPackage } from "../../helper.mjs";
|
|
11
11
|
class SqliteConnection extends ConnectionAbs {
|
|
12
12
|
static {
|
|
13
13
|
__name(this, "SqliteConnection");
|
|
@@ -34,8 +34,12 @@ class SqliteConnection extends ConnectionAbs {
|
|
|
34
34
|
};
|
|
35
35
|
constructor(params) {
|
|
36
36
|
super();
|
|
37
|
+
if (!SqliteConnection.sqlite) {
|
|
38
|
+
SqliteConnection.sqlite = loadPackage("better-sqlite3");
|
|
39
|
+
}
|
|
37
40
|
this.config = { ...SqliteConnection.defaults, ...params };
|
|
38
41
|
}
|
|
42
|
+
static sqlite;
|
|
39
43
|
/**
|
|
40
44
|
* Establishes a connection to the SQLite database
|
|
41
45
|
* Creates or opens the database file specified in the configuration
|
|
@@ -43,7 +47,7 @@ class SqliteConnection extends ConnectionAbs {
|
|
|
43
47
|
async connect() {
|
|
44
48
|
this.eventManager.emit("connect").catch(() => {
|
|
45
49
|
});
|
|
46
|
-
this.connection = new
|
|
50
|
+
this.connection = new SqliteConnection.sqlite(this.config.filename, {
|
|
47
51
|
readonly: this.config.readonly,
|
|
48
52
|
fileMustExist: this.config.fileMustExist,
|
|
49
53
|
timeout: this.config.timeout,
|
|
@@ -202,7 +206,7 @@ class SqliteConnection extends ConnectionAbs {
|
|
|
202
206
|
*/
|
|
203
207
|
async createDatabase(name) {
|
|
204
208
|
const dbPath = name.endsWith(".db") ? name : `${name}.db`;
|
|
205
|
-
const tempDb = new
|
|
209
|
+
const tempDb = new SqliteConnection.sqlite(dbPath);
|
|
206
210
|
tempDb.close();
|
|
207
211
|
}
|
|
208
212
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/databases/sqlite/SqliteConnection.mts"],"sourcesContent":["import { connection_events, Connection as ConnectionAbs } from '../../Connection.mjs';\nimport Database from 'better-sqlite3';\nimport { CompiledSql } from '../../types.mjs';\nimport { Query } from '../../Query.mjs';\nimport { SqliteQueryGrammar } from './SqliteQueryGrammar.mjs';\nimport { Schema } from '../../Schema.mjs';\nimport { SqliteSchemaGrammar } from './SqliteSchemaGrammar.mjs';\nimport { EventManager } from '@devbro/neko-helper';\nimport * as fs from 'fs';\n\n/**\n * Configuration options for SQLite database connection\n */\nexport interface SqliteConfig {\n /** Path to the SQLite database file */\n filename: string;\n /** Open the database in read-only mode (default: false) */\n readonly?: boolean;\n /** Throw an error if the database file doesn't exist (default: false) */\n fileMustExist?: boolean;\n /** Timeout in milliseconds for database operations (default: 5000) */\n timeout?: number;\n /** Optional verbose logging function for debugging SQL statements */\n verbose?: (message?: unknown, ...additionalArgs: unknown[]) => void;\n}\n\n/**\n * SQLite database connection implementation\n *\n * Provides a connection to SQLite databases using better-sqlite3.\n * Supports transactions, queries, schema operations, and database management.\n */\nexport class SqliteConnection extends ConnectionAbs {\n private eventManager = new EventManager();\n\n on(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.on(event, listener);\n return this;\n }\n off(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.off(event, listener);\n return this;\n }\n emit(event: connection_events, ...args: any[]): Promise<boolean> {\n return this.eventManager.emit(event, ...args);\n }\n\n connection: Database.Database | undefined;\n private config: SqliteConfig;\n\n /** Default configuration values for SQLite connections */\n static defaults: Partial<SqliteConfig> = {\n readonly: false,\n fileMustExist: false,\n timeout: 5000,\n };\n\n constructor(params: SqliteConfig) {\n super();\n this.config = { ...SqliteConnection.defaults, ...params } as SqliteConfig;\n }\n\n /**\n * Establishes a connection to the SQLite database\n * Creates or opens the database file specified in the configuration\n */\n async connect(): Promise<boolean> {\n this.eventManager.emit('connect').catch(() => {});\n this.connection = new Database(this.config.filename, {\n readonly: this.config.readonly,\n fileMustExist: this.config.fileMustExist,\n timeout: this.config.timeout,\n verbose: this.config.verbose,\n });\n return true;\n }\n\n /**\n * Executes a SQL query against the database\n * Automatically detects SELECT queries and queries with RETURNING clauses\n *\n * @param sql - Compiled SQL or raw SQL string to execute\n * @returns Query results (rows for SELECT, run info for INSERT/UPDATE/DELETE)\n */\n async runQuery(sql: CompiledSql | string): Promise<any> {\n if (typeof sql === 'string') {\n sql = { sql: sql, bindings: [], parts: [sql] };\n }\n\n this.eventManager.emit('query', { sql: sql.sql, bindings: sql.bindings }).catch(() => {});\n\n if (!this.isConnected()) {\n await this.connect();\n }\n\n try {\n const stmt = this.connection!.prepare(sql.sql);\n\n // Check if the query is a SELECT or contains RETURNING clause\n const sqlUpper = sql.sql.trim().toUpperCase();\n if (sqlUpper.startsWith('SELECT') || sqlUpper.includes('RETURNING')) {\n return stmt.all(...sql.bindings);\n } else {\n const result = stmt.run(...sql.bindings);\n return result;\n }\n } catch (error) {\n this.eventManager.emit('error', error).catch(() => {});\n throw error;\n }\n }\n\n /**\n * Executes a query and returns an iterator for streaming results\n * Useful for large result sets to avoid loading all rows into memory\n *\n * @param sql - Compiled SQL to execute\n * @returns Iterator over query results\n */\n async runCursor(sql: CompiledSql): Promise<any> {\n // SQLite doesn't have native cursor support, return iterator\n if (!this.isConnected()) {\n await this.connect();\n }\n const stmt = this.connection!.prepare(sql.sql);\n return stmt.iterate(...sql.bindings);\n }\n\n /**\n * Closes the database connection\n */\n async disconnect(): Promise<boolean> {\n if (this.connection === undefined) {\n return true;\n }\n this.connection.close();\n this.connection = undefined;\n this.eventManager.emit('disconnect').catch(() => {});\n return true;\n }\n\n /**\n * Creates a new query builder instance for this connection\n */\n getQuery(): Query {\n return new Query(this, new SqliteQueryGrammar());\n }\n\n /**\n * Creates a new schema builder instance for this connection\n */\n getSchema(): Schema {\n return new Schema(this, new SqliteSchemaGrammar());\n }\n\n /**\n * Gets the query grammar for building SQL statements\n */\n getQueryGrammar(): SqliteQueryGrammar {\n return new SqliteQueryGrammar();\n }\n\n /**\n * Gets the schema grammar for building DDL statements\n */\n getSchemaGrammar(): SqliteSchemaGrammar {\n return new SqliteSchemaGrammar();\n }\n\n /**\n * Starts a new database transaction\n */\n async beginTransaction(): Promise<void> {\n await this.runQuery({\n sql: 'BEGIN TRANSACTION',\n bindings: [],\n parts: ['BEGIN', 'TRANSACTION'],\n });\n }\n\n /**\n * Commits the current transaction\n */\n async commit(): Promise<void> {\n await this.runQuery({ sql: 'COMMIT', bindings: [], parts: ['COMMIT'] });\n }\n\n /**\n * Rolls back the current transaction\n */\n async rollback(): Promise<void> {\n await this.runQuery({ sql: 'ROLLBACK', bindings: [], parts: ['ROLLBACK'] });\n }\n\n /**\n * Checks if the database connection is active\n */\n isConnected(): boolean {\n return this.connection !== undefined && this.connection.open;\n }\n\n /**\n * Validates and escapes a SQLite identifier (database name, table name, etc.)\n * Uses a whitelist approach to ensure only safe characters are allowed\n *\n * @param name - The identifier to validate and escape\n * @returns The escaped identifier, quoted if it's a reserved keyword\n * @throws Error if the identifier contains invalid characters\n */\n private validateAndEscapeIdentifier(name: string): string {\n // SQLite identifiers can contain: letters, digits, underscores\n // They must start with a letter or underscore\n const validIdentifierPattern = /^[a-zA-Z_][a-zA-Z0-9_]*$/;\n\n if (!validIdentifierPattern.test(name)) {\n throw new Error(\n `Invalid identifier: \"${name}\". Identifiers must start with a letter or underscore and contain only letters, digits, and underscores.`\n );\n }\n\n // SQLite reserved keywords that should be quoted\n const reservedKeywords = new Set([\n 'table',\n 'database',\n 'order',\n 'group',\n 'select',\n 'insert',\n 'update',\n 'delete',\n 'index',\n 'from',\n 'where',\n ]);\n\n // Quote the identifier if it's a reserved keyword\n if (reservedKeywords.has(name.toLowerCase())) {\n // Escape any double quotes in the name\n const escapedName = name.replace(/\"/g, '\"\"');\n return `\"${escapedName}\"`;\n }\n\n return name;\n }\n\n /**\n * Creates a new SQLite database file\n *\n * @param name - Name or path of the database file to create\n */\n async createDatabase(name: string): Promise<void> {\n // SQLite databases are files, creating a database means creating a new connection\n const dbPath = name.endsWith('.db') ? name : `${name}.db`;\n const tempDb = new Database(dbPath);\n tempDb.close();\n }\n\n /**\n * Deletes a SQLite database file\n *\n * @param name - Name or path of the database file to delete\n */\n async dropDatabase(name: string): Promise<void> {\n // SQLite databases are files, dropping means deleting the file\n const dbPath = name.endsWith('.db') ? name : `${name}.db`;\n if (fs.existsSync(dbPath)) {\n fs.unlinkSync(dbPath);\n }\n }\n\n /**\n * Lists available databases\n * For SQLite, this returns the current database filename\n *\n * @returns Array containing the current database filename\n */\n async listDatabases(): Promise<string[]> {\n // SQLite doesn't have a concept of multiple databases in the same connection\n // This would require listing files in a directory\n return [this.config.filename];\n }\n\n /**\n * Checks if a database file exists\n *\n * @param name - Name or path of the database file to check\n * @returns True if the database file exists, false otherwise\n */\n async existsDatabase(name: string): Promise<boolean> {\n const dbPath = name.endsWith('.db') ? name : `${name}.db`;\n return fs.existsSync(dbPath);\n }\n}\n"],"mappings":";;AAAA,SAA4B,cAAc,qBAAqB;AAC/D,OAAO,cAAc;AAErB,SAAS,aAAa;AACtB,SAAS,0BAA0B;AACnC,SAAS,cAAc;AACvB,SAAS,2BAA2B;AACpC,SAAS,oBAAoB;AAC7B,YAAY,QAAQ;AAwBb,MAAM,yBAAyB,cAAc;AAAA,EAhCpD,OAgCoD;AAAA;AAAA;AAAA,EAC1C,eAAe,IAAI,aAAa;AAAA,EAExC,GAAG,OAA0B,UAA0C;AACrE,SAAK,aAAa,GAAG,OAAO,QAAQ;AACpC,WAAO;AAAA,EACT;AAAA,EACA,IAAI,OAA0B,UAA0C;AACtE,SAAK,aAAa,IAAI,OAAO,QAAQ;AACrC,WAAO;AAAA,EACT;AAAA,EACA,KAAK,UAA6B,MAA+B;AAC/D,WAAO,KAAK,aAAa,KAAK,OAAO,GAAG,IAAI;AAAA,EAC9C;AAAA,EAEA;AAAA,EACQ;AAAA;AAAA,EAGR,OAAO,WAAkC;AAAA,IACvC,UAAU;AAAA,IACV,eAAe;AAAA,IACf,SAAS;AAAA,EACX;AAAA,EAEA,YAAY,QAAsB;AAChC,UAAM;AACN,SAAK,SAAS,EAAE,GAAG,iBAAiB,UAAU,GAAG,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I,OAAO,GAAG,IAAI;AACpD,WAAO,GAAG,WAAW,MAAM;AAAA,EAC7B;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../../src/databases/sqlite/SqliteConnection.mts"],"sourcesContent":["import { connection_events, Connection as ConnectionAbs } from '../../Connection.mjs';\nimport type Database from 'better-sqlite3';\nimport { CompiledSql } from '../../types.mjs';\nimport { Query } from '../../Query.mjs';\nimport { SqliteQueryGrammar } from './SqliteQueryGrammar.mjs';\nimport { Schema } from '../../Schema.mjs';\nimport { SqliteSchemaGrammar } from './SqliteSchemaGrammar.mjs';\nimport { EventManager } from '@devbro/neko-helper';\nimport * as fs from 'fs';\nimport { loadPackage } from '../../helper.mjs';\n\n/**\n * Configuration options for SQLite database connection\n */\nexport interface SqliteConfig {\n /** Path to the SQLite database file */\n filename: string;\n /** Open the database in read-only mode (default: false) */\n readonly?: boolean;\n /** Throw an error if the database file doesn't exist (default: false) */\n fileMustExist?: boolean;\n /** Timeout in milliseconds for database operations (default: 5000) */\n timeout?: number;\n /** Optional verbose logging function for debugging SQL statements */\n verbose?: (message?: unknown, ...additionalArgs: unknown[]) => void;\n}\n\n/**\n * SQLite database connection implementation\n *\n * Provides a connection to SQLite databases using better-sqlite3.\n * Supports transactions, queries, schema operations, and database management.\n */\nexport class SqliteConnection extends ConnectionAbs {\n private eventManager = new EventManager();\n\n on(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.on(event, listener);\n return this;\n }\n off(event: connection_events, listener: (...args: any[]) => void): this {\n this.eventManager.off(event, listener);\n return this;\n }\n emit(event: connection_events, ...args: any[]): Promise<boolean> {\n return this.eventManager.emit(event, ...args);\n }\n\n connection: Database.Database | undefined;\n private config: SqliteConfig;\n\n /** Default configuration values for SQLite connections */\n static defaults: Partial<SqliteConfig> = {\n readonly: false,\n fileMustExist: false,\n timeout: 5000,\n };\n\n constructor(params: SqliteConfig) {\n super();\n if (!SqliteConnection.sqlite) {\n SqliteConnection.sqlite = loadPackage('better-sqlite3');\n }\n this.config = { ...SqliteConnection.defaults, ...params } as SqliteConfig;\n }\n\n static sqlite: typeof Database;\n\n /**\n * Establishes a connection to the SQLite database\n * Creates or opens the database file specified in the configuration\n */\n async connect(): Promise<boolean> {\n this.eventManager.emit('connect').catch(() => {});\n this.connection = new SqliteConnection.sqlite(this.config.filename, {\n readonly: this.config.readonly,\n fileMustExist: this.config.fileMustExist,\n timeout: this.config.timeout,\n verbose: this.config.verbose,\n });\n return true;\n }\n\n /**\n * Executes a SQL query against the database\n * Automatically detects SELECT queries and queries with RETURNING clauses\n *\n * @param sql - Compiled SQL or raw SQL string to execute\n * @returns Query results (rows for SELECT, run info for INSERT/UPDATE/DELETE)\n */\n async runQuery(sql: CompiledSql | string): Promise<any> {\n if (typeof sql === 'string') {\n sql = { sql: sql, bindings: [], parts: [sql] };\n }\n\n this.eventManager.emit('query', { sql: sql.sql, bindings: sql.bindings }).catch(() => {});\n\n if (!this.isConnected()) {\n await this.connect();\n }\n\n try {\n const stmt = this.connection!.prepare(sql.sql);\n\n // Check if the query is a SELECT or contains RETURNING clause\n const sqlUpper = sql.sql.trim().toUpperCase();\n if (sqlUpper.startsWith('SELECT') || sqlUpper.includes('RETURNING')) {\n return stmt.all(...sql.bindings);\n } else {\n const result = stmt.run(...sql.bindings);\n return result;\n }\n } catch (error) {\n this.eventManager.emit('error', error).catch(() => {});\n throw error;\n }\n }\n\n /**\n * Executes a query and returns an iterator for streaming results\n * Useful for large result sets to avoid loading all rows into memory\n *\n * @param sql - Compiled SQL to execute\n * @returns Iterator over query results\n */\n async runCursor(sql: CompiledSql): Promise<any> {\n // SQLite doesn't have native cursor support, return iterator\n if (!this.isConnected()) {\n await this.connect();\n }\n const stmt = this.connection!.prepare(sql.sql);\n return stmt.iterate(...sql.bindings);\n }\n\n /**\n * Closes the database connection\n */\n async disconnect(): Promise<boolean> {\n if (this.connection === undefined) {\n return true;\n }\n this.connection.close();\n this.connection = undefined;\n this.eventManager.emit('disconnect').catch(() => {});\n return true;\n }\n\n /**\n * Creates a new query builder instance for this connection\n */\n getQuery(): Query {\n return new Query(this, new SqliteQueryGrammar());\n }\n\n /**\n * Creates a new schema builder instance for this connection\n */\n getSchema(): Schema {\n return new Schema(this, new SqliteSchemaGrammar());\n }\n\n /**\n * Gets the query grammar for building SQL statements\n */\n getQueryGrammar(): SqliteQueryGrammar {\n return new SqliteQueryGrammar();\n }\n\n /**\n * Gets the schema grammar for building DDL statements\n */\n getSchemaGrammar(): SqliteSchemaGrammar {\n return new SqliteSchemaGrammar();\n }\n\n /**\n * Starts a new database transaction\n */\n async beginTransaction(): Promise<void> {\n await this.runQuery({\n sql: 'BEGIN TRANSACTION',\n bindings: [],\n parts: ['BEGIN', 'TRANSACTION'],\n });\n }\n\n /**\n * Commits the current transaction\n */\n async commit(): Promise<void> {\n await this.runQuery({ sql: 'COMMIT', bindings: [], parts: ['COMMIT'] });\n }\n\n /**\n * Rolls back the current transaction\n */\n async rollback(): Promise<void> {\n await this.runQuery({ sql: 'ROLLBACK', bindings: [], parts: ['ROLLBACK'] });\n }\n\n /**\n * Checks if the database connection is active\n */\n isConnected(): boolean {\n return this.connection !== undefined && this.connection.open;\n }\n\n /**\n * Validates and escapes a SQLite identifier (database name, table name, etc.)\n * Uses a whitelist approach to ensure only safe characters are allowed\n *\n * @param name - The identifier to validate and escape\n * @returns The escaped identifier, quoted if it's a reserved keyword\n * @throws Error if the identifier contains invalid characters\n */\n private validateAndEscapeIdentifier(name: string): string {\n // SQLite identifiers can contain: letters, digits, underscores\n // They must start with a letter or underscore\n const validIdentifierPattern = /^[a-zA-Z_][a-zA-Z0-9_]*$/;\n\n if (!validIdentifierPattern.test(name)) {\n throw new Error(\n `Invalid identifier: \"${name}\". Identifiers must start with a letter or underscore and contain only letters, digits, and underscores.`\n );\n }\n\n // SQLite reserved keywords that should be quoted\n const reservedKeywords = new Set([\n 'table',\n 'database',\n 'order',\n 'group',\n 'select',\n 'insert',\n 'update',\n 'delete',\n 'index',\n 'from',\n 'where',\n ]);\n\n // Quote the identifier if it's a reserved keyword\n if (reservedKeywords.has(name.toLowerCase())) {\n // Escape any double quotes in the name\n const escapedName = name.replace(/\"/g, '\"\"');\n return `\"${escapedName}\"`;\n }\n\n return name;\n }\n\n /**\n * Creates a new SQLite database file\n *\n * @param name - Name or path of the database file to create\n */\n async createDatabase(name: string): Promise<void> {\n // SQLite databases are files, creating a database means creating a new connection\n const dbPath = name.endsWith('.db') ? name : `${name}.db`;\n const tempDb = new SqliteConnection.sqlite(dbPath);\n tempDb.close();\n }\n\n /**\n * Deletes a SQLite database file\n *\n * @param name - Name or path of the database file to delete\n */\n async dropDatabase(name: string): Promise<void> {\n // SQLite databases are files, dropping means deleting the file\n const dbPath = name.endsWith('.db') ? name : `${name}.db`;\n if (fs.existsSync(dbPath)) {\n fs.unlinkSync(dbPath);\n }\n }\n\n /**\n * Lists available databases\n * For SQLite, this returns the current database filename\n *\n * @returns Array containing the current database filename\n */\n async listDatabases(): Promise<string[]> {\n // SQLite doesn't have a concept of multiple databases in the same connection\n // This would require listing files in a directory\n return [this.config.filename];\n }\n\n /**\n * Checks if a database file exists\n *\n * @param name - Name or path of the database file to check\n * @returns True if the database file exists, false otherwise\n */\n async existsDatabase(name: string): Promise<boolean> {\n const dbPath = name.endsWith('.db') ? name : `${name}.db`;\n return fs.existsSync(dbPath);\n }\n}\n"],"mappings":";;AAAA,SAA4B,cAAc,qBAAqB;AAG/D,SAAS,aAAa;AACtB,SAAS,0BAA0B;AACnC,SAAS,cAAc;AACvB,SAAS,2BAA2B;AACpC,SAAS,oBAAoB;AAC7B,YAAY,QAAQ;AACpB,SAAS,mBAAmB;AAwBrB,MAAM,yBAAyB,cAAc;AAAA,EAjCpD,OAiCoD;AAAA;AAAA;AAAA,EAC1C,eAAe,IAAI,aAAa;AAAA,EAExC,GAAG,OAA0B,UAA0C;AACrE,SAAK,aAAa,GAAG,OAAO,QAAQ;AACpC,WAAO;AAAA,EACT;AAAA,EACA,IAAI,OAA0B,UA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I,iBAAiB,OAAO,MAAM;AACjD,WAAO,MAAM;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,aAAa,MAA6B;AAE9C,UAAM,SAAS,KAAK,SAAS,KAAK,IAAI,OAAO,GAAG,IAAI;AACpD,QAAI,GAAG,WAAW,MAAM,GAAG;AACzB,SAAG,WAAW,MAAM;AAAA,IACtB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,gBAAmC;AAGvC,WAAO,CAAC,KAAK,OAAO,QAAQ;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,eAAe,MAAgC;AACnD,UAAM,SAAS,KAAK,SAAS,KAAK,IAAI,OAAO,GAAG,IAAI;AACpD,WAAO,GAAG,WAAW,MAAM;AAAA,EAC7B;AACF;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import { createRequire } from "module";
|
|
4
|
+
const req = createRequire(import.meta.url);
|
|
5
|
+
function loadPackage(name) {
|
|
6
|
+
try {
|
|
7
|
+
return req(name);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (Error.isError(error) && error.code === "MODULE_NOT_FOUND") {
|
|
10
|
+
error.message = `Package "${name}" is not installed. Please install proper db driver to use this database connection.`;
|
|
11
|
+
}
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
__name(loadPackage, "loadPackage");
|
|
16
|
+
export {
|
|
17
|
+
loadPackage
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=helper.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/helper.mts"],"sourcesContent":["import { createRequire } from 'module';\n\n// When bundled to CJS, tsup will handle import.meta.url appropriately\nconst req = createRequire(import.meta.url);\n\nexport function loadPackage(name: string) {\n try {\n return req(name);\n } catch (error: any) {\n // @ts-ignore\n if (Error.isError(error) && error.code === 'MODULE_NOT_FOUND') {\n error.message = `Package \"${name}\" is not installed. Please install proper db driver to use this database connection.`;\n }\n\n throw error;\n }\n}\n"],"mappings":";;AAAA,SAAS,qBAAqB;AAG9B,MAAM,MAAM,cAAc,YAAY,GAAG;AAElC,SAAS,YAAY,MAAc;AACxC,MAAI;AACF,WAAO,IAAI,IAAI;AAAA,EACjB,SAAS,OAAY;AAEnB,QAAI,MAAM,QAAQ,KAAK,KAAK,MAAM,SAAS,oBAAoB;AAC7D,YAAM,UAAU,YAAY,IAAI;AAAA,IAClC;AAEA,UAAM;AAAA,EACR;AACF;AAXgB;","names":[]}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -10,6 +10,7 @@ export { MysqlSchemaGrammar } from './databases/mysql/MysqlSchemaGrammar.mjs';
|
|
|
10
10
|
export { B as Blueprint, a as Column, C as ColumnPropertiesType, m as CompiledSql, b as Connection, F as ForeignKeyConstraint, I as IndexConstraint, J as JoinCondition, P as Parameter, d as Query, e as QueryGrammar, Q as QueryParts, S as Schema, f as SchemaGrammar, c as connection_events, n as havingType, o as joinType, s as selectType, w as whereBasic, i as whereNested, k as whereNull, g as whereOp, h as whereOpColumn, j as whereRaw, l as whereType } from './Blueprint-DjP_Sfrr.mjs';
|
|
11
11
|
export { Migration } from './Migration.mjs';
|
|
12
12
|
import 'pg';
|
|
13
|
+
import 'pg-cursor';
|
|
13
14
|
import 'better-sqlite3';
|
|
14
15
|
import 'mysql2/promise';
|
|
15
16
|
import '@devbro/neko-helper';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devbro/neko-sql",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.40",
|
|
4
4
|
"description": "generic sql generator",
|
|
5
5
|
"main": "./dist/cjs/index.js",
|
|
6
6
|
"module": "./dist/esm/index.mjs",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@faker-js/faker": "^9.5.1",
|
|
32
32
|
"@types/better-sqlite3": "^7.6.12",
|
|
33
|
-
"@types/node": "^
|
|
33
|
+
"@types/node": "^24.1.0",
|
|
34
34
|
"@types/pg": "^8.11.2",
|
|
35
35
|
"@types/pg-cursor": "^2.7.2",
|
|
36
36
|
"@typescript-eslint/eslint-plugin": "^7.1.1",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"husky": "^9.1.7",
|
|
40
40
|
"prettier": "^3.5.3",
|
|
41
41
|
"tsup": "^8.0.2",
|
|
42
|
-
"typescript": "^5.
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
43
|
"vitest": "^3.2.4",
|
|
44
44
|
"pg": "^8.11.3",
|
|
45
45
|
"pg-cursor": "^2.15.0",
|