@technicity/data-service-generator 0.22.2 → 0.23.0-next.0
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/dist/src/generation/generate.d.ts +21 -0
- package/dist/src/generation/generate.js +2349 -0
- package/dist/src/index.d.ts +4 -0
- package/dist/src/index.js +11 -0
- package/dist/src/lib/CustomError.d.ts +3 -0
- package/dist/src/lib/CustomError.js +10 -0
- package/dist/src/lib/capitalizeFirstLetter.d.ts +1 -0
- package/dist/src/lib/capitalizeFirstLetter.js +6 -0
- package/dist/src/lib/getDuplicates.d.ts +1 -0
- package/dist/src/lib/getDuplicates.js +9 -0
- package/dist/src/lib/isNotNullOrUndefined.d.ts +1 -0
- package/dist/src/lib/isNotNullOrUndefined.js +7 -0
- package/dist/src/runtime/Cache.d.ts +28 -0
- package/dist/src/runtime/Cache.js +142 -0
- package/dist/src/runtime/IRuntime.d.ts +209 -0
- package/dist/src/runtime/IRuntime.js +12 -0
- package/dist/src/runtime/RuntimeMySQL.d.ts +26 -0
- package/dist/src/runtime/RuntimeMySQL.js +132 -0
- package/dist/src/runtime/RuntimePostgreSQL.d.ts +30 -0
- package/dist/src/runtime/RuntimePostgreSQL.js +73 -0
- package/dist/src/runtime/RuntimeSQLite.d.ts +42 -0
- package/dist/src/runtime/RuntimeSQLite.js +150 -0
- package/dist/src/runtime/Stats.d.ts +8 -0
- package/dist/src/runtime/Stats.js +31 -0
- package/dist/src/runtime/lib/MySQL.d.ts +13 -0
- package/dist/src/runtime/lib/MySQL.js +116 -0
- package/dist/src/runtime/lib/PostgreSQL.d.ts +14 -0
- package/dist/src/runtime/lib/PostgreSQL.js +110 -0
- package/dist/src/runtime/lib/SDKBadWhereError.d.ts +4 -0
- package/dist/src/runtime/lib/SDKBadWhereError.js +10 -0
- package/dist/src/runtime/lib/SDKNotFoundError.d.ts +4 -0
- package/dist/src/runtime/lib/SDKNotFoundError.js +10 -0
- package/dist/src/runtime/lib/addNullFallbacks.d.ts +1 -0
- package/dist/src/runtime/lib/addNullFallbacks.js +32 -0
- package/dist/src/runtime/lib/addNullFallbacks.test.d.ts +1 -0
- package/dist/src/runtime/lib/addNullFallbacks.test.js +206 -0
- package/dist/src/runtime/lib/cursor.d.ts +2 -0
- package/dist/src/runtime/lib/cursor.js +10 -0
- package/dist/src/runtime/lib/getDateTimeStringMySQL.d.ts +1 -0
- package/dist/src/runtime/lib/getDateTimeStringMySQL.js +7 -0
- package/dist/src/runtime/lib/getOrderBy.d.ts +5 -0
- package/dist/src/runtime/lib/getOrderBy.js +52 -0
- package/dist/src/runtime/lib/getSqlAst.d.ts +2 -0
- package/dist/src/runtime/lib/getSqlAst.js +245 -0
- package/dist/src/runtime/lib/getWhere.d.ts +2 -0
- package/dist/src/runtime/lib/getWhere.js +20 -0
- package/dist/src/runtime/lib/shared.d.ts +13 -0
- package/dist/src/runtime/lib/shared.js +1118 -0
- package/dist/src/runtime/lib/stringifyWhere.d.ts +18 -0
- package/dist/src/runtime/lib/stringifyWhere.js +257 -0
- package/dist/src/runtime/lib/stringifyWhere.test.d.ts +1 -0
- package/dist/src/runtime/lib/stringifyWhere.test.js +245 -0
- package/dist/src/runtime/lib/utility.d.ts +5 -0
- package/dist/src/runtime/lib/utility.js +14 -0
- package/dist/src/traverseFieldArgs.d.ts +2 -0
- package/dist/src/traverseFieldArgs.js +17 -0
- package/dist/src/traverseFieldArgs.test.d.ts +1 -0
- package/dist/src/traverseFieldArgs.test.js +56 -0
- package/dist/test/addWhereValidTrue.d.ts +1 -0
- package/dist/test/addWhereValidTrue.js +39 -0
- package/dist/test/globalSetup.d.ts +13 -0
- package/dist/test/globalSetup.js +436 -0
- package/dist/test/postgres/__generated__/sdk-ts/artifacts.d.ts +8425 -0
- package/dist/test/postgres/__generated__/sdk-ts/artifacts.js +10469 -0
- package/dist/test/postgres/__generated__/sdk-ts/index.js +12162 -0
- package/package.json +5 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.traverseFieldArgs = exports.SDKBadWhereError = exports.SDKNotFoundError = exports.generate = void 0;
|
|
4
|
+
var generate_1 = require("./generation/generate");
|
|
5
|
+
Object.defineProperty(exports, "generate", { enumerable: true, get: function () { return generate_1.generate; } });
|
|
6
|
+
var SDKNotFoundError_1 = require("./runtime/lib/SDKNotFoundError");
|
|
7
|
+
Object.defineProperty(exports, "SDKNotFoundError", { enumerable: true, get: function () { return SDKNotFoundError_1.SDKNotFoundError; } });
|
|
8
|
+
var SDKBadWhereError_1 = require("./runtime/lib/SDKBadWhereError");
|
|
9
|
+
Object.defineProperty(exports, "SDKBadWhereError", { enumerable: true, get: function () { return SDKBadWhereError_1.SDKBadWhereError; } });
|
|
10
|
+
var traverseFieldArgs_1 = require("./traverseFieldArgs");
|
|
11
|
+
Object.defineProperty(exports, "traverseFieldArgs", { enumerable: true, get: function () { return traverseFieldArgs_1.traverseFieldArgs; } });
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CustomError = void 0;
|
|
4
|
+
class CustomError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.CustomError = CustomError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function capitalizeFirstLetter(str: string): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getDuplicates<T extends any>(arr: Array<T>): any[];
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDuplicates = getDuplicates;
|
|
4
|
+
function getDuplicates(arr) {
|
|
5
|
+
return arr.reduce((agg, col) => {
|
|
6
|
+
agg.filter[col] = agg.filter[col] ? agg.dup.push(col) : 2;
|
|
7
|
+
return agg;
|
|
8
|
+
}, { filter: {}, dup: [] }).dup;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isNotNullOrUndefined<T extends Object>(input: null | undefined | T): input is T;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import Redis, { Cluster } from "ioredis";
|
|
2
|
+
import { TResolveParams } from "./IRuntime";
|
|
3
|
+
import Stats from "./Stats";
|
|
4
|
+
export type RedisConfig = {
|
|
5
|
+
host: string;
|
|
6
|
+
port: number;
|
|
7
|
+
tls?: boolean;
|
|
8
|
+
db?: number;
|
|
9
|
+
socketTimeout?: number;
|
|
10
|
+
clusterMode?: boolean;
|
|
11
|
+
};
|
|
12
|
+
declare class Cache {
|
|
13
|
+
client: Redis | Cluster;
|
|
14
|
+
waiting?: Set<() => void> | undefined;
|
|
15
|
+
stats?: Stats;
|
|
16
|
+
constructor(redisConfig: RedisConfig, debug?: string[]);
|
|
17
|
+
debug(message: string): void;
|
|
18
|
+
pending(): Promise<void>;
|
|
19
|
+
from(input: TResolveParams): Promise<{
|
|
20
|
+
request: string;
|
|
21
|
+
cached: any;
|
|
22
|
+
}>;
|
|
23
|
+
insert(key: string, payload: any): Promise<void>;
|
|
24
|
+
read(key: string): Promise<any>;
|
|
25
|
+
purge(...uuids: string[]): Promise<void>;
|
|
26
|
+
shutdown(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export default Cache;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
const ioredis_1 = __importDefault(require("ioredis"));
|
|
8
|
+
const loglevel_1 = __importDefault(require("loglevel"));
|
|
9
|
+
const utility_1 = require("./lib/utility");
|
|
10
|
+
const Stats_1 = __importDefault(require("./Stats"));
|
|
11
|
+
class Cache {
|
|
12
|
+
constructor(redisConfig, debug) {
|
|
13
|
+
this.waiting = new Set();
|
|
14
|
+
if (debug?.includes("Cache")) {
|
|
15
|
+
loglevel_1.default.setLevel("DEBUG");
|
|
16
|
+
}
|
|
17
|
+
const { host, port } = redisConfig;
|
|
18
|
+
const tls = redisConfig.tls ? true : false;
|
|
19
|
+
const clusterMode = redisConfig.clusterMode ? true : false;
|
|
20
|
+
const db = redisConfig.db == null ? 0 : redisConfig.db;
|
|
21
|
+
const socketTimeout = redisConfig.socketTimeout == null ? 50000 : redisConfig.socketTimeout;
|
|
22
|
+
let client = undefined;
|
|
23
|
+
if (clusterMode) {
|
|
24
|
+
client = new ioredis_1.default.Cluster([
|
|
25
|
+
{
|
|
26
|
+
host,
|
|
27
|
+
port
|
|
28
|
+
}
|
|
29
|
+
], {
|
|
30
|
+
dnsLookup: (address, callback) => callback(null, address),
|
|
31
|
+
redisOptions: {
|
|
32
|
+
tls: tls ? {} : undefined,
|
|
33
|
+
lazyConnect: true,
|
|
34
|
+
commandTimeout: socketTimeout
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
client = new ioredis_1.default({
|
|
40
|
+
host,
|
|
41
|
+
port,
|
|
42
|
+
db,
|
|
43
|
+
lazyConnect: true,
|
|
44
|
+
tls: tls ? {} : undefined
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
this.client = client;
|
|
48
|
+
// call connect() if not already connected or in the process of connecting
|
|
49
|
+
if (client.status !== "connect" &&
|
|
50
|
+
client.status !== "connecting" &&
|
|
51
|
+
client.status !== "reconnecting") {
|
|
52
|
+
client.connect();
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
loglevel_1.default.info("DB SDK client status is currently: " + client.status);
|
|
56
|
+
}
|
|
57
|
+
client.on("connect", () => {
|
|
58
|
+
loglevel_1.default.info(`DB SDK connected to redis server at ${host}:${port}`);
|
|
59
|
+
if (this.waiting)
|
|
60
|
+
this.waiting.forEach((x) => x());
|
|
61
|
+
this.waiting = undefined;
|
|
62
|
+
});
|
|
63
|
+
client.on("error", (err) => {
|
|
64
|
+
loglevel_1.default.error("ERROR: redis client");
|
|
65
|
+
loglevel_1.default.error(err);
|
|
66
|
+
});
|
|
67
|
+
if (debug?.includes("Stats"))
|
|
68
|
+
this.stats = new Stats_1.default(client);
|
|
69
|
+
}
|
|
70
|
+
debug(message) {
|
|
71
|
+
loglevel_1.default.debug(`\n-- CACHE: ${message}\n`);
|
|
72
|
+
}
|
|
73
|
+
async pending() {
|
|
74
|
+
if (this.waiting)
|
|
75
|
+
return new Promise((res) => this.waiting.add(res));
|
|
76
|
+
}
|
|
77
|
+
async from(input) {
|
|
78
|
+
let { action, args, fields, resource } = input;
|
|
79
|
+
const request = JSON.stringify({
|
|
80
|
+
action,
|
|
81
|
+
resource,
|
|
82
|
+
args,
|
|
83
|
+
fields
|
|
84
|
+
});
|
|
85
|
+
const key = (0, crypto_1.createHash)("sha256").update(request).digest("hex");
|
|
86
|
+
const cached = await this.read(key);
|
|
87
|
+
return {
|
|
88
|
+
request: key,
|
|
89
|
+
cached
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
async insert(key, payload) {
|
|
93
|
+
const redis = this.client;
|
|
94
|
+
const json = JSON.stringify(payload);
|
|
95
|
+
const regex = /"uuid":"(.+?)"/g;
|
|
96
|
+
const pending = [];
|
|
97
|
+
for (let result; (result = regex.exec(json));) {
|
|
98
|
+
const uuid = result[1];
|
|
99
|
+
if (!uuid)
|
|
100
|
+
continue;
|
|
101
|
+
pending.push(redis.sadd(`deps:${uuid}`, [key]), redis.sadd(`cached:${key}`, [uuid]));
|
|
102
|
+
}
|
|
103
|
+
if (!pending.length)
|
|
104
|
+
return;
|
|
105
|
+
this.debug(`insert: ${key.substring(0, 6)}`);
|
|
106
|
+
await Promise.all([redis.set(`cache:${key}`, json), ...pending]);
|
|
107
|
+
}
|
|
108
|
+
async read(key) {
|
|
109
|
+
await this.pending();
|
|
110
|
+
const data = await this.client.get(`cache:${key}`);
|
|
111
|
+
const shorthand = key.substring(0, 6);
|
|
112
|
+
if (data) {
|
|
113
|
+
this.debug(`hit: ${shorthand}`);
|
|
114
|
+
return JSON.parse(data);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
this.debug(`miss: ${shorthand}`);
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async purge(...uuids) {
|
|
122
|
+
const redis = this.client;
|
|
123
|
+
await (0, utility_1.mapAsync)(uuids, (uuid) => {
|
|
124
|
+
const getDependancies = redis.smembers(`deps:${uuid}`);
|
|
125
|
+
return [
|
|
126
|
+
(0, utility_1.mapAsync)(getDependancies, (key) => {
|
|
127
|
+
const getDependants = redis.smembers(`cached:${key}`);
|
|
128
|
+
return [
|
|
129
|
+
(0, utility_1.mapAsync)(getDependants, (uuid) => redis.srem(`deps:${uuid}`, [key])),
|
|
130
|
+
redis.del(`cache:${key}`),
|
|
131
|
+
redis.del(`cached:${key}`)
|
|
132
|
+
];
|
|
133
|
+
}),
|
|
134
|
+
redis.del(`deps:${uuid}`)
|
|
135
|
+
];
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
async shutdown() {
|
|
139
|
+
await this.client.disconnect();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.default = Cache;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
export interface IRuntime {
|
|
3
|
+
resolve(input: TResolveParams): Promise<any>;
|
|
4
|
+
$queryRaw(sql: string, values?: any[]): Promise<any>;
|
|
5
|
+
$use: (middleware: TMiddleware) => void;
|
|
6
|
+
$whereNeedsProcessing(where: any): boolean;
|
|
7
|
+
$prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<any>;
|
|
8
|
+
$shutdown(): Promise<void>;
|
|
9
|
+
$startTransaction(input?: {
|
|
10
|
+
isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
|
|
11
|
+
}): Promise<TBeginTransactionResult>;
|
|
12
|
+
}
|
|
13
|
+
export type TAction = "findUnique" | "findMany" | "findManyPaginated" | "create" | "update" | "updateMany" | "delete" | "deleteMany";
|
|
14
|
+
export type TResolveParams = {
|
|
15
|
+
resource: string;
|
|
16
|
+
action: TAction;
|
|
17
|
+
args?: IArgs;
|
|
18
|
+
data?: any;
|
|
19
|
+
artifacts: IArtifacts;
|
|
20
|
+
fields?: TSelect;
|
|
21
|
+
context?: TContext;
|
|
22
|
+
skipCache?: boolean;
|
|
23
|
+
dbCall?: TDbCall;
|
|
24
|
+
eventTarget: EventTarget;
|
|
25
|
+
onHandler?: TOnHandler;
|
|
26
|
+
asyncLocalStorage?: AsyncLocalStorage<{
|
|
27
|
+
isInOnHandler?: boolean;
|
|
28
|
+
isTransaction?: boolean;
|
|
29
|
+
}>;
|
|
30
|
+
passBeforeValueToAfterCallback: boolean;
|
|
31
|
+
sdk?: unknown;
|
|
32
|
+
};
|
|
33
|
+
export type TOnHandler = (sdk: unknown, input: unknown, output: unknown, before: unknown, TContext: TContext) => Promise<void>;
|
|
34
|
+
export declare class EventOnHandlerError<T> extends Event {
|
|
35
|
+
error: T;
|
|
36
|
+
resource: string;
|
|
37
|
+
action: TAction;
|
|
38
|
+
constructor(message: string, data: ConstructorParameters<typeof Event>[1] & {
|
|
39
|
+
error: T;
|
|
40
|
+
resource: string;
|
|
41
|
+
action: TAction;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
export type TContext = {
|
|
45
|
+
[k: string]: any;
|
|
46
|
+
};
|
|
47
|
+
export type TMiddleware = (params: TResolveParams, next: (params: TResolveParams) => Promise<any>) => Promise<any>;
|
|
48
|
+
export type IDialect = "mysql" | "sqlite" | "postgresql";
|
|
49
|
+
export type TDbCall = (q: string) => Promise<any>;
|
|
50
|
+
export type TFormatQuery = (q: string, values: any[]) => string;
|
|
51
|
+
export type TBeginTransaction = () => Promise<TBeginTransactionResult>;
|
|
52
|
+
type TBeginTransactionResult = {
|
|
53
|
+
dbCall: (q: string) => Promise<any>;
|
|
54
|
+
commit: () => Promise<void>;
|
|
55
|
+
rollback: () => Promise<void>;
|
|
56
|
+
queryRaw: (q: string, values: any[]) => Promise<any>;
|
|
57
|
+
};
|
|
58
|
+
export type ISupplementClientOpts = boolean;
|
|
59
|
+
export type IOrderBy = {
|
|
60
|
+
column: string;
|
|
61
|
+
direction: any;
|
|
62
|
+
}[];
|
|
63
|
+
export type IArgs = {
|
|
64
|
+
[k: string]: any;
|
|
65
|
+
};
|
|
66
|
+
export type TSelect = {
|
|
67
|
+
[k: string]: boolean | {
|
|
68
|
+
$fields?: TSelect;
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
type IWhere = (table: string, args: IArgs) => string | undefined;
|
|
72
|
+
type IASTChildColumn = {
|
|
73
|
+
type: "column";
|
|
74
|
+
name: string;
|
|
75
|
+
fieldName: string;
|
|
76
|
+
as: string;
|
|
77
|
+
fromOtherTable?: string;
|
|
78
|
+
};
|
|
79
|
+
type IASTChildComposite = {
|
|
80
|
+
type: "composite";
|
|
81
|
+
name: string[];
|
|
82
|
+
fieldName: string;
|
|
83
|
+
as: string;
|
|
84
|
+
fromOtherTable?: string;
|
|
85
|
+
};
|
|
86
|
+
type ISqlJoin = (t1: string, t2: string, args: IArgs) => string;
|
|
87
|
+
type ISqlBatch = {
|
|
88
|
+
thisKey: IASTChildColumn;
|
|
89
|
+
parentKey: IASTChildColumn;
|
|
90
|
+
};
|
|
91
|
+
type IJunction = {
|
|
92
|
+
sqlTable: string;
|
|
93
|
+
as: string;
|
|
94
|
+
uniqueKey: string | string[];
|
|
95
|
+
where?: IWhere;
|
|
96
|
+
sqlBatch: {
|
|
97
|
+
thisKey: IASTChildColumn;
|
|
98
|
+
parentKey: IASTChildColumn;
|
|
99
|
+
sqlJoin: ISqlJoin;
|
|
100
|
+
};
|
|
101
|
+
} | {
|
|
102
|
+
sqlTable: string;
|
|
103
|
+
as: string;
|
|
104
|
+
where?: IWhere;
|
|
105
|
+
sqlJoins: ISqlJoin[];
|
|
106
|
+
};
|
|
107
|
+
export type IGetSQLASTInput = {
|
|
108
|
+
table: string;
|
|
109
|
+
fieldName: string;
|
|
110
|
+
fields: TSelect;
|
|
111
|
+
args?: IArgs;
|
|
112
|
+
where?: IWhere;
|
|
113
|
+
sqlJoin?: ISqlJoin;
|
|
114
|
+
sqlBatch?: ISqlBatch;
|
|
115
|
+
junction?: IJunction;
|
|
116
|
+
grabMany: boolean;
|
|
117
|
+
getWhere: (table: string, args: any, dialect: IDialect, orderBy?: IOrderBy | undefined, rowWithMatchingCursor?: any) => string | null;
|
|
118
|
+
orderBy?: IOrderBy;
|
|
119
|
+
rowWithMatchingCursor?: any;
|
|
120
|
+
artifacts: IArtifacts;
|
|
121
|
+
dialect: IDialect;
|
|
122
|
+
firstChild?: IASTChildColumn | IASTChildComposite;
|
|
123
|
+
escapeId: (x: string) => string;
|
|
124
|
+
};
|
|
125
|
+
export type IRelation = {
|
|
126
|
+
table: string;
|
|
127
|
+
foreignKey: string;
|
|
128
|
+
referencedTable: string;
|
|
129
|
+
referencedKey: string;
|
|
130
|
+
nullable: boolean;
|
|
131
|
+
};
|
|
132
|
+
type IOneToManyOrManyToOne = "one-to-many__many-to-one";
|
|
133
|
+
type IManyToMany = "many-to-many";
|
|
134
|
+
type IRelationKind = "one-to-many" | "many-to-one";
|
|
135
|
+
export type IRelationOneToManyOrManyToOne = {
|
|
136
|
+
type: IOneToManyOrManyToOne;
|
|
137
|
+
kind: IRelationKind;
|
|
138
|
+
grabMany: boolean;
|
|
139
|
+
table: string;
|
|
140
|
+
name: string;
|
|
141
|
+
relation: IRelation;
|
|
142
|
+
nullable: boolean;
|
|
143
|
+
};
|
|
144
|
+
export type IRelationManyToMany = {
|
|
145
|
+
type: IManyToMany;
|
|
146
|
+
grabMany: boolean;
|
|
147
|
+
table: string;
|
|
148
|
+
junctionTable: string;
|
|
149
|
+
name: string;
|
|
150
|
+
relations: IRelation[];
|
|
151
|
+
};
|
|
152
|
+
export type IRelationField = IRelationOneToManyOrManyToOne | IRelationManyToMany;
|
|
153
|
+
export type IMappedField = {
|
|
154
|
+
name: string;
|
|
155
|
+
as: string;
|
|
156
|
+
type: string;
|
|
157
|
+
nullable: boolean;
|
|
158
|
+
table: string;
|
|
159
|
+
foreignKey: string;
|
|
160
|
+
referencedTable: string;
|
|
161
|
+
referencedKey: string;
|
|
162
|
+
};
|
|
163
|
+
export type IArtifacts = {
|
|
164
|
+
[k: string]: {
|
|
165
|
+
table: string;
|
|
166
|
+
primaryKey: string;
|
|
167
|
+
scalarFields: string[];
|
|
168
|
+
relationFields: {
|
|
169
|
+
[k: string]: IRelationField;
|
|
170
|
+
};
|
|
171
|
+
mappedFields: {
|
|
172
|
+
[k: string]: IMappedField;
|
|
173
|
+
} | null;
|
|
174
|
+
uniqueFields: {
|
|
175
|
+
name: string;
|
|
176
|
+
type: string;
|
|
177
|
+
nullable: boolean;
|
|
178
|
+
}[];
|
|
179
|
+
dateTimeFields: {
|
|
180
|
+
[k: string]: boolean;
|
|
181
|
+
};
|
|
182
|
+
dateTimeFieldsCount: number;
|
|
183
|
+
fields: TField[];
|
|
184
|
+
dump: {
|
|
185
|
+
schema: string;
|
|
186
|
+
} | null;
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
export type TFieldType = "string" | "boolean" | "number" | "integer";
|
|
190
|
+
export type TField = {
|
|
191
|
+
kind: "scalar";
|
|
192
|
+
type: TFieldType;
|
|
193
|
+
name: string;
|
|
194
|
+
nullable: boolean;
|
|
195
|
+
hasDefaultValue: boolean;
|
|
196
|
+
mapped?: true;
|
|
197
|
+
} | {
|
|
198
|
+
kind: "enum";
|
|
199
|
+
values: unknown[];
|
|
200
|
+
name: string;
|
|
201
|
+
nullable: boolean;
|
|
202
|
+
} | {
|
|
203
|
+
kind: "object";
|
|
204
|
+
type: string;
|
|
205
|
+
name: string;
|
|
206
|
+
isList: boolean;
|
|
207
|
+
nullable: boolean;
|
|
208
|
+
};
|
|
209
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventOnHandlerError = void 0;
|
|
4
|
+
class EventOnHandlerError extends Event {
|
|
5
|
+
constructor(message, data) {
|
|
6
|
+
super(message, data);
|
|
7
|
+
this.error = data.error;
|
|
8
|
+
this.resource = data.resource;
|
|
9
|
+
this.action = data.action;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.EventOnHandlerError = EventOnHandlerError;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { RedisConfig } from "./Cache";
|
|
2
|
+
import type { IRuntime, TMiddleware, TResolveParams, IArtifacts, ISupplementClientOpts } from "./IRuntime";
|
|
3
|
+
import { MySQL } from "./lib/MySQL";
|
|
4
|
+
export declare class RuntimeMySQL implements IRuntime {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(clientOpts: ConstructorParameters<typeof MySQL>[0], otherOpts: {
|
|
7
|
+
supplementClientOpts?: ISupplementClientOpts;
|
|
8
|
+
redis?: RedisConfig;
|
|
9
|
+
}, artifacts: IArtifacts);
|
|
10
|
+
resolve(input: TResolveParams): Promise<any>;
|
|
11
|
+
$queryRaw(sql: string, values?: any[]): Promise<import("mysql2").OkPacket | import("mysql2").ResultSetHeader | import("mysql2").ResultSetHeader[] | import("mysql2").RowDataPacket[] | import("mysql2").RowDataPacket[][] | import("mysql2").OkPacket[] | [import("mysql2").RowDataPacket[], import("mysql2").ResultSetHeader]>;
|
|
12
|
+
$use(middleware: TMiddleware): Promise<void>;
|
|
13
|
+
$whereNeedsProcessing(where: any): boolean;
|
|
14
|
+
$prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<{}>;
|
|
15
|
+
$shutdown(): Promise<void>;
|
|
16
|
+
$startTransaction(input?: {
|
|
17
|
+
isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
|
|
18
|
+
}): Promise<{
|
|
19
|
+
commit: () => Promise<void>;
|
|
20
|
+
rollback: () => Promise<void>;
|
|
21
|
+
dbCall: (q: string) => Promise<import("mysql2").OkPacket | import("mysql2").ResultSetHeader | import("mysql2").ResultSetHeader[] | import("mysql2").RowDataPacket[] | import("mysql2").RowDataPacket[][] | import("mysql2").OkPacket[] | [import("mysql2").RowDataPacket[], import("mysql2").ResultSetHeader]>;
|
|
22
|
+
queryRaw: (q: string, values: any[]) => Promise<import("mysql2").OkPacket | import("mysql2").ResultSetHeader | import("mysql2").ResultSetHeader[] | import("mysql2").RowDataPacket[] | import("mysql2").RowDataPacket[][] | import("mysql2").OkPacket[] | [import("mysql2").RowDataPacket[], import("mysql2").ResultSetHeader]>;
|
|
23
|
+
}>;
|
|
24
|
+
private dbCall;
|
|
25
|
+
private formatQuery;
|
|
26
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
26
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
27
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
28
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
29
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
30
|
+
};
|
|
31
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
32
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
33
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
34
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
35
|
+
};
|
|
36
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
var _RuntimeMySQL_dialect, _RuntimeMySQL_mysqlClient, _RuntimeMySQL_clientCache, _RuntimeMySQL_middlewareHandler;
|
|
40
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
+
exports.RuntimeMySQL = void 0;
|
|
42
|
+
const SqlString = __importStar(require("sqlstring"));
|
|
43
|
+
const Cache_1 = __importDefault(require("./Cache"));
|
|
44
|
+
const MySQL_1 = require("./lib/MySQL");
|
|
45
|
+
const shared_1 = require("./lib/shared");
|
|
46
|
+
const loglevel_1 = __importDefault(require("loglevel"));
|
|
47
|
+
loglevel_1.default.setDefaultLevel("INFO");
|
|
48
|
+
class RuntimeMySQL {
|
|
49
|
+
constructor(clientOpts, otherOpts, artifacts) {
|
|
50
|
+
_RuntimeMySQL_dialect.set(this, "mysql");
|
|
51
|
+
_RuntimeMySQL_mysqlClient.set(this, void 0);
|
|
52
|
+
_RuntimeMySQL_clientCache.set(this, void 0);
|
|
53
|
+
_RuntimeMySQL_middlewareHandler.set(this, void 0);
|
|
54
|
+
__classPrivateFieldSet(this, _RuntimeMySQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
|
|
55
|
+
if (otherOpts.redis)
|
|
56
|
+
__classPrivateFieldSet(this, _RuntimeMySQL_clientCache, new Cache_1.default(otherOpts.redis, clientOpts?.debug), "f");
|
|
57
|
+
if (otherOpts.supplementClientOpts) {
|
|
58
|
+
clientOpts = {
|
|
59
|
+
supportBigNumbers: true,
|
|
60
|
+
typeCast: (field, next) => {
|
|
61
|
+
if (field.type == "TINY" && field.length == 1) {
|
|
62
|
+
return field.string() == "1";
|
|
63
|
+
}
|
|
64
|
+
// if (field.type === "DATE") {
|
|
65
|
+
// return field.string();
|
|
66
|
+
// }
|
|
67
|
+
if (field.type === "TIMESTAMP" ||
|
|
68
|
+
field.type === "DATE" ||
|
|
69
|
+
field.type === "DATETIME") {
|
|
70
|
+
const x = field.string();
|
|
71
|
+
if (x == null) {
|
|
72
|
+
return x;
|
|
73
|
+
}
|
|
74
|
+
return new Date(x).toISOString();
|
|
75
|
+
}
|
|
76
|
+
if (field.type === "VAR_STRING" || field.type === "STRING") {
|
|
77
|
+
return field.string();
|
|
78
|
+
}
|
|
79
|
+
return next();
|
|
80
|
+
},
|
|
81
|
+
...clientOpts
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
clientOpts = {
|
|
86
|
+
typeCast: (field, next) => {
|
|
87
|
+
if (field.type == "TINY" && field.length == 1) {
|
|
88
|
+
return field.string() == "1";
|
|
89
|
+
}
|
|
90
|
+
if (field.type === "VAR_STRING" || field.type === "STRING") {
|
|
91
|
+
return field.string();
|
|
92
|
+
}
|
|
93
|
+
return next();
|
|
94
|
+
},
|
|
95
|
+
...clientOpts
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
__classPrivateFieldSet(this, _RuntimeMySQL_mysqlClient, new MySQL_1.MySQL(clientOpts), "f");
|
|
99
|
+
}
|
|
100
|
+
async resolve(input) {
|
|
101
|
+
return (0, shared_1.resolve)(input, input.dbCall ?? this.dbCall.bind(this), this.formatQuery.bind(this), this.$startTransaction.bind(this), __classPrivateFieldGet(this, _RuntimeMySQL_dialect, "f"), __classPrivateFieldGet(this, _RuntimeMySQL_middlewareHandler, "f"), input.context ?? {}, __classPrivateFieldGet(this, _RuntimeMySQL_clientCache, "f"));
|
|
102
|
+
}
|
|
103
|
+
async $queryRaw(sql, values) {
|
|
104
|
+
return this.dbCall(this.formatQuery(sql, values ?? []));
|
|
105
|
+
}
|
|
106
|
+
async $use(middleware) {
|
|
107
|
+
__classPrivateFieldGet(this, _RuntimeMySQL_middlewareHandler, "f").register(middleware);
|
|
108
|
+
}
|
|
109
|
+
$whereNeedsProcessing(where) {
|
|
110
|
+
return (0, shared_1.whereNeedsProcessing)(where);
|
|
111
|
+
}
|
|
112
|
+
async $prepareWhere(artifacts, table, data) {
|
|
113
|
+
return (0, shared_1._prepareWhere)(artifacts, table, data, this.dbCall.bind(this), this.formatQuery.bind(this));
|
|
114
|
+
}
|
|
115
|
+
async $shutdown() {
|
|
116
|
+
if (__classPrivateFieldGet(this, _RuntimeMySQL_clientCache, "f")) {
|
|
117
|
+
await __classPrivateFieldGet(this, _RuntimeMySQL_clientCache, "f").shutdown();
|
|
118
|
+
}
|
|
119
|
+
await __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").endPool();
|
|
120
|
+
}
|
|
121
|
+
async $startTransaction(input) {
|
|
122
|
+
return __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").beginTransaction(input?.isolationLevel);
|
|
123
|
+
}
|
|
124
|
+
dbCall(q) {
|
|
125
|
+
return __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").query(q);
|
|
126
|
+
}
|
|
127
|
+
formatQuery(q, values) {
|
|
128
|
+
return SqlString.format(q, values);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
exports.RuntimeMySQL = RuntimeMySQL;
|
|
132
|
+
_RuntimeMySQL_dialect = new WeakMap(), _RuntimeMySQL_mysqlClient = new WeakMap(), _RuntimeMySQL_clientCache = new WeakMap(), _RuntimeMySQL_middlewareHandler = new WeakMap();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { RedisConfig } from "./Cache";
|
|
2
|
+
import type { IRuntime, TMiddleware, TResolveParams, IArtifacts, ISupplementClientOpts } from "./IRuntime";
|
|
3
|
+
import { PostgreSQL } from "./lib/PostgreSQL";
|
|
4
|
+
export declare class RuntimePostgreSQL implements IRuntime {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(clientOpts: ConstructorParameters<typeof PostgreSQL>[0], otherOpts: {
|
|
7
|
+
supplementClientOpts?: ISupplementClientOpts;
|
|
8
|
+
redis?: RedisConfig;
|
|
9
|
+
}, artifacts: IArtifacts);
|
|
10
|
+
resolve(input: TResolveParams): Promise<any>;
|
|
11
|
+
$queryRaw(sql: string, values?: any[]): Promise<any[]>;
|
|
12
|
+
$use(middleware: TMiddleware): Promise<void>;
|
|
13
|
+
$whereNeedsProcessing(where: any): boolean;
|
|
14
|
+
$prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<{}>;
|
|
15
|
+
$shutdown(): Promise<void>;
|
|
16
|
+
$startTransaction(input?: {
|
|
17
|
+
isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
|
|
18
|
+
}): Promise<{
|
|
19
|
+
commit: () => Promise<void>;
|
|
20
|
+
rollback: () => Promise<void>;
|
|
21
|
+
dbCall: (q: string) => Promise<any[]>;
|
|
22
|
+
queryRaw: (q: string, values: any[]) => Promise<any[]>;
|
|
23
|
+
}>;
|
|
24
|
+
private dbCall;
|
|
25
|
+
/**
|
|
26
|
+
* Converts MySQL-style ?? (identifier) and ? (value) placeholders to
|
|
27
|
+
* pg-format placeholders (%I and %L) and formats the query.
|
|
28
|
+
*/
|
|
29
|
+
private formatQuery;
|
|
30
|
+
}
|