@storecraft/database-sql-base 1.0.17 → 1.0.18
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/kysely.aggregate.dialect.js +11 -1
- package/migrate.js +41 -6
- package/package.json +1 -1
- package/tests/runner.sqlite-local.test.js +1 -1
- package/tests/aggregate.js +0 -38
@@ -16,7 +16,17 @@ import {
|
|
16
16
|
*/
|
17
17
|
|
18
18
|
/**
|
19
|
-
* @description
|
19
|
+
* @description A special dialect that aggregates / records queries.
|
20
|
+
* This is useful for
|
21
|
+
* - testing / debugging
|
22
|
+
* - generating migration files for cloud based databases
|
23
|
+
* such as `libsql` / `turso` / `d1`
|
24
|
+
*
|
25
|
+
* NOTE:
|
26
|
+
* - This dialect does not support transactions
|
27
|
+
* - This is only useful for non-interactive queries, which are
|
28
|
+
* basically transactions, that do not depend on results of previous queries.
|
29
|
+
* Which is the philosophy of `storecraft` and is quite common in migrations.
|
20
30
|
*
|
21
31
|
* @implements {Dialect}
|
22
32
|
*/
|
package/migrate.js
CHANGED
@@ -46,17 +46,17 @@ export const get_migrations = async (dialect_type='SQLITE') => {
|
|
46
46
|
return migrations;
|
47
47
|
}
|
48
48
|
|
49
|
-
console.log(
|
50
|
-
|
51
|
-
)
|
49
|
+
// console.log(
|
50
|
+
// await get_migrations()
|
51
|
+
// )
|
52
52
|
|
53
53
|
|
54
54
|
/**
|
55
55
|
*
|
56
56
|
* @param {SQL} db_driver
|
57
|
-
* @param {boolean} [
|
57
|
+
* @param {boolean} [release_db_upon_completion=true]
|
58
58
|
*/
|
59
|
-
export async function migrateToLatest(db_driver,
|
59
|
+
export async function migrateToLatest(db_driver, release_db_upon_completion=true) {
|
60
60
|
if(!db_driver?.client)
|
61
61
|
throw new Error('No Kysely client found !!!');
|
62
62
|
|
@@ -104,6 +104,41 @@ export async function migrateToLatest(db_driver, destroy_db_upon_completion=true
|
|
104
104
|
process.exit(1)
|
105
105
|
}
|
106
106
|
|
107
|
-
if(
|
107
|
+
if(release_db_upon_completion)
|
108
108
|
await db.destroy();
|
109
109
|
}
|
110
|
+
|
111
|
+
|
112
|
+
/**
|
113
|
+
* @description Just for education and debugging, do not use !!!
|
114
|
+
* @param {string} stmt
|
115
|
+
* @param {any[] | Record<string, any>} params
|
116
|
+
*/
|
117
|
+
export const prepare_and_bind = (stmt='', params=[]) => {
|
118
|
+
const params_object = Array.isArray(params) ?
|
119
|
+
params.reduce((a, v, idx) => ({ ...a, [idx+1]: v}), {}) :
|
120
|
+
params;
|
121
|
+
|
122
|
+
let current = 0;
|
123
|
+
let result = ''
|
124
|
+
let index_run = 1;
|
125
|
+
for (let m of stmt.matchAll(/\?[0-9]*/g)) {
|
126
|
+
result += stmt.slice(current, m.index);
|
127
|
+
|
128
|
+
const match_string = m[0];
|
129
|
+
let index_access = match_string.length > 1 ?
|
130
|
+
Number(match_string.slice(1)) :
|
131
|
+
index_run;
|
132
|
+
|
133
|
+
result += "'" + params_object[index_access] + "'";
|
134
|
+
|
135
|
+
current = m.index + m[0].length;
|
136
|
+
index_run+=1;
|
137
|
+
}
|
138
|
+
|
139
|
+
result += stmt.slice(current);
|
140
|
+
|
141
|
+
return result;
|
142
|
+
}
|
143
|
+
|
144
|
+
|
package/package.json
CHANGED
@@ -10,7 +10,7 @@ import { homedir } from 'node:os';
|
|
10
10
|
import { join } from 'node:path';
|
11
11
|
|
12
12
|
export const sqlite_dialect = new SqliteDialect({
|
13
|
-
database: async () => new SQLite(
|
13
|
+
database: async () => new SQLite(':memory:'),
|
14
14
|
});
|
15
15
|
|
16
16
|
export const create_app = async () => {
|
package/tests/aggregate.js
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
import 'dotenv/config';
|
2
|
-
import { App } from '@storecraft/core';
|
3
|
-
import { SQL } from '@storecraft/database-sql-base';
|
4
|
-
import { migrateToLatest } from '@storecraft/database-sql-base/migrate.js';
|
5
|
-
import { NodePlatform } from '@storecraft/core/platform/node';
|
6
|
-
import { api } from '@storecraft/core/test-runner'
|
7
|
-
import SQLite from 'better-sqlite3'
|
8
|
-
import { SqliteDialect } from 'kysely';
|
9
|
-
import { homedir } from 'node:os';
|
10
|
-
import { join } from 'node:path';
|
11
|
-
import { up } from '../migrations.sqlite/00000_init_tables.js'
|
12
|
-
import { AggregateDialect } from '../kysely.aggregate.dialect.js'
|
13
|
-
|
14
|
-
export const sqlite_dialect = new SqliteDialect(
|
15
|
-
{
|
16
|
-
database: async () => new SQLite(join(homedir(), 'db.sqlite')),
|
17
|
-
}
|
18
|
-
);
|
19
|
-
|
20
|
-
export const test = async () => {
|
21
|
-
const aggregate_dialect = new AggregateDialect(
|
22
|
-
{
|
23
|
-
dialect: sqlite_dialect,
|
24
|
-
}
|
25
|
-
);
|
26
|
-
|
27
|
-
const db = new SQL({
|
28
|
-
dialect: aggregate_dialect,
|
29
|
-
dialect_type: 'SQLITE'
|
30
|
-
});
|
31
|
-
|
32
|
-
await up(db.client);
|
33
|
-
|
34
|
-
const queries = aggregate_dialect.queries;
|
35
|
-
console.log({queries})
|
36
|
-
|
37
|
-
}
|
38
|
-
test();
|