@snowtop/ent 0.1.0-alpha63 → 0.1.0-alpha65
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/core/base.d.ts +10 -2
- package/core/ent.d.ts +28 -4
- package/core/ent.js +48 -3
- package/package.json +1 -1
- package/testutils/db/temp_db.js +1 -0
- package/testutils/parse_sql.d.ts +6 -0
- package/testutils/parse_sql.js +15 -5
package/core/base.d.ts
CHANGED
|
@@ -72,6 +72,7 @@ export interface QueryDataOptions {
|
|
|
72
72
|
orderby?: string;
|
|
73
73
|
groupby?: string;
|
|
74
74
|
limit?: number;
|
|
75
|
+
disableTransformations?: boolean;
|
|
75
76
|
}
|
|
76
77
|
export interface LoadRowOptions extends QueryableDataOptions {
|
|
77
78
|
}
|
|
@@ -85,13 +86,20 @@ export interface EditRowOptions extends CreateRowOptions {
|
|
|
85
86
|
whereClause: clause.Clause;
|
|
86
87
|
}
|
|
87
88
|
interface LoadableEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer> {
|
|
88
|
-
loaderFactory:
|
|
89
|
+
loaderFactory: LoaderFactoryWithOptions;
|
|
89
90
|
ent: EntConstructor<TEnt, TViewer>;
|
|
90
91
|
}
|
|
92
|
+
interface LoaderFactoryWithOptions extends LoaderFactory<any, Data | null> {
|
|
93
|
+
options?: SelectDataOptions;
|
|
94
|
+
}
|
|
91
95
|
export interface LoadEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer> extends LoadableEntOptions<TEnt, TViewer>, SelectBaseDataOptions {
|
|
92
96
|
fieldPrivacy?: Map<string, PrivacyPolicy>;
|
|
93
97
|
}
|
|
94
|
-
export interface
|
|
98
|
+
export interface SelectCustomDataOptions extends SelectBaseDataOptions {
|
|
99
|
+
clause?: clause.Clause;
|
|
100
|
+
loaderFactory?: LoaderFactoryWithOptions;
|
|
101
|
+
}
|
|
102
|
+
export interface LoadCustomEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer> extends SelectCustomDataOptions {
|
|
95
103
|
ent: EntConstructor<TEnt, TViewer>;
|
|
96
104
|
fieldPrivacy?: Map<string, PrivacyPolicy>;
|
|
97
105
|
}
|
package/core/ent.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Queryer, SyncQueryer } from "./db";
|
|
2
|
-
import { Viewer, Ent, ID, LoadRowsOptions, LoadRowOptions, Data, DataOptions, QueryableDataOptions, EditRowOptions, LoadEntOptions, LoadCustomEntOptions, EdgeQueryableDataOptions, Context,
|
|
2
|
+
import { Viewer, Ent, ID, LoadRowsOptions, LoadRowOptions, Data, DataOptions, QueryableDataOptions, EditRowOptions, LoadEntOptions, LoadCustomEntOptions, EdgeQueryableDataOptions, Context, CreateRowOptions, QueryDataOptions, SelectCustomDataOptions } from "./base";
|
|
3
3
|
import { Executor } from "../action/action";
|
|
4
4
|
import * as clause from "./clause";
|
|
5
5
|
import { Builder } from "../action";
|
|
@@ -15,13 +15,37 @@ export declare function loadEnts<TEnt extends Ent<TViewer>, TViewer extends View
|
|
|
15
15
|
export declare function loadEntsList<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, options: LoadEntOptions<TEnt, TViewer>, ...ids: ID[]): Promise<TEnt[]>;
|
|
16
16
|
export declare function loadEntsFromClause<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, clause: clause.Clause, options: LoadEntOptions<TEnt, TViewer>): Promise<Map<ID, TEnt>>;
|
|
17
17
|
export declare function loadCustomEnts<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, options: LoadCustomEntOptions<TEnt, TViewer>, query: CustomQuery): Promise<TEnt[]>;
|
|
18
|
-
interface
|
|
18
|
+
interface parameterizedQueryOptions {
|
|
19
19
|
query: string;
|
|
20
20
|
values?: any[];
|
|
21
21
|
logValues?: any[];
|
|
22
22
|
}
|
|
23
|
-
export declare type CustomQuery = string |
|
|
24
|
-
|
|
23
|
+
export declare type CustomQuery = string | parameterizedQueryOptions | clause.Clause | QueryDataOptions;
|
|
24
|
+
/**
|
|
25
|
+
* Note that if there's default read transformations (e.g. soft delete) and a clause is passed in
|
|
26
|
+
* either as Clause or QueryDataOptions without {disableTransformations: true}, the default transformation
|
|
27
|
+
* (e.g. soft delete) is applied.
|
|
28
|
+
*
|
|
29
|
+
* Passing a full SQL string or Paramterized SQL string doesn't apply it and the given string is sent to the
|
|
30
|
+
* database as written.
|
|
31
|
+
*
|
|
32
|
+
* e.g.
|
|
33
|
+
* Foo.loadCustom(opts, 'SELECT * FROM foo') // doesn't change the query
|
|
34
|
+
* Foo.loadCustom(opts, { query: 'SELECT * FROM foo WHERE id = ?', values: [1]}) // doesn't change the query
|
|
35
|
+
* Foo.loadCustom(opts, query.Eq('time', Date.now())) // changes the query
|
|
36
|
+
* Foo.loadCustom(opts, {
|
|
37
|
+
* clause: query.LessEq('time', Date.now()),
|
|
38
|
+
* limit: 100,
|
|
39
|
+
* orderby: 'time',
|
|
40
|
+
* }) // changes the query
|
|
41
|
+
* Foo.loadCustom(opts, {
|
|
42
|
+
* clause: query.LessEq('time', Date.now()),
|
|
43
|
+
* limit: 100,
|
|
44
|
+
* orderby: 'time',
|
|
45
|
+
* disableTransformations: false
|
|
46
|
+
* }) // doesn't change the query
|
|
47
|
+
*/
|
|
48
|
+
export declare function loadCustomData(options: SelectCustomDataOptions, query: CustomQuery, context: Context | undefined): Promise<Data[]>;
|
|
25
49
|
export declare function loadDerivedEnt<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, data: Data, loader: new (viewer: TViewer, data: Data) => TEnt): Promise<TEnt | null>;
|
|
26
50
|
export declare function loadDerivedEntX<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, data: Data, loader: new (viewer: TViewer, data: Data) => TEnt): Promise<TEnt>;
|
|
27
51
|
export declare function loadRowX(options: LoadRowOptions): Promise<Data>;
|
package/core/ent.js
CHANGED
|
@@ -226,32 +226,77 @@ function isClause(opts) {
|
|
|
226
226
|
const cls = opts;
|
|
227
227
|
return cls.clause !== undefined && cls.values !== undefined;
|
|
228
228
|
}
|
|
229
|
-
function
|
|
229
|
+
function isParameterizedQuery(opts) {
|
|
230
230
|
return opts.query !== undefined;
|
|
231
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Note that if there's default read transformations (e.g. soft delete) and a clause is passed in
|
|
234
|
+
* either as Clause or QueryDataOptions without {disableTransformations: true}, the default transformation
|
|
235
|
+
* (e.g. soft delete) is applied.
|
|
236
|
+
*
|
|
237
|
+
* Passing a full SQL string or Paramterized SQL string doesn't apply it and the given string is sent to the
|
|
238
|
+
* database as written.
|
|
239
|
+
*
|
|
240
|
+
* e.g.
|
|
241
|
+
* Foo.loadCustom(opts, 'SELECT * FROM foo') // doesn't change the query
|
|
242
|
+
* Foo.loadCustom(opts, { query: 'SELECT * FROM foo WHERE id = ?', values: [1]}) // doesn't change the query
|
|
243
|
+
* Foo.loadCustom(opts, query.Eq('time', Date.now())) // changes the query
|
|
244
|
+
* Foo.loadCustom(opts, {
|
|
245
|
+
* clause: query.LessEq('time', Date.now()),
|
|
246
|
+
* limit: 100,
|
|
247
|
+
* orderby: 'time',
|
|
248
|
+
* }) // changes the query
|
|
249
|
+
* Foo.loadCustom(opts, {
|
|
250
|
+
* clause: query.LessEq('time', Date.now()),
|
|
251
|
+
* limit: 100,
|
|
252
|
+
* orderby: 'time',
|
|
253
|
+
* disableTransformations: false
|
|
254
|
+
* }) // doesn't change the query
|
|
255
|
+
*/
|
|
232
256
|
async function loadCustomData(options, query, context) {
|
|
257
|
+
//loaderFactory not here...
|
|
258
|
+
// options.
|
|
259
|
+
function getClause(cls) {
|
|
260
|
+
let optClause = options.clause || options.loaderFactory?.options?.clause;
|
|
261
|
+
if (typeof optClause === "function") {
|
|
262
|
+
optClause = optClause();
|
|
263
|
+
}
|
|
264
|
+
if (!optClause) {
|
|
265
|
+
return cls;
|
|
266
|
+
}
|
|
267
|
+
return clause.And(cls, optClause);
|
|
268
|
+
}
|
|
233
269
|
if (typeof query === "string") {
|
|
234
270
|
// no caching, perform raw query
|
|
235
271
|
return await performRawQuery(query, [], []);
|
|
236
272
|
}
|
|
237
273
|
else if (isClause(query)) {
|
|
274
|
+
// if a Clause is passed in and we have a default clause
|
|
275
|
+
// associated with the query, pass that in
|
|
276
|
+
// if we want to disableTransformations, need to indicate that with
|
|
277
|
+
// disableTransformations option
|
|
238
278
|
// this will have rudimentary caching but nothing crazy
|
|
239
279
|
return await loadRows({
|
|
240
280
|
...options,
|
|
241
|
-
clause: query,
|
|
281
|
+
clause: getClause(query),
|
|
242
282
|
context: context,
|
|
243
283
|
});
|
|
244
284
|
}
|
|
245
|
-
else if (
|
|
285
|
+
else if (isParameterizedQuery(query)) {
|
|
246
286
|
// no caching, perform raw query
|
|
247
287
|
return await performRawQuery(query.query, query.values || [], query.logValues);
|
|
248
288
|
}
|
|
249
289
|
else {
|
|
290
|
+
let cls = query.clause;
|
|
291
|
+
if (!query.disableTransformations) {
|
|
292
|
+
cls = getClause(cls);
|
|
293
|
+
}
|
|
250
294
|
// this will have rudimentary caching but nothing crazy
|
|
251
295
|
return await loadRows({
|
|
252
296
|
...query,
|
|
253
297
|
...options,
|
|
254
298
|
context: context,
|
|
299
|
+
clause: cls,
|
|
255
300
|
});
|
|
256
301
|
}
|
|
257
302
|
}
|
package/package.json
CHANGED
package/testutils/db/temp_db.js
CHANGED
package/testutils/parse_sql.d.ts
CHANGED
|
@@ -9,6 +9,12 @@ export declare class EqOp {
|
|
|
9
9
|
constructor(col: string, value: any);
|
|
10
10
|
apply(data: Data): boolean;
|
|
11
11
|
}
|
|
12
|
+
export declare class NotEqOp {
|
|
13
|
+
private col;
|
|
14
|
+
private value;
|
|
15
|
+
constructor(col: string, value: any);
|
|
16
|
+
apply(data: Data): boolean;
|
|
17
|
+
}
|
|
12
18
|
export declare class GreaterOp {
|
|
13
19
|
private col;
|
|
14
20
|
private value;
|
package/testutils/parse_sql.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.performQuery = exports.OrOp = exports.AndOp = exports.InOp = exports.LessEqOp = exports.GreaterEqOp = exports.LessOp = exports.GreaterOp = exports.EqOp = exports.getDataToReturn = void 0;
|
|
3
|
+
exports.performQuery = exports.OrOp = exports.AndOp = exports.InOp = exports.LessEqOp = exports.GreaterEqOp = exports.LessOp = exports.GreaterOp = exports.NotEqOp = exports.EqOp = exports.getDataToReturn = void 0;
|
|
4
4
|
const node_sql_parser_1 = require("node-sql-parser");
|
|
5
5
|
function getTableName(table) {
|
|
6
6
|
if (Array.isArray(table)) {
|
|
@@ -184,6 +184,16 @@ class EqOp {
|
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
exports.EqOp = EqOp;
|
|
187
|
+
class NotEqOp {
|
|
188
|
+
constructor(col, value) {
|
|
189
|
+
this.col = col;
|
|
190
|
+
this.value = value;
|
|
191
|
+
}
|
|
192
|
+
apply(data) {
|
|
193
|
+
return data[this.col] !== this.value;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
exports.NotEqOp = NotEqOp;
|
|
187
197
|
class GreaterOp {
|
|
188
198
|
constructor(col, value) {
|
|
189
199
|
this.col = col;
|
|
@@ -308,11 +318,11 @@ function getOp(where, values) {
|
|
|
308
318
|
case "OR":
|
|
309
319
|
return new OrOp([getOp(where.left, values), getOp(where.right, values)]);
|
|
310
320
|
case "IS":
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
321
|
+
return new EqOp(getColumnFromRef(where.left), where.right?.value);
|
|
322
|
+
case "IS NOT":
|
|
323
|
+
return new NotEqOp(getColumnFromRef(where.left), where.right?.value);
|
|
314
324
|
default:
|
|
315
|
-
console.
|
|
325
|
+
console.debug(where);
|
|
316
326
|
throw new Error(`unsupported op ${where.operator}`);
|
|
317
327
|
}
|
|
318
328
|
}
|