@contember/database
Advanced tools
Comparing version 1.3.5 to 1.4.0-alpha.1
@@ -9,2 +9,3 @@ import { With } from './builders/internal/With'; | ||
export * from './builders'; | ||
export * from './metadata'; | ||
export * from './queryable'; | ||
@@ -11,0 +12,0 @@ export * from './Literal'; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./builders"), exports); | ||
__exportStar(require("./metadata"), exports); | ||
__exportStar(require("./queryable"), exports); | ||
@@ -22,0 +23,0 @@ __exportStar(require("./Literal"), exports); |
import { Client } from '../client'; | ||
import { DatabaseMetadata } from '../metadata'; | ||
export type ConstraintType = 'foreignKey' | 'unique'; | ||
export declare class ConstraintHelper { | ||
private readonly client; | ||
private fkConstraintNames; | ||
private fkConstraintsLevel; | ||
constructor(client: Client); | ||
areFkConstraintsDeferred(): boolean; | ||
setFkConstraintsDeferred(): Promise<void>; | ||
setFkConstraintsImmediate(): Promise<void>; | ||
private setFkConstraintsPolicy; | ||
private readonly metadata; | ||
private level; | ||
constructor(client: Client, metadata: DatabaseMetadata); | ||
areConstraintsDeferred(type: ConstraintType): boolean; | ||
setConstraintsDeferred(type: ConstraintType): Promise<void>; | ||
setConstraintsImmediate(type: ConstraintType): Promise<void>; | ||
private setConstraintsPolicy; | ||
private formatConstraints; | ||
} | ||
//# sourceMappingURL=ConstraintHelper.d.ts.map |
@@ -6,26 +6,26 @@ "use strict"; | ||
class ConstraintHelper { | ||
constructor(client) { | ||
constructor(client, metadata) { | ||
this.client = client; | ||
this.fkConstraintNames = null; | ||
this.fkConstraintsLevel = 0; | ||
this.metadata = metadata; | ||
this.level = { unique: 0, foreignKey: 0 }; | ||
} | ||
areFkConstraintsDeferred() { | ||
return this.fkConstraintsLevel > 0; | ||
areConstraintsDeferred(type) { | ||
return this.level[type] > 0; | ||
} | ||
async setFkConstraintsDeferred() { | ||
this.fkConstraintsLevel++; | ||
if (this.fkConstraintsLevel > 1) { | ||
async setConstraintsDeferred(type) { | ||
this.level[type]++; | ||
if (this.level[type] > 1) { | ||
return; | ||
} | ||
return await this.setFkConstraintsPolicy('DEFERRED'); | ||
return await this.setConstraintsPolicy(type, 'DEFERRED'); | ||
} | ||
async setFkConstraintsImmediate() { | ||
this.fkConstraintsLevel--; | ||
if (this.fkConstraintsLevel > 0) { | ||
async setConstraintsImmediate(type) { | ||
this.level[type]--; | ||
if (this.level[type] > 0) { | ||
return; | ||
} | ||
return await this.setFkConstraintsPolicy('IMMEDIATE'); | ||
return await this.setConstraintsPolicy(type, 'IMMEDIATE'); | ||
} | ||
async setFkConstraintsPolicy(policy) { | ||
const constraints = await this.formatConstraints(); | ||
async setConstraintsPolicy(type, policy) { | ||
const constraints = await this.formatConstraints(type); | ||
if (constraints === null) { | ||
@@ -36,13 +36,6 @@ return; | ||
} | ||
async formatConstraints() { | ||
if (this.fkConstraintNames === null) { | ||
this.fkConstraintNames = (await this.client.query(`SELECT con.conname AS name | ||
FROM pg_catalog.pg_constraint con | ||
INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace | ||
WHERE nsp.nspname = ? and con.condeferrable = true and contype = ?`, [this.client.schema, 'f'])).rows.map(it => it.name); | ||
} | ||
if (this.fkConstraintNames.length === 0) { | ||
return null; | ||
} | ||
return this.fkConstraintNames.map(it => `${(0, sql_1.wrapIdentifier)(this.client.schema)}.${(0, sql_1.wrapIdentifier)(it)}`).join(', '); | ||
async formatConstraints(type) { | ||
const constraints = type === 'unique' ? this.metadata.uniqueConstraints : this.metadata.foreignKeys; | ||
const names = constraints.filter({ deferrable: true }).getNames(); | ||
return names.length === 0 ? null : names.map(it => `${(0, sql_1.wrapIdentifier)(this.client.schema)}.${(0, sql_1.wrapIdentifier)(it)}`).join(', '); | ||
} | ||
@@ -49,0 +42,0 @@ } |
{ | ||
"name": "@contember/database", | ||
"version": "1.3.5", | ||
"version": "1.4.0-alpha.1", | ||
"license": "Apache-2.0", | ||
@@ -15,6 +15,6 @@ "main": "dist/src/index.js", | ||
"dependencies": { | ||
"@contember/queryable": "1.3.5" | ||
"@contember/queryable": "1.4.0-alpha.1" | ||
}, | ||
"devDependencies": { | ||
"@contember/database-tester": "1.3.5", | ||
"@contember/database-tester": "1.4.0-alpha.1", | ||
"@types/node": "^18", | ||
@@ -21,0 +21,0 @@ "pg": "^8.9.0" |
@@ -11,2 +11,3 @@ import { With } from './builders/internal/With' | ||
export * from './builders' | ||
export * from './metadata' | ||
export * from './queryable' | ||
@@ -13,0 +14,0 @@ export * from './Literal' |
import { Client } from '../client' | ||
import { wrapIdentifier } from './sql' | ||
import { DatabaseMetadata } from '../metadata' | ||
export type ConstraintType = 'foreignKey' | 'unique' | ||
export class ConstraintHelper { | ||
private fkConstraintNames: string[] | null = null | ||
private fkConstraintsLevel = 0 | ||
private level: Record<ConstraintType, number> = { unique: 0, foreignKey: 0 } | ||
constructor(private readonly client: Client) {} | ||
constructor( | ||
private readonly client: Client, | ||
private readonly metadata: DatabaseMetadata, | ||
) {} | ||
public areFkConstraintsDeferred(): boolean { | ||
return this.fkConstraintsLevel > 0 | ||
public areConstraintsDeferred(type: ConstraintType): boolean { | ||
return this.level[type] > 0 | ||
} | ||
public async setFkConstraintsDeferred(): Promise<void> { | ||
this.fkConstraintsLevel++ | ||
if (this.fkConstraintsLevel > 1) { | ||
public async setConstraintsDeferred(type: ConstraintType): Promise<void> { | ||
this.level[type]++ | ||
if (this.level[type] > 1) { | ||
return | ||
} | ||
return await this.setFkConstraintsPolicy('DEFERRED') | ||
return await this.setConstraintsPolicy(type, 'DEFERRED') | ||
} | ||
public async setFkConstraintsImmediate(): Promise<void> { | ||
this.fkConstraintsLevel-- | ||
if (this.fkConstraintsLevel > 0) { | ||
public async setConstraintsImmediate(type: ConstraintType): Promise<void> { | ||
this.level[type]-- | ||
if (this.level[type] > 0) { | ||
return | ||
} | ||
return await this.setFkConstraintsPolicy('IMMEDIATE') | ||
return await this.setConstraintsPolicy(type, 'IMMEDIATE') | ||
} | ||
private async setFkConstraintsPolicy(policy: 'DEFERRED' | 'IMMEDIATE') { | ||
const constraints = await this.formatConstraints() | ||
private async setConstraintsPolicy(type: ConstraintType, policy: 'DEFERRED' | 'IMMEDIATE') { | ||
const constraints = await this.formatConstraints(type) | ||
if (constraints === null) { | ||
@@ -38,19 +44,7 @@ return | ||
private async formatConstraints(): Promise<string | null> { | ||
if (this.fkConstraintNames === null) { | ||
this.fkConstraintNames = ( | ||
await this.client.query<{ name: string }>( | ||
`SELECT con.conname AS name | ||
FROM pg_catalog.pg_constraint con | ||
INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace | ||
WHERE nsp.nspname = ? and con.condeferrable = true and contype = ?`, | ||
[this.client.schema, 'f'], | ||
) | ||
).rows.map(it => it.name) | ||
} | ||
if (this.fkConstraintNames.length === 0) { | ||
return null | ||
} | ||
return this.fkConstraintNames.map(it => `${wrapIdentifier(this.client.schema)}.${wrapIdentifier(it)}`).join(', ') | ||
private async formatConstraints(type: ConstraintType): Promise<string | null> { | ||
const constraints = type === 'unique' ? this.metadata.uniqueConstraints : this.metadata.foreignKeys | ||
const names = constraints.filter({ deferrable: true }).getNames() | ||
return names.length === 0 ? null : names.map(it => `${wrapIdentifier(this.client.schema)}.${wrapIdentifier(it)}`).join(', ') | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
692550
314
9960
2
+ Added@contember/queryable@1.4.0-alpha.1(transitive)
- Removed@contember/queryable@1.3.5(transitive)