@storecraft/database-sql-base 1.0.14 → 1.0.15

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/migrate.js CHANGED
@@ -23,7 +23,7 @@ export async function migrateToLatest(db_driver, destroy_db_upon_completion=true
23
23
  if(!db_driver?.client)
24
24
  throw new Error('No Kysely client found !!!');
25
25
 
26
- console.log('Resolving migrations')
26
+ console.log('Resolving migrations. This may take 2 minutes ...')
27
27
 
28
28
  let db = db_driver.client;
29
29
 
@@ -63,7 +63,7 @@ export async function migrateToLatest(db_driver, destroy_db_upon_completion=true
63
63
 
64
64
  if (error) {
65
65
  console.error('failed to migrate')
66
- console.error(error)
66
+ console.error(JSON.stringify(error, null, 2))
67
67
  process.exit(1)
68
68
  }
69
69
 
@@ -1,10 +1,10 @@
1
+ /**
2
+ * @import { Database } from '../types.sql.tables.js'
3
+ */
1
4
  import { Kysely } from 'kysely'
2
5
  import { upsert } from '../src/con.templates.js'
3
6
  import { templates } from '@storecraft/core/assets/seed-templates.js';
4
7
 
5
- /**
6
- * @typedef {import('../types.sql.tables.js').Database} Database
7
- */
8
8
 
9
9
  /**
10
10
  *
@@ -9,15 +9,18 @@ import { Kysely } from 'kysely'
9
9
  */
10
10
  export async function up(db) {
11
11
 
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();
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
+ }
21
24
  }
22
25
 
23
26
  /**
@@ -25,13 +28,16 @@ export async function up(db) {
25
28
  * @param {Kysely<Database>} db
26
29
  */
27
30
  export async function down(db) {
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();
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
+ }
37
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storecraft/database-sql-base",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "Official SQL Database driver for storecraft",
5
5
  "license": "MIT",
6
6
  "author": "Tomer Shalev (https://github.com/store-craft)",
@@ -18,12 +18,23 @@ export const table_name = 'templates'
18
18
  * otherwise, just return the original value.
19
19
  * @param {string} val
20
20
  */
21
- const decode_if_base64 = val => {
21
+ const encode_base64_if_needed = val => {
22
+ if(val.startsWith('base64_'))
23
+ return val;
24
+
25
+ return 'base64_' + base64.encode(val);
26
+ }
27
+
28
+ /**
29
+ * @description if `base64_` prefixed then decode the postfix and return it,
30
+ * otherwise, just return the original value.
31
+ * @param {string} val
32
+ */
33
+ const decode_base64_if_needed = val => {
22
34
  if(!val.startsWith('base64_'))
23
35
  return val;
24
36
 
25
- const b64 = val.split('base64_').at(1) ?? '';
26
- return base64.decode(b64);
37
+ return base64.decode(val.split('base64_').at(-1) ?? '');
27
38
  }
28
39
 
29
40
  /**
@@ -45,8 +56,8 @@ export const upsert = (client) => {
45
56
  active: item.active ? 1 : 0,
46
57
  title: item.title,
47
58
  handle: item.handle,
48
- template_html: decode_if_base64(item.template_html),
49
- template_text: decode_if_base64(item.template_text),
59
+ template_html: encode_base64_if_needed(item.template_html),
60
+ template_text: encode_base64_if_needed(item.template_text),
50
61
  reference_example_input: JSON.stringify(item.reference_example_input ?? {})
51
62
  });
52
63
  }
@@ -78,7 +89,17 @@ const get = (driver) => {
78
89
  )
79
90
  .where(where_id_or_handle_table(id_or_handle))
80
91
  .executeTakeFirst()
81
- .then(sanitize);
92
+ .then(sanitize)
93
+ .then(
94
+ (item) => {
95
+ if(!item) return null;
96
+
97
+ item.template_html = decode_base64_if_needed(item.template_html);
98
+ item.template_text = decode_base64_if_needed(item.template_text);
99
+
100
+ return item;
101
+ }
102
+ )
82
103
  }
83
104
  }
84
105
 
@@ -134,7 +155,18 @@ const list = (driver) => {
134
155
  )
135
156
  .orderBy(query_to_sort(query, table_name))
136
157
  .limit(query.limitToLast ?? query.limit ?? 10)
137
- .execute();
158
+ .execute()
159
+ .then(
160
+ (items) => {
161
+ return items.map(
162
+ (item) => {
163
+ item.template_html = decode_base64_if_needed(item.template_html);
164
+ item.template_text = decode_base64_if_needed(item.template_text);
165
+ return item;
166
+ }
167
+ )
168
+ }
169
+ );
138
170
 
139
171
  if(query.limitToLast) items.reverse();
140
172