@petradb/drizzle 1.2.0 → 1.2.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.
- package/dist/index.d.ts +17 -8
- package/dist/index.js +129 -14
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { entityKind } from "drizzle-orm/entity";
|
|
2
|
+
import { PgDatabase } from "drizzle-orm/pg-core/db";
|
|
3
|
+
import { type PgQueryResultHKT } from "drizzle-orm/pg-core/session";
|
|
4
|
+
import type { DrizzleConfig } from "drizzle-orm/utils";
|
|
5
|
+
export interface PetraDBSession {
|
|
3
6
|
execute(sql: string, options?: {
|
|
4
7
|
rowMode?: "object" | "array";
|
|
5
8
|
}): Promise<any[]>;
|
|
@@ -10,10 +13,16 @@ interface PetraDBSession {
|
|
|
10
13
|
};
|
|
11
14
|
close(): Promise<void>;
|
|
12
15
|
}
|
|
13
|
-
export interface
|
|
14
|
-
|
|
16
|
+
export interface PetraDbQueryResult {
|
|
17
|
+
rows: Record<string, unknown>[];
|
|
18
|
+
rowCount: number;
|
|
19
|
+
}
|
|
20
|
+
export declare class PetraDbDatabase<TSchema extends Record<string, unknown> = Record<string, never>> extends PgDatabase<PetraDbQueryResultHKT, TSchema> {
|
|
21
|
+
static readonly [entityKind] = "PetraDbDatabase";
|
|
15
22
|
}
|
|
16
|
-
export
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
export {
|
|
23
|
+
export interface PetraDbQueryResultHKT extends PgQueryResultHKT {
|
|
24
|
+
type: PetraDbQueryResult;
|
|
25
|
+
}
|
|
26
|
+
export declare function drizzle<TSchema extends Record<string, unknown> = Record<string, never>>(session: PetraDBSession, config?: DrizzleConfig<TSchema>): PetraDbDatabase<TSchema> & {
|
|
27
|
+
$session: PetraDBSession;
|
|
28
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,136 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { entityKind } from "drizzle-orm/entity";
|
|
2
|
+
import { DefaultLogger, NoopLogger } from "drizzle-orm/logger";
|
|
3
|
+
import { PgDatabase } from "drizzle-orm/pg-core/db";
|
|
4
|
+
import { PgDialect } from "drizzle-orm/pg-core/dialect";
|
|
5
|
+
import { PgPreparedQuery, PgSession, PgTransaction, } from "drizzle-orm/pg-core/session";
|
|
6
|
+
import { extractTablesRelationalConfig, createTableRelationsHelpers, } from "drizzle-orm/relations";
|
|
7
|
+
import { fillPlaceholders, sql } from "drizzle-orm/sql";
|
|
8
|
+
// drizzle-orm exports mapResultRow at runtime but not in its type declarations
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
import { mapResultRow } from "drizzle-orm/utils";
|
|
11
|
+
class PetraDbPreparedQuery extends PgPreparedQuery {
|
|
12
|
+
petraSession;
|
|
13
|
+
queryString;
|
|
14
|
+
params;
|
|
15
|
+
logger;
|
|
16
|
+
fields;
|
|
17
|
+
_isResponseInArrayMode;
|
|
18
|
+
customResultMapper;
|
|
19
|
+
static [entityKind] = "PetraDbPreparedQuery";
|
|
20
|
+
constructor(petraSession, queryString, params, logger, fields, _isResponseInArrayMode, customResultMapper, query, queryMetadata, cacheConfig) {
|
|
21
|
+
super(query ?? { sql: queryString, params }, undefined, queryMetadata, cacheConfig);
|
|
22
|
+
this.petraSession = petraSession;
|
|
23
|
+
this.queryString = queryString;
|
|
24
|
+
this.params = params;
|
|
25
|
+
this.logger = logger;
|
|
26
|
+
this.fields = fields;
|
|
27
|
+
this._isResponseInArrayMode = _isResponseInArrayMode;
|
|
28
|
+
this.customResultMapper = customResultMapper;
|
|
29
|
+
}
|
|
30
|
+
async execute(placeholderValues = {}) {
|
|
31
|
+
const params = fillPlaceholders(this.params, placeholderValues);
|
|
32
|
+
this.logger.logQuery(this.queryString, params);
|
|
33
|
+
const { fields, queryString, petraSession, customResultMapper, } = this;
|
|
34
|
+
// joinsNotNullableMap is set internally by drizzle query builders
|
|
35
|
+
const joinsNotNullableMap = this.joinsNotNullableMap;
|
|
36
|
+
if (!fields && !customResultMapper) {
|
|
37
|
+
// Raw execute path (INSERT/UPDATE/DELETE without RETURNING, or raw SQL)
|
|
38
|
+
const results = await this._exec(queryString, params, "object");
|
|
39
|
+
const resultSet = results[0];
|
|
40
|
+
return {
|
|
41
|
+
rows: resultSet?.rows ?? [],
|
|
42
|
+
rowCount: resultSet?.rows?.length ?? 0,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
// Query with field mapping (SELECT, or mutations with RETURNING)
|
|
46
|
+
const results = await this._exec(queryString, params, "array");
|
|
47
|
+
const resultSet = results[0];
|
|
48
|
+
const rows = resultSet?.rows ?? [];
|
|
49
|
+
if (customResultMapper) {
|
|
50
|
+
return customResultMapper(rows);
|
|
51
|
+
}
|
|
52
|
+
return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
|
|
53
|
+
}
|
|
54
|
+
async _exec(queryString, params, rowMode) {
|
|
5
55
|
if (params.length > 0) {
|
|
6
|
-
const stmt =
|
|
7
|
-
|
|
56
|
+
const stmt = this.petraSession.prepare(queryString);
|
|
57
|
+
return stmt.execute(params, { rowMode });
|
|
58
|
+
}
|
|
59
|
+
return this.petraSession.execute(queryString, { rowMode });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
class PetraDbSession extends PgSession {
|
|
63
|
+
petraSession;
|
|
64
|
+
schema;
|
|
65
|
+
static [entityKind] = "PetraDbSession";
|
|
66
|
+
logger;
|
|
67
|
+
constructor(petraSession, dialect, schema, options = {}) {
|
|
68
|
+
super(dialect);
|
|
69
|
+
this.petraSession = petraSession;
|
|
70
|
+
this.schema = schema;
|
|
71
|
+
this.logger = options.logger ?? new NoopLogger();
|
|
72
|
+
}
|
|
73
|
+
prepareQuery(query, fields, name, isResponseInArrayMode, customResultMapper, queryMetadata, cacheConfig) {
|
|
74
|
+
return new PetraDbPreparedQuery(this.petraSession, query.sql, query.params, this.logger, fields, isResponseInArrayMode, customResultMapper, query, queryMetadata, cacheConfig);
|
|
75
|
+
}
|
|
76
|
+
async transaction(transaction, config) {
|
|
77
|
+
const tx = new PetraDbTransaction(this.dialect, this, this.schema);
|
|
78
|
+
await tx.execute(sql `begin${config ? sql ` ${tx.getTransactionConfigSQL(config)}` : undefined}`);
|
|
79
|
+
try {
|
|
80
|
+
const result = await transaction(tx);
|
|
81
|
+
await tx.execute(sql `commit`);
|
|
82
|
+
return result;
|
|
8
83
|
}
|
|
9
|
-
|
|
10
|
-
|
|
84
|
+
catch (error) {
|
|
85
|
+
await tx.execute(sql `rollback`);
|
|
86
|
+
throw error;
|
|
11
87
|
}
|
|
12
|
-
|
|
13
|
-
|
|
88
|
+
}
|
|
89
|
+
async count(query) {
|
|
90
|
+
const res = await this.execute(query);
|
|
91
|
+
return Number(res.rows[0]["count"]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
class PetraDbTransaction extends PgTransaction {
|
|
95
|
+
static [entityKind] = "PetraDbTransaction";
|
|
96
|
+
async transaction(transaction) {
|
|
97
|
+
const savepointName = `sp${this.nestedIndex + 1}`;
|
|
98
|
+
const tx = new PetraDbTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
|
|
99
|
+
await tx.execute(sql.raw(`savepoint ${savepointName}`));
|
|
100
|
+
try {
|
|
101
|
+
const result = await transaction(tx);
|
|
102
|
+
await tx.execute(sql.raw(`release savepoint ${savepointName}`));
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
await tx.execute(sql.raw(`rollback to savepoint ${savepointName}`));
|
|
107
|
+
throw err;
|
|
14
108
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export class PetraDbDatabase extends PgDatabase {
|
|
112
|
+
static [entityKind] = "PetraDbDatabase";
|
|
113
|
+
}
|
|
114
|
+
export function drizzle(session, config) {
|
|
115
|
+
const dialect = new PgDialect({ casing: config?.casing });
|
|
116
|
+
let logger;
|
|
117
|
+
if (config?.logger === true) {
|
|
118
|
+
logger = new DefaultLogger();
|
|
119
|
+
}
|
|
120
|
+
else if (config?.logger !== false && config?.logger !== undefined) {
|
|
121
|
+
logger = config.logger;
|
|
122
|
+
}
|
|
123
|
+
let schema;
|
|
124
|
+
if (config?.schema) {
|
|
125
|
+
const tablesConfig = extractTablesRelationalConfig(config.schema, createTableRelationsHelpers);
|
|
126
|
+
schema = {
|
|
127
|
+
fullSchema: config.schema,
|
|
128
|
+
schema: tablesConfig.tables,
|
|
129
|
+
tableNamesMap: tablesConfig.tableNamesMap,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
const petraDbSession = new PetraDbSession(session, dialect, schema, { logger });
|
|
133
|
+
const db = new PetraDbDatabase(dialect, petraDbSession, schema);
|
|
19
134
|
db.$session = session;
|
|
20
135
|
return db;
|
|
21
136
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@petradb/drizzle",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Drizzle ORM driver for PetraDB",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"test": "node --test test/integration.mjs"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"@petradb/engine": ">=1.2.
|
|
21
|
+
"@petradb/engine": ">=1.2.15",
|
|
22
22
|
"drizzle-orm": ">=0.38.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|