@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.
@@ -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
- }
@@ -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
-