@storecraft/database-sql-base 1.0.0

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.
Files changed (49) hide show
  1. package/README.md +126 -0
  2. package/TODO.md +2 -0
  3. package/db-strategy.md +3 -0
  4. package/driver.js +190 -0
  5. package/index.js +3 -0
  6. package/migrate.js +66 -0
  7. package/migrations.mssql/00000_init_tables.js +268 -0
  8. package/migrations.mysql/00000_init_tables.js +372 -0
  9. package/migrations.mysql/00001_seed_email_templates.js +1 -0
  10. package/migrations.postgres/00000_init_tables.js +358 -0
  11. package/migrations.postgres/00001_seed_email_templates.js +1 -0
  12. package/migrations.shared/00001_seed_email_templates.js +260 -0
  13. package/migrations.sqlite/00000_init_tables.js +357 -0
  14. package/migrations.sqlite/00001_seed_email_templates.js +1 -0
  15. package/package.json +47 -0
  16. package/src/con.auth_users.js +159 -0
  17. package/src/con.collections.js +197 -0
  18. package/src/con.customers.js +202 -0
  19. package/src/con.discounts.js +225 -0
  20. package/src/con.discounts.utils.js +180 -0
  21. package/src/con.helpers.json.js +231 -0
  22. package/src/con.helpers.json.mssql.js +233 -0
  23. package/src/con.helpers.json.mysql.js +239 -0
  24. package/src/con.helpers.json.postgres.js +223 -0
  25. package/src/con.helpers.json.sqlite.js +263 -0
  26. package/src/con.images.js +230 -0
  27. package/src/con.notifications.js +149 -0
  28. package/src/con.orders.js +156 -0
  29. package/src/con.posts.js +147 -0
  30. package/src/con.products.js +497 -0
  31. package/src/con.search.js +148 -0
  32. package/src/con.shared.js +616 -0
  33. package/src/con.shipping.js +147 -0
  34. package/src/con.storefronts.js +301 -0
  35. package/src/con.tags.js +120 -0
  36. package/src/con.templates.js +133 -0
  37. package/src/kysely.sanitize.plugin.js +40 -0
  38. package/src/utils.funcs.js +77 -0
  39. package/src/utils.query.js +195 -0
  40. package/tests/query.cursor.test.js +389 -0
  41. package/tests/query.vql.test.js +71 -0
  42. package/tests/runner.mssql-local.test.js +118 -0
  43. package/tests/runner.mysql-local.test.js +101 -0
  44. package/tests/runner.postgres-local.test.js +99 -0
  45. package/tests/runner.sqlite-local.test.js +99 -0
  46. package/tests/sandbox.test.js +71 -0
  47. package/tsconfig.json +21 -0
  48. package/types.public.d.ts +19 -0
  49. package/types.sql.tables.d.ts +247 -0
@@ -0,0 +1,71 @@
1
+ // import { test } from 'uvu';
2
+ // import * as assert from 'uvu/assert';
3
+ // import { query_vql_to_mongo } from '../src/utils.query.js'
4
+
5
+ // test('VQL', async () => {
6
+ // const vql_ast = {
7
+ // op: '&',
8
+ // args: [
9
+ // {
10
+ // op: 'LEAF',
11
+ // value: 'name:tomer'
12
+ // },
13
+ // {
14
+ // op: '&',
15
+ // args: [
16
+ // {
17
+ // op: 'LEAF',
18
+ // value: 'tag:genre_a'
19
+ // },
20
+ // {
21
+ // op: '!',
22
+ // args: [
23
+ // {
24
+ // op: 'LEAF',
25
+ // value: 'tag:genre_b'
26
+ // }
27
+ // ]
28
+ // }
29
+ // ],
30
+ // group: true
31
+ // }
32
+ // ],
33
+ // group: true
34
+ // };
35
+
36
+ // const mongo = {
37
+ // "$and": [
38
+ // {
39
+ // "search": {
40
+ // "$regex": "^name:tomer$"
41
+ // }
42
+ // },
43
+ // {
44
+ // "$and": [
45
+ // {
46
+ // "search": {
47
+ // "$regex": "^tag:genre_a$"
48
+ // }
49
+ // },
50
+ // {
51
+ // "$nor": [
52
+ // {
53
+ // "search": {
54
+ // "$regex": "^tag:genre_b$"
55
+ // }
56
+ // }
57
+ // ]
58
+ // }
59
+ // ]
60
+ // }
61
+ // ]
62
+ // };
63
+
64
+ // const m1 = query_vql_to_mongo(vql_ast);
65
+
66
+ // // console.log(JSON.stringify(m1, null, 2))
67
+
68
+ // assert.equal(m1, mongo);
69
+ // });
70
+
71
+ // test.run();
@@ -0,0 +1,118 @@
1
+ import { App } from '@storecraft/core';
2
+ import { SQL } from '@storecraft/database-sql-base';
3
+ import { NodePlatform } from '@storecraft/platform-node';
4
+ import { api_index } from '@storecraft/test-runner'
5
+ import { MssqlDialect } from 'kysely';
6
+ import * as Tedious from 'tedious';
7
+ import * as Tarn from 'tarn';
8
+
9
+ const dialect = new MssqlDialect({
10
+ tarn: {
11
+ ...Tarn,
12
+ options: {
13
+ min: 0,
14
+ max: 10,
15
+ },
16
+ },
17
+ tedious: {
18
+ ...Tedious,
19
+ connectionFactory: () => new Tedious.Connection({
20
+ authentication: {
21
+ options: {
22
+ password: process.env.MSSQL_PASSWORD,
23
+ userName: process.env.MSSQL_USER,
24
+ },
25
+ type: 'default',
26
+ },
27
+ options: {
28
+ port: parseInt(process.env.MSSQL_PORT),
29
+ trustServerCertificate: true,
30
+ },
31
+ server: process.env.MSSQL_HOST,
32
+ }),
33
+ },
34
+ });
35
+
36
+ export const create_app = async () => {
37
+ let app = new App(
38
+ new NodePlatform(),
39
+ new SQL({
40
+ dialect: dialect,
41
+ dialect_type: 'MSSQL'
42
+ }),
43
+ null, null, {
44
+ auth_admins_emails: ['admin@sc.com'],
45
+ auth_password_hash_rounds: 100,
46
+ auth_secret_access_token: 'auth_secret_access_token',
47
+ auth_secret_refresh_token: 'auth_secret_refresh_token'
48
+ }
49
+ );
50
+
51
+ await app.init();
52
+ await app.db.migrateToLatest();
53
+ return app;
54
+ }
55
+
56
+ async function test() {
57
+ const app = await create_app();
58
+
59
+ Object.entries(api_index).slice(0, -1).forEach(
60
+ ([name, runner]) => {
61
+ runner.create(app).run();
62
+ }
63
+ );
64
+ const last_test = Object.values(api_index).at(-1).create(app);
65
+ last_test.after(async () => { await app.db.disconnect() });
66
+ last_test.run();
67
+ }
68
+
69
+ test();
70
+
71
+ async function test2() {
72
+ const app = await create_app();
73
+
74
+ // api_index.api_auth_test.create(app).run();
75
+
76
+ // api_index.api_tags_crud_test.create(app).run();
77
+ // api_index.api_tags_list_test.create(app).run();
78
+
79
+ // api_index.api_collections_crud_test.create(app).run();
80
+ // api_index.api_collections_list_test.create(app).run();
81
+ // api_index.api_collections_products_test.create(app).run();
82
+
83
+ // api_index.api_products_crud_test.create(app).run();
84
+ // api_index.api_products_collections_test.create(app).run();
85
+ // api_index.api_products_list_test.create(app).run();
86
+ // api_index.api_products_discounts_test.create(app).run();
87
+ // api_index.api_products_variants_test.create(app).run();
88
+
89
+ // api_index.api_shipping_crud_test.create(app).run();
90
+ // api_index.api_shipping_list_test.create(app).run();
91
+
92
+ // api_index.api_posts_crud_test.create(app).run();
93
+ // api_index.api_posts_list_test.create(app).run();
94
+
95
+ // api_index.api_customers_crud_test.create(app).run();
96
+ // api_index.api_customers_list_test.create(app).run();
97
+
98
+ // api_index.api_orders_crud_test.create(app).run();
99
+ // api_index.api_orders_list_test.create(app).run();
100
+
101
+ // api_index.api_storefronts_crud_test.create(app).run();
102
+ // api_index.api_storefronts_list_test.create(app).run();
103
+ // api_index.api_storefronts_all_connections_test.create(app).run();
104
+
105
+ // api_index.api_notifications_crud_test.create(app).run();
106
+ // api_index.api_notifications_list_test.create(app).run();
107
+
108
+ api_index.api_images_crud_test.create(app).run();
109
+ api_index.api_images_list_test.create(app).run();
110
+
111
+ // api_index.api_discounts_crud_test.create(app).run();
112
+ // api_index.api_discounts_list_test.create(app).run();
113
+ // api_index.api_discounts_products_test.create(app).run();
114
+
115
+
116
+ }
117
+
118
+ // test2();
@@ -0,0 +1,101 @@
1
+ import { App } from '@storecraft/core';
2
+ import { SQL } from '@storecraft/database-sql-base';
3
+ import { NodePlatform } from '@storecraft/platform-node';
4
+ import { api_index } from '@storecraft/test-runner'
5
+ import { MysqlDialect } from 'kysely';
6
+ import { createPool } from 'mysql2'
7
+
8
+ export const dialect = new MysqlDialect({
9
+ pool: createPool({
10
+ database: process.env.MYSQL_DB_NAME,
11
+ host: process.env.MYSQL_HOST,
12
+ port: parseInt(process.env.MYSQL_PORT),
13
+ user: process.env.MYSQL_USER,
14
+ password: process.env.MYSQL_PASSWORD,
15
+ })
16
+ });
17
+
18
+ export const create_app = async () => {
19
+ let app = new App(
20
+ new NodePlatform(),
21
+ new SQL({
22
+ dialect: dialect,
23
+ dialect_type: 'MYSQL'
24
+ }),
25
+ null, null, {
26
+ auth_admins_emails: ['admin@sc.com'],
27
+ auth_password_hash_rounds: 100,
28
+ auth_secret_access_token: 'auth_secret_access_token',
29
+ auth_secret_refresh_token: 'auth_secret_refresh_token'
30
+ }
31
+ );
32
+
33
+ await app.init();
34
+ await app.db.migrateToLatest();
35
+
36
+ return app;
37
+ }
38
+
39
+ async function test() {
40
+ const app = await create_app();
41
+
42
+ Object.entries(api_index).slice(0, -1).forEach(
43
+ ([name, runner]) => {
44
+ runner.create(app).run();
45
+ }
46
+ );
47
+ const last_test = Object.values(api_index).at(-1).create(app);
48
+ last_test.after(async () => { await app.db.disconnect() });
49
+ last_test.run();
50
+ }
51
+
52
+ test();
53
+
54
+ async function test2() {
55
+ const app = await create_app();
56
+
57
+ // api_index.api_auth_test.create(app).run();
58
+
59
+ // api_index.api_tags_crud_test.create(app).run();
60
+ // api_index.api_tags_list_test.create(app).run();
61
+
62
+ // api_index.api_collections_crud_test.create(app).run();
63
+ // api_index.api_collections_list_test.create(app).run();
64
+ // api_index.api_collections_products_test.create(app).run();
65
+
66
+ // api_index.api_products_crud_test.create(app).run();
67
+ // api_index.api_products_collections_test.create(app).run();
68
+ // api_index.api_products_list_test.create(app).run();
69
+ // api_index.api_products_discounts_test.create(app).run();
70
+ // api_index.api_products_variants_test.create(app).run();
71
+
72
+ // api_index.api_shipping_crud_test.create(app).run();
73
+ // api_index.api_shipping_list_test.create(app).run();
74
+
75
+ // api_index.api_posts_crud_test.create(app).run();
76
+ // api_index.api_posts_list_test.create(app).run();
77
+
78
+ // api_index.api_customers_crud_test.create(app).run();
79
+ // api_index.api_customers_list_test.create(app).run();
80
+
81
+ api_index.api_orders_crud_test.create(app).run();
82
+ api_index.api_orders_list_test.create(app).run();
83
+
84
+ api_index.api_storefronts_crud_test.create(app).run();
85
+ api_index.api_storefronts_list_test.create(app).run();
86
+ api_index.api_storefronts_all_connections_test.create(app).run();
87
+
88
+ api_index.api_notifications_crud_test.create(app).run();
89
+ api_index.api_notifications_list_test.create(app).run();
90
+
91
+ api_index.api_images_crud_test.create(app).run();
92
+ api_index.api_images_list_test.create(app).run();
93
+
94
+ api_index.api_discounts_crud_test.create(app).run();
95
+ api_index.api_discounts_list_test.create(app).run();
96
+ api_index.api_discounts_products_test.create(app).run();
97
+
98
+
99
+ }
100
+
101
+ // test2();
@@ -0,0 +1,99 @@
1
+ import { App } from '@storecraft/core';
2
+ import { SQL } from '@storecraft/database-sql-base';
3
+ import { NodePlatform } from '@storecraft/platform-node';
4
+ import { api_index } from '@storecraft/test-runner'
5
+ import { PostgresDialect } from 'kysely';
6
+ import pg from 'pg'
7
+
8
+ const pg_dialect = new PostgresDialect({
9
+ pool: new pg.Pool({
10
+ host: process.env.POSTGRES_HOST,
11
+ port: parseInt(process.env.POSTGRES_PORT),
12
+ user: process.env.POSTGRES_USER,
13
+ password: process.env.POSTGRES_PASSWORD,
14
+ })
15
+ });
16
+
17
+ export const create_app = async () => {
18
+ let app = new App(
19
+ new NodePlatform(),
20
+ new SQL({
21
+ dialect: pg_dialect,
22
+ dialect_type: 'POSTGRES'
23
+ }),
24
+ null, null, {
25
+ auth_admins_emails: ['admin@sc.com'],
26
+ auth_password_hash_rounds: 100,
27
+ auth_secret_access_token: 'auth_secret_access_token',
28
+ auth_secret_refresh_token: 'auth_secret_refresh_token'
29
+ }
30
+ );
31
+
32
+ await app.init();
33
+ await app.db.migrateToLatest();
34
+ return app;
35
+ }
36
+
37
+ async function test() {
38
+ const app = await create_app();
39
+
40
+ Object.entries(api_index).slice(0, -1).forEach(
41
+ ([name, runner]) => {
42
+ runner.create(app).run();
43
+ }
44
+ );
45
+ const last_test = Object.values(api_index).at(-1).create(app);
46
+ last_test.after(async () => { await app.db.disconnect() });
47
+ last_test.run();
48
+ }
49
+
50
+ test();
51
+
52
+ async function test2() {
53
+ const app = await create_app();
54
+
55
+ api_index.api_auth_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();
@@ -0,0 +1,99 @@
1
+ import { App } from '@storecraft/core';
2
+ import { SQL } from '@storecraft/database-sql-base';
3
+ import { NodePlatform } from '@storecraft/platform-node';
4
+ import { api_index } from '@storecraft/test-runner'
5
+ import SQLite from 'better-sqlite3'
6
+ import { SqliteDialect } from 'kysely';
7
+ import { homedir } from 'node:os';
8
+ import { join } from 'node:path';
9
+
10
+ export const sqlite_dialect = new SqliteDialect({
11
+ database: async () => new SQLite(join(homedir(), 'db.sqlite')),
12
+ });
13
+
14
+ export const create_app = async () => {
15
+ let app = new App(
16
+ new NodePlatform(),
17
+ new SQL({
18
+ dialect: sqlite_dialect,
19
+ dialect_type: 'SQLITE'
20
+ }),
21
+ null, null, {
22
+ auth_admins_emails: ['admin@sc.com'],
23
+ auth_password_hash_rounds: 100,
24
+ auth_secret_access_token: 'auth_secret_access_token',
25
+ auth_secret_refresh_token: 'auth_secret_refresh_token'
26
+ }
27
+ );
28
+
29
+ await app.init();
30
+ await app.db.migrateToLatest();
31
+
32
+ return app;
33
+ }
34
+
35
+ async function test() {
36
+ const app = await create_app();
37
+
38
+ Object.entries(api_index).slice(0, -1).forEach(
39
+ ([name, runner]) => {
40
+ runner.create(app).run();
41
+ }
42
+ );
43
+ const last_test = Object.values(api_index).at(-1).create(app);
44
+ last_test.after(async () => { await app.db.disconnect() });
45
+ last_test.run();
46
+ }
47
+
48
+ 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();
@@ -0,0 +1,71 @@
1
+ import { App } from '@storecraft/core';
2
+ import { MongoDB } from '@storecraft/database-mongodb-node';
3
+ import { NodePlatform } from '@storecraft/platform-node';
4
+ import { SqliteDialect } from 'kysely';
5
+ import { homedir } from 'os';
6
+ import { join } from 'path';
7
+ import SQLite from 'better-sqlite3'
8
+ import { SQL } from '../index.js';
9
+
10
+ export const sqlite_dialect = new SqliteDialect({
11
+ database: async () => new SQLite(join(homedir(), 'db.sqlite')),
12
+ });
13
+
14
+ export const create_app = async () => {
15
+ let app = new App(
16
+ new NodePlatform(),
17
+ new SQL({
18
+ dialect: sqlite_dialect,
19
+ dialect_type: 'SQLITE'
20
+ }),
21
+ null, null, {
22
+ auth_admins_emails: ['admin@sc.com'],
23
+ auth_password_hash_rounds: 100,
24
+ auth_secret_access_token: 'auth_secret_access_token',
25
+ auth_secret_refresh_token: 'auth_secret_refresh_token'
26
+ }
27
+ );
28
+
29
+ await app.init();
30
+ await app.db.migrateToLatest();
31
+
32
+ return app;
33
+ }
34
+
35
+ /**
36
+ *
37
+ * @template R
38
+ *
39
+ * @param {(()=>Promise<R>) | (() => R)} fn
40
+ */
41
+ const withTime = async (fn) => {
42
+ const n1 = Date.now() ;
43
+ const r = await fn();
44
+ const delta = Date.now() - n1;
45
+ console.log(delta);
46
+ return r;
47
+ }
48
+
49
+ async function test() {
50
+ const app = await withTime(create_app);
51
+
52
+ await app.db.migrateToLatest(false);
53
+
54
+
55
+ const doit = async () => {
56
+ let items = await app.db.resources.search.quicksearch(
57
+ {
58
+ vql: 'ship 2',
59
+ sortBy: ['updated_at']
60
+ }
61
+ );
62
+ return items;
63
+ }
64
+
65
+ const items = await withTime(doit);
66
+
67
+ // console.log('items ', items)
68
+ }
69
+
70
+ test();
71
+
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compileOnSave": false,
3
+ "compilerOptions": {
4
+ "noEmit": true,
5
+ "allowJs": true,
6
+ "checkJs": true,
7
+ "target": "ESNext",
8
+ "resolveJsonModule": true,
9
+ "moduleResolution": "NodeNext",
10
+ "module": "NodeNext",
11
+ "composite": true,
12
+ },
13
+ "include": [
14
+ "*",
15
+ "src/*",
16
+ "migrations.sqlite/*",
17
+ "migrations.postgres/*",
18
+ "migrations.mysql/*",
19
+ "tests/*.js"
20
+ ]
21
+ }
@@ -0,0 +1,19 @@
1
+ import type { Dialect } from 'kysely'
2
+
3
+ export * from './index.js'
4
+
5
+ export type SqlDialectType = 'SQLITE' | 'POSTGRES' | 'MYSQL';
6
+
7
+ /**
8
+ * The Storecraft SQL config
9
+ */
10
+ export type Config = {
11
+ /**
12
+ * The Kysely dialect
13
+ */
14
+ dialect: Dialect,
15
+ /**
16
+ * The type of the sql dialect `SQLITE`, `POSTGRES`, `MYSQL`, `MSSQL`
17
+ */
18
+ dialect_type: SqlDialectType;
19
+ }