Skip to content

Commit

Permalink
Merge pull request #70 from cerebruminc/lucianbuzzo/sql-parser
Browse files Browse the repository at this point in the history
fix: use upstream node-sql-parser package
  • Loading branch information
LucianBuzzo authored Dec 11, 2023
2 parents fcc8fcf + 91779eb commit ba82f6e
Show file tree
Hide file tree
Showing 6 changed files with 2,396 additions and 36 deletions.
42 changes: 21 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"generate": "prisma generate",
"build": "tsc",
"test": "rome ci src",
"test": "rome ci src test",
"lint:fix": "rome check --apply src test",
"test:integration": "jest test/integration",
"test:compose:integration": "docker compose -f docker-compose.yml --profile with-sut up db sut --exit-code-from sut",
"setup": "prisma generate && prisma migrate dev",
Expand All @@ -29,7 +30,7 @@
"uuid": "^9.0.0"
},
"dependencies": {
"@lucianbuzzo/node-sql-parser": "^4.6.6",
"node-sql-parser": "^4.12.0",
"lodash": "^4.17.21"
},
"peerDependencies": {
Expand Down
24 changes: 14 additions & 10 deletions src/expressions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Prisma, PrismaClient } from "@prisma/client";
import { Dictionary, get } from "lodash";
import { PrismaClient } from "@prisma/client";
import random from "lodash/random";
import matches from "lodash/matches";
import { Parser } from "@lucianbuzzo/node-sql-parser";
import { escapeIdentifier, escapeLiteral } from "./escape";
import { RuntimeDataModel } from "@prisma/client/runtime/library";
import { Parser } from "node-sql-parser";
import { escapeLiteral } from "./escape";
import { defineDmmfProperty } from "@prisma/client/runtime/library";

// This is black magic to get the runtime data model from the Prisma client
// It's not exported, so we need to use some type infiltration to get it
export type RuntimeDataModel = Parameters<typeof defineDmmfProperty>[1];

const PRISMA_NUMERIC_TYPES = ["Int", "BigInt", "Float", "Decimal"];

Expand All @@ -25,7 +28,7 @@ const deepFind = (obj: any, subObj: any): any => {
type Token = {
astFragment: any;
};
type Tokens = Dictionary<Token>;
type Tokens = Record<string, Token>;

export type Expression<ContextKeys extends string = string> =
| string
Expand Down Expand Up @@ -62,7 +65,7 @@ const tokenizeWhereExpression = (
/** The Prisma client to use for metadata */
client: PrismaClient,
/** The Prisma where expression to be tokenized */
where: Dictionary<any>,
where: Record<string, any>,
/** The base table we are generating an expression for */
table: string,
/** The model name being queried. e.g. 'User' */
Expand All @@ -71,7 +74,7 @@ const tokenizeWhereExpression = (
tokens: Tokens = {},
): {
tokens: Tokens;
where: Dictionary<any>;
where: Record<string, any>;
} => {
for (const field in where) {
// Get field data from the prisma client for the model and field being queried
Expand Down Expand Up @@ -254,8 +257,9 @@ export const expressionToSQL = async (getExpression: Expression, table: string):

const parameterizedStatement = deepFind(ast, {
right: {
type: "origin",
value: `$${i + 1}`,
type: "var",
name: i + 1,
prefix: "$",
},
});

Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Prisma, PrismaClient } from "@prisma/client";
import { RuntimeDataModel } from "@prisma/client/runtime/library";
import difference from "lodash/difference";
import flatMap from "lodash/flatMap";
import map from "lodash/map";
import toPairs from "lodash/toPairs";
import * as crypto from "crypto";
import { Expression, expressionToSQL } from "./expressions";
import { Expression, expressionToSQL, RuntimeDataModel } from "./expressions";

const VALID_OPERATIONS = ["SELECT", "UPDATE", "INSERT", "DELETE"] as const;

Expand Down
1 change: 0 additions & 1 deletion test/integration/rbac.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PrismaClient, User } from "@prisma/client";
import { setup } from "../../src";
import { v4 as uuid } from "uuid";
import { update } from "lodash";

let adminClient: PrismaClient;

Expand Down
Loading

0 comments on commit ba82f6e

Please sign in to comment.