@iamkirbki/database-handler-pg 4.0.4 → 4.0.6
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/PostgresSchemaBuilder.d.ts.map +1 -1
- package/dist/PostgresSchemaBuilder.js +2 -2
- package/dist/PostgresStatement.js +1 -1
- package/dist/PostgresTableSchemaBuilder.d.ts +19 -7
- package/dist/PostgresTableSchemaBuilder.d.ts.map +1 -1
- package/dist/PostgresTableSchemaBuilder.js +121 -20
- package/package.json +4 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PostgresSchemaBuilder.d.ts","sourceRoot":"","sources":["../src/PostgresSchemaBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AAC7E,OAAO,eAAe,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAEzE,qBAAa,qBAAsB,YAAW,qBAAqB;IAEnD,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,eAAe;IAGvC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,0BAA0B,KAAK,IAAI;IAW/
|
|
1
|
+
{"version":3,"file":"PostgresSchemaBuilder.d.ts","sourceRoot":"","sources":["../src/PostgresSchemaBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AAC7E,OAAO,eAAe,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AAEzE,qBAAa,qBAAsB,YAAW,qBAAqB;IAEnD,OAAO,CAAC,QAAQ;gBAAR,QAAQ,EAAE,eAAe;IAGvC,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,0BAA0B,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAW/F,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOtC,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,0BAA0B,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;CAc5G"}
|
|
@@ -21,14 +21,14 @@ export class PostgresSchemaBuilder {
|
|
|
21
21
|
const cols = tableBuilder.build();
|
|
22
22
|
const query = `CREATE TABLE IF NOT EXISTS ${name} ${cols}`;
|
|
23
23
|
const statement = yield this._adapter.prepare(query);
|
|
24
|
-
statement.run();
|
|
24
|
+
yield statement.run();
|
|
25
25
|
});
|
|
26
26
|
}
|
|
27
27
|
dropTable(name) {
|
|
28
28
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
29
|
const query = `DROP TABLE IF EXISTS ${name}`;
|
|
30
30
|
const statement = yield this._adapter.prepare(query);
|
|
31
|
-
statement.run();
|
|
31
|
+
yield statement.run();
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
// eslint-disable-next-line no-unused-vars
|
|
@@ -23,7 +23,7 @@ export default class PostgresStatement {
|
|
|
23
23
|
const paramMap = new Map();
|
|
24
24
|
let paramIndex = 1;
|
|
25
25
|
const values = [];
|
|
26
|
-
const transformedQuery = this._query.replace(/@(\w+)/g, (_match, paramName) => {
|
|
26
|
+
const transformedQuery = this._query.replace(/@(\w+(?:\.\w+)?)/g, (_match, paramName) => {
|
|
27
27
|
if (!paramMap.has(paramName)) {
|
|
28
28
|
paramMap.set(paramName, paramIndex++);
|
|
29
29
|
values.push(parameters[paramName]);
|
|
@@ -1,13 +1,25 @@
|
|
|
1
1
|
import { SchemaTableBuilder } from "@iamkirbki/database-handler-core";
|
|
2
2
|
export declare class PostgresTableSchemaBuilder extends SchemaTableBuilder {
|
|
3
3
|
build(): string;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
boolean(name
|
|
4
|
+
uuid(name: string): this;
|
|
5
|
+
string(name: string, length?: number): this;
|
|
6
|
+
text(name: string): this;
|
|
7
|
+
integer(name: string): this;
|
|
8
|
+
decimal(name: string, precision?: number, scale?: number): this;
|
|
9
|
+
float(name: string): this;
|
|
10
|
+
boolean(name: string): this;
|
|
11
|
+
json(name: string): this;
|
|
12
|
+
enum(name: string, values: string[]): this;
|
|
13
|
+
timestamp(name: string): this;
|
|
14
|
+
time(name: string): this;
|
|
11
15
|
timestamps(): this;
|
|
16
|
+
increments(): this;
|
|
17
|
+
primaryKey(): this;
|
|
18
|
+
foreignKey(referenceTable: string, referenceColumn: string): this;
|
|
19
|
+
unique(): this;
|
|
20
|
+
nullable(): this;
|
|
21
|
+
defaultTo(value: unknown): this;
|
|
22
|
+
softDeletes(): this;
|
|
23
|
+
morphs(name: string): this;
|
|
12
24
|
}
|
|
13
25
|
//# sourceMappingURL=PostgresTableSchemaBuilder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PostgresTableSchemaBuilder.d.ts","sourceRoot":"","sources":["../src/PostgresTableSchemaBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAEtE,qBAAa,0BAA2B,SAAQ,kBAAkB;IAC9D,KAAK,IAAI,MAAM;
|
|
1
|
+
{"version":3,"file":"PostgresTableSchemaBuilder.d.ts","sourceRoot":"","sources":["../src/PostgresTableSchemaBuilder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAEtE,qBAAa,0BAA2B,SAAQ,kBAAkB;IAC9D,KAAK,IAAI,MAAM;IAmBf,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOxB,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAO3C,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOxB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAO3B,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI;IAW/D,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOzB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAO3B,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOxB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI;IAW1C,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAO7B,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAOxB,UAAU,IAAI,IAAI;IAiBlB,UAAU;IASV,UAAU,IAAI,IAAI;IASlB,UAAU,CAAC,cAAc,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,IAAI;IAUjE,MAAM,IAAI,IAAI;IASd,QAAQ,IAAI,IAAI;IAShB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAmB/B,WAAW,IAAI,IAAI;IASnB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;CAI7B"}
|
|
@@ -13,43 +13,45 @@ export class PostgresTableSchemaBuilder extends SchemaTableBuilder {
|
|
|
13
13
|
});
|
|
14
14
|
return `(${columnDefinitions.join(', ')})`;
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
// Datatypes
|
|
17
|
+
uuid(name) {
|
|
17
18
|
return this.addColumn({
|
|
18
19
|
name,
|
|
19
|
-
|
|
20
|
+
datatype: 'UUID',
|
|
20
21
|
});
|
|
21
22
|
}
|
|
22
|
-
|
|
23
|
+
string(name, length) {
|
|
23
24
|
return this.addColumn({
|
|
24
25
|
name,
|
|
25
|
-
|
|
26
|
+
datatype: length ? `VARCHAR(${length})` : 'VARCHAR',
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
return this;
|
|
29
|
+
text(name) {
|
|
30
|
+
return this.addColumn({
|
|
31
|
+
name,
|
|
32
|
+
datatype: 'TEXT',
|
|
33
|
+
});
|
|
36
34
|
}
|
|
37
|
-
|
|
35
|
+
integer(name) {
|
|
38
36
|
return this.addColumn({
|
|
39
37
|
name,
|
|
40
|
-
datatype: '
|
|
38
|
+
datatype: 'INTEGER',
|
|
41
39
|
});
|
|
42
40
|
}
|
|
43
|
-
|
|
41
|
+
decimal(name, precision, scale) {
|
|
42
|
+
let datatype = 'DECIMAL';
|
|
43
|
+
if (precision !== undefined && scale !== undefined) {
|
|
44
|
+
datatype = `DECIMAL(${precision},${scale})`;
|
|
45
|
+
}
|
|
44
46
|
return this.addColumn({
|
|
45
47
|
name,
|
|
46
|
-
datatype
|
|
48
|
+
datatype,
|
|
47
49
|
});
|
|
48
50
|
}
|
|
49
|
-
|
|
51
|
+
float(name) {
|
|
50
52
|
return this.addColumn({
|
|
51
53
|
name,
|
|
52
|
-
datatype: '
|
|
54
|
+
datatype: 'REAL',
|
|
53
55
|
});
|
|
54
56
|
}
|
|
55
57
|
boolean(name) {
|
|
@@ -58,17 +60,116 @@ export class PostgresTableSchemaBuilder extends SchemaTableBuilder {
|
|
|
58
60
|
datatype: 'BOOLEAN',
|
|
59
61
|
});
|
|
60
62
|
}
|
|
63
|
+
json(name) {
|
|
64
|
+
return this.addColumn({
|
|
65
|
+
name,
|
|
66
|
+
datatype: 'JSONB',
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
enum(name, values) {
|
|
70
|
+
// Postgres supports ENUM types, but they must be created before use.
|
|
71
|
+
// For table column definition, use a CHECK constraint for inline enums.
|
|
72
|
+
const quotedValues = values.map(v => `'${v.replace(/'/g, "''")}'`).join(', ');
|
|
73
|
+
return this.addColumn({
|
|
74
|
+
name,
|
|
75
|
+
datatype: 'TEXT',
|
|
76
|
+
constraints: [`CHECK (${name} IN (${quotedValues}))`],
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
timestamp(name) {
|
|
80
|
+
return this.addColumn({
|
|
81
|
+
name,
|
|
82
|
+
datatype: 'TIMESTAMP',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
time(name) {
|
|
86
|
+
return this.addColumn({
|
|
87
|
+
name,
|
|
88
|
+
datatype: 'TIME',
|
|
89
|
+
});
|
|
90
|
+
}
|
|
61
91
|
timestamps() {
|
|
62
92
|
this.addColumn({
|
|
63
93
|
name: 'created_at',
|
|
64
94
|
datatype: 'TIMESTAMP',
|
|
65
|
-
constraints: ['
|
|
95
|
+
constraints: ['DEFAULT CURRENT_TIMESTAMP']
|
|
66
96
|
});
|
|
67
97
|
this.addColumn({
|
|
68
98
|
name: 'updated_at',
|
|
69
99
|
datatype: 'TIMESTAMP',
|
|
70
|
-
constraints: ['
|
|
100
|
+
constraints: ['DEFAULT CURRENT_TIMESTAMP']
|
|
71
101
|
});
|
|
72
102
|
return this;
|
|
73
103
|
}
|
|
104
|
+
// Constraints
|
|
105
|
+
increments() {
|
|
106
|
+
if (this.columns.length === 0) {
|
|
107
|
+
throw new Error('increments() requires a previous column. Call a datatype method first.');
|
|
108
|
+
}
|
|
109
|
+
return this.addColumn({
|
|
110
|
+
autoincrement: true,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
primaryKey() {
|
|
114
|
+
if (this.columns.length === 0) {
|
|
115
|
+
throw new Error('primaryKey() requires a previous column. Call a datatype method first.');
|
|
116
|
+
}
|
|
117
|
+
return this.addColumn({
|
|
118
|
+
constraints: ['PRIMARY KEY'],
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
foreignKey(referenceTable, referenceColumn) {
|
|
122
|
+
if (this.columns.length === 0) {
|
|
123
|
+
throw new Error('foreignKey() requires a previous column. Call a datatype method first.');
|
|
124
|
+
}
|
|
125
|
+
const constraint = `REFERENCES ${referenceTable}(${referenceColumn})`;
|
|
126
|
+
return this.addColumn({
|
|
127
|
+
constraints: [constraint],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
unique() {
|
|
131
|
+
if (this.columns.length === 0) {
|
|
132
|
+
throw new Error('unique() requires a previous column. Call a datatype method first.');
|
|
133
|
+
}
|
|
134
|
+
return this.addColumn({
|
|
135
|
+
constraints: ['UNIQUE'],
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
nullable() {
|
|
139
|
+
if (this.columns.length === 0) {
|
|
140
|
+
throw new Error('nullable() requires a previous column. Call a datatype method first.');
|
|
141
|
+
}
|
|
142
|
+
return this.addColumn({
|
|
143
|
+
constraints: ['NULLABLE'],
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
defaultTo(value) {
|
|
147
|
+
if (this.columns.length === 0) {
|
|
148
|
+
throw new Error('defaultTo() requires a previous column. Call a datatype method first.');
|
|
149
|
+
}
|
|
150
|
+
let defaultValue;
|
|
151
|
+
if (typeof value === 'string') {
|
|
152
|
+
defaultValue = `'${value}'`;
|
|
153
|
+
}
|
|
154
|
+
else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
155
|
+
defaultValue = value;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
defaultValue = String(value);
|
|
159
|
+
}
|
|
160
|
+
return this.addColumn({
|
|
161
|
+
constraints: [`DEFAULT ${defaultValue}`],
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
softDeletes() {
|
|
165
|
+
this.addColumn({
|
|
166
|
+
name: 'deleted_at',
|
|
167
|
+
datatype: 'TIMESTAMP',
|
|
168
|
+
});
|
|
169
|
+
return this.nullable();
|
|
170
|
+
}
|
|
171
|
+
morphs(name) {
|
|
172
|
+
this.integer(`${name}_id`);
|
|
173
|
+
return this.string(`${name}_type`);
|
|
174
|
+
}
|
|
74
175
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iamkirbki/database-handler-pg",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.6",
|
|
4
4
|
"author": "iamkirbki",
|
|
5
5
|
"description": "PostgreSQL adapter for @kirbkis-database-handler/core",
|
|
6
6
|
"license": "ISC",
|
|
@@ -32,7 +32,9 @@
|
|
|
32
32
|
"pg": "^8.16.3"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
+
"@types/node": "^24.10.1",
|
|
35
36
|
"@types/pg": "^8.15.6",
|
|
36
|
-
"typescript": "^5.9.3"
|
|
37
|
+
"typescript": "^5.9.3",
|
|
38
|
+
"tsc-alias": "^1.8.16"
|
|
37
39
|
}
|
|
38
40
|
}
|