@kurdel/db 0.1.0-beta.1
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/LICENSE +21 -0
- package/README.md +42 -0
- package/lib/consts.d.ts +1 -0
- package/lib/consts.js +2 -0
- package/lib/consts.js.map +1 -0
- package/lib/database-driver.d.ts +7 -0
- package/lib/database-driver.js +6 -0
- package/lib/database-driver.js.map +1 -0
- package/lib/database-factory.d.ts +6 -0
- package/lib/database-factory.js +12 -0
- package/lib/database-factory.js.map +1 -0
- package/lib/db-connector.d.ts +7 -0
- package/lib/db-connector.js +26 -0
- package/lib/db-connector.js.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +5 -0
- package/lib/index.js.map +1 -0
- package/lib/interfaces.d.ts +35 -0
- package/lib/interfaces.js +2 -0
- package/lib/interfaces.js.map +1 -0
- package/lib/query-builder.d.ts +16 -0
- package/lib/query-builder.js +50 -0
- package/lib/query-builder.js.map +1 -0
- package/lib/sqlite-db.d.ts +15 -0
- package/lib/sqlite-db.js +93 -0
- package/lib/sqlite-db.js.map +1 -0
- package/lib/sqlite-driver.d.ts +13 -0
- package/lib/sqlite-driver.js +36 -0
- package/lib/sqlite-driver.js.map +1 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 Andrii Sorokin
|
|
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,42 @@
|
|
|
1
|
+
# @kurdel/db
|
|
2
|
+
|
|
3
|
+
Database contracts, SQLite connectivity, and query-building utilities for
|
|
4
|
+
Kurdel applications.
|
|
5
|
+
|
|
6
|
+
## Transactions
|
|
7
|
+
|
|
8
|
+
Use `IDatabase.transaction` to execute related operations atomically:
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
const user = await db.transaction(async transaction => {
|
|
12
|
+
const created = await transaction.get({
|
|
13
|
+
sql: 'INSERT INTO users (email) VALUES (?) RETURNING id, email;',
|
|
14
|
+
params: ['ada@example.test'],
|
|
15
|
+
});
|
|
16
|
+
await transaction.run({
|
|
17
|
+
sql: 'INSERT INTO user_roles (user_id, role_id) VALUES (?, ?);',
|
|
18
|
+
params: [created.id, 2],
|
|
19
|
+
});
|
|
20
|
+
return created;
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The callback result is returned after commit. Throwing or rejecting rolls back
|
|
25
|
+
every operation performed through the supplied `IDatabaseSession`.
|
|
26
|
+
|
|
27
|
+
Always use the callback's `transaction` argument inside the callback. Calling
|
|
28
|
+
the outer `db` object would schedule work outside the transaction and may wait
|
|
29
|
+
for the callback to finish. Nested transactions are intentionally not exposed
|
|
30
|
+
by `IDatabaseSession`.
|
|
31
|
+
|
|
32
|
+
The SQLite implementation serializes the complete callback with respect to
|
|
33
|
+
all other operations on its connection. This prevents another request from
|
|
34
|
+
interleaving a query between `BEGIN` and `COMMIT`.
|
|
35
|
+
|
|
36
|
+
Custom database implementations must provide the same atomic callback
|
|
37
|
+
semantics and ensure that the supplied session stays on one connection for the
|
|
38
|
+
duration of the transaction.
|
|
39
|
+
|
|
40
|
+
## License
|
|
41
|
+
|
|
42
|
+
MIT © Andrii Sorokin
|
package/lib/consts.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const DB_CONFIG_FILENAME = "db.config.json";
|
package/lib/consts.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,kBAAkB,GAAG,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database-driver.js","sourceRoot":"","sources":["../src/database-driver.ts"],"names":[],"mappings":"AAEA,MAAM,OAAgB,cAAc;IAGlC,YAAY,MAAS;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CAKF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ISQLiteConfig } from './sqlite-driver.js';
|
|
2
|
+
import { SQLiteDriver } from './sqlite-driver.js';
|
|
3
|
+
export type ICombinedDatabaseConfig = ISQLiteConfig;
|
|
4
|
+
export declare class DatabaseFactory {
|
|
5
|
+
static createDriver(config: ICombinedDatabaseConfig): SQLiteDriver;
|
|
6
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { SQLiteDriver } from './sqlite-driver.js';
|
|
2
|
+
export class DatabaseFactory {
|
|
3
|
+
static createDriver(config) {
|
|
4
|
+
switch (config.type) {
|
|
5
|
+
case 'sqlite':
|
|
6
|
+
return new SQLiteDriver(config);
|
|
7
|
+
default:
|
|
8
|
+
throw new Error(`Unsupported database type: ${config.type}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=database-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database-factory.js","sourceRoot":"","sources":["../src/database-factory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAIlD,MAAM,OAAO,eAAe;IAC1B,MAAM,CAAC,YAAY,CAAC,MAA+B;QACjD,QAAQ,MAAM,CAAC,IAAI,EAAE,CAAC;YACpB,KAAK,QAAQ;gBACX,OAAO,IAAI,YAAY,CAAC,MAAuB,CAAC,CAAC;YACnD;gBACE,MAAM,IAAI,KAAK,CAAC,8BAA8B,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { JSONLoader } from '@kurdel/common';
|
|
2
|
+
import { DB_CONFIG_FILENAME } from './consts.js';
|
|
3
|
+
import { DatabaseFactory } from './database-factory.js';
|
|
4
|
+
export class DBConnector {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.jsonLoader = new JSONLoader();
|
|
7
|
+
}
|
|
8
|
+
async run() {
|
|
9
|
+
try {
|
|
10
|
+
const dbConfig = this.jsonLoader.load(DB_CONFIG_FILENAME);
|
|
11
|
+
return this.establish(dbConfig);
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
throw new Error(`Database connection failed: ${String(err)}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async establish(dbConfig) {
|
|
18
|
+
const driver = DatabaseFactory.createDriver(dbConfig);
|
|
19
|
+
await driver.connect();
|
|
20
|
+
if (!driver.connection) {
|
|
21
|
+
throw new Error('Database connection failed');
|
|
22
|
+
}
|
|
23
|
+
return driver.connection;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=db-connector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"db-connector.js","sourceRoot":"","sources":["../src/db-connector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD,MAAM,OAAO,WAAW;IAGtB;QACE,IAAI,CAAC,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;IACrC,CAAC;IAEM,KAAK,CAAC,GAAG;QACd,IAAI,CAAC;YACH,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;YAC1D,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QAClC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,+BAA+B,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAChE,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,SAAS,CAAC,QAAiC;QACvD,MAAM,MAAM,GAAG,eAAe,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;QACtD,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;QACvB,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;YACvB,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAChD,CAAC;QACD,OAAO,MAAM,CAAC,UAAU,CAAC;IAC3B,CAAC;CACF"}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { IDatabase, type IDatabaseSession, type IQueryBuilder, type IDatabaseConfig, type DatabaseQuery, } from './interfaces.js';
|
|
2
|
+
export { DatabaseFactory } from './database-factory.js';
|
|
3
|
+
export { DBConnector } from './db-connector.js';
|
|
4
|
+
export { QueryBuilder } from './query-builder.js';
|
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,GAKV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface IDatabaseConfig {
|
|
2
|
+
type: string;
|
|
3
|
+
host?: string;
|
|
4
|
+
filename?: string;
|
|
5
|
+
port?: number;
|
|
6
|
+
user?: string;
|
|
7
|
+
password?: string;
|
|
8
|
+
}
|
|
9
|
+
export type DatabaseQuery = {
|
|
10
|
+
sql: string;
|
|
11
|
+
params: any[];
|
|
12
|
+
};
|
|
13
|
+
export declare const IDatabase: unique symbol;
|
|
14
|
+
export interface IDatabaseSession {
|
|
15
|
+
get(query: DatabaseQuery): Promise<any>;
|
|
16
|
+
all(query: DatabaseQuery): Promise<any>;
|
|
17
|
+
run(query: DatabaseQuery): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
export interface IDatabase extends IDatabaseSession {
|
|
20
|
+
/**
|
|
21
|
+
* Runs work atomically on an isolated database session.
|
|
22
|
+
*
|
|
23
|
+
* Queries inside the callback must use the supplied transaction session.
|
|
24
|
+
* Resolves with the callback result after commit and rolls back on failure.
|
|
25
|
+
*/
|
|
26
|
+
transaction<T>(work: (transaction: IDatabaseSession) => Promise<T>): Promise<T>;
|
|
27
|
+
close(): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
export interface IQueryBuilder {
|
|
30
|
+
insert(table: string, data: Record<string, any>): IQueryBuilder;
|
|
31
|
+
select(fields: string | string[]): IQueryBuilder;
|
|
32
|
+
from(table: string): IQueryBuilder;
|
|
33
|
+
where(condition: string, params?: any[]): IQueryBuilder;
|
|
34
|
+
build(): DatabaseQuery;
|
|
35
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../src/interfaces.ts"],"names":[],"mappings":"AAcA,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { DatabaseQuery, IQueryBuilder } from './interfaces.js';
|
|
2
|
+
type SelectOptions = {
|
|
3
|
+
fn?: 'MAX' | 'MIN' | 'COUNT';
|
|
4
|
+
as?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare class QueryBuilder implements IQueryBuilder {
|
|
7
|
+
private sql;
|
|
8
|
+
private params;
|
|
9
|
+
select(fields: string | string[], options?: SelectOptions): QueryBuilder;
|
|
10
|
+
insert(table: string, data: Record<string, any>): QueryBuilder;
|
|
11
|
+
delete(): QueryBuilder;
|
|
12
|
+
from(table: string): QueryBuilder;
|
|
13
|
+
where(condition: string, params?: any[]): QueryBuilder;
|
|
14
|
+
build(): DatabaseQuery;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export class QueryBuilder {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.sql = '';
|
|
4
|
+
this.params = [];
|
|
5
|
+
}
|
|
6
|
+
select(fields, options = {}) {
|
|
7
|
+
if (options.fn) {
|
|
8
|
+
this.sql = `SELECT ${options.fn}(${fields}) `;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
this.sql = `SELECT ${Array.isArray(fields) ? fields.join(', ') : fields} `;
|
|
12
|
+
}
|
|
13
|
+
if (options.as) {
|
|
14
|
+
this.sql = this.sql + `AS ${options.as} `;
|
|
15
|
+
}
|
|
16
|
+
this.params = [];
|
|
17
|
+
return this;
|
|
18
|
+
}
|
|
19
|
+
insert(table, data) {
|
|
20
|
+
const keys = Object.keys(data);
|
|
21
|
+
const values = Object.values(data);
|
|
22
|
+
const columns = keys.join(', ');
|
|
23
|
+
const placeholders = keys.map(() => '?').join(', ');
|
|
24
|
+
this.sql = `INSERT INTO ${table} (${columns}) VALUES (${placeholders}) `;
|
|
25
|
+
this.params = values;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
delete() {
|
|
29
|
+
this.sql = `DELETE `;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
from(table) {
|
|
33
|
+
this.sql += `FROM ${table} `;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
where(condition, params) {
|
|
37
|
+
this.sql += `WHERE ${condition} `;
|
|
38
|
+
if (params) {
|
|
39
|
+
this.params.push(...params);
|
|
40
|
+
}
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
build() {
|
|
44
|
+
const result = { sql: this.sql.trim(), params: [...this.params] };
|
|
45
|
+
this.sql = '';
|
|
46
|
+
this.params = [];
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//# sourceMappingURL=query-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-builder.js","sourceRoot":"","sources":["../src/query-builder.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,YAAY;IAAzB;QACU,QAAG,GAAW,EAAE,CAAC;QACjB,WAAM,GAAU,EAAE,CAAC;IAkD7B,CAAC;IAhDC,MAAM,CAAC,MAAyB,EAAE,UAAyB,EAAE;QAC3D,IAAI,OAAO,CAAC,EAAE,EAAE,CAAC;YACf,IAAI,CAAC,GAAG,GAAG,UAAU,OAAO,CAAC,EAAE,IAAI,MAAM,IAAI,CAAC;QAChD,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,GAAG,GAAG,UAAU,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAA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CAAC;IACd,CAAC;IAED,KAAK;QACH,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAClE,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACd,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { DatabaseQuery, IDatabase, IDatabaseSession } from './interfaces.js';
|
|
2
|
+
export declare class SQLiteDB implements IDatabase {
|
|
3
|
+
private db;
|
|
4
|
+
private queue;
|
|
5
|
+
constructor(path: string);
|
|
6
|
+
get({ sql, params }: DatabaseQuery): Promise<any>;
|
|
7
|
+
all({ sql, params }: DatabaseQuery): Promise<any>;
|
|
8
|
+
run({ sql, params }: DatabaseQuery): Promise<void>;
|
|
9
|
+
transaction<T>(work: (transaction: IDatabaseSession) => Promise<T>): Promise<T>;
|
|
10
|
+
close(): Promise<void>;
|
|
11
|
+
private getRaw;
|
|
12
|
+
private allRaw;
|
|
13
|
+
private runRaw;
|
|
14
|
+
private enqueue;
|
|
15
|
+
}
|
package/lib/sqlite-db.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import sqlite3 from 'sqlite3';
|
|
2
|
+
export class SQLiteDB {
|
|
3
|
+
constructor(path) {
|
|
4
|
+
this.queue = Promise.resolve();
|
|
5
|
+
this.db = new sqlite3.Database(path, err => {
|
|
6
|
+
if (err) {
|
|
7
|
+
console.error('Could not connect to database', err);
|
|
8
|
+
}
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
get({ sql, params }) {
|
|
12
|
+
return this.enqueue(() => this.getRaw({ sql, params }));
|
|
13
|
+
}
|
|
14
|
+
all({ sql, params }) {
|
|
15
|
+
return this.enqueue(() => this.allRaw({ sql, params }));
|
|
16
|
+
}
|
|
17
|
+
run({ sql, params }) {
|
|
18
|
+
return this.enqueue(() => this.runRaw({ sql, params }));
|
|
19
|
+
}
|
|
20
|
+
transaction(work) {
|
|
21
|
+
return this.enqueue(async () => {
|
|
22
|
+
await this.runRaw({ sql: 'BEGIN IMMEDIATE;', params: [] });
|
|
23
|
+
const transaction = {
|
|
24
|
+
get: query => this.getRaw(query),
|
|
25
|
+
all: query => this.allRaw(query),
|
|
26
|
+
run: query => this.runRaw(query),
|
|
27
|
+
};
|
|
28
|
+
try {
|
|
29
|
+
const result = await work(transaction);
|
|
30
|
+
await this.runRaw({ sql: 'COMMIT;', params: [] });
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
await this.runRaw({ sql: 'ROLLBACK;', params: [] });
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
close() {
|
|
40
|
+
return this.enqueue(() => new Promise((resolve, reject) => {
|
|
41
|
+
this.db.close(err => {
|
|
42
|
+
if (err) {
|
|
43
|
+
reject(err);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
resolve();
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
getRaw({ sql, params }) {
|
|
52
|
+
return new Promise((resolve, reject) => {
|
|
53
|
+
this.db.get(sql, params, (err, result) => {
|
|
54
|
+
if (err) {
|
|
55
|
+
reject(err);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
resolve(result);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
allRaw({ sql, params }) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
this.db.all(sql, params, (err, result) => {
|
|
66
|
+
if (err) {
|
|
67
|
+
reject(err);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
resolve(result);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
runRaw({ sql, params }) {
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
this.db.run(sql, params, function (err) {
|
|
78
|
+
if (err) {
|
|
79
|
+
reject(err);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
resolve();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
enqueue(operation) {
|
|
88
|
+
const result = this.queue.then(operation, operation);
|
|
89
|
+
this.queue = result.then(() => undefined, () => undefined);
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=sqlite-db.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-db.js","sourceRoot":"","sources":["../src/sqlite-db.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAG9B,MAAM,OAAO,QAAQ;IAInB,YAAY,IAAY;QAFhB,UAAK,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;QAG/C,IAAI,CAAC,EAAE,GAAG,IAAI,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE;YACzC,IAAI,GAAG,EAAE,CAAC;gBACR,OAAO,CAAC,KAAK,CAAC,+BAA+B,EAAE,GAAG,CAAC,CAAC;YACtD,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,GAAG,CAAC,EAAE,GAAG,EAAE,MAAM,EAAiB;QACvC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;IAC1D,CAAC;IAEM,GAAG,CAAC,EAAE,GAAG,EAAE,MAAM,EAAiB;QACvC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAA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IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,GAAG;gBACpC,IAAI,GAAG,EAAE,CAAC;oBACR,MAAM,CAAC,GAAG,CAAC,CAAC;gBACd,CAAC;qBAAM,CAAC;oBACN,OAAO,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;IAEO,OAAO,CAAI,SAA2B;QAC5C,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;QACrD,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAC3D,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DatabaseDriver } from './database-driver.js';
|
|
2
|
+
import type { IDatabaseConfig } from './interfaces.js';
|
|
3
|
+
import { SQLiteDB } from './sqlite-db.js';
|
|
4
|
+
export interface ISQLiteConfig extends IDatabaseConfig {
|
|
5
|
+
filename: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class SQLiteDriver extends DatabaseDriver<ISQLiteConfig> {
|
|
8
|
+
private db?;
|
|
9
|
+
constructor(config: ISQLiteConfig);
|
|
10
|
+
connect(): Promise<void>;
|
|
11
|
+
disconnect(): Promise<void>;
|
|
12
|
+
get connection(): SQLiteDB | undefined;
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DatabaseDriver } from './database-driver.js';
|
|
2
|
+
import { SQLiteDB } from './sqlite-db.js';
|
|
3
|
+
export class SQLiteDriver extends DatabaseDriver {
|
|
4
|
+
constructor(config) {
|
|
5
|
+
super(config);
|
|
6
|
+
}
|
|
7
|
+
async connect() {
|
|
8
|
+
this.db = await new Promise((resolve, reject) => {
|
|
9
|
+
try {
|
|
10
|
+
const db = new SQLiteDB(this.config.filename);
|
|
11
|
+
return resolve(db);
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
return reject(err);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async disconnect() {
|
|
19
|
+
await new Promise((resolve, reject) => {
|
|
20
|
+
if (this.db) {
|
|
21
|
+
try {
|
|
22
|
+
this.db.close();
|
|
23
|
+
this.db = undefined;
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
return reject(err);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return resolve(null);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
get connection() {
|
|
33
|
+
return this.db;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=sqlite-driver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-driver.js","sourceRoot":"","sources":["../src/sqlite-driver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAM1C,MAAM,OAAO,YAAa,SAAQ,cAA6B;IAG7D,YAAY,MAAqB;QAC/B,KAAK,CAAC,MAAM,CAAC,CAAC;IAChB,CAAC;IAED,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,EAAE,GAAG,MAAM,IAAI,OAAO,CAAW,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACxD,IAAI,CAAC;gBACH,MAAM,EAAE,GAAG,IAAI,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;gBAC9C,OAAO,OAAO,CAAC,EAAE,CAAC,CAAC;YACrB,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACb,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;YACrB,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACpC,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC;gBACZ,IAAI,CAAC;oBACH,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC;oBAChB,IAAI,CAAC,EAAE,GAAG,SAAS,CAAC;gBACtB,CAAC;gBAAC,OAAO,GAAG,EAAE,CAAC;oBACb,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;gBACrB,CAAC;YACH,CAAC;YACD,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;IAED,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,EAAE,CAAC;IACjB,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kurdel/db",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "Database abstractions and SQLite adapter for Kurdel",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./lib/index.js"
|
|
8
|
+
},
|
|
9
|
+
"main": "./lib/index.js",
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"files": [
|
|
12
|
+
"lib"
|
|
13
|
+
],
|
|
14
|
+
"scripts": {
|
|
15
|
+
"prepack": "npm run build",
|
|
16
|
+
"test": "vitest run",
|
|
17
|
+
"clean": "rimraf lib ../../.cache/tsconfig.db.build.tsbuildinfo",
|
|
18
|
+
"build": "tsc -p tsconfig.build.json && tsc-alias -p tsconfig.build.json",
|
|
19
|
+
"build:force": "npm run clean && tsc -p tsconfig.build.json --force && tsc-alias -p tsconfig.build.json"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"kurdel",
|
|
23
|
+
"database",
|
|
24
|
+
"sqlite",
|
|
25
|
+
"framework"
|
|
26
|
+
],
|
|
27
|
+
"author": "Andrii Sorokin",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": "^20.19.0 || ^22.12.0 || >=24.0.0"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/ignorantic/kurdel.git",
|
|
35
|
+
"directory": "packages/db"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/ignorantic/kurdel/tree/main/packages/db#readme",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/ignorantic/kurdel/issues"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"tag": "beta"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@types/node": "^20.9.0",
|
|
47
|
+
"rimraf": "^3.0.2",
|
|
48
|
+
"tsc-alias": "^1.8.16"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"sqlite3": "^5.1.6"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@kurdel/common": "0.1.0-beta.1"
|
|
55
|
+
}
|
|
56
|
+
}
|