@storecraft/database-sql-base 1.0.0 → 1.0.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/README.md +30 -9
- package/driver.js +4 -20
- package/index.js +1 -1
- package/migrate.js +25 -19
- package/migrations.mysql/00000_init_tables.js +15 -14
- package/migrations.postgres/00000_init_tables.js +15 -14
- package/migrations.shared/00001_seed_email_templates copy.js +262 -0
- package/migrations.shared/00001_seed_email_templates.js +3 -1
- package/migrations.sqlite/00000_init_tables.js +12 -12
- package/migrations.sqlite/00002_test.js +26 -0
- package/package.json +2 -2
- package/src/con.auth_users.js +1 -0
- package/src/con.helpers.json.js +3 -3
- package/src/con.orders.js +17 -2
- package/src/con.shared.experiment.js +723 -0
- package/src/con.shared.js +12 -12
- package/tests/runner.mssql-local.test.js +3 -3
- package/tests/runner.mysql-local.test.js +13 -11
- package/tests/runner.postgres-local.test.js +14 -10
- package/tests/runner.sqlite-local.test.js +12 -10
- package/tests/sandbox.test.js +15 -13
- package/types.public.d.ts +12 -4
package/README.md
CHANGED
@@ -1,7 +1,16 @@
|
|
1
1
|
# Storecraft `SQL` driver
|
2
2
|
|
3
|
+
<div style="text-align:center">
|
4
|
+
<img src='https://storecraft.app/storecraft-color.svg'
|
5
|
+
width='90%'' />
|
6
|
+
</div><hr/><br/>
|
7
|
+
|
3
8
|
Official `SQL` driver for `StoreCraft` with the dialects abstracted with `Kysely` or your own drivers.
|
4
9
|
|
10
|
+
```bash
|
11
|
+
npm i @storecraft/database-sql-base
|
12
|
+
```
|
13
|
+
|
5
14
|
## usage
|
6
15
|
|
7
16
|
```js
|
@@ -11,17 +20,29 @@ import { join } from "node:path";
|
|
11
20
|
import { homedir } from "node:os";
|
12
21
|
|
13
22
|
import { App } from '@storecraft/core'
|
14
|
-
import { NodePlatform } from '@storecraft/
|
15
|
-
import {
|
16
|
-
import {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
import { NodePlatform } from '@storecraft/platforms/node';
|
24
|
+
import { SQL } from '@storecraft/database-sql-base'
|
25
|
+
import { migrateToLatest } from '@storecraft/database-sql-base/migrate.js'
|
26
|
+
import { NodeLocalStorage } from '@storecraft/storage-local/node'
|
27
|
+
|
28
|
+
const app = new App(
|
29
|
+
{
|
30
|
+
auth_admins_emails: ['admin@sc.com'],
|
31
|
+
auth_secret_access_token: 'auth_secret_access_token',
|
32
|
+
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
33
|
+
}
|
34
|
+
)
|
35
|
+
.withPlatform(new NodePlatform())
|
36
|
+
.withDatabase(
|
37
|
+
new SQL({
|
38
|
+
dialect: sqlite_dialect,
|
39
|
+
dialect_type: 'SQLITE'
|
40
|
+
})
|
41
|
+
)
|
42
|
+
.withStorage(new NodeLocalStorage(join(homedir(), 'tomer')))
|
23
43
|
|
24
44
|
await app.init();
|
45
|
+
await migrateToLatest(app.db, false);
|
25
46
|
|
26
47
|
const server = http.createServer(app.handler).listen(
|
27
48
|
8000,
|
package/driver.js
CHANGED
@@ -26,8 +26,8 @@ const assert = (b, msg) => {
|
|
26
26
|
}
|
27
27
|
|
28
28
|
/**
|
29
|
-
* @typedef {import('./types.public.
|
30
|
-
* @typedef {import('./types.sql.tables.
|
29
|
+
* @typedef {import('./types.public.d.ts').Config} Config
|
30
|
+
* @typedef {import('./types.sql.tables.d.ts').Database} Database
|
31
31
|
* @typedef {import('kysely').Dialect} Dialect
|
32
32
|
* @typedef {import('@storecraft/core/v-database').db_driver} db_driver
|
33
33
|
*/
|
@@ -88,18 +88,6 @@ export class SQL {
|
|
88
88
|
);
|
89
89
|
}
|
90
90
|
|
91
|
-
/**
|
92
|
-
*
|
93
|
-
* @param {boolean} [destroy_db_upon_completion=false]
|
94
|
-
*/
|
95
|
-
async migrateToLatest(destroy_db_upon_completion=false) {
|
96
|
-
this.throwIfNotReady();
|
97
|
-
|
98
|
-
const { migrateToLatest } = await import('./migrate.js');
|
99
|
-
|
100
|
-
await migrateToLatest(this, destroy_db_upon_completion);
|
101
|
-
};
|
102
|
-
|
103
91
|
/**
|
104
92
|
*
|
105
93
|
* @param {App<any, any, any>} app
|
@@ -147,8 +135,8 @@ export class SQL {
|
|
147
135
|
return this.#_resources;
|
148
136
|
}
|
149
137
|
|
150
|
-
get name
|
151
|
-
return 'main';
|
138
|
+
get name() {
|
139
|
+
return this?.config?.db_name ?? 'main';
|
152
140
|
}
|
153
141
|
|
154
142
|
get app() {
|
@@ -183,8 +171,4 @@ export class SQL {
|
|
183
171
|
return this.dialectType==='MYSQL';
|
184
172
|
}
|
185
173
|
|
186
|
-
get isMssql() {
|
187
|
-
return this.dialectType==='MSSQL';
|
188
|
-
}
|
189
|
-
|
190
174
|
}
|
package/index.js
CHANGED
package/migrate.js
CHANGED
@@ -29,31 +29,37 @@ export async function migrateToLatest(db_driver, destroy_db_upon_completion=true
|
|
29
29
|
|
30
30
|
current.driver = db_driver;
|
31
31
|
|
32
|
-
const migrator = new Migrator(
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
const migrator = new Migrator(
|
33
|
+
{
|
34
|
+
db,
|
35
|
+
provider: new FileMigrationProvider(
|
36
|
+
{
|
37
|
+
fs,
|
38
|
+
path,
|
39
|
+
migrationFolder: path.join(
|
40
|
+
__dirname,
|
41
|
+
`migrations.${db_driver.dialectType.toLowerCase()}`
|
42
|
+
),
|
43
|
+
}
|
40
44
|
),
|
41
|
-
}
|
42
|
-
|
45
|
+
}
|
46
|
+
);
|
43
47
|
|
44
48
|
const { error, results } = await migrator.migrateToLatest();
|
45
49
|
|
46
|
-
results?.forEach(
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
+
results?.forEach(
|
51
|
+
(it) => {
|
52
|
+
if (it.status === 'Success') {
|
53
|
+
console.log(
|
54
|
+
`migration "${it.migrationName}" was executed successfully`
|
50
55
|
);
|
51
|
-
|
52
|
-
|
53
|
-
|
56
|
+
} else if (it.status === 'Error') {
|
57
|
+
console.error(
|
58
|
+
`failed to execute migration "${it.migrationName}"`
|
54
59
|
);
|
60
|
+
}
|
55
61
|
}
|
56
|
-
|
62
|
+
);
|
57
63
|
|
58
64
|
if (error) {
|
59
65
|
console.error('failed to migrate')
|
@@ -62,5 +68,5 @@ export async function migrateToLatest(db_driver, destroy_db_upon_completion=true
|
|
62
68
|
}
|
63
69
|
|
64
70
|
if(destroy_db_upon_completion)
|
65
|
-
await db.destroy()
|
71
|
+
await db.destroy();
|
66
72
|
}
|
@@ -30,7 +30,7 @@ const add_base_columns = tb => {
|
|
30
30
|
const create_entity_to_value_table = (db, table_name) => {
|
31
31
|
|
32
32
|
return db.schema
|
33
|
-
.createTable(table_name)
|
33
|
+
.createTable(table_name).ifNotExists()
|
34
34
|
.addColumn('id', 'integer',
|
35
35
|
(col) => col.autoIncrement().primaryKey()
|
36
36
|
)
|
@@ -47,7 +47,7 @@ const create_entity_to_value_table = (db, table_name) => {
|
|
47
47
|
* @param {keyof Database} table_name
|
48
48
|
*/
|
49
49
|
const create_safe_table = (db, table_name) => {
|
50
|
-
return db.schema.createTable(table_name);
|
50
|
+
return db.schema.createTable(table_name).ifNotExists();
|
51
51
|
}
|
52
52
|
|
53
53
|
/**
|
@@ -56,7 +56,7 @@ const create_safe_table = (db, table_name) => {
|
|
56
56
|
* @param {keyof Database} table_name
|
57
57
|
*/
|
58
58
|
const drop_safe_table = (db, table_name) => {
|
59
|
-
return db.schema.dropTable(table_name).execute();
|
59
|
+
return db.schema.dropTable(table_name).ifExists().execute();
|
60
60
|
}
|
61
61
|
|
62
62
|
/**
|
@@ -67,22 +67,22 @@ const drop_safe_table = (db, table_name) => {
|
|
67
67
|
*/
|
68
68
|
const create_base_indexes = async (db, table_name, include_id=true, include_handle=true) => {
|
69
69
|
if(include_id) {
|
70
|
-
await db.schema.createIndex(`index_${table_name}_id_updated_at_asc`)
|
70
|
+
await db.schema.createIndex(`index_${table_name}_id_updated_at_asc`).ifNotExists()
|
71
71
|
.on(table_name)
|
72
72
|
.columns(['id', 'updated_at asc'])
|
73
73
|
.execute();
|
74
|
-
await db.schema.createIndex(`index_${table_name}_id_updated_at_desc`)
|
74
|
+
await db.schema.createIndex(`index_${table_name}_id_updated_at_desc`).ifNotExists()
|
75
75
|
.on(table_name)
|
76
76
|
.columns(['id', 'updated_at desc'])
|
77
77
|
.execute();
|
78
78
|
}
|
79
79
|
|
80
80
|
if(include_handle) {
|
81
|
-
await db.schema.createIndex(`index_${table_name}_handle_updated_at_asc`)
|
81
|
+
await db.schema.createIndex(`index_${table_name}_handle_updated_at_asc`).ifNotExists()
|
82
82
|
.on(table_name)
|
83
83
|
.columns(['handle', 'updated_at asc'])
|
84
84
|
.execute();
|
85
|
-
await db.schema.createIndex(`index_${table_name}_handle_updated_at_desc`)
|
85
|
+
await db.schema.createIndex(`index_${table_name}_handle_updated_at_desc`).ifNotExists()
|
86
86
|
.on(table_name)
|
87
87
|
.columns(['handle', 'updated_at desc'])
|
88
88
|
.execute();
|
@@ -94,26 +94,26 @@ const create_base_indexes = async (db, table_name, include_id=true, include_hand
|
|
94
94
|
* @param {keyof Pick<Database, 'entity_to_media' |
|
95
95
|
* 'entity_to_search_terms' | 'entity_to_tags_projections' |
|
96
96
|
* 'products_to_collections' | 'products_to_discounts' |
|
97
|
-
* 'products_to_variants' | 'storefronts_to_other'>} table_name
|
97
|
+
* 'products_to_variants' | 'storefronts_to_other' | 'products_to_related_products'>} table_name
|
98
98
|
*/
|
99
99
|
const create_entity_table_indexes = async (db, table_name) => {
|
100
|
-
await db.schema.createIndex(`index_${table_name}_entity_id`)
|
100
|
+
await db.schema.createIndex(`index_${table_name}_entity_id`).ifNotExists()
|
101
101
|
.on(table_name)
|
102
102
|
.column('entity_id')
|
103
103
|
.execute();
|
104
|
-
await db.schema.createIndex(`index_${table_name}_entity_handle`)
|
104
|
+
await db.schema.createIndex(`index_${table_name}_entity_handle`).ifNotExists()
|
105
105
|
.on(table_name)
|
106
106
|
.column('entity_handle')
|
107
107
|
.execute();
|
108
|
-
await db.schema.createIndex(`index_${table_name}_value`)
|
108
|
+
await db.schema.createIndex(`index_${table_name}_value`).ifNotExists()
|
109
109
|
.on(table_name)
|
110
110
|
.column('value')
|
111
111
|
.execute();
|
112
|
-
await db.schema.createIndex(`index_${table_name}_reporter`)
|
112
|
+
await db.schema.createIndex(`index_${table_name}_reporter`).ifNotExists()
|
113
113
|
.on(table_name)
|
114
114
|
.column('reporter')
|
115
115
|
.execute();
|
116
|
-
await db.schema.createIndex(`index_${table_name}_context`)
|
116
|
+
await db.schema.createIndex(`index_${table_name}_context`).ifNotExists()
|
117
117
|
.on(table_name)
|
118
118
|
.column('context')
|
119
119
|
.execute();
|
@@ -150,7 +150,8 @@ export async function up(db) {
|
|
150
150
|
let tb = create_safe_table(db, 'templates');
|
151
151
|
tb = add_base_columns(tb);
|
152
152
|
tb = tb.addColumn('title', 'text');
|
153
|
-
tb = tb.addColumn('
|
153
|
+
tb = tb.addColumn('template_html', 'text');
|
154
|
+
tb = tb.addColumn('template_text', 'text');
|
154
155
|
tb = tb.addColumn('reference_example_input', 'json');
|
155
156
|
await tb.execute();
|
156
157
|
await create_base_indexes(db, 'templates');
|
@@ -29,7 +29,7 @@ const add_base_columns = tb => {
|
|
29
29
|
const create_entity_to_value_table = (db, table_name) => {
|
30
30
|
|
31
31
|
return db.schema
|
32
|
-
.createTable(table_name)
|
32
|
+
.createTable(table_name).ifNotExists()
|
33
33
|
.addColumn('id', 'bigserial',
|
34
34
|
(col) => col.primaryKey()
|
35
35
|
)
|
@@ -46,7 +46,7 @@ const create_entity_to_value_table = (db, table_name) => {
|
|
46
46
|
* @param {keyof Database} table_name
|
47
47
|
*/
|
48
48
|
const create_safe_table = (db, table_name) => {
|
49
|
-
return db.schema.createTable(table_name);
|
49
|
+
return db.schema.createTable(table_name).ifNotExists();
|
50
50
|
}
|
51
51
|
|
52
52
|
/**
|
@@ -55,7 +55,7 @@ const create_safe_table = (db, table_name) => {
|
|
55
55
|
* @param {keyof Database} table_name
|
56
56
|
*/
|
57
57
|
const drop_safe_table = (db, table_name) => {
|
58
|
-
return db.schema.dropTable(table_name).execute();
|
58
|
+
return db.schema.dropTable(table_name).ifExists().execute();
|
59
59
|
}
|
60
60
|
|
61
61
|
/**
|
@@ -66,22 +66,22 @@ const drop_safe_table = (db, table_name) => {
|
|
66
66
|
*/
|
67
67
|
const create_base_indexes = async (db, table_name, include_id=true, include_handle=true) => {
|
68
68
|
if(include_id) {
|
69
|
-
await db.schema.createIndex(`index_${table_name}_id_updated_at_asc`)
|
69
|
+
await db.schema.createIndex(`index_${table_name}_id_updated_at_asc`).ifNotExists()
|
70
70
|
.on(table_name)
|
71
71
|
.columns(['id', 'updated_at asc'])
|
72
72
|
.execute();
|
73
|
-
await db.schema.createIndex(`index_${table_name}_id_updated_at_desc`)
|
73
|
+
await db.schema.createIndex(`index_${table_name}_id_updated_at_desc`).ifNotExists()
|
74
74
|
.on(table_name)
|
75
75
|
.columns(['id', 'updated_at desc'])
|
76
76
|
.execute();
|
77
77
|
}
|
78
78
|
|
79
79
|
if(include_handle) {
|
80
|
-
await db.schema.createIndex(`index_${table_name}_handle_updated_at_asc`)
|
80
|
+
await db.schema.createIndex(`index_${table_name}_handle_updated_at_asc`).ifNotExists()
|
81
81
|
.on(table_name)
|
82
82
|
.columns(['handle', 'updated_at asc'])
|
83
83
|
.execute();
|
84
|
-
await db.schema.createIndex(`index_${table_name}_handle_updated_at_desc`)
|
84
|
+
await db.schema.createIndex(`index_${table_name}_handle_updated_at_desc`).ifNotExists()
|
85
85
|
.on(table_name)
|
86
86
|
.columns(['handle', 'updated_at desc'])
|
87
87
|
.execute();
|
@@ -94,26 +94,26 @@ const create_base_indexes = async (db, table_name, include_id=true, include_hand
|
|
94
94
|
* @param {keyof Pick<Database, 'entity_to_media' |
|
95
95
|
* 'entity_to_search_terms' | 'entity_to_tags_projections' |
|
96
96
|
* 'products_to_collections' | 'products_to_discounts' |
|
97
|
-
* 'products_to_variants' | 'storefronts_to_other'>} table_name
|
97
|
+
* 'products_to_variants' | 'storefronts_to_other' | 'products_to_related_products'>} table_name
|
98
98
|
*/
|
99
99
|
const create_entity_table_indexes = async (db, table_name) => {
|
100
|
-
await db.schema.createIndex(`index_${table_name}_entity_id`)
|
100
|
+
await db.schema.createIndex(`index_${table_name}_entity_id`).ifNotExists()
|
101
101
|
.on(table_name)
|
102
102
|
.column('entity_id')
|
103
103
|
.execute();
|
104
|
-
await db.schema.createIndex(`index_${table_name}_entity_handle`)
|
104
|
+
await db.schema.createIndex(`index_${table_name}_entity_handle`).ifNotExists()
|
105
105
|
.on(table_name)
|
106
106
|
.column('entity_handle')
|
107
107
|
.execute();
|
108
|
-
await db.schema.createIndex(`index_${table_name}_value`)
|
108
|
+
await db.schema.createIndex(`index_${table_name}_value`).ifNotExists()
|
109
109
|
.on(table_name)
|
110
110
|
.column('value')
|
111
111
|
.execute();
|
112
|
-
await db.schema.createIndex(`index_${table_name}_reporter`)
|
112
|
+
await db.schema.createIndex(`index_${table_name}_reporter`).ifNotExists()
|
113
113
|
.on(table_name)
|
114
114
|
.column('reporter')
|
115
115
|
.execute();
|
116
|
-
await db.schema.createIndex(`index_${table_name}_context`)
|
116
|
+
await db.schema.createIndex(`index_${table_name}_context`).ifNotExists()
|
117
117
|
.on(table_name)
|
118
118
|
.column('context')
|
119
119
|
.execute();
|
@@ -148,7 +148,8 @@ export async function up(db) {
|
|
148
148
|
let tb = create_safe_table(db, 'templates');
|
149
149
|
tb = add_base_columns(tb);
|
150
150
|
tb = tb.addColumn('title', 'text');
|
151
|
-
tb = tb.addColumn('
|
151
|
+
tb = tb.addColumn('template_html', 'text');
|
152
|
+
tb = tb.addColumn('template_text', 'text');
|
152
153
|
tb = tb.addColumn('reference_example_input', 'json');
|
153
154
|
await tb.execute();
|
154
155
|
await create_base_indexes(db, 'templates');
|