@storecraft/database-sql-base 1.0.0 → 1.0.2
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 -12
- package/index.js +172 -1
- package/{tsconfig.json → jsconfig.json} +1 -5
- package/migrate.js +25 -19
- package/migrations.mysql/00000_init_tables.js +6 -5
- 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 +5 -238
- package/migrations.sqlite/00000_init_tables.js +12 -12
- package/package.json +2 -4
- package/src/con.auth_users.js +5 -2
- package/src/con.collections.js +2 -2
- package/src/con.customers.js +2 -2
- package/src/con.discounts.js +3 -3
- package/src/con.discounts.utils.js +11 -11
- package/src/con.helpers.json.js +3 -3
- package/src/con.images.js +5 -5
- package/src/con.notifications.js +2 -2
- package/src/con.orders.js +19 -4
- package/src/con.posts.js +2 -2
- package/src/con.products.js +5 -5
- package/src/con.search.js +7 -7
- package/src/con.shared.experiment.js +723 -0
- package/src/con.shared.js +55 -21
- package/src/con.shipping.js +2 -2
- package/src/con.storefronts.js +2 -2
- package/src/con.tags.js +2 -2
- package/src/con.templates.js +22 -7
- package/src/utils.query.js +6 -6
- package/tests/runner.mssql-local.test.js +10 -6
- package/tests/runner.mysql-local.test.js +16 -63
- package/tests/runner.postgres-local.test.js +17 -62
- package/tests/runner.sqlite-local.test.js +15 -64
- package/tests/sandbox.test.js +15 -13
- package/types.public.d.ts +12 -4
- package/types.sql.tables.d.ts +4 -2
- package/driver.js +0 -190
- package/tests/query.cursor.test.js +0 -389
- package/tests/query.vql.test.js +0 -71
@@ -1,7 +1,8 @@
|
|
1
1
|
import { App } from '@storecraft/core';
|
2
2
|
import { SQL } from '@storecraft/database-sql-base';
|
3
|
-
import {
|
4
|
-
import
|
3
|
+
import { migrateToLatest } from '@storecraft/database-sql-base/migrate.js';
|
4
|
+
import { NodePlatform } from '@storecraft/core/platform/node';
|
5
|
+
import { api } from '@storecraft/core/test-runner'
|
5
6
|
import SQLite from 'better-sqlite3'
|
6
7
|
import { SqliteDialect } from 'kysely';
|
7
8
|
import { homedir } from 'node:os';
|
@@ -12,22 +13,23 @@ export const sqlite_dialect = new SqliteDialect({
|
|
12
13
|
});
|
13
14
|
|
14
15
|
export const create_app = async () => {
|
15
|
-
|
16
|
-
|
17
|
-
new SQL({
|
18
|
-
dialect: sqlite_dialect,
|
19
|
-
dialect_type: 'SQLITE'
|
20
|
-
}),
|
21
|
-
null, null, {
|
16
|
+
const app = new App(
|
17
|
+
{
|
22
18
|
auth_admins_emails: ['admin@sc.com'],
|
23
|
-
auth_password_hash_rounds: 100,
|
24
19
|
auth_secret_access_token: 'auth_secret_access_token',
|
25
20
|
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
26
21
|
}
|
22
|
+
)
|
23
|
+
.withPlatform(new NodePlatform())
|
24
|
+
.withDatabase(
|
25
|
+
new SQL({
|
26
|
+
dialect: sqlite_dialect,
|
27
|
+
dialect_type: 'SQLITE'
|
28
|
+
})
|
27
29
|
);
|
28
30
|
|
29
31
|
await app.init();
|
30
|
-
await app.db
|
32
|
+
await migrateToLatest(app.db, false);
|
31
33
|
|
32
34
|
return app;
|
33
35
|
}
|
@@ -35,65 +37,14 @@ export const create_app = async () => {
|
|
35
37
|
async function test() {
|
36
38
|
const app = await create_app();
|
37
39
|
|
38
|
-
Object.entries(
|
40
|
+
Object.entries(api).slice(0, -1).forEach(
|
39
41
|
([name, runner]) => {
|
40
42
|
runner.create(app).run();
|
41
43
|
}
|
42
44
|
);
|
43
|
-
const last_test = Object.values(
|
45
|
+
const last_test = Object.values(api).at(-1).create(app);
|
44
46
|
last_test.after(async () => { await app.db.disconnect() });
|
45
47
|
last_test.run();
|
46
48
|
}
|
47
49
|
|
48
50
|
test();
|
49
|
-
|
50
|
-
async function test2() {
|
51
|
-
const app = await create_app();
|
52
|
-
|
53
|
-
// api_index.api_auth_test.create(app).run();
|
54
|
-
|
55
|
-
api_index.api_checkout_test.create(app).run();
|
56
|
-
|
57
|
-
// api_index.api_tags_crud_test.create(app).run();
|
58
|
-
// api_index.api_tags_list_test.create(app).run();
|
59
|
-
|
60
|
-
// api_index.api_collections_crud_test.create(app).run();
|
61
|
-
// api_index.api_collections_list_test.create(app).run();
|
62
|
-
// api_index.api_collections_products_test.create(app).run();
|
63
|
-
|
64
|
-
// api_index.api_products_crud_test.create(app).run();
|
65
|
-
// api_index.api_products_collections_test.create(app).run();
|
66
|
-
// api_index.api_products_list_test.create(app).run();
|
67
|
-
// api_index.api_products_discounts_test.create(app).run();
|
68
|
-
// api_index.api_products_variants_test.create(app).run();
|
69
|
-
|
70
|
-
// api_index.api_shipping_crud_test.create(app).run();
|
71
|
-
// api_index.api_shipping_list_test.create(app).run();
|
72
|
-
|
73
|
-
// api_index.api_posts_crud_test.create(app).run();
|
74
|
-
// api_index.api_posts_list_test.create(app).run();
|
75
|
-
|
76
|
-
// api_index.api_customers_crud_test.create(app).run();
|
77
|
-
// api_index.api_customers_list_test.create(app).run();
|
78
|
-
|
79
|
-
// api_index.api_orders_crud_test.create(app).run();
|
80
|
-
// api_index.api_orders_list_test.create(app).run();
|
81
|
-
|
82
|
-
// api_index.api_storefronts_crud_test.create(app).run();
|
83
|
-
// api_index.api_storefronts_list_test.create(app).run();
|
84
|
-
// api_index.api_storefronts_all_connections_test.create(app).run();
|
85
|
-
|
86
|
-
// api_index.api_notifications_crud_test.create(app).run();
|
87
|
-
// api_index.api_notifications_list_test.create(app).run();
|
88
|
-
|
89
|
-
// api_index.api_images_crud_test.create(app).run();
|
90
|
-
// api_index.api_images_list_test.create(app).run();
|
91
|
-
|
92
|
-
// api_index.api_discounts_crud_test.create(app).run();
|
93
|
-
// api_index.api_discounts_list_test.create(app).run();
|
94
|
-
// api_index.api_discounts_products_test.create(app).run();
|
95
|
-
|
96
|
-
|
97
|
-
}
|
98
|
-
|
99
|
-
// test2();
|
package/tests/sandbox.test.js
CHANGED
@@ -1,33 +1,35 @@
|
|
1
1
|
import { App } from '@storecraft/core';
|
2
|
-
import {
|
3
|
-
import { NodePlatform } from '@storecraft/platform-node';
|
2
|
+
import { NodePlatform } from '@storecraft/core/platform/node';
|
4
3
|
import { SqliteDialect } from 'kysely';
|
5
4
|
import { homedir } from 'os';
|
6
5
|
import { join } from 'path';
|
7
6
|
import SQLite from 'better-sqlite3'
|
8
7
|
import { SQL } from '../index.js';
|
8
|
+
import { migrateToLatest } from '../migrate.js';
|
9
9
|
|
10
10
|
export const sqlite_dialect = new SqliteDialect({
|
11
11
|
database: async () => new SQLite(join(homedir(), 'db.sqlite')),
|
12
12
|
});
|
13
13
|
|
14
14
|
export const create_app = async () => {
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
dialect: sqlite_dialect,
|
19
|
-
dialect_type: 'SQLITE'
|
20
|
-
}),
|
21
|
-
null, null, {
|
15
|
+
|
16
|
+
const app = new App(
|
17
|
+
{
|
22
18
|
auth_admins_emails: ['admin@sc.com'],
|
23
|
-
auth_password_hash_rounds: 100,
|
24
19
|
auth_secret_access_token: 'auth_secret_access_token',
|
25
20
|
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
26
21
|
}
|
27
|
-
)
|
22
|
+
)
|
23
|
+
.withPlatform(new NodePlatform())
|
24
|
+
.withDatabase(
|
25
|
+
new SQL({
|
26
|
+
dialect: sqlite_dialect,
|
27
|
+
dialect_type: 'SQLITE'
|
28
|
+
})
|
29
|
+
)
|
28
30
|
|
29
31
|
await app.init();
|
30
|
-
await app.db
|
32
|
+
await migrateToLatest(app.db, false);
|
31
33
|
|
32
34
|
return app;
|
33
35
|
}
|
@@ -49,7 +51,7 @@ const withTime = async (fn) => {
|
|
49
51
|
async function test() {
|
50
52
|
const app = await withTime(create_app);
|
51
53
|
|
52
|
-
await app.db
|
54
|
+
await migrateToLatest(app.db, false);
|
53
55
|
|
54
56
|
|
55
57
|
const doit = async () => {
|
package/types.public.d.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
import type { Dialect } from 'kysely'
|
1
|
+
import type { Dialect } from 'kysely';
|
2
2
|
|
3
|
-
export
|
3
|
+
export { SQL } from './index.js';
|
4
4
|
|
5
5
|
export type SqlDialectType = 'SQLITE' | 'POSTGRES' | 'MYSQL';
|
6
6
|
|
@@ -9,11 +9,19 @@ export type SqlDialectType = 'SQLITE' | 'POSTGRES' | 'MYSQL';
|
|
9
9
|
*/
|
10
10
|
export type Config = {
|
11
11
|
/**
|
12
|
-
*
|
12
|
+
* @description Database name
|
13
|
+
*
|
14
|
+
* @default main
|
15
|
+
*/
|
16
|
+
db_name?: string,
|
17
|
+
|
18
|
+
/**
|
19
|
+
* @description The `Kysely` dialect
|
13
20
|
*/
|
14
21
|
dialect: Dialect,
|
22
|
+
|
15
23
|
/**
|
16
|
-
* The type of the sql dialect `SQLITE`, `POSTGRES`, `MYSQL
|
24
|
+
* @description The type of the sql dialect `SQLITE`, `POSTGRES`, `MYSQL`
|
17
25
|
*/
|
18
26
|
dialect_type: SqlDialectType;
|
19
27
|
}
|
package/types.sql.tables.d.ts
CHANGED
@@ -11,7 +11,7 @@ import { AttributeType, AuthUserType, Role, TagType,
|
|
11
11
|
OrderPaymentGatewayData, NotificationType,
|
12
12
|
NotificationAction,
|
13
13
|
DiscountInfo,
|
14
|
-
DiscountApplicationEnum} from '@storecraft/core/
|
14
|
+
DiscountApplicationEnum} from '@storecraft/core/api'
|
15
15
|
import {
|
16
16
|
ColumnType,
|
17
17
|
Generated,
|
@@ -120,8 +120,10 @@ export interface Base {
|
|
120
120
|
export interface AuthUserTypeTable extends Base {
|
121
121
|
email: string;
|
122
122
|
password: string;
|
123
|
-
confirmed_mail: number
|
123
|
+
confirmed_mail: number;
|
124
124
|
roles: JSONColumnType<Role[]>;
|
125
|
+
firstname: string;
|
126
|
+
lastname: string;
|
125
127
|
}
|
126
128
|
|
127
129
|
export interface TagsTable extends Base {
|
package/driver.js
DELETED
@@ -1,190 +0,0 @@
|
|
1
|
-
import { App } from '@storecraft/core';
|
2
|
-
import { impl as auth_users } from './src/con.auth_users.js';
|
3
|
-
import { impl as collections } from './src/con.collections.js';
|
4
|
-
import { impl as customers } from './src/con.customers.js';
|
5
|
-
import { impl as discounts } from './src/con.discounts.js';
|
6
|
-
import { impl as images } from './src/con.images.js';
|
7
|
-
import { impl as notifications } from './src/con.notifications.js';
|
8
|
-
import { impl as orders } from './src/con.orders.js';
|
9
|
-
import { impl as posts } from './src/con.posts.js';
|
10
|
-
import { impl as products } from './src/con.products.js';
|
11
|
-
import { impl as shipping } from './src/con.shipping.js';
|
12
|
-
import { impl as storefronts } from './src/con.storefronts.js';
|
13
|
-
import { impl as tags } from './src/con.tags.js';
|
14
|
-
import { impl as templates } from './src/con.templates.js';
|
15
|
-
import { impl as search } from './src/con.search.js';
|
16
|
-
import { Kysely, ParseJSONResultsPlugin } from 'kysely'
|
17
|
-
import { SanitizePlugin } from './src/kysely.sanitize.plugin.js';
|
18
|
-
|
19
|
-
|
20
|
-
/**
|
21
|
-
* @param {any} b
|
22
|
-
* @param {string} msg
|
23
|
-
*/
|
24
|
-
const assert = (b, msg) => {
|
25
|
-
if(!Boolean(b)) throw new Error(msg);
|
26
|
-
}
|
27
|
-
|
28
|
-
/**
|
29
|
-
* @typedef {import('./types.public.js').Config} Config
|
30
|
-
* @typedef {import('./types.sql.tables.js').Database} Database
|
31
|
-
* @typedef {import('kysely').Dialect} Dialect
|
32
|
-
* @typedef {import('@storecraft/core/v-database').db_driver} db_driver
|
33
|
-
*/
|
34
|
-
|
35
|
-
/**
|
36
|
-
* @implements {db_driver}
|
37
|
-
*/
|
38
|
-
export class SQL {
|
39
|
-
|
40
|
-
/** @type {boolean} */
|
41
|
-
#_is_ready;
|
42
|
-
|
43
|
-
/** @type {App<any, any, any>} */
|
44
|
-
#_app;
|
45
|
-
|
46
|
-
/** @type {Config} */
|
47
|
-
#_config;
|
48
|
-
|
49
|
-
/** @type {Kysely<Database>} */
|
50
|
-
#_client;
|
51
|
-
|
52
|
-
/** @type {db_driver["resources"]} */
|
53
|
-
#_resources;
|
54
|
-
|
55
|
-
/**
|
56
|
-
*
|
57
|
-
* @param {Config} [config] config
|
58
|
-
*/
|
59
|
-
constructor(config) {
|
60
|
-
this.#_is_ready = false;
|
61
|
-
this.#_config = config;
|
62
|
-
|
63
|
-
assert(
|
64
|
-
this.#_config.dialect,
|
65
|
-
'No Dialect found !'
|
66
|
-
);
|
67
|
-
|
68
|
-
assert(
|
69
|
-
this.#_config.dialect_type,
|
70
|
-
'No Dialect Type specified !'
|
71
|
-
);
|
72
|
-
|
73
|
-
this.#_client = new Kysely(
|
74
|
-
{
|
75
|
-
dialect: this.#_config.dialect,
|
76
|
-
plugins: [
|
77
|
-
new ParseJSONResultsPlugin(),
|
78
|
-
new SanitizePlugin()
|
79
|
-
]
|
80
|
-
}
|
81
|
-
);
|
82
|
-
}
|
83
|
-
|
84
|
-
throwIfNotReady() {
|
85
|
-
assert(
|
86
|
-
this.isReady,
|
87
|
-
'Database not ready !!! you need to `.init()` it'
|
88
|
-
);
|
89
|
-
}
|
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
|
-
/**
|
104
|
-
*
|
105
|
-
* @param {App<any, any, any>} app
|
106
|
-
*
|
107
|
-
*
|
108
|
-
* @returns {Promise<this>}
|
109
|
-
*/
|
110
|
-
async init(app) {
|
111
|
-
if(this.isReady)
|
112
|
-
return this;
|
113
|
-
|
114
|
-
this.#_app = app;
|
115
|
-
|
116
|
-
this.#_resources = {
|
117
|
-
auth_users: auth_users(this),
|
118
|
-
collections: collections(this),
|
119
|
-
customers: customers(this),
|
120
|
-
discounts: discounts(this),
|
121
|
-
images: images(this),
|
122
|
-
notifications: notifications(this),
|
123
|
-
orders: orders(this),
|
124
|
-
posts: posts(this),
|
125
|
-
products: products(this),
|
126
|
-
storefronts: storefronts(this),
|
127
|
-
tags: tags(this),
|
128
|
-
shipping_methods: shipping(this),
|
129
|
-
templates: templates(this),
|
130
|
-
search: search(this),
|
131
|
-
}
|
132
|
-
|
133
|
-
this.#_is_ready = true;
|
134
|
-
|
135
|
-
return this;
|
136
|
-
}
|
137
|
-
|
138
|
-
async disconnect() {
|
139
|
-
await this.client.destroy();
|
140
|
-
return true;
|
141
|
-
}
|
142
|
-
|
143
|
-
/**
|
144
|
-
* `database` resources
|
145
|
-
*/
|
146
|
-
get resources () {
|
147
|
-
return this.#_resources;
|
148
|
-
}
|
149
|
-
|
150
|
-
get name () {
|
151
|
-
return 'main';
|
152
|
-
}
|
153
|
-
|
154
|
-
get app() {
|
155
|
-
return this.#_app;
|
156
|
-
}
|
157
|
-
|
158
|
-
get client() {
|
159
|
-
return this.#_client;
|
160
|
-
}
|
161
|
-
|
162
|
-
get config() {
|
163
|
-
return this.#_config;
|
164
|
-
}
|
165
|
-
|
166
|
-
get isReady() {
|
167
|
-
return this.#_is_ready;
|
168
|
-
}
|
169
|
-
|
170
|
-
get dialectType() {
|
171
|
-
return this.#_config.dialect_type;
|
172
|
-
}
|
173
|
-
|
174
|
-
get isSqlite() {
|
175
|
-
return this.dialectType==='SQLITE';
|
176
|
-
}
|
177
|
-
|
178
|
-
get isPostgres() {
|
179
|
-
return this.dialectType==='POSTGRES';
|
180
|
-
}
|
181
|
-
|
182
|
-
get isMysql() {
|
183
|
-
return this.dialectType==='MYSQL';
|
184
|
-
}
|
185
|
-
|
186
|
-
get isMssql() {
|
187
|
-
return this.dialectType==='MSSQL';
|
188
|
-
}
|
189
|
-
|
190
|
-
}
|