@technicity/data-service-generator 0.2.8 → 0.4.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.
Files changed (43) hide show
  1. package/dist/CustomError.d.ts +3 -0
  2. package/dist/SDKNotFoundError.d.ts +4 -0
  3. package/dist/addNullFallbacks.d.ts +1 -0
  4. package/dist/addNullFallbacks.js +3 -0
  5. package/dist/cursor.d.ts +2 -0
  6. package/dist/generate.d.ts +20 -0
  7. package/dist/generate.js +100 -111
  8. package/dist/getDateTimeStringMySQL.d.ts +1 -0
  9. package/dist/getDuplicates.d.ts +1 -0
  10. package/dist/getOrderBy.d.ts +5 -0
  11. package/dist/getOrderBy.js +54 -0
  12. package/dist/getSqlAst.d.ts +2 -0
  13. package/dist/getSqlAst.js +4 -34
  14. package/dist/getWhere.d.ts +2 -0
  15. package/dist/getWhere.js +1 -2
  16. package/dist/index.d.ts +2 -0
  17. package/dist/index.js +3 -1
  18. package/dist/isNotNullOrUndefined.d.ts +1 -0
  19. package/dist/ksql.d.ts +9 -0
  20. package/dist/ksql.js +53 -0
  21. package/dist/mysql.d.ts +2 -0
  22. package/dist/mysql.js +0 -3
  23. package/dist/runTransforms.d.ts +2 -0
  24. package/dist/runtime/IRuntime.d.ts +47 -0
  25. package/dist/{IDialect.js → runtime/IRuntime.js} +0 -0
  26. package/dist/runtime/RuntimeKSQL.d.ts +19 -0
  27. package/dist/runtime/RuntimeKSQL.js +128 -0
  28. package/dist/runtime/RuntimeMSSQL.d.ts +21 -0
  29. package/dist/runtime/RuntimeMSSQL.js +88 -0
  30. package/dist/runtime/RuntimeMySQL.d.ts +20 -0
  31. package/dist/runtime/RuntimeMySQL.js +117 -0
  32. package/dist/runtime/__MSSQL.d.ts +7 -0
  33. package/dist/runtime/__MSSQL.js +46 -0
  34. package/dist/runtime/__shared.d.ts +32 -0
  35. package/dist/{runtime.js → runtime/__shared.js} +213 -230
  36. package/dist/runtime/__typeCastMSSQL.d.ts +3 -0
  37. package/dist/runtime/__typeCastMSSQL.js +40 -0
  38. package/dist/stringifyWhere.d.ts +18 -0
  39. package/dist/stringifyWhere.js +41 -50
  40. package/dist/types.d.ts +142 -0
  41. package/dist/types.js +2 -0
  42. package/package.json +1 -1
  43. package/dist/driverOpts.js +0 -54
@@ -0,0 +1,3 @@
1
+ export declare class CustomError extends Error {
2
+ constructor(message: string);
3
+ }
@@ -0,0 +1,4 @@
1
+ import { CustomError } from "./CustomError";
2
+ export declare class SDKNotFoundError extends CustomError {
3
+ constructor();
4
+ }
@@ -0,0 +1 @@
1
+ export declare function addNullFallbacks(sqlAST: any, data: any): void;
@@ -4,6 +4,9 @@
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
5
  exports.addNullFallbacks = void 0;
6
6
  function addNullFallbacks(sqlAST, data) {
7
+ if (data == null) {
8
+ return;
9
+ }
7
10
  for (let child of sqlAST.children ?? []) {
8
11
  if (child.type === "table") {
9
12
  if (Array.isArray(data)) {
@@ -0,0 +1,2 @@
1
+ export declare function encodeCursor(cursor: string): string;
2
+ export declare function decodeCursor(cursor: string): any;
@@ -0,0 +1,20 @@
1
+ import type { IDialect, ISupplementClientOpts } from "./types";
2
+ declare type ISpecialCaseUuidColumn = boolean;
3
+ declare type IIncludeMappedFields = boolean;
4
+ declare type IGenerateInput = {
5
+ dialect: IDialect;
6
+ database: string;
7
+ user: string;
8
+ password?: string;
9
+ host?: string;
10
+ port?: number;
11
+ server?: string;
12
+ outdir: string;
13
+ tables?: Array<string>;
14
+ excludeTables?: Array<string>;
15
+ specialCaseUuidColumn?: ISpecialCaseUuidColumn;
16
+ includeMappedFields?: IIncludeMappedFields;
17
+ supplementClientOpts?: ISupplementClientOpts;
18
+ };
19
+ export declare function generate(input: IGenerateInput): Promise<void>;
20
+ export {};
package/dist/generate.js CHANGED
@@ -32,7 +32,7 @@ async function generate(input) {
32
32
  }
33
33
  const specialCaseUuidColumn = input.specialCaseUuidColumn ?? true;
34
34
  const includeMappedFields = input.includeMappedFields ?? true;
35
- const supplementDriverOpts = input.supplementDriverOpts ?? true;
35
+ const supplementClientOpts = input.supplementClientOpts ?? true;
36
36
  dialect = input.dialect;
37
37
  init(input);
38
38
  let tables = await getTableNames();
@@ -56,7 +56,7 @@ async function generate(input) {
56
56
  getDeleteListData(x),
57
57
  ]));
58
58
  const artifactsSource = await getArtifactsSource(tables, includeMappedFields);
59
- const sdkSource = await getSDKSource(data, specialCaseUuidColumn, supplementDriverOpts);
59
+ const sdkSource = await getSDKSource(data, specialCaseUuidColumn, supplementClientOpts);
60
60
  const sdkFilename = "index.ts";
61
61
  const artifactsFilename = "artifacts.ts";
62
62
  const tsConfigJSON = {
@@ -88,8 +88,7 @@ async function generate(input) {
88
88
  fs.writeFileSync(path.join(tmpDirPath, artifactsFilename), artifactsSource);
89
89
  const typesDirPath = path.join(tmpDirPath, "types");
90
90
  fse.mkdirpSync(typesDirPath);
91
- fs.writeFileSync(path.join(typesDirPath, "Paginate.ts"), getTypePaginate());
92
- fs.writeFileSync(path.join(typesDirPath, "ListPaginated.ts"), getTypeListPaginated());
91
+ fs.writeFileSync(path.join(typesDirPath, "_shared.ts"), getTypeShared());
93
92
  for (let x of data) {
94
93
  if (x.kind === "getOne") {
95
94
  fs.writeFileSync(path.join(typesDirPath, x.typeFieldsName + ".ts"), x.typeFields);
@@ -117,10 +116,6 @@ async function generate(input) {
117
116
  child_process.execSync(`npx -p @vercel/ncc@${nccVersion} ncc build ./${sdkFilename} -o ${tmpBuildOutputPath} -e ./artifacts`, { cwd: tmpDirPath, stdio: "inherit" });
118
117
  // TODO: workaround for artifacts.js not being output by ncc
119
118
  fs.writeFileSync(path.join(tmpBuildOutputPath, "artifacts.js"), artifactsSource.replace("export const artifacts = ", "module.exports.artifacts = "));
120
- // TODO: workaround for SDKNotFoundError not being imported properly
121
- fs.writeFileSync(path.join(tmpBuildOutputPath, "index.d.ts"), fs
122
- .readFileSync(path.join(tmpBuildOutputPath, "index.d.ts"), "utf-8")
123
- .replace('import { SDKNotFoundError } from "./src/runtime";', '// @ts-ignore\nimport { SDKNotFoundError } from "./src/runtime";'));
124
119
  if (!fs.existsSync(outdir)) {
125
120
  fse.mkdirpSync(outdir);
126
121
  }
@@ -141,7 +136,7 @@ function init(input) {
141
136
  });
142
137
  query = mysql.query;
143
138
  }
144
- if (dialect === "mssql") {
139
+ if (dialect === "mssql" || dialect === "ksql") {
145
140
  const pool = new mssql.ConnectionPool({
146
141
  server: server ?? "localhost",
147
142
  user,
@@ -161,13 +156,12 @@ function init(input) {
161
156
  }
162
157
  }
163
158
  // It's a bit awkward to put __whereNeedsProcessing, __prepareWhere on the class,
164
- // but it allows us to share the same database pool, driverOpts, etc.
165
- async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts) {
159
+ // but it allows us to share the same database pool, clientOpts, etc.
160
+ async function getSDKSource(input, specialCaseUuidColumn, supplementClientOpts) {
161
+ // TODO: runtime: IRuntime
166
162
  const src = `import { v4 as uuidv4 } from "uuid";
167
- import { init, resolve, post, patch, patchList, del, deleteList, whereNeedsProcessing, _prepareWhere, SDKNotFoundError } from "./src/runtime";
168
163
  import { artifacts } from "./artifacts";
169
- import type { Paginate } from "./types/Paginate";
170
- import type { ListPaginated } from "./types/ListPaginated";
164
+ import type { Paginate, ListPaginated } from "./types/_shared";
171
165
  ${input.reduce((acc, x) => {
172
166
  if (x.kind === "getOne") {
173
167
  acc += `import type { ${x.typeReturnBaseName} } from "./types/${x.typeReturnBaseName}";`;
@@ -185,40 +179,30 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
185
179
  }
186
180
  return acc;
187
181
  }, "")}
188
-
189
- export { SDKNotFoundError };
190
-
191
- type IDialect = "mysql" | "mssql";
192
182
 
193
183
  export class SDK {
194
- dialect: IDialect;
184
+ runtime: any;
195
185
 
196
- constructor(opts: {
197
- dialect: IDialect;
198
- driverOpts: {
199
- database: string;
200
- user?: string;
201
- password?: string;
202
- host?: string;
203
- port?: string | number;
204
- [k: string]: any;
205
- }
186
+ constructor(opts: {
187
+ runtime: any;
188
+ clientOpts: { [k: string]: any; },
189
+ otherOpts?: { [k: string]: any; }
206
190
  }) {
207
- this.dialect = opts.dialect;
208
- init(this.dialect, ${supplementDriverOpts === true
209
- ? "{supplementDriverOpts: true, ...opts.driverOpts}"
210
- : "opts.driverOpts"});
191
+ opts.otherOpts = opts.otherOpts ?? {}
192
+ this.runtime = new opts.runtime(opts.clientOpts, ${supplementClientOpts === true
193
+ ? "{ supplementClientOpts: true, ...opts.otherOpts }"
194
+ : "opts.otherOpts"}, artifacts);
211
195
  }
212
196
 
213
197
  __whereNeedsProcessing(where: any) {
214
- return whereNeedsProcessing(where);
198
+ return this.runtime.whereNeedsProcessing(where);
215
199
  }
216
200
 
217
201
  async __prepareWhere(
218
202
  table: string,
219
203
  where: any
220
204
  ) {
221
- return _prepareWhere(artifacts, table, where, this.dialect);
205
+ return this.runtime._prepareWhere(artifacts, table, where);
222
206
  }
223
207
 
224
208
  ${(await Promise.all(input.flatMap(async (x) => {
@@ -239,16 +223,15 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
239
223
  .join(" | ")},
240
224
  param2?: { fields?: ${x.typeFieldsName}, correlationId?: string }
241
225
  ): Promise<${x.typeReturnBaseName}> {
242
- return resolve(
226
+ return this.runtime.resolve(
243
227
  {
244
228
  table: "${x.table}",
245
229
  fieldName: "${x.fieldName}",
246
230
  args: { $where: param1 },
247
231
  fields: param2?.fields,
248
232
  grabMany: false,
249
- artifacts,
250
- },
251
- { dialect: this.dialect }
233
+ artifacts
234
+ }
252
235
  );
253
236
  }`;
254
237
  }
@@ -257,7 +240,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
257
240
  param1: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, $limit?: number },
258
241
  param2?: { fields?: ${x.typeFieldsName}, correlationId?: string }
259
242
  ): Promise<Array<${x.typeReturnBaseName}>> {
260
- return resolve(
243
+ return this.runtime.resolve(
261
244
  {
262
245
  table: "${x.table}",
263
246
  fieldName: "${x.fieldName}",
@@ -266,8 +249,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
266
249
  grabMany: true,
267
250
  artifacts,
268
251
  kind: "list"
269
- },
270
- { dialect: this.dialect }
252
+ }
271
253
  );
272
254
  }`;
273
255
  }
@@ -276,7 +258,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
276
258
  param1: { $where?: ${x.typeWhereName}, $orderBy?: ${x.typeOrderByName}, $paginate: Paginate },
277
259
  param2?: { fields?: ${x.typeFieldsName}, correlationId?: string }
278
260
  ): Promise<ListPaginated<${x.typeReturnBaseName}>> {
279
- return resolve(
261
+ return this.runtime.resolve(
280
262
  {
281
263
  table: "${x.table}",
282
264
  fieldName: "${x.fieldName}",
@@ -285,8 +267,7 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
285
267
  grabMany: true,
286
268
  artifacts,
287
269
  kind: "listPaginated"
288
- },
289
- { dialect: this.dialect }
270
+ }
290
271
  );
291
272
  }`;
292
273
  }
@@ -300,23 +281,13 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
300
281
  uniqueColumns.find((x) => x.name === "uuid")
301
282
  ? `if (!Object.prototype.hasOwnProperty.call(payload, "uuid")) { payload = {...payload, uuid: uuidv4()} }`
302
283
  : ""}
303
- const id = await post({
284
+ return this.runtime.post({
304
285
  table: "${x.table}",
305
286
  data: payload,
306
287
  artifacts,
307
- dialect: this.dialect
288
+ fieldName: "${x.fieldName}",
289
+ fields: param2?.fields,
308
290
  });
309
- return resolve(
310
- {
311
- table: "${x.table}",
312
- fieldName: "${x.fieldName}",
313
- args: { $where: { id } },
314
- fields: param2?.fields,
315
- grabMany: false,
316
- artifacts,
317
- },
318
- { dialect: this.dialect }
319
- );
320
291
  }`;
321
292
  }
322
293
  if (x.kind === "patchOne") {
@@ -327,24 +298,14 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
327
298
  data: ${x.typeDataName},
328
299
  param2?: { fields?: ${x.typeFieldsName}, correlationId?: string }
329
300
  ): Promise<${x.typeReturnBaseName}> {
330
- await patch({
301
+ return this.runtime.patch({
331
302
  table: "${x.table}",
332
- findOne: param1,
303
+ args: { $where: param1 },
333
304
  data,
334
305
  artifacts,
335
- dialect: this.dialect
306
+ fieldName: "${x.fieldName}",
307
+ fields: param2?.fields,
336
308
  });
337
- return resolve(
338
- {
339
- table: "${x.table}",
340
- fieldName: "${x.fieldName}",
341
- args: { $where: param1 },
342
- fields: param2?.fields,
343
- grabMany: false,
344
- artifacts,
345
- },
346
- { dialect: this.dialect }
347
- );
348
309
  }`;
349
310
  }
350
311
  if (x.kind === "patchList") {
@@ -353,25 +314,14 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
353
314
  data: ${x.typeDataName},
354
315
  param2?: { fields?: ${x.typeFieldsName}, correlationId?: string }
355
316
  ): Promise<Array<${x.typeReturnBaseName}>> {
356
- await patchList({
317
+ return this.runtime.patchList({
357
318
  table: "${x.table}",
358
- where: param1.$where,
319
+ args: param1,
359
320
  data,
360
321
  artifacts,
361
- dialect: this.dialect
322
+ fieldName: "${x.fieldName}",
323
+ fields: param2?.fields,
362
324
  });
363
- return resolve(
364
- {
365
- table: "${x.table}",
366
- fieldName: "${x.fieldName}",
367
- args: param1,
368
- fields: param2?.fields,
369
- grabMany: true,
370
- artifacts,
371
- kind: "list"
372
- },
373
- { dialect: this.dialect }
374
- );
375
325
  }`;
376
326
  }
377
327
  if (x.kind === "deleteOne") {
@@ -381,10 +331,9 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
381
331
  .join(" | ")},
382
332
  param2?: { correlationId?: string }
383
333
  ): Promise<void> {
384
- await del({
334
+ await this.runtime.del({
385
335
  table: "${x.table}",
386
- findOne: param1,
387
- dialect: this.dialect
336
+ args: { $where: param1 },
388
337
  });
389
338
  }`;
390
339
  }
@@ -393,11 +342,10 @@ async function getSDKSource(input, specialCaseUuidColumn, supplementDriverOpts)
393
342
  param1: { $where?: ${x.typeWhereName} },
394
343
  param2?: { correlationId?: string }
395
344
  ): Promise<void> {
396
- await deleteList({
345
+ await this.runtime.deleteList({
397
346
  table: "${x.table}",
398
- where: param1.$where,
399
- artifacts,
400
- dialect: this.dialect
347
+ args: param1,
348
+ artifacts
401
349
  });
402
350
  }`;
403
351
  }
@@ -590,6 +538,23 @@ async function getTypeDataPatch(table, name, specialCaseUuidColumn, includeMappe
590
538
  }
591
539
  }
592
540
  let properties = getJSONSchemaObjProperties(tableMeta);
541
+ let mustImportTUpdateOperationsString = false;
542
+ let mustImportTUpdateOperationsNumber = false;
543
+ for (let key in properties) {
544
+ const type = unwrapJSONType(properties[key].type);
545
+ if (type === "string") {
546
+ properties[key] = {
547
+ oneOf: [properties[key], { tsType: "TUpdateOperationsString" }],
548
+ };
549
+ mustImportTUpdateOperationsString = true;
550
+ }
551
+ else if (type === "number" || type === "integer") {
552
+ properties[key] = {
553
+ oneOf: [properties[key], { tsType: "TUpdateOperationsNumber" }],
554
+ };
555
+ mustImportTUpdateOperationsNumber = true;
556
+ }
557
+ }
593
558
  if (includeMappedFields) {
594
559
  const mappedFields = await getMappedFields(table);
595
560
  properties = {
@@ -606,7 +571,28 @@ async function getTypeDataPatch(table, name, specialCaseUuidColumn, includeMappe
606
571
  additionalProperties: false,
607
572
  required: [],
608
573
  };
609
- return json_schema_to_typescript_1.compile(jsonSchema, name);
574
+ let type = await json_schema_to_typescript_1.compile(jsonSchema, name);
575
+ if (mustImportTUpdateOperationsString || mustImportTUpdateOperationsNumber) {
576
+ const imports = [];
577
+ if (mustImportTUpdateOperationsString) {
578
+ imports.push("TUpdateOperationsString");
579
+ }
580
+ if (mustImportTUpdateOperationsNumber) {
581
+ imports.push("TUpdateOperationsNumber");
582
+ }
583
+ const importsStr = imports
584
+ .map((x) => `import type { ${x} } from "./_shared";`)
585
+ .join("\n");
586
+ type = importsStr + "\n\n" + type;
587
+ }
588
+ return type;
589
+ }
590
+ function unwrapJSONType(type) {
591
+ if (Array.isArray(type)) {
592
+ // Gets first non-null type
593
+ return type.filter((x) => x !== "null")[0];
594
+ }
595
+ return type;
610
596
  }
611
597
  async function getMappedFields(table) {
612
598
  const relationsManyToOne = await getRelationsManyToOne(table).then((xs) => xs.filter((x) => x.foreignKey.endsWith("Id")));
@@ -803,7 +789,7 @@ async function getJSONSchemaOrderBy(table) {
803
789
  ],
804
790
  };
805
791
  }
806
- function getTypePaginate() {
792
+ function getTypeShared() {
807
793
  const src = `export type Paginate = {
808
794
  first: number;
809
795
  after?: string;
@@ -812,20 +798,23 @@ function getTypePaginate() {
812
798
  last: number;
813
799
  after?: string;
814
800
  before?: string;
815
- }`;
816
- return prettier.format(src, { parser: "typescript" });
817
801
  }
818
- function getTypeListPaginated() {
819
- const src = `export type ListPaginated<T> = {
820
- paginationInfo: {
821
- hasPreviousPage: boolean,
822
- hasNextPage: boolean,
823
- startCursor: string | number,
824
- endCursor: string | number,
825
- totalCount: number,
826
- },
827
- results: Array<T>,
828
- }`;
802
+
803
+ export type ListPaginated<T> = {
804
+ paginationInfo: {
805
+ hasPreviousPage: boolean,
806
+ hasNextPage: boolean,
807
+ startCursor: string | number,
808
+ endCursor: string | number,
809
+ totalCount: number,
810
+ },
811
+ results: Array<T>,
812
+ }
813
+
814
+ export type TUpdateOperationsString = {$prepend: string} | {$append: string}
815
+
816
+ export type TUpdateOperationsNumber = {$increment: number} | {$decrement: number}
817
+ `;
829
818
  return prettier.format(src, { parser: "typescript" });
830
819
  }
831
820
  async function getTypeFields(table, name, includeMappedFields) {
@@ -1239,7 +1228,7 @@ const getTableMeta = _.memoize(async function getTableMeta(table) {
1239
1228
  if (dialect === "mysql") {
1240
1229
  return query("DESCRIBE ??", [table]).then((xs) => _.sortBy((x) => x.Field, xs));
1241
1230
  }
1242
- if (dialect === "mssql") {
1231
+ if (dialect === "mssql" || dialect === "ksql") {
1243
1232
  const primaryColumn = await query(`SELECT columns.name as COLUMN_NAME
1244
1233
  FROM sys.tables tables
1245
1234
  JOIN sys.columns columns
@@ -1437,7 +1426,7 @@ async function getTableNames() {
1437
1426
  if (dialect === "mysql") {
1438
1427
  return query("SHOW TABLES").then((xs) => xs.flatMap((x) => Object.values(x)).sort());
1439
1428
  }
1440
- if (dialect === "mssql") {
1429
+ if (dialect === "mssql" || dialect === "ksql") {
1441
1430
  return query("SELECT * FROM INFORMATION_SCHEMA.TABLES").then((xs) => xs
1442
1431
  .map((x) => x["TABLE_NAME"])
1443
1432
  .filter((x) => !x.startsWith("dbo_") && !mssqlTableExcludes.has(x))
@@ -0,0 +1 @@
1
+ export declare function getDateTimeStringMySQL(dateTimeString: string): string;
@@ -0,0 +1 @@
1
+ export declare function getDuplicates<T extends any>(arr: Array<T>): any[];
@@ -0,0 +1,5 @@
1
+ import { IArgs, IOrderBy } from "./types";
2
+ export declare function getOrderBy(args: IArgs | undefined, primaryKey: string): {
3
+ orderBy: IOrderBy | undefined;
4
+ flip: boolean;
5
+ };
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getOrderBy = void 0;
4
+ // https://gist.github.com/pcattori/2bb645d587e45c9fdbcabf5cef7a7106
5
+ function getOrderBy(args, primaryKey) {
6
+ let out = undefined;
7
+ let flip = false;
8
+ if (Array.isArray(args?.$orderBy)) {
9
+ out = args?.$orderBy.map((x) => {
10
+ const [column, direction] = Object.entries(x)[0];
11
+ return { column, direction };
12
+ });
13
+ }
14
+ else if (typeof args?.$orderBy === "object" && args.$orderBy != null) {
15
+ const [column, direction] = Object.entries(args.$orderBy)[0];
16
+ out = [{ column, direction }];
17
+ }
18
+ if (args?.$paginate?.first != null || args?.$paginate?.last != null) {
19
+ // If paginating and primaryKey is not in orderBy, add it to end.
20
+ // Not necessarily "asc"; see flip.
21
+ if (!out?.find((x) => x.column === primaryKey)) {
22
+ const orderByPrimaryKey = { column: primaryKey, direction: "asc" };
23
+ if (out == null) {
24
+ out = [orderByPrimaryKey];
25
+ }
26
+ else {
27
+ out = out.concat(orderByPrimaryKey);
28
+ }
29
+ }
30
+ flip = getFlip(args, out, primaryKey);
31
+ if (flip) {
32
+ out = flipOrderByDirection(out);
33
+ }
34
+ }
35
+ return { orderBy: out, flip };
36
+ }
37
+ exports.getOrderBy = getOrderBy;
38
+ function getFlip(args, orderBy, primaryKey) {
39
+ if (args?.$paginate?.first != null &&
40
+ orderBy?.find((x) => x.column === primaryKey)?.direction === "desc") {
41
+ return true;
42
+ }
43
+ if (args?.$paginate?.last != null &&
44
+ orderBy?.find((x) => x.column === primaryKey)?.direction === "asc") {
45
+ return true;
46
+ }
47
+ return false;
48
+ }
49
+ function flipOrderByDirection(orderBy) {
50
+ return orderBy.map((x) => ({ ...x, direction: flipDirection(x.direction) }));
51
+ }
52
+ function flipDirection(direction) {
53
+ return direction === "asc" ? "desc" : "asc";
54
+ }
@@ -0,0 +1,2 @@
1
+ import { IGetSQLASTInput } from "./types";
2
+ export declare function getSqlAst(input: IGetSQLASTInput): any;
package/dist/getSqlAst.js CHANGED
@@ -7,6 +7,7 @@ const TSqlString = require("tsqlstring");
7
7
  // @ts-ignore
8
8
  const alias_namespace_1 = require("join-monster/dist/alias-namespace");
9
9
  const _ = require("lodash/fp");
10
+ const getOrderBy_1 = require("./getOrderBy");
10
11
  const namespace = new alias_namespace_1.default(true);
11
12
  function getSqlAst(input) {
12
13
  const { table, fieldName, fields, args, grabMany, sqlJoin, sqlBatch, junction, getWhere, artifacts, rowWithCursorId, dialect, } = input;
@@ -15,7 +16,7 @@ function getSqlAst(input) {
15
16
  const format = dialect === "mysql"
16
17
  ? SqlString.format.bind(SqlString)
17
18
  : TSqlString.format.bind(TSqlString);
18
- const orderBy = args?.$orderBy == null ? undefined : getOrderBy(args, primaryKey);
19
+ const orderBy = input.orderBy ?? getOrderBy_1.getOrderBy(args, primaryKey)?.orderBy;
19
20
  let where = input.where;
20
21
  if (input.where == null) {
21
22
  where = (table, args) => {
@@ -24,7 +25,7 @@ function getSqlAst(input) {
24
25
  argsMapped = _.cloneDeep(argsMapped);
25
26
  argsMapped.$where = argsMapped.$where[0];
26
27
  }
27
- const whereResult = getWhere(table, argsMapped, primaryKey, dialect, orderBy, rowWithCursorId);
28
+ const whereResult = getWhere(table, argsMapped, dialect, orderBy, rowWithCursorId);
28
29
  if (whereResult == null) {
29
30
  return undefined;
30
31
  }
@@ -111,7 +112,7 @@ function getSqlAst(input) {
111
112
  }
112
113
  const argsMapped = _.cloneDeep(args);
113
114
  argsMapped.$where = argsMapped.$where[1];
114
- const whereResult = getWhere(table, argsMapped, artifacts[relationField.junctionTable].primaryKey, dialect, orderBy, rowWithCursorId);
115
+ const whereResult = getWhere(table, argsMapped, dialect, orderBy, rowWithCursorId);
115
116
  if (whereResult == null) {
116
117
  return undefined;
117
118
  }
@@ -198,34 +199,3 @@ function columnToASTChild(columnName, namespace, fromOtherTable) {
198
199
  function toClumsyName(keyArr) {
199
200
  return keyArr.map((name) => name.slice(0, 3)).join("#");
200
201
  }
201
- function getOrderBy(args, primaryKey) {
202
- let out = undefined;
203
- if (Array.isArray(args.$orderBy)) {
204
- out = args.$orderBy.map((x) => {
205
- const [column, direction] = Object.entries(x)[0];
206
- return { column, direction };
207
- });
208
- }
209
- else {
210
- const [column, direction] = Object.entries(args.$orderBy)[0];
211
- out = [{ column, direction }];
212
- }
213
- if (args?.$paginate?.first != null) {
214
- out = out
215
- .filter((x) => x.column !== primaryKey)
216
- // id needs to be last
217
- .concat({ column: primaryKey, direction: "asc" });
218
- }
219
- else if (args?.$paginate?.last != null) {
220
- out = out
221
- .filter((x) => x.column !== primaryKey)
222
- // Need to flip if we're paginating backwards.
223
- .map((x) => ({
224
- column: x.column,
225
- direction: x.direction === "asc" ? "desc" : "asc",
226
- }))
227
- // id needs to be last
228
- .concat({ column: primaryKey, direction: "desc" });
229
- }
230
- return out;
231
- }
@@ -0,0 +1,2 @@
1
+ import type { IOrderBy, IDialect } from "./types";
2
+ export declare function getWhere(table: string, args: any, dialect: IDialect, orderBy?: IOrderBy | undefined, rowWithCursorId?: any): string | null;
package/dist/getWhere.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getWhere = void 0;
4
4
  const stringifyWhere_1 = require("./stringifyWhere");
5
- function getWhere(table, args, primaryKey, dialect, orderBy, rowWithCursorId) {
5
+ function getWhere(table, args, dialect, orderBy, rowWithCursorId) {
6
6
  if (args?.$where == null && args?.$paginate == null) {
7
7
  return null;
8
8
  }
@@ -13,7 +13,6 @@ function getWhere(table, args, primaryKey, dialect, orderBy, rowWithCursorId) {
13
13
  dialect,
14
14
  args,
15
15
  orderBy,
16
- primaryKey,
17
16
  rowWithCursorId,
18
17
  }) || null);
19
18
  }
@@ -0,0 +1,2 @@
1
+ export { generate } from "./generate";
2
+ export { SDKNotFoundError } from "./SDKNotFoundError";
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generate = void 0;
3
+ exports.SDKNotFoundError = exports.generate = void 0;
4
4
  var generate_1 = require("./generate");
5
5
  Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return generate_1.generate; } });
6
+ var SDKNotFoundError_1 = require("./SDKNotFoundError");
7
+ Object.defineProperty(exports, "SDKNotFoundError", { enumerable: true, get: function () { return SDKNotFoundError_1.SDKNotFoundError; } });
@@ -0,0 +1 @@
1
+ export declare function isNotNullOrUndefined<T extends Object>(input: null | undefined | T): input is T;
package/dist/ksql.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export declare class KSQL {
2
+ hostname: string;
3
+ port: number;
4
+ constructor(options: {
5
+ hostname: string;
6
+ port: number;
7
+ });
8
+ streamQuery(s: string): Promise<any[]>;
9
+ }