@storecraft/database-sql-base 1.0.16 → 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.
@@ -16,7 +16,17 @@ import {
16
16
  */
17
17
 
18
18
  /**
19
- * @description Official Storecraft Cloudflare D1 adapter on Worker
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
@@ -37,25 +37,26 @@ export const get_migrations = async (dialect_type='SQLITE') => {
37
37
 
38
38
  for (const file of files) {
39
39
  if(file.endsWith('.js')) {
40
+ const file_name = file.split('.').slice(0, -1).join('.');
40
41
  const migration = await import(path.join(__dirname, folder, file));
41
- migrations[file] = migration;
42
+ migrations[file_name] = migration;
42
43
  }
43
44
  }
44
45
 
45
46
  return migrations;
46
47
  }
47
48
 
48
- console.log(
49
- await get_migrations()
50
- )
49
+ // console.log(
50
+ // await get_migrations()
51
+ // )
51
52
 
52
53
 
53
54
  /**
54
55
  *
55
56
  * @param {SQL} db_driver
56
- * @param {boolean} [destroy_db_upon_completion=true]
57
+ * @param {boolean} [release_db_upon_completion=true]
57
58
  */
58
- export async function migrateToLatest(db_driver, destroy_db_upon_completion=true) {
59
+ export async function migrateToLatest(db_driver, release_db_upon_completion=true) {
59
60
  if(!db_driver?.client)
60
61
  throw new Error('No Kysely client found !!!');
61
62
 
@@ -103,6 +104,41 @@ export async function migrateToLatest(db_driver, destroy_db_upon_completion=true
103
104
  process.exit(1)
104
105
  }
105
106
 
106
- if(destroy_db_upon_completion)
107
+ if(release_db_upon_completion)
107
108
  await db.destroy();
108
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
+
@@ -9,18 +9,15 @@ import { Kysely } from 'kysely'
9
9
  */
10
10
  export async function up(db) {
11
11
 
12
- try {
13
- await db.schema
14
- .alterTable('auth_users')
15
- .addColumn('firstname', 'text')
16
- .execute();
17
-
18
- await db.schema
19
- .alterTable('auth_users')
20
- .addColumn('lastname', 'text')
21
- .execute();
22
- } catch (e) {
23
- }
12
+ await db.schema
13
+ .alterTable('auth_users')
14
+ .addColumn('firstname', 'text')
15
+ .execute();
16
+
17
+ await db.schema
18
+ .alterTable('auth_users')
19
+ .addColumn('lastname', 'text')
20
+ .execute();
24
21
  }
25
22
 
26
23
  /**
@@ -28,16 +25,13 @@ export async function up(db) {
28
25
  * @param {Kysely<Database>} db
29
26
  */
30
27
  export async function down(db) {
31
- try {
32
- await db.schema
33
- .alterTable('auth_users')
34
- .dropColumn('firstname')
35
- .execute();
36
-
37
- await db.schema
38
- .alterTable('auth_users')
39
- .dropColumn('lastname')
40
- .execute();
41
- } catch (e) {
42
- }
28
+ await db.schema
29
+ .alterTable('auth_users')
30
+ .dropColumn('firstname')
31
+ .execute();
32
+
33
+ await db.schema
34
+ .alterTable('auth_users')
35
+ .dropColumn('lastname')
36
+ .execute();
43
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/database-sql-base",
3
- "version": "1.0.16",
3
+ "version": "1.0.18",
4
4
  "description": "Official SQL Database driver for storecraft",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
@@ -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(join(homedir(), 'db.sqlite')),
13
+ database: async () => new SQLite(':memory:'),
14
14
  });
15
15
 
16
16
  export const create_app = async () => {
@@ -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();