@storecraft/database-sql-base 1.0.12 → 1.0.14
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/index.js +20 -19
- package/migrations.mysql/00003_alter_auth_users.js +1 -0
- package/migrations.postgres/00003_alter_auth_users.js +1 -0
- package/migrations.shared/00003_alter_auth_users.js +37 -0
- package/migrations.sqlite/00003_alter_auth_users.js +1 -0
- package/package.json +1 -1
- package/src/con.auth_users.js +21 -20
- package/src/con.collections.js +123 -22
- package/src/con.customers.js +51 -16
- package/src/con.discounts.js +193 -58
- package/src/con.discounts.utils.js +13 -12
- package/src/con.helpers.json.js +34 -30
- package/src/con.helpers.json.mysql.js +39 -19
- package/src/con.helpers.json.postgres.js +14 -8
- package/src/con.helpers.json.sqlite.js +35 -15
- package/src/con.notifications.js +10 -6
- package/src/con.orders.js +4 -3
- package/src/con.posts.js +6 -6
- package/src/con.products.js +82 -33
- package/src/con.search.js +1 -7
- package/src/con.shared.experiment.js +1 -0
- package/src/con.shared.js +110 -63
- package/src/con.shipping.js +6 -6
- package/src/con.storefronts.js +170 -90
- package/src/con.tags.js +9 -5
- package/src/con.templates.js +3 -1
- package/src/utils.funcs.js +6 -2
- package/src/utils.query.js +24 -20
- package/src/utils.types.d.ts +25 -0
- package/tests/Untitled-1.sqlite3-query +19 -0
- package/tests/sandbox.js +210 -0
- package/types.sql.tables.d.ts +76 -22
- package/src/con.helpers.json.mssql.js +0 -233
- package/tests/sandbox.test.js +0 -73
@@ -1,233 +0,0 @@
|
|
1
|
-
import { sql } from "kysely"
|
2
|
-
|
3
|
-
/**
|
4
|
-
* An MS SQL Server helper for aggregating a subquery into a JSON array.
|
5
|
-
*
|
6
|
-
* NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
|
7
|
-
* Otherwise the nested selections will be returned as JSON strings.
|
8
|
-
*
|
9
|
-
* The plugin can be installed like this:
|
10
|
-
*
|
11
|
-
* ```ts
|
12
|
-
* const db = new Kysely({
|
13
|
-
* dialect: new MssqlDialect(config),
|
14
|
-
* plugins: [new ParseJSONResultsPlugin()]
|
15
|
-
* })
|
16
|
-
* ```
|
17
|
-
*
|
18
|
-
* ### Examples
|
19
|
-
*
|
20
|
-
* ```ts
|
21
|
-
* const result = await db
|
22
|
-
* .selectFrom('person')
|
23
|
-
* .select((eb) => [
|
24
|
-
* 'id',
|
25
|
-
* jsonArrayFrom(
|
26
|
-
* eb.selectFrom('pet')
|
27
|
-
* .select(['pet.id as pet_id', 'pet.name'])
|
28
|
-
* .whereRef('pet.owner_id', '=', 'person.id')
|
29
|
-
* .orderBy('pet.name')
|
30
|
-
* .modifyEnd(sql`offset 0 rows`)
|
31
|
-
* ).as('pets')
|
32
|
-
* ])
|
33
|
-
* .execute()
|
34
|
-
*
|
35
|
-
* result[0].id
|
36
|
-
* result[0].pets[0].pet_id
|
37
|
-
* result[0].pets[0].name
|
38
|
-
* ```
|
39
|
-
*
|
40
|
-
* The generated SQL (MS SQL Server):
|
41
|
-
*
|
42
|
-
* ```sql
|
43
|
-
* select "id", (
|
44
|
-
* select coalesce((select * from (
|
45
|
-
* select "pet"."id" as "pet_id", "pet"."name"
|
46
|
-
* from "pet"
|
47
|
-
* where "pet"."owner_id" = "person"."id"
|
48
|
-
* order by "pet"."name"
|
49
|
-
* offset 0 rows
|
50
|
-
* ) as "agg" for json path, include_null_values), '[]')
|
51
|
-
* ) as "pets"
|
52
|
-
* from "person"
|
53
|
-
* ```
|
54
|
-
*
|
55
|
-
* @template O
|
56
|
-
* @param {import("kysely").Expression<O>} expr
|
57
|
-
* @returns {import("kysely").RawBuilder<import("kysely").Simplify<O>[]>}
|
58
|
-
*/
|
59
|
-
export function mssql_jsonArrayFrom(expr) {
|
60
|
-
return sql`coalesce((select * from ${expr} as agg for json path, include_null_values), '[]')`
|
61
|
-
}
|
62
|
-
|
63
|
-
/**
|
64
|
-
* An MS SQL Server helper for aggregating a subquery into a JSON array.
|
65
|
-
*
|
66
|
-
* NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
|
67
|
-
* Otherwise the nested selections will be returned as JSON strings.
|
68
|
-
*
|
69
|
-
* The plugin can be installed like this:
|
70
|
-
*
|
71
|
-
* ```ts
|
72
|
-
* const db = new Kysely({
|
73
|
-
* dialect: new MssqlDialect(config),
|
74
|
-
* plugins: [new ParseJSONResultsPlugin()]
|
75
|
-
* })
|
76
|
-
* ```
|
77
|
-
*
|
78
|
-
* ### Examples
|
79
|
-
*
|
80
|
-
* ```ts
|
81
|
-
* const result = await db
|
82
|
-
* .selectFrom('person')
|
83
|
-
* .select((eb) => [
|
84
|
-
* 'id',
|
85
|
-
* jsonArrayFrom(
|
86
|
-
* eb.selectFrom('pet')
|
87
|
-
* .select(['pet.id as pet_id', 'pet.name'])
|
88
|
-
* .whereRef('pet.owner_id', '=', 'person.id')
|
89
|
-
* .orderBy('pet.name')
|
90
|
-
* .modifyEnd(sql`offset 0 rows`)
|
91
|
-
* ).as('pets')
|
92
|
-
* ])
|
93
|
-
* .execute()
|
94
|
-
*
|
95
|
-
* result[0].id
|
96
|
-
* result[0].pets[0].pet_id
|
97
|
-
* result[0].pets[0].name
|
98
|
-
* ```
|
99
|
-
*
|
100
|
-
* The generated SQL (MS SQL Server):
|
101
|
-
*
|
102
|
-
* ```sql
|
103
|
-
* select "id", (
|
104
|
-
* select coalesce((select * from (
|
105
|
-
* select "pet"."id" as "pet_id", "pet"."name"
|
106
|
-
* from "pet"
|
107
|
-
* where "pet"."owner_id" = "person"."id"
|
108
|
-
* order by "pet"."name"
|
109
|
-
* offset 0 rows
|
110
|
-
* ) as "agg" for json path, include_null_values), '[]')
|
111
|
-
* ) as "pets"
|
112
|
-
* from "person"
|
113
|
-
* ```
|
114
|
-
*
|
115
|
-
* @template O
|
116
|
-
* @param {import("kysely").Expression<O>} expr
|
117
|
-
* @returns {import("kysely").RawBuilder<import("kysely").Simplify<O>[]>}
|
118
|
-
*/
|
119
|
-
export function mssql_stringArrayFrom(expr) {
|
120
|
-
return sql`coalesce((select * from ${expr} as agg for json path, include_null_values), '[]')`
|
121
|
-
}
|
122
|
-
|
123
|
-
|
124
|
-
/**
|
125
|
-
* An MS SQL Server helper for turning a subquery into a JSON object.
|
126
|
-
*
|
127
|
-
* The subquery must only return one row.
|
128
|
-
*
|
129
|
-
* NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
|
130
|
-
* Otherwise the nested selections will be returned as JSON strings.
|
131
|
-
*
|
132
|
-
* The plugin can be installed like this:
|
133
|
-
*
|
134
|
-
* ```ts
|
135
|
-
* const db = new Kysely({
|
136
|
-
* dialect: new MssqlDialect(config),
|
137
|
-
* plugins: [new ParseJSONResultsPlugin()]
|
138
|
-
* })
|
139
|
-
* ```
|
140
|
-
*
|
141
|
-
* ### Examples
|
142
|
-
*
|
143
|
-
* ```ts
|
144
|
-
* const result = await db
|
145
|
-
* .selectFrom('person')
|
146
|
-
* .select((eb) => [
|
147
|
-
* 'id',
|
148
|
-
* jsonObjectFrom(
|
149
|
-
* eb.selectFrom('pet')
|
150
|
-
* .select(['pet.id as pet_id', 'pet.name'])
|
151
|
-
* .whereRef('pet.owner_id', '=', 'person.id')
|
152
|
-
* .where('pet.is_favorite', '=', 1)
|
153
|
-
* ).as('favorite_pet')
|
154
|
-
* ])
|
155
|
-
* .execute()
|
156
|
-
*
|
157
|
-
* result[0].id
|
158
|
-
* result[0].favorite_pet.pet_id
|
159
|
-
* result[0].favorite_pet.name
|
160
|
-
* ```
|
161
|
-
*
|
162
|
-
* The generated SQL (MS SQL Server):
|
163
|
-
*
|
164
|
-
* ```sql
|
165
|
-
* select "id", (
|
166
|
-
* select * from (
|
167
|
-
* select "pet"."id" as "pet_id", "pet"."name"
|
168
|
-
* from "pet"
|
169
|
-
* where "pet"."owner_id" = "person"."id"
|
170
|
-
* and "pet"."is_favorite" = @1
|
171
|
-
* ) as "agg" for json path, include_null_values, without_array_wrapper
|
172
|
-
* ) as "favorite_pet"
|
173
|
-
* from "person"
|
174
|
-
* ```
|
175
|
-
*
|
176
|
-
* @template O
|
177
|
-
* @param {import("kysely").Expression<O>} expr
|
178
|
-
* @returns {import("kysely").RawBuilder<import("kysely").Simplify<O> | null>}
|
179
|
-
*/
|
180
|
-
export function mssql_jsonObjectFrom(expr) {
|
181
|
-
return sql`(select * from ${expr} as agg for json path, include_null_values, without_array_wrapper)`
|
182
|
-
}
|
183
|
-
|
184
|
-
/**
|
185
|
-
* The MS SQL Server `json_query` function, single argument variant.
|
186
|
-
*
|
187
|
-
* NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
|
188
|
-
* Otherwise the nested selections will be returned as JSON strings.
|
189
|
-
*
|
190
|
-
* The plugin can be installed like this:
|
191
|
-
*
|
192
|
-
* ```ts
|
193
|
-
* const db = new Kysely({
|
194
|
-
* dialect: new MssqlDialect(config),
|
195
|
-
* plugins: [new ParseJSONResultsPlugin()]
|
196
|
-
* })
|
197
|
-
* ```
|
198
|
-
*
|
199
|
-
* ### Examples
|
200
|
-
*
|
201
|
-
* ```ts
|
202
|
-
* const result = await db
|
203
|
-
* .selectFrom('person')
|
204
|
-
* .select((eb) => [
|
205
|
-
* 'id',
|
206
|
-
* jsonBuildObject({
|
207
|
-
* first: eb.ref('first_name'),
|
208
|
-
* last: eb.ref('last_name'),
|
209
|
-
* full: eb.fn('concat', ['first_name', eb.val(' '), 'last_name'])
|
210
|
-
* }).as('name')
|
211
|
-
* ])
|
212
|
-
* .execute()
|
213
|
-
* ```
|
214
|
-
*
|
215
|
-
* The generated SQL (MS SQL Server):
|
216
|
-
*
|
217
|
-
* ```sql
|
218
|
-
* select "id", json_query(
|
219
|
-
* '{"first":"'+"first_name"+',"last":"'+"last_name"+',"full":"'+concat("first_name", ' ', "last_name")+'"}'
|
220
|
-
* ) as "name"
|
221
|
-
* from "person"
|
222
|
-
* ```
|
223
|
-
*
|
224
|
-
* @template {Record<string, import("kysely").Expression<unknown>>} O
|
225
|
-
* @param {O} obj
|
226
|
-
* @returns {import("kysely").RawBuilder<import("kysely").Simplify<{[K in keyof O]: O[K] extends Expression<infer V> ? V : never}>>}
|
227
|
-
*/
|
228
|
-
export function mssql_jsonBuildObject(obj) {
|
229
|
-
return sql`json_query('{${sql.join(
|
230
|
-
Object.keys(obj).map((k) => sql`"${sql.raw(k)}":"'+${obj[k]}+'"`),
|
231
|
-
sql`,`,
|
232
|
-
)}}')`
|
233
|
-
}
|
package/tests/sandbox.test.js
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
import { App } from '@storecraft/core';
|
2
|
-
import { NodePlatform } from '@storecraft/core/platform/node';
|
3
|
-
import { SqliteDialect } from 'kysely';
|
4
|
-
import { homedir } from 'os';
|
5
|
-
import { join } from 'path';
|
6
|
-
import SQLite from 'better-sqlite3'
|
7
|
-
import { SQL } from '../index.js';
|
8
|
-
import { migrateToLatest } from '../migrate.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
|
-
|
16
|
-
const app = new App(
|
17
|
-
{
|
18
|
-
auth_admins_emails: ['admin@sc.com'],
|
19
|
-
auth_secret_access_token: 'auth_secret_access_token',
|
20
|
-
auth_secret_refresh_token: 'auth_secret_refresh_token'
|
21
|
-
}
|
22
|
-
)
|
23
|
-
.withPlatform(new NodePlatform())
|
24
|
-
.withDatabase(
|
25
|
-
new SQL({
|
26
|
-
dialect: sqlite_dialect,
|
27
|
-
dialect_type: 'SQLITE'
|
28
|
-
})
|
29
|
-
)
|
30
|
-
|
31
|
-
await app.init();
|
32
|
-
await migrateToLatest(app.db, false);
|
33
|
-
|
34
|
-
return app;
|
35
|
-
}
|
36
|
-
|
37
|
-
/**
|
38
|
-
*
|
39
|
-
* @template R
|
40
|
-
*
|
41
|
-
* @param {(()=>Promise<R>) | (() => R)} fn
|
42
|
-
*/
|
43
|
-
const withTime = async (fn) => {
|
44
|
-
const n1 = Date.now() ;
|
45
|
-
const r = await fn();
|
46
|
-
const delta = Date.now() - n1;
|
47
|
-
console.log(delta);
|
48
|
-
return r;
|
49
|
-
}
|
50
|
-
|
51
|
-
async function test() {
|
52
|
-
const app = await withTime(create_app);
|
53
|
-
|
54
|
-
await migrateToLatest(app.db, false);
|
55
|
-
|
56
|
-
|
57
|
-
const doit = async () => {
|
58
|
-
let items = await app.db.resources.search.quicksearch(
|
59
|
-
{
|
60
|
-
vql: 'ship 2',
|
61
|
-
sortBy: ['updated_at']
|
62
|
-
}
|
63
|
-
);
|
64
|
-
return items;
|
65
|
-
}
|
66
|
-
|
67
|
-
const items = await withTime(doit);
|
68
|
-
|
69
|
-
// console.log('items ', items)
|
70
|
-
}
|
71
|
-
|
72
|
-
test();
|
73
|
-
|