@storecraft/database-sqlite 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.
@@ -0,0 +1,223 @@
1
+ import { sql} from 'kysely'
2
+ import { extract_first_selection } from './con.helpers.json.js';
3
+
4
+
5
+ /**
6
+ * A SQLite helper for aggregating a subquery into a JSON array.
7
+ *
8
+ * NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
9
+ * Otherwise the nested selections will be returned as JSON strings.
10
+ *
11
+ * The plugin can be installed like this:
12
+ *
13
+ * ```ts
14
+ * const db = new Kysely({
15
+ * dialect: new SqliteDialect(config),
16
+ * plugins: [new ParseJSONResultsPlugin()]
17
+ * })
18
+ * ```
19
+ *
20
+ * ### Examples
21
+ *
22
+ * ```ts
23
+ * const result = await db
24
+ * .selectFrom('person')
25
+ * .select((eb) => [
26
+ * 'id',
27
+ * stringArrayFrom(
28
+ * eb.selectFrom('pet')
29
+ * .select('pet.name')
30
+ * .whereRef('pet.owner_id', '=', 'person.id')
31
+ * .orderBy('pet.name')
32
+ * ).as('pets')
33
+ * ])
34
+ * .execute()
35
+ *
36
+ * result[0].pets = ['name1', 'name2', ....]
37
+ * ```
38
+ *
39
+ * The generated SQL (SQLite):
40
+ *
41
+ * ```sql
42
+ * select "id", (
43
+ * select coalesce(json_group_array("agg"."name"), '[]') from (
44
+ * select "pet"."name"
45
+ * from "pet"
46
+ * where "pet"."owner_id" = "person"."id"
47
+ * order by "pet"."name"
48
+ * ) as "agg"
49
+ * ) as "pets"
50
+ * from "person"
51
+ * ```
52
+ * @template O
53
+ * @param {import('./con.helpers.json.js').SelectQueryBuilderExpression<O>} expr
54
+ * @returns {import('kysely').RawBuilder<import('kysely').Simplify<O>[]>}
55
+ */
56
+ export function pg_stringArrayFrom(expr) {
57
+ const arg = extract_first_selection(expr, 'agg');
58
+ return sql`(select coalesce(json_agg(${sql.join([arg])}), '[]') from ${expr} as agg)`
59
+ }
60
+
61
+ /**
62
+ * A postgres helper for aggregating a subquery (or other expression) into a JSONB array.
63
+ *
64
+ * ### Examples
65
+ *
66
+ * <!-- siteExample("select", "Nested array", 110) -->
67
+ *
68
+ * While kysely is not an ORM and it doesn't have the concept of relations, we do provide
69
+ * helpers for fetching nested objects and arrays in a single query. In this example we
70
+ * use the `jsonArrayFrom` helper to fetch person's pets along with the person's id.
71
+ *
72
+ * Please keep in mind that the helpers under the `kysely/helpers` folder, including
73
+ * `jsonArrayFrom`, are not guaranteed to work with third party dialects. In order for
74
+ * them to work, the dialect must automatically parse the `json` data type into
75
+ * javascript JSON values like objects and arrays. Some dialects might simply return
76
+ * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
77
+ * to parse the results.
78
+ *
79
+ * ```ts
80
+ * import { jsonArrayFrom } from 'kysely/helpers/postgres'
81
+ *
82
+ * const result = await db
83
+ * .selectFrom('person')
84
+ * .select((eb) => [
85
+ * 'id',
86
+ * jsonArrayFrom(
87
+ * eb.selectFrom('pet')
88
+ * .select(['pet.id as pet_id', 'pet.name'])
89
+ * .whereRef('pet.owner_id', '=', 'person.id')
90
+ * .orderBy('pet.name')
91
+ * ).as('pets')
92
+ * ])
93
+ * .execute()
94
+ * ```
95
+ *
96
+ * The generated SQL (PostgreSQL):
97
+ *
98
+ * ```sql
99
+ * select "id", (
100
+ * select coalesce(json_agg(agg), '[]') from (
101
+ * select "pet"."id" as "pet_id", "pet"."name"
102
+ * from "pet"
103
+ * where "pet"."owner_id" = "person"."id"
104
+ * order by "pet"."name"
105
+ * ) as agg
106
+ * ) as "pets"
107
+ * from "person"
108
+ * ```
109
+ * @template O
110
+ * @param {import('kysely').Expression<O>} expr
111
+ * @returns {import('kysely').RawBuilder<import('kysely').Simplify<O>[]>}
112
+ */
113
+ export function pg_jsonArrayFrom(expr) {
114
+ return sql`(select coalesce(json_agg(agg), '[]') from ${expr} as agg)`
115
+ }
116
+
117
+ /**
118
+ * A postgres helper for turning a subquery (or other expression) into a JSON object.
119
+ *
120
+ * The subquery must only return one row.
121
+ *
122
+ * ### Examples
123
+ *
124
+ * <!-- siteExample("select", "Nested object", 120) -->
125
+ *
126
+ * While kysely is not an ORM and it doesn't have the concept of relations, we do provide
127
+ * helpers for fetching nested objects and arrays in a single query. In this example we
128
+ * use the `jsonObjectFrom` helper to fetch person's favorite pet along with the person's id.
129
+ *
130
+ * Please keep in mind that the helpers under the `kysely/helpers` folder, including
131
+ * `jsonObjectFrom`, are not guaranteed to work with 3rd party dialects. In order for
132
+ * them to work, the dialect must automatically parse the `json` data type into
133
+ * javascript JSON values like objects and arrays. Some dialects might simply return
134
+ * the data as a JSON string. In these cases you can use the built in `ParseJSONResultsPlugin`
135
+ * to parse the results.
136
+ *
137
+ * ```ts
138
+ * import { jsonObjectFrom } from 'kysely/helpers/postgres'
139
+ *
140
+ * const result = await db
141
+ * .selectFrom('person')
142
+ * .select((eb) => [
143
+ * 'id',
144
+ * jsonObjectFrom(
145
+ * eb.selectFrom('pet')
146
+ * .select(['pet.id as pet_id', 'pet.name'])
147
+ * .whereRef('pet.owner_id', '=', 'person.id')
148
+ * .where('pet.is_favorite', '=', true)
149
+ * ).as('favorite_pet')
150
+ * ])
151
+ * .execute()
152
+ * ```
153
+ *
154
+ * The generated SQL (PostgreSQL):
155
+ *
156
+ * ```sql
157
+ * select "id", (
158
+ * select to_json(obj) from (
159
+ * select "pet"."id" as "pet_id", "pet"."name"
160
+ * from "pet"
161
+ * where "pet"."owner_id" = "person"."id"
162
+ * and "pet"."is_favorite" = $1
163
+ * ) as obj
164
+ * ) as "favorite_pet"
165
+ * from "person"
166
+ * ```
167
+ * @template O
168
+ * @param {import('kysely').Expression<O>} expr
169
+ * @returns {import('kysely').RawBuilder<import('kysely').Simplify<O> | null>}
170
+ */
171
+ export function pg_jsonObjectFrom(expr) {
172
+ return sql`(select to_json(obj) from ${expr} as obj)`
173
+ }
174
+
175
+ /**
176
+ * The PostgreSQL `json_build_object` function.
177
+ *
178
+ * NOTE: This helper is only guaranteed to fully work with the built-in `PostgresDialect`.
179
+ * While the produced SQL is compatible with all PostgreSQL databases, some 3rd party dialects
180
+ * may not parse the nested JSON into objects. In these cases you can use the built in
181
+ * `ParseJSONResultsPlugin` to parse the results.
182
+ *
183
+ * ### Examples
184
+ *
185
+ * ```ts
186
+ * const result = await db
187
+ * .selectFrom('person')
188
+ * .select((eb) => [
189
+ * 'id',
190
+ * jsonBuildObject({
191
+ * first: eb.ref('first_name'),
192
+ * last: eb.ref('last_name'),
193
+ * full: sql<string>`first_name || ' ' || last_name`
194
+ * }).as('name')
195
+ * ])
196
+ * .execute()
197
+ *
198
+ * result[0].id
199
+ * result[0].name.first
200
+ * result[0].name.last
201
+ * result[0].name.full
202
+ * ```
203
+ *
204
+ * The generated SQL (PostgreSQL):
205
+ *
206
+ * ```sql
207
+ * select "id", json_build_object(
208
+ * 'first', first_name,
209
+ * 'last', last_name,
210
+ * 'full', first_name || ' ' || last_name
211
+ * ) as "name"
212
+ * from "person"
213
+ * ```
214
+ * @template {Record<string, import('kysely').Expression<unknown>>} O
215
+ * @param {O} obj
216
+ * @returns { import('kysely').RawBuilder<import('kysely').Simplify<{[K in keyof O]: O[K] extends Expression<infer V> ? V : never}>>}
217
+ >}
218
+ */
219
+ export function pg_jsonBuildObject(obj) {
220
+ return sql`json_build_object(${sql.join(
221
+ Object.keys(obj).flatMap((k) => [sql.lit(k), obj[k]]),
222
+ )})`
223
+ }
@@ -0,0 +1,263 @@
1
+ import { SelectQueryNode, sql} from 'kysely'
2
+ import { extract_first_selection, getJsonObjectArgs } from './con.helpers.json.js';
3
+
4
+ /**
5
+ * A SQLite helper for aggregating a subquery into a JSON array.
6
+ *
7
+ * NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
8
+ * Otherwise the nested selections will be returned as JSON strings.
9
+ *
10
+ * The plugin can be installed like this:
11
+ *
12
+ * ```ts
13
+ * const db = new Kysely({
14
+ * dialect: new SqliteDialect(config),
15
+ * plugins: [new ParseJSONResultsPlugin()]
16
+ * })
17
+ * ```
18
+ *
19
+ * ### Examples
20
+ *
21
+ * ```ts
22
+ * const result = await db
23
+ * .selectFrom('person')
24
+ * .select((eb) => [
25
+ * 'id',
26
+ * jsonArrayFrom(
27
+ * eb.selectFrom('pet')
28
+ * .select(['pet.id as pet_id', 'pet.name'])
29
+ * .whereRef('pet.owner_id', '=', 'person.id')
30
+ * .orderBy('pet.name')
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 (SQLite):
41
+ *
42
+ * ```sql
43
+ * select "id", (
44
+ * select coalesce(json_group_array(json_object(
45
+ * 'pet_id', "agg"."pet_id",
46
+ * 'name', "agg"."name"
47
+ * )), '[]') from (
48
+ * select "pet"."id" as "pet_id", "pet"."name"
49
+ * from "pet"
50
+ * where "pet"."owner_id" = "person"."id"
51
+ * order by "pet"."name"
52
+ * ) as "agg"
53
+ * ) as "pets"
54
+ * from "person"
55
+ * ```
56
+ * @template O
57
+ * @param {import('./con.helpers.json.js').SelectQueryBuilderExpression<O>} expr
58
+ * @returns {import('kysely').RawBuilder<import('kysely').Simplify<O>[]>}
59
+ */
60
+ export function sqlite_jsonArrayFrom(expr) {
61
+
62
+ return sql`(select coalesce(json_group_array(json_object(${sql.join(
63
+ getSqliteJsonObjectArgs(expr.toOperationNode(), 'agg')
64
+ )})), '[]') from ${expr} as agg)`
65
+ }
66
+
67
+
68
+ /**
69
+ * A SQLite helper for aggregating a subquery into a JSON array.
70
+ *
71
+ * NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
72
+ * Otherwise the nested selections will be returned as JSON strings.
73
+ *
74
+ * The plugin can be installed like this:
75
+ *
76
+ * ```ts
77
+ * const db = new Kysely({
78
+ * dialect: new SqliteDialect(config),
79
+ * plugins: [new ParseJSONResultsPlugin()]
80
+ * })
81
+ * ```
82
+ *
83
+ * ### Examples
84
+ *
85
+ * ```ts
86
+ * const result = await db
87
+ * .selectFrom('person')
88
+ * .select((eb) => [
89
+ * 'id',
90
+ * stringArrayFrom(
91
+ * eb.selectFrom('pet')
92
+ * .select('pet.name')
93
+ * .whereRef('pet.owner_id', '=', 'person.id')
94
+ * .orderBy('pet.name')
95
+ * ).as('pets')
96
+ * ])
97
+ * .execute()
98
+ *
99
+ * result[0].pets = ['name1', 'name2', ....]
100
+ * ```
101
+ *
102
+ * The generated SQL (SQLite):
103
+ *
104
+ * ```sql
105
+ * select "id", (
106
+ * select coalesce(json_group_array("agg"."name"), '[]') from (
107
+ * select "pet"."name"
108
+ * from "pet"
109
+ * where "pet"."owner_id" = "person"."id"
110
+ * order by "pet"."name"
111
+ * ) as "agg"
112
+ * ) as "pets"
113
+ * from "person"
114
+ * ```
115
+ * @template O
116
+ * @param {import('./con.helpers.json.js').SelectQueryBuilderExpression<O>} expr
117
+ * @returns {import('kysely').RawBuilder<import('kysely').Simplify<O>[]>}
118
+ */
119
+ export function sqlite_stringArrayFrom(expr) {
120
+ const arg = extract_first_selection(expr, 'agg');
121
+ return sql`(select coalesce(json_group_array(${sql.join([arg])}), '[]') from ${expr} as agg)`
122
+ }
123
+
124
+ /**
125
+ * A SQLite 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 SqliteDialect(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', '=', true)
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 (SQLite):
163
+ *
164
+ * ```sql
165
+ * select "id", (
166
+ * select json_object(
167
+ * 'pet_id', "obj"."pet_id",
168
+ * 'name', "obj"."name"
169
+ * ) from (
170
+ * select "pet"."id" as "pet_id", "pet"."name"
171
+ * from "pet"
172
+ * where "pet"."owner_id" = "person"."id"
173
+ * and "pet"."is_favorite" = ?
174
+ * ) as obj
175
+ * ) as "favorite_pet"
176
+ * from "person";
177
+ * ```
178
+ *
179
+ * @template O
180
+ * @param {import('./con.helpers.json.js').SelectQueryBuilderExpression<O>} expr
181
+ * @returns {import('kysely').RawBuilder<import('kysely').Simplify<O> | null>}
182
+ */
183
+ export function sqlite_jsonObjectFrom(expr) {
184
+ return sql`(select json_object(${sql.join(
185
+ getSqliteJsonObjectArgs(expr.toOperationNode(), 'obj'),
186
+ )}) from ${expr} as obj)`
187
+ }
188
+
189
+ /**
190
+ * The SQLite `json_object` function.
191
+ *
192
+ * NOTE: This helper only works correctly if you've installed the `ParseJSONResultsPlugin`.
193
+ * Otherwise the nested selections will be returned as JSON strings.
194
+ *
195
+ * The plugin can be installed like this:
196
+ *
197
+ * ```ts
198
+ * const db = new Kysely({
199
+ * dialect: new SqliteDialect(config),
200
+ * plugins: [new ParseJSONResultsPlugin()]
201
+ * })
202
+ * ```
203
+ *
204
+ * ### Examples
205
+ *
206
+ * ```ts
207
+ * const result = await db
208
+ * .selectFrom('person')
209
+ * .select((eb) => [
210
+ * 'id',
211
+ * jsonBuildObject({
212
+ * first: eb.ref('first_name'),
213
+ * last: eb.ref('last_name'),
214
+ * full: sql<string>`first_name || ' ' || last_name`
215
+ * }).as('name')
216
+ * ])
217
+ * .execute()
218
+ *
219
+ * result[0].id
220
+ * result[0].name.first
221
+ * result[0].name.last
222
+ * result[0].name.full
223
+ * ```
224
+ *
225
+ * The generated SQL (SQLite):
226
+ *
227
+ * ```sql
228
+ * select "id", json_object(
229
+ * 'first', first_name,
230
+ * 'last', last_name,
231
+ * 'full', "first_name" || ' ' || "last_name"
232
+ * ) as "name"
233
+ * from "person"
234
+ * ```
235
+ */
236
+ // export function jsonBuildObject<O extends Record<string, Expression<unknown>>>(
237
+ // obj: O,
238
+ // ): RawBuilder<
239
+ // Simplify<{
240
+ // [K in keyof O]: O[K] extends Expression<infer V> ? V : never
241
+ // }>
242
+ // > {
243
+ // return sql`json_object(${sql.join(
244
+ // Object.keys(obj).flatMap((k) => [sql.lit(k), obj[k]]),
245
+ // )})`
246
+ // }
247
+
248
+
249
+ /**
250
+ *
251
+ * @param {SelectQueryNode} node
252
+ * @param {string} table
253
+ * @returns {import('kysely').Expression<unknown>[]}
254
+ */
255
+ function getSqliteJsonObjectArgs(node, table) {
256
+ try {
257
+ return getJsonObjectArgs(node, table)
258
+ } catch {
259
+ throw new Error(
260
+ 'SQLite jsonArrayFrom and jsonObjectFrom functions can only handle explicit selections due to limitations of the json_object function. selectAll() is not allowed in the subquery.',
261
+ )
262
+ }
263
+ }
@@ -0,0 +1,230 @@
1
+ import { func } from '@storecraft/core/v-api'
2
+ import { SQL } from '../index.js'
3
+ import { count_regular, delete_me, delete_search_of,
4
+ insert_search_of, regular_upsert_me, where_id_or_handle_table
5
+ } from './con.shared.js'
6
+ import { sanitize_array } from './utils.funcs.js'
7
+ import { query_to_eb, query_to_sort } from './utils.query.js'
8
+ import { Transaction } from 'kysely'
9
+ import { ID } from '@storecraft/core/v-api/utils.func.js'
10
+ import {
11
+ image_url_to_handle, image_url_to_name
12
+ } from '@storecraft/core/v-api/con.images.logic.js'
13
+
14
+ /**
15
+ * @typedef {import('@storecraft/core/v-database').db_images} db_col
16
+ */
17
+ export const table_name = 'images'
18
+
19
+ /**
20
+ * @param {SQL} driver
21
+ * @returns {db_col["upsert"]}
22
+ */
23
+ const upsert = (driver) => {
24
+ return async (item, search_terms) => {
25
+ const c = driver.client;
26
+ try {
27
+ const t = await c.transaction().execute(
28
+ async (trx) => {
29
+ await insert_search_of(
30
+ trx, search_terms, item.id, item.handle, table_name
31
+ );
32
+ await regular_upsert_me(trx, table_name, {
33
+ created_at: item.created_at,
34
+ updated_at: item.updated_at,
35
+ id: item.id,
36
+ handle: item.handle,
37
+ name: item.name,
38
+ url: item.url,
39
+ });
40
+ }
41
+ );
42
+ } catch(e) {
43
+ console.log(e);
44
+ return false;
45
+ }
46
+ return true;
47
+ }
48
+ }
49
+
50
+
51
+ /**
52
+ * @param {SQL} driver
53
+ * @returns {db_col["get"]}
54
+ */
55
+ const get = (driver) => {
56
+ return (id_or_handle, options) => {
57
+ return driver.client
58
+ .selectFrom(table_name)
59
+ .selectAll()
60
+ .where(where_id_or_handle_table(id_or_handle))
61
+ .executeTakeFirst();
62
+ }
63
+ }
64
+
65
+
66
+ /**
67
+ * @param {SQL} driver
68
+ * @returns {db_col["remove"]}
69
+ */
70
+ const remove = (driver) => {
71
+ return async (id_or_handle) => {
72
+ const img = await driver.client
73
+ .selectFrom(table_name)
74
+ .selectAll()
75
+ .where(where_id_or_handle_table(id_or_handle))
76
+ .executeTakeFirst();
77
+
78
+ try {
79
+ await driver.client.transaction().execute(
80
+ async (trx) => {
81
+ // remove images -> media
82
+ await trx
83
+ .deleteFrom('entity_to_media')
84
+ .where('value', '=', img.url)
85
+ .execute();
86
+ // entities
87
+ await delete_search_of(trx, id_or_handle);
88
+ // delete me
89
+ await delete_me(trx, table_name, id_or_handle);
90
+ }
91
+ );
92
+
93
+ } catch(e) {
94
+ console.log(e);
95
+ return false;
96
+ }
97
+ return true;
98
+ }
99
+ }
100
+
101
+ /**
102
+ * report media usages
103
+ * @param {SQL} driver
104
+ * @returns {db_col["report_document_media"]}
105
+ */
106
+ export const report_document_media = (driver) => {
107
+ /**
108
+ * @param {Transaction<import('../index.js').Database>} [transaction]
109
+ */
110
+ return async (item, transaction) => {
111
+ if(!(item?.media?.length))
112
+ return;
113
+
114
+ /**
115
+ *
116
+ * @param {Transaction<import('../index.js').Database>} trx
117
+ */
118
+ const doit = async (trx) => {
119
+ const dates = func.apply_dates({});
120
+
121
+ const ms = item.media.map(
122
+ m => (
123
+ {
124
+ handle: image_url_to_handle(m),
125
+ url: m,
126
+ name: image_url_to_name(m),
127
+ id: ID('img'),
128
+ created_at: dates.created_at,
129
+ updated_at: dates.updated_at,
130
+ }
131
+ )
132
+ );
133
+ const handles = ms.map(m => m.handle);
134
+
135
+ await trx.deleteFrom(table_name).where(
136
+ 'handle', 'in', handles
137
+ ).execute();
138
+ await trx.insertInto(table_name).values(
139
+ ms
140
+ ).execute();
141
+ // search stuff
142
+ // remove by reporter
143
+ await trx.deleteFrom('entity_to_search_terms').where(
144
+ eb => eb.and([
145
+ eb('reporter', '=', item.id),
146
+ eb('context', '=', table_name),
147
+ ]
148
+ )
149
+ ).execute();
150
+ const search = func.union(
151
+ item['title'], func.to_tokens(item['title'])
152
+ );
153
+ if(search.length) {
154
+ const A = ms.map(m => ({
155
+ entity_id: m.id,
156
+ entity_handle: m.handle,
157
+ context: table_name,
158
+ reporter: item.id
159
+ })
160
+ );
161
+
162
+ const B = search.reduce(
163
+ (p, c) => {
164
+ p.push(...A.map(a => ({...a, value: c})));
165
+ return p;
166
+ }, []
167
+ );
168
+
169
+ await trx.insertInto('entity_to_search_terms').values(
170
+ B
171
+ ).execute();
172
+ }
173
+
174
+ }
175
+
176
+ if(transaction) {
177
+ await doit(transaction);
178
+ } else {
179
+ try {
180
+ const t = await driver.client
181
+ .transaction()
182
+ .execute(doit);
183
+ } catch(e) {
184
+ console.log(e);
185
+ }
186
+ }
187
+ }
188
+ }
189
+
190
+ /**
191
+ * @param {SQL} driver
192
+ * @returns {db_col["list"]}
193
+ */
194
+ const list = (driver) => {
195
+ return async (query) => {
196
+
197
+ const items = await driver.client
198
+ .selectFrom(table_name)
199
+ .selectAll()
200
+ .where(
201
+ (eb) => {
202
+ return query_to_eb(eb, query, table_name);
203
+ }
204
+ )
205
+ .orderBy(query_to_sort(query))
206
+ .limit(query.limitToLast ?? query.limit ?? 10)
207
+ .execute();
208
+
209
+ if(query.limitToLast) items.reverse();
210
+
211
+ return sanitize_array(items);
212
+ }
213
+ }
214
+
215
+
216
+ /**
217
+ * @param {SQL} driver
218
+ * @return {db_col}}
219
+ * */
220
+ export const impl = (driver) => {
221
+
222
+ return {
223
+ get: get(driver),
224
+ upsert: upsert(driver),
225
+ remove: remove(driver),
226
+ list: list(driver),
227
+ report_document_media: report_document_media(driver),
228
+ count: count_regular(driver, table_name),
229
+ }
230
+ }