relq 1.0.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/LICENSE +21 -0
- package/README.md +862 -0
- package/dist/addons/buffer.js +1869 -0
- package/dist/addons/pg-cursor.js +1425 -0
- package/dist/addons/pg-format.js +2248 -0
- package/dist/addons/pg.js +4790 -0
- package/dist/bin/relq.js +2 -0
- package/dist/cjs/cache/index.cjs +9 -0
- package/dist/cjs/cache/query-cache.cjs +311 -0
- package/dist/cjs/cli/commands/add.cjs +82 -0
- package/dist/cjs/cli/commands/commit.cjs +145 -0
- package/dist/cjs/cli/commands/diff.cjs +84 -0
- package/dist/cjs/cli/commands/export.cjs +333 -0
- package/dist/cjs/cli/commands/fetch.cjs +59 -0
- package/dist/cjs/cli/commands/generate.cjs +242 -0
- package/dist/cjs/cli/commands/history.cjs +165 -0
- package/dist/cjs/cli/commands/import.cjs +524 -0
- package/dist/cjs/cli/commands/init.cjs +437 -0
- package/dist/cjs/cli/commands/introspect.cjs +142 -0
- package/dist/cjs/cli/commands/log.cjs +62 -0
- package/dist/cjs/cli/commands/migrate.cjs +167 -0
- package/dist/cjs/cli/commands/pull.cjs +410 -0
- package/dist/cjs/cli/commands/push.cjs +165 -0
- package/dist/cjs/cli/commands/rollback.cjs +169 -0
- package/dist/cjs/cli/commands/status.cjs +110 -0
- package/dist/cjs/cli/commands/sync.cjs +79 -0
- package/dist/cjs/cli/index.cjs +275 -0
- package/dist/cjs/cli/utils/change-tracker.cjs +446 -0
- package/dist/cjs/cli/utils/commit-manager.cjs +239 -0
- package/dist/cjs/cli/utils/config-loader.cjs +127 -0
- package/dist/cjs/cli/utils/env-loader.cjs +62 -0
- package/dist/cjs/cli/utils/fast-introspect.cjs +398 -0
- package/dist/cjs/cli/utils/git-utils.cjs +404 -0
- package/dist/cjs/cli/utils/migration-generator.cjs +269 -0
- package/dist/cjs/cli/utils/relqignore.cjs +114 -0
- package/dist/cjs/cli/utils/repo-manager.cjs +515 -0
- package/dist/cjs/cli/utils/schema-comparator.cjs +313 -0
- package/dist/cjs/cli/utils/schema-diff.cjs +284 -0
- package/dist/cjs/cli/utils/schema-hash.cjs +108 -0
- package/dist/cjs/cli/utils/schema-introspect.cjs +455 -0
- package/dist/cjs/cli/utils/snapshot-manager.cjs +223 -0
- package/dist/cjs/cli/utils/spinner.cjs +108 -0
- package/dist/cjs/cli/utils/sql-generator.cjs +520 -0
- package/dist/cjs/cli/utils/sql-parser.cjs +999 -0
- package/dist/cjs/cli/utils/type-generator.cjs +2061 -0
- package/dist/cjs/condition/array-condition-builder.cjs +503 -0
- package/dist/cjs/condition/array-numeric-condition-builder.cjs +186 -0
- package/dist/cjs/condition/array-specialized-condition-builder.cjs +206 -0
- package/dist/cjs/condition/array-string-condition-builder.cjs +146 -0
- package/dist/cjs/condition/base-condition-builder.cjs +2 -0
- package/dist/cjs/condition/condition-collector.cjs +284 -0
- package/dist/cjs/condition/fulltext-condition-builder.cjs +61 -0
- package/dist/cjs/condition/geometric-condition-builder.cjs +208 -0
- package/dist/cjs/condition/index.cjs +25 -0
- package/dist/cjs/condition/jsonb-condition-builder.cjs +160 -0
- package/dist/cjs/condition/network-condition-builder.cjs +230 -0
- package/dist/cjs/condition/range-condition-builder.cjs +82 -0
- package/dist/cjs/config/config.cjs +190 -0
- package/dist/cjs/config/index.cjs +9 -0
- package/dist/cjs/constants/pg-values.cjs +68 -0
- package/dist/cjs/copy/copy-builder.cjs +316 -0
- package/dist/cjs/copy/index.cjs +6 -0
- package/dist/cjs/core/query-builder.cjs +440 -0
- package/dist/cjs/core/relq-client.cjs +1831 -0
- package/dist/cjs/core/typed-kuery-client.cjs +2 -0
- package/dist/cjs/count/count-builder.cjs +88 -0
- package/dist/cjs/count/index.cjs +5 -0
- package/dist/cjs/cte/cte-builder.cjs +89 -0
- package/dist/cjs/cte/index.cjs +5 -0
- package/dist/cjs/ddl/function.cjs +48 -0
- package/dist/cjs/ddl/index.cjs +7 -0
- package/dist/cjs/ddl/sql.cjs +54 -0
- package/dist/cjs/delete/delete-builder.cjs +135 -0
- package/dist/cjs/delete/index.cjs +5 -0
- package/dist/cjs/errors/relq-errors.cjs +329 -0
- package/dist/cjs/examples/fulltext-search-test.cjs +122 -0
- package/dist/cjs/explain/explain-builder.cjs +99 -0
- package/dist/cjs/explain/index.cjs +5 -0
- package/dist/cjs/function/create-function-builder.cjs +196 -0
- package/dist/cjs/function/index.cjs +6 -0
- package/dist/cjs/functions/advanced-functions.cjs +241 -0
- package/dist/cjs/functions/case-builder.cjs +66 -0
- package/dist/cjs/functions/geometric-functions.cjs +104 -0
- package/dist/cjs/functions/index.cjs +184 -0
- package/dist/cjs/functions/network-functions.cjs +86 -0
- package/dist/cjs/functions/sql-functions.cjs +431 -0
- package/dist/cjs/index.cjs +164 -0
- package/dist/cjs/indexing/create-index-builder.cjs +187 -0
- package/dist/cjs/indexing/drop-index-builder.cjs +89 -0
- package/dist/cjs/indexing/index-types.cjs +2 -0
- package/dist/cjs/indexing/index.cjs +8 -0
- package/dist/cjs/insert/conflict-builder.cjs +173 -0
- package/dist/cjs/insert/index.cjs +5 -0
- package/dist/cjs/insert/insert-builder.cjs +254 -0
- package/dist/cjs/introspect/index.cjs +229 -0
- package/dist/cjs/maintenance/index.cjs +6 -0
- package/dist/cjs/maintenance/vacuum-builder.cjs +166 -0
- package/dist/cjs/pubsub/index.cjs +7 -0
- package/dist/cjs/pubsub/listen-notify-builder.cjs +57 -0
- package/dist/cjs/pubsub/listener-connection.cjs +180 -0
- package/dist/cjs/raw/index.cjs +5 -0
- package/dist/cjs/raw/raw-query-builder.cjs +27 -0
- package/dist/cjs/schema/index.cjs +15 -0
- package/dist/cjs/schema/schema-builder.cjs +1167 -0
- package/dist/cjs/schema-builder.cjs +21 -0
- package/dist/cjs/schema-definition/column-types.cjs +829 -0
- package/dist/cjs/schema-definition/index.cjs +62 -0
- package/dist/cjs/schema-definition/introspection.cjs +620 -0
- package/dist/cjs/schema-definition/partitions.cjs +129 -0
- package/dist/cjs/schema-definition/pg-enum.cjs +76 -0
- package/dist/cjs/schema-definition/pg-function.cjs +91 -0
- package/dist/cjs/schema-definition/pg-sequence.cjs +56 -0
- package/dist/cjs/schema-definition/pg-trigger.cjs +108 -0
- package/dist/cjs/schema-definition/relations.cjs +98 -0
- package/dist/cjs/schema-definition/sql-expressions.cjs +202 -0
- package/dist/cjs/schema-definition/table-definition.cjs +636 -0
- package/dist/cjs/select/aggregate-builder.cjs +179 -0
- package/dist/cjs/select/index.cjs +5 -0
- package/dist/cjs/select/select-builder.cjs +233 -0
- package/dist/cjs/sequence/index.cjs +7 -0
- package/dist/cjs/sequence/sequence-builder.cjs +264 -0
- package/dist/cjs/table/alter-table-builder.cjs +146 -0
- package/dist/cjs/table/constraint-builder.cjs +102 -0
- package/dist/cjs/table/create-table-builder.cjs +248 -0
- package/dist/cjs/table/index.cjs +17 -0
- package/dist/cjs/table/partition-builder.cjs +131 -0
- package/dist/cjs/table/truncate-builder.cjs +70 -0
- package/dist/cjs/transaction/index.cjs +6 -0
- package/dist/cjs/transaction/transaction-builder.cjs +78 -0
- package/dist/cjs/trigger/create-trigger-builder.cjs +174 -0
- package/dist/cjs/trigger/index.cjs +6 -0
- package/dist/cjs/types/aggregate-types.cjs +2 -0
- package/dist/cjs/types/config-types.cjs +40 -0
- package/dist/cjs/types/inference-types.cjs +18 -0
- package/dist/cjs/types/pagination-types.cjs +7 -0
- package/dist/cjs/types/result-types.cjs +2 -0
- package/dist/cjs/types/schema-types.cjs +2 -0
- package/dist/cjs/types/subscription-types.cjs +2 -0
- package/dist/cjs/types.cjs +2 -0
- package/dist/cjs/update/array-update-builder.cjs +205 -0
- package/dist/cjs/update/index.cjs +13 -0
- package/dist/cjs/update/update-builder.cjs +195 -0
- package/dist/cjs/utils/case-converter.cjs +58 -0
- package/dist/cjs/utils/environment-detection.cjs +120 -0
- package/dist/cjs/utils/index.cjs +10 -0
- package/dist/cjs/utils/pool-defaults.cjs +106 -0
- package/dist/cjs/utils/type-coercion.cjs +118 -0
- package/dist/cjs/view/create-view-builder.cjs +180 -0
- package/dist/cjs/view/index.cjs +7 -0
- package/dist/cjs/window/index.cjs +5 -0
- package/dist/cjs/window/window-builder.cjs +80 -0
- package/dist/config.cjs +1 -0
- package/dist/config.d.ts +655 -0
- package/dist/config.js +1 -0
- package/dist/esm/cache/index.js +1 -0
- package/dist/esm/cache/query-cache.js +303 -0
- package/dist/esm/cli/commands/add.js +78 -0
- package/dist/esm/cli/commands/commit.js +109 -0
- package/dist/esm/cli/commands/diff.js +81 -0
- package/dist/esm/cli/commands/export.js +297 -0
- package/dist/esm/cli/commands/fetch.js +56 -0
- package/dist/esm/cli/commands/generate.js +206 -0
- package/dist/esm/cli/commands/history.js +129 -0
- package/dist/esm/cli/commands/import.js +488 -0
- package/dist/esm/cli/commands/init.js +401 -0
- package/dist/esm/cli/commands/introspect.js +106 -0
- package/dist/esm/cli/commands/log.js +59 -0
- package/dist/esm/cli/commands/migrate.js +131 -0
- package/dist/esm/cli/commands/pull.js +374 -0
- package/dist/esm/cli/commands/push.js +129 -0
- package/dist/esm/cli/commands/rollback.js +133 -0
- package/dist/esm/cli/commands/status.js +107 -0
- package/dist/esm/cli/commands/sync.js +76 -0
- package/dist/esm/cli/index.js +240 -0
- package/dist/esm/cli/utils/change-tracker.js +405 -0
- package/dist/esm/cli/utils/commit-manager.js +191 -0
- package/dist/esm/cli/utils/config-loader.js +86 -0
- package/dist/esm/cli/utils/env-loader.js +57 -0
- package/dist/esm/cli/utils/fast-introspect.js +362 -0
- package/dist/esm/cli/utils/git-utils.js +347 -0
- package/dist/esm/cli/utils/migration-generator.js +263 -0
- package/dist/esm/cli/utils/relqignore.js +74 -0
- package/dist/esm/cli/utils/repo-manager.js +444 -0
- package/dist/esm/cli/utils/schema-comparator.js +307 -0
- package/dist/esm/cli/utils/schema-diff.js +276 -0
- package/dist/esm/cli/utils/schema-hash.js +69 -0
- package/dist/esm/cli/utils/schema-introspect.js +418 -0
- package/dist/esm/cli/utils/snapshot-manager.js +179 -0
- package/dist/esm/cli/utils/spinner.js +101 -0
- package/dist/esm/cli/utils/sql-generator.js +504 -0
- package/dist/esm/cli/utils/sql-parser.js +992 -0
- package/dist/esm/cli/utils/type-generator.js +2058 -0
- package/dist/esm/condition/array-condition-builder.js +495 -0
- package/dist/esm/condition/array-numeric-condition-builder.js +182 -0
- package/dist/esm/condition/array-specialized-condition-builder.js +200 -0
- package/dist/esm/condition/array-string-condition-builder.js +142 -0
- package/dist/esm/condition/base-condition-builder.js +1 -0
- package/dist/esm/condition/condition-collector.js +275 -0
- package/dist/esm/condition/fulltext-condition-builder.js +53 -0
- package/dist/esm/condition/geometric-condition-builder.js +200 -0
- package/dist/esm/condition/index.js +7 -0
- package/dist/esm/condition/jsonb-condition-builder.js +152 -0
- package/dist/esm/condition/network-condition-builder.js +222 -0
- package/dist/esm/condition/range-condition-builder.js +74 -0
- package/dist/esm/config/config.js +150 -0
- package/dist/esm/config/index.js +1 -0
- package/dist/esm/constants/pg-values.js +63 -0
- package/dist/esm/copy/copy-builder.js +308 -0
- package/dist/esm/copy/index.js +1 -0
- package/dist/esm/core/query-builder.js +426 -0
- package/dist/esm/core/relq-client.js +1791 -0
- package/dist/esm/core/typed-kuery-client.js +1 -0
- package/dist/esm/count/count-builder.js +81 -0
- package/dist/esm/count/index.js +1 -0
- package/dist/esm/cte/cte-builder.js +82 -0
- package/dist/esm/cte/index.js +1 -0
- package/dist/esm/ddl/function.js +45 -0
- package/dist/esm/ddl/index.js +2 -0
- package/dist/esm/ddl/sql.js +51 -0
- package/dist/esm/delete/delete-builder.js +128 -0
- package/dist/esm/delete/index.js +1 -0
- package/dist/esm/errors/relq-errors.js +310 -0
- package/dist/esm/examples/fulltext-search-test.js +117 -0
- package/dist/esm/explain/explain-builder.js +95 -0
- package/dist/esm/explain/index.js +1 -0
- package/dist/esm/function/create-function-builder.js +188 -0
- package/dist/esm/function/index.js +1 -0
- package/dist/esm/functions/advanced-functions.js +231 -0
- package/dist/esm/functions/case-builder.js +58 -0
- package/dist/esm/functions/geometric-functions.js +97 -0
- package/dist/esm/functions/index.js +171 -0
- package/dist/esm/functions/network-functions.js +79 -0
- package/dist/esm/functions/sql-functions.js +421 -0
- package/dist/esm/index.js +34 -0
- package/dist/esm/indexing/create-index-builder.js +180 -0
- package/dist/esm/indexing/drop-index-builder.js +81 -0
- package/dist/esm/indexing/index-types.js +1 -0
- package/dist/esm/indexing/index.js +2 -0
- package/dist/esm/insert/conflict-builder.js +162 -0
- package/dist/esm/insert/index.js +1 -0
- package/dist/esm/insert/insert-builder.js +247 -0
- package/dist/esm/introspect/index.js +224 -0
- package/dist/esm/maintenance/index.js +1 -0
- package/dist/esm/maintenance/vacuum-builder.js +158 -0
- package/dist/esm/pubsub/index.js +1 -0
- package/dist/esm/pubsub/listen-notify-builder.js +48 -0
- package/dist/esm/pubsub/listener-connection.js +173 -0
- package/dist/esm/raw/index.js +1 -0
- package/dist/esm/raw/raw-query-builder.js +20 -0
- package/dist/esm/schema/index.js +1 -0
- package/dist/esm/schema/schema-builder.js +1150 -0
- package/dist/esm/schema-builder.js +2 -0
- package/dist/esm/schema-definition/column-types.js +738 -0
- package/dist/esm/schema-definition/index.js +10 -0
- package/dist/esm/schema-definition/introspection.js +614 -0
- package/dist/esm/schema-definition/partitions.js +123 -0
- package/dist/esm/schema-definition/pg-enum.js +70 -0
- package/dist/esm/schema-definition/pg-function.js +85 -0
- package/dist/esm/schema-definition/pg-sequence.js +50 -0
- package/dist/esm/schema-definition/pg-trigger.js +102 -0
- package/dist/esm/schema-definition/relations.js +90 -0
- package/dist/esm/schema-definition/sql-expressions.js +193 -0
- package/dist/esm/schema-definition/table-definition.js +630 -0
- package/dist/esm/select/aggregate-builder.js +172 -0
- package/dist/esm/select/index.js +1 -0
- package/dist/esm/select/select-builder.js +226 -0
- package/dist/esm/sequence/index.js +1 -0
- package/dist/esm/sequence/sequence-builder.js +255 -0
- package/dist/esm/table/alter-table-builder.js +138 -0
- package/dist/esm/table/constraint-builder.js +95 -0
- package/dist/esm/table/create-table-builder.js +241 -0
- package/dist/esm/table/index.js +5 -0
- package/dist/esm/table/partition-builder.js +121 -0
- package/dist/esm/table/truncate-builder.js +63 -0
- package/dist/esm/transaction/index.js +1 -0
- package/dist/esm/transaction/transaction-builder.js +70 -0
- package/dist/esm/trigger/create-trigger-builder.js +166 -0
- package/dist/esm/trigger/index.js +1 -0
- package/dist/esm/types/aggregate-types.js +1 -0
- package/dist/esm/types/config-types.js +36 -0
- package/dist/esm/types/inference-types.js +12 -0
- package/dist/esm/types/pagination-types.js +4 -0
- package/dist/esm/types/result-types.js +1 -0
- package/dist/esm/types/schema-types.js +1 -0
- package/dist/esm/types/subscription-types.js +1 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/update/array-update-builder.js +192 -0
- package/dist/esm/update/index.js +2 -0
- package/dist/esm/update/update-builder.js +188 -0
- package/dist/esm/utils/case-converter.js +55 -0
- package/dist/esm/utils/environment-detection.js +113 -0
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/pool-defaults.js +100 -0
- package/dist/esm/utils/type-coercion.js +110 -0
- package/dist/esm/view/create-view-builder.js +171 -0
- package/dist/esm/view/index.js +1 -0
- package/dist/esm/window/index.js +1 -0
- package/dist/esm/window/window-builder.js +73 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +10341 -0
- package/dist/index.js +1 -0
- package/dist/schema-builder.cjs +1 -0
- package/dist/schema-builder.d.ts +2272 -0
- package/dist/schema-builder.js +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.partitionStrategyFactory = void 0;
|
|
4
|
+
exports.partition = partition;
|
|
5
|
+
exports.generatePartitionBySQL = generatePartitionBySQL;
|
|
6
|
+
exports.generateChildPartitionSQL = generateChildPartitionSQL;
|
|
7
|
+
function getColumnName(column) {
|
|
8
|
+
if (typeof column === 'string')
|
|
9
|
+
return column;
|
|
10
|
+
return column.$columnName || column.name || 'unknown';
|
|
11
|
+
}
|
|
12
|
+
function createListBuilder(column) {
|
|
13
|
+
const builder = {
|
|
14
|
+
$type: 'LIST',
|
|
15
|
+
$column: column,
|
|
16
|
+
sub(fn) {
|
|
17
|
+
const subStrategy = fn(exports.partitionStrategyFactory);
|
|
18
|
+
return {
|
|
19
|
+
...this,
|
|
20
|
+
$subpartition: subStrategy,
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
return builder;
|
|
25
|
+
}
|
|
26
|
+
function createRangeBuilder(column) {
|
|
27
|
+
const builder = {
|
|
28
|
+
$type: 'RANGE',
|
|
29
|
+
$column: column,
|
|
30
|
+
sub(fn) {
|
|
31
|
+
const subStrategy = fn(exports.partitionStrategyFactory);
|
|
32
|
+
return {
|
|
33
|
+
...this,
|
|
34
|
+
$subpartition: subStrategy,
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return builder;
|
|
39
|
+
}
|
|
40
|
+
function createHashBuilder(column, modulus) {
|
|
41
|
+
return {
|
|
42
|
+
$type: 'HASH',
|
|
43
|
+
$column: column,
|
|
44
|
+
$modulus: modulus,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
exports.partitionStrategyFactory = {
|
|
48
|
+
list(column) {
|
|
49
|
+
return createListBuilder(getColumnName(column));
|
|
50
|
+
},
|
|
51
|
+
range(column) {
|
|
52
|
+
return createRangeBuilder(getColumnName(column));
|
|
53
|
+
},
|
|
54
|
+
hash(column, modulus) {
|
|
55
|
+
return createHashBuilder(getColumnName(column), modulus);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
function formatValue(value) {
|
|
59
|
+
if (value instanceof Date) {
|
|
60
|
+
return value.toISOString().split('T')[0];
|
|
61
|
+
}
|
|
62
|
+
return String(value);
|
|
63
|
+
}
|
|
64
|
+
function partition(name) {
|
|
65
|
+
return {
|
|
66
|
+
$name: name,
|
|
67
|
+
in(values) {
|
|
68
|
+
const normalized = Array.isArray(values) ? values : [values];
|
|
69
|
+
return {
|
|
70
|
+
$name: name,
|
|
71
|
+
$partitionType: 'LIST',
|
|
72
|
+
$values: normalized,
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
from(value) {
|
|
76
|
+
const fromValue = formatValue(value);
|
|
77
|
+
return {
|
|
78
|
+
to(toValue) {
|
|
79
|
+
return {
|
|
80
|
+
$name: name,
|
|
81
|
+
$partitionType: 'RANGE',
|
|
82
|
+
$from: fromValue,
|
|
83
|
+
$to: formatValue(toValue),
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
remainder(value) {
|
|
89
|
+
return {
|
|
90
|
+
$name: name,
|
|
91
|
+
$partitionType: 'HASH',
|
|
92
|
+
$remainder: value,
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
default() {
|
|
96
|
+
return {
|
|
97
|
+
$name: name,
|
|
98
|
+
$partitionType: 'DEFAULT',
|
|
99
|
+
$isDefault: true,
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function generatePartitionBySQL(strategy) {
|
|
105
|
+
let sql = `PARTITION BY ${strategy.$type} (${strategy.$column})`;
|
|
106
|
+
return sql;
|
|
107
|
+
}
|
|
108
|
+
function generateChildPartitionSQL(parentTable, child, subpartitionStrategy) {
|
|
109
|
+
let sql = `CREATE TABLE IF NOT EXISTS ${child.$name} PARTITION OF ${parentTable}`;
|
|
110
|
+
switch (child.$partitionType) {
|
|
111
|
+
case 'LIST':
|
|
112
|
+
const values = child.$values?.map(v => `'${v}'`).join(', ') || '';
|
|
113
|
+
sql += ` FOR VALUES IN (${values})`;
|
|
114
|
+
break;
|
|
115
|
+
case 'RANGE':
|
|
116
|
+
sql += ` FOR VALUES FROM ('${child.$from}') TO ('${child.$to}')`;
|
|
117
|
+
break;
|
|
118
|
+
case 'HASH':
|
|
119
|
+
sql += ` FOR VALUES WITH (MODULUS ${child.$modulus}, REMAINDER ${child.$remainder})`;
|
|
120
|
+
break;
|
|
121
|
+
case 'DEFAULT':
|
|
122
|
+
sql += ` DEFAULT`;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
if (subpartitionStrategy) {
|
|
126
|
+
sql += ` ${generatePartitionBySQL(subpartitionStrategy)}`;
|
|
127
|
+
}
|
|
128
|
+
return sql + ';';
|
|
129
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pgEnum = pgEnum;
|
|
4
|
+
exports.generateEnumSQL = generateEnumSQL;
|
|
5
|
+
exports.dropEnumSQL = dropEnumSQL;
|
|
6
|
+
exports.addEnumValueSQL = addEnumValueSQL;
|
|
7
|
+
function pgEnum(name, values) {
|
|
8
|
+
const config = function (columnName) {
|
|
9
|
+
const col = {
|
|
10
|
+
$type: name,
|
|
11
|
+
$enumName: name,
|
|
12
|
+
$checkValues: values,
|
|
13
|
+
};
|
|
14
|
+
if (columnName) {
|
|
15
|
+
col.$columnName = columnName;
|
|
16
|
+
}
|
|
17
|
+
return Object.assign(col, {
|
|
18
|
+
notNull() {
|
|
19
|
+
col.$nullable = false;
|
|
20
|
+
return Object.assign(this, { $nullable: false });
|
|
21
|
+
},
|
|
22
|
+
nullable() {
|
|
23
|
+
col.$nullable = true;
|
|
24
|
+
return Object.assign(this, { $nullable: true });
|
|
25
|
+
},
|
|
26
|
+
default(value) {
|
|
27
|
+
col.$default = value;
|
|
28
|
+
return Object.assign(this, { $default: value });
|
|
29
|
+
},
|
|
30
|
+
primaryKey() {
|
|
31
|
+
col.$primaryKey = true;
|
|
32
|
+
return Object.assign(this, { $primaryKey: true });
|
|
33
|
+
},
|
|
34
|
+
unique() {
|
|
35
|
+
col.$unique = true;
|
|
36
|
+
return Object.assign(this, { $unique: true });
|
|
37
|
+
},
|
|
38
|
+
array() {
|
|
39
|
+
col.$array = true;
|
|
40
|
+
col.$type = `${name}[]`;
|
|
41
|
+
return Object.assign(this, { $array: true, $type: `${name}[]` });
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperties(config, {
|
|
46
|
+
$enumName: { value: name, enumerable: true },
|
|
47
|
+
$enumValues: { value: values, enumerable: true },
|
|
48
|
+
$inferEnum: { value: undefined },
|
|
49
|
+
values: { value: values, enumerable: true },
|
|
50
|
+
name: { value: name, enumerable: true },
|
|
51
|
+
includes: {
|
|
52
|
+
value: (value) => {
|
|
53
|
+
return typeof value === 'string' && values.includes(value);
|
|
54
|
+
},
|
|
55
|
+
enumerable: true,
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
return config;
|
|
59
|
+
}
|
|
60
|
+
function generateEnumSQL(name, values) {
|
|
61
|
+
const escapedValues = values.map(v => `'${v.replace(/'/g, "''")}'`).join(', ');
|
|
62
|
+
return `CREATE TYPE "${name}" AS ENUM (${escapedValues});`;
|
|
63
|
+
}
|
|
64
|
+
function dropEnumSQL(name) {
|
|
65
|
+
return `DROP TYPE IF EXISTS "${name}" CASCADE;`;
|
|
66
|
+
}
|
|
67
|
+
function addEnumValueSQL(enumName, newValue, position) {
|
|
68
|
+
let sql = `ALTER TYPE "${enumName}" ADD VALUE '${newValue.replace(/'/g, "''")}'`;
|
|
69
|
+
if (position?.before) {
|
|
70
|
+
sql += ` BEFORE '${position.before.replace(/'/g, "''")}'`;
|
|
71
|
+
}
|
|
72
|
+
else if (position?.after) {
|
|
73
|
+
sql += ` AFTER '${position.after.replace(/'/g, "''")}'`;
|
|
74
|
+
}
|
|
75
|
+
return sql + ';';
|
|
76
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateFunctionSQL = generateFunctionSQL;
|
|
4
|
+
exports.dropFunctionSQL = dropFunctionSQL;
|
|
5
|
+
exports.pgFunction = pgFunction;
|
|
6
|
+
exports.isFunctionConfig = isFunctionConfig;
|
|
7
|
+
function generateFunctionSQL(config) {
|
|
8
|
+
const { $functionName, $options } = config;
|
|
9
|
+
const { args = [], returns, language = 'plpgsql', volatility, parallel, security, strict, leakproof, cost, rows, setConfig, body, raw, } = $options;
|
|
10
|
+
const parts = ['CREATE OR REPLACE FUNCTION', $functionName];
|
|
11
|
+
const argStrings = args.map(arg => {
|
|
12
|
+
let argStr = '';
|
|
13
|
+
if (arg.mode && arg.mode !== 'IN') {
|
|
14
|
+
argStr += arg.mode + ' ';
|
|
15
|
+
}
|
|
16
|
+
argStr += arg.name + ' ' + arg.type;
|
|
17
|
+
if (arg.default !== undefined) {
|
|
18
|
+
argStr += ' DEFAULT ' + arg.default;
|
|
19
|
+
}
|
|
20
|
+
return argStr;
|
|
21
|
+
});
|
|
22
|
+
parts.push('(' + argStrings.join(', ') + ')');
|
|
23
|
+
parts.push('\nRETURNS ' + returns);
|
|
24
|
+
parts.push('\nLANGUAGE ' + language);
|
|
25
|
+
if (volatility) {
|
|
26
|
+
parts.push('\n' + volatility);
|
|
27
|
+
}
|
|
28
|
+
if (parallel) {
|
|
29
|
+
parts.push('\nPARALLEL ' + parallel);
|
|
30
|
+
}
|
|
31
|
+
if (security) {
|
|
32
|
+
parts.push('\nSECURITY ' + security);
|
|
33
|
+
}
|
|
34
|
+
if (strict) {
|
|
35
|
+
parts.push('\nSTRICT');
|
|
36
|
+
}
|
|
37
|
+
if (leakproof) {
|
|
38
|
+
parts.push('\nLEAKPROOF');
|
|
39
|
+
}
|
|
40
|
+
if (cost !== undefined) {
|
|
41
|
+
parts.push('\nCOST ' + cost);
|
|
42
|
+
}
|
|
43
|
+
if (rows !== undefined) {
|
|
44
|
+
parts.push('\nROWS ' + rows);
|
|
45
|
+
}
|
|
46
|
+
if (setConfig) {
|
|
47
|
+
for (const [key, value] of Object.entries(setConfig)) {
|
|
48
|
+
parts.push(`\nSET ${key} = '${value}'`);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
parts.push('\nAS $$');
|
|
52
|
+
if (raw) {
|
|
53
|
+
parts.push('\n' + raw.trim());
|
|
54
|
+
}
|
|
55
|
+
else if (body) {
|
|
56
|
+
const trimmedBody = body.trim();
|
|
57
|
+
if (language.toLowerCase() === 'plpgsql' &&
|
|
58
|
+
!trimmedBody.toUpperCase().startsWith('BEGIN') &&
|
|
59
|
+
!trimmedBody.toUpperCase().startsWith('DECLARE')) {
|
|
60
|
+
parts.push('\nBEGIN');
|
|
61
|
+
parts.push('\n ' + trimmedBody.replace(/\n/g, '\n '));
|
|
62
|
+
parts.push('\nEND;');
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
parts.push('\n' + trimmedBody);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
parts.push('\n$$');
|
|
69
|
+
return parts.join('');
|
|
70
|
+
}
|
|
71
|
+
function dropFunctionSQL(config, ifExists = true) {
|
|
72
|
+
const { $functionName, $options } = config;
|
|
73
|
+
const argTypes = ($options.args || [])
|
|
74
|
+
.filter(arg => !arg.mode || arg.mode === 'IN' || arg.mode === 'INOUT' || arg.mode === 'VARIADIC')
|
|
75
|
+
.map(arg => arg.type)
|
|
76
|
+
.join(', ');
|
|
77
|
+
return `DROP FUNCTION ${ifExists ? 'IF EXISTS ' : ''}${$functionName}(${argTypes})`;
|
|
78
|
+
}
|
|
79
|
+
function pgFunction(name, options) {
|
|
80
|
+
return {
|
|
81
|
+
$functionName: name,
|
|
82
|
+
$options: options,
|
|
83
|
+
$type: 'function',
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function isFunctionConfig(value) {
|
|
87
|
+
return (typeof value === 'object' &&
|
|
88
|
+
value !== null &&
|
|
89
|
+
'$type' in value &&
|
|
90
|
+
value.$type === 'function');
|
|
91
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pgSequence = pgSequence;
|
|
4
|
+
exports.generateSequenceSQL = generateSequenceSQL;
|
|
5
|
+
exports.dropSequenceSQL = dropSequenceSQL;
|
|
6
|
+
exports.isSequenceConfig = isSequenceConfig;
|
|
7
|
+
function pgSequence(name, options = {}) {
|
|
8
|
+
return {
|
|
9
|
+
_type: 'sequence',
|
|
10
|
+
name,
|
|
11
|
+
options,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function generateSequenceSQL(seq) {
|
|
15
|
+
const parts = [`CREATE SEQUENCE ${seq.name}`];
|
|
16
|
+
if (seq.options.as) {
|
|
17
|
+
parts.push(`AS ${seq.options.as.toUpperCase()}`);
|
|
18
|
+
}
|
|
19
|
+
if (seq.options.start !== undefined) {
|
|
20
|
+
parts.push(`START WITH ${seq.options.start}`);
|
|
21
|
+
}
|
|
22
|
+
if (seq.options.increment !== undefined) {
|
|
23
|
+
parts.push(`INCREMENT BY ${seq.options.increment}`);
|
|
24
|
+
}
|
|
25
|
+
if (seq.options.minValue === null) {
|
|
26
|
+
parts.push('NO MINVALUE');
|
|
27
|
+
}
|
|
28
|
+
else if (seq.options.minValue !== undefined) {
|
|
29
|
+
parts.push(`MINVALUE ${seq.options.minValue}`);
|
|
30
|
+
}
|
|
31
|
+
if (seq.options.maxValue === null) {
|
|
32
|
+
parts.push('NO MAXVALUE');
|
|
33
|
+
}
|
|
34
|
+
else if (seq.options.maxValue !== undefined) {
|
|
35
|
+
parts.push(`MAXVALUE ${seq.options.maxValue}`);
|
|
36
|
+
}
|
|
37
|
+
if (seq.options.cache !== undefined) {
|
|
38
|
+
parts.push(`CACHE ${seq.options.cache}`);
|
|
39
|
+
}
|
|
40
|
+
if (seq.options.cycle !== undefined) {
|
|
41
|
+
parts.push(seq.options.cycle ? 'CYCLE' : 'NO CYCLE');
|
|
42
|
+
}
|
|
43
|
+
if (seq.options.ownedBy) {
|
|
44
|
+
parts.push(`OWNED BY ${seq.options.ownedBy}`);
|
|
45
|
+
}
|
|
46
|
+
return parts.join('\n ') + ';';
|
|
47
|
+
}
|
|
48
|
+
function dropSequenceSQL(seq) {
|
|
49
|
+
return `DROP SEQUENCE IF EXISTS ${seq.name};`;
|
|
50
|
+
}
|
|
51
|
+
function isSequenceConfig(value) {
|
|
52
|
+
return (typeof value === 'object' &&
|
|
53
|
+
value !== null &&
|
|
54
|
+
'_type' in value &&
|
|
55
|
+
value._type === 'sequence');
|
|
56
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateTriggerSQL = generateTriggerSQL;
|
|
4
|
+
exports.dropTriggerSQL = dropTriggerSQL;
|
|
5
|
+
exports.pgTrigger = pgTrigger;
|
|
6
|
+
exports.isTriggerConfig = isTriggerConfig;
|
|
7
|
+
function getTableName(table) {
|
|
8
|
+
if (typeof table === 'string') {
|
|
9
|
+
return table;
|
|
10
|
+
}
|
|
11
|
+
if (typeof table === 'object' && table !== null) {
|
|
12
|
+
if ('$tableName' in table) {
|
|
13
|
+
return table.$tableName;
|
|
14
|
+
}
|
|
15
|
+
if ('name' in table) {
|
|
16
|
+
return table.name;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
throw new Error('Invalid table reference in trigger definition');
|
|
20
|
+
}
|
|
21
|
+
function getFunctionName(func) {
|
|
22
|
+
if (typeof func === 'string') {
|
|
23
|
+
return func;
|
|
24
|
+
}
|
|
25
|
+
return func.$functionName;
|
|
26
|
+
}
|
|
27
|
+
function generateTriggerSQL(config) {
|
|
28
|
+
const { $triggerName, $options } = config;
|
|
29
|
+
const { on, before, after, insteadOf, updateOf, forEachRow = true, forEachStatement, when, referencing, constraint, deferrable, initially, execute, executeArgs, } = $options;
|
|
30
|
+
const parts = ['CREATE'];
|
|
31
|
+
if (constraint) {
|
|
32
|
+
parts.push('CONSTRAINT');
|
|
33
|
+
}
|
|
34
|
+
parts.push('TRIGGER', $triggerName);
|
|
35
|
+
let timing;
|
|
36
|
+
let events;
|
|
37
|
+
if (before) {
|
|
38
|
+
timing = 'BEFORE';
|
|
39
|
+
events = Array.isArray(before) ? before : [before];
|
|
40
|
+
}
|
|
41
|
+
else if (after) {
|
|
42
|
+
timing = 'AFTER';
|
|
43
|
+
events = Array.isArray(after) ? after : [after];
|
|
44
|
+
}
|
|
45
|
+
else if (insteadOf) {
|
|
46
|
+
timing = 'INSTEAD OF';
|
|
47
|
+
events = Array.isArray(insteadOf) ? insteadOf : [insteadOf];
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
throw new Error('Trigger must have before, after, or insteadOf timing');
|
|
51
|
+
}
|
|
52
|
+
parts.push('\n ' + timing);
|
|
53
|
+
const eventStrings = events.map(event => {
|
|
54
|
+
if (event === 'UPDATE' && updateOf && updateOf.length > 0) {
|
|
55
|
+
return 'UPDATE OF ' + updateOf.join(', ');
|
|
56
|
+
}
|
|
57
|
+
return event;
|
|
58
|
+
});
|
|
59
|
+
parts.push(eventStrings.join(' OR '));
|
|
60
|
+
parts.push('ON', getTableName(on));
|
|
61
|
+
if (deferrable) {
|
|
62
|
+
parts.push('\n DEFERRABLE');
|
|
63
|
+
if (initially) {
|
|
64
|
+
parts.push('INITIALLY', initially);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (referencing) {
|
|
68
|
+
const refParts = ['\n REFERENCING'];
|
|
69
|
+
if (referencing.oldTable) {
|
|
70
|
+
refParts.push('OLD TABLE AS', referencing.oldTable);
|
|
71
|
+
}
|
|
72
|
+
if (referencing.newTable) {
|
|
73
|
+
refParts.push('NEW TABLE AS', referencing.newTable);
|
|
74
|
+
}
|
|
75
|
+
parts.push(refParts.join(' '));
|
|
76
|
+
}
|
|
77
|
+
if (forEachStatement) {
|
|
78
|
+
parts.push('\n FOR EACH STATEMENT');
|
|
79
|
+
}
|
|
80
|
+
else if (forEachRow !== false) {
|
|
81
|
+
parts.push('\n FOR EACH ROW');
|
|
82
|
+
}
|
|
83
|
+
if (when) {
|
|
84
|
+
parts.push('\n WHEN (' + when + ')');
|
|
85
|
+
}
|
|
86
|
+
const funcName = getFunctionName(execute);
|
|
87
|
+
const args = executeArgs ? executeArgs.join(', ') : '';
|
|
88
|
+
parts.push('\n EXECUTE FUNCTION', funcName + '(' + args + ')');
|
|
89
|
+
return parts.join(' ');
|
|
90
|
+
}
|
|
91
|
+
function dropTriggerSQL(config, ifExists = true) {
|
|
92
|
+
const { $triggerName, $options } = config;
|
|
93
|
+
const tableName = getTableName($options.on);
|
|
94
|
+
return `DROP TRIGGER ${ifExists ? 'IF EXISTS ' : ''}${$triggerName} ON ${tableName}`;
|
|
95
|
+
}
|
|
96
|
+
function pgTrigger(name, options) {
|
|
97
|
+
return {
|
|
98
|
+
$triggerName: name,
|
|
99
|
+
$options: options,
|
|
100
|
+
$type: 'trigger',
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function isTriggerConfig(value) {
|
|
104
|
+
return (typeof value === 'object' &&
|
|
105
|
+
value !== null &&
|
|
106
|
+
'$type' in value &&
|
|
107
|
+
value.$type === 'trigger');
|
|
108
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.one = one;
|
|
4
|
+
exports.many = many;
|
|
5
|
+
exports.manyToMany = manyToMany;
|
|
6
|
+
exports.defineRelations = defineRelations;
|
|
7
|
+
exports.getJoinCondition = getJoinCondition;
|
|
8
|
+
exports.generateJoinSQL = generateJoinSQL;
|
|
9
|
+
function one(from, to, options) {
|
|
10
|
+
const foreignKey = options?.foreignKey || `${to.$name}_id`;
|
|
11
|
+
const references = options?.references || findPrimaryKey(to) || 'id';
|
|
12
|
+
return {
|
|
13
|
+
$type: 'one',
|
|
14
|
+
$from: from,
|
|
15
|
+
$to: to,
|
|
16
|
+
$foreignKey: foreignKey,
|
|
17
|
+
$references: references,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function many(from, to, options) {
|
|
21
|
+
const foreignKey = options?.foreignKey || `${from.$name}_id`;
|
|
22
|
+
const references = options?.references || findPrimaryKey(from) || 'id';
|
|
23
|
+
return {
|
|
24
|
+
$type: 'many',
|
|
25
|
+
$from: from,
|
|
26
|
+
$to: to,
|
|
27
|
+
$foreignKey: foreignKey,
|
|
28
|
+
$references: references,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function manyToMany(from, to, through, options) {
|
|
32
|
+
const fromKey = options?.fromKey || findPrimaryKey(from) || 'id';
|
|
33
|
+
const toKey = options?.toKey || findPrimaryKey(to) || 'id';
|
|
34
|
+
const throughFromKey = options?.throughFromKey || `${from.$name}_id`;
|
|
35
|
+
const throughToKey = options?.throughToKey || `${to.$name}_id`;
|
|
36
|
+
return {
|
|
37
|
+
$type: 'manyToMany',
|
|
38
|
+
$from: from,
|
|
39
|
+
$to: to,
|
|
40
|
+
$foreignKey: fromKey,
|
|
41
|
+
$references: toKey,
|
|
42
|
+
$through: through,
|
|
43
|
+
$throughFromKey: throughFromKey,
|
|
44
|
+
$throughToKey: throughToKey,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function findPrimaryKey(table) {
|
|
48
|
+
if (table.$primaryKey && table.$primaryKey.length === 1) {
|
|
49
|
+
return table.$primaryKey[0];
|
|
50
|
+
}
|
|
51
|
+
for (const [colName, colConfig] of Object.entries(table.$columns)) {
|
|
52
|
+
if (colConfig.$primaryKey) {
|
|
53
|
+
return colName;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
function defineRelations(config) {
|
|
59
|
+
return {
|
|
60
|
+
tables: config.tables,
|
|
61
|
+
relations: config.relations(config.tables),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function getJoinCondition(relation) {
|
|
65
|
+
if (relation.$type === 'manyToMany' && relation.$through) {
|
|
66
|
+
return {
|
|
67
|
+
type: 'manyToMany',
|
|
68
|
+
from: { table: relation.$from.$name, column: relation.$foreignKey },
|
|
69
|
+
to: { table: relation.$to.$name, column: relation.$references },
|
|
70
|
+
through: {
|
|
71
|
+
table: relation.$through.$name,
|
|
72
|
+
fromColumn: relation.$throughFromKey,
|
|
73
|
+
toColumn: relation.$throughToKey,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
type: relation.$type,
|
|
79
|
+
from: { table: relation.$from.$name, column: relation.$foreignKey },
|
|
80
|
+
to: { table: relation.$to.$name, column: relation.$references },
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function generateJoinSQL(relation, fromAlias, toAlias) {
|
|
84
|
+
const from = fromAlias || relation.$from.$name;
|
|
85
|
+
const to = toAlias || relation.$to.$name;
|
|
86
|
+
if (relation.$type === 'manyToMany' && relation.$through) {
|
|
87
|
+
const throughTable = relation.$through.$name;
|
|
88
|
+
const throughAlias = `${throughTable}_join`;
|
|
89
|
+
return [
|
|
90
|
+
`JOIN "${throughTable}" AS "${throughAlias}" ON "${from}"."${relation.$foreignKey}" = "${throughAlias}"."${relation.$throughFromKey}"`,
|
|
91
|
+
`JOIN "${relation.$to.$name}" AS "${to}" ON "${throughAlias}"."${relation.$throughToKey}" = "${to}"."${relation.$references}"`,
|
|
92
|
+
].join('\n');
|
|
93
|
+
}
|
|
94
|
+
if (relation.$type === 'one') {
|
|
95
|
+
return `JOIN "${relation.$to.$name}" AS "${to}" ON "${from}"."${relation.$foreignKey}" = "${to}"."${relation.$references}"`;
|
|
96
|
+
}
|
|
97
|
+
return `JOIN "${relation.$to.$name}" AS "${to}" ON "${to}"."${relation.$foreignKey}" = "${from}"."${relation.$references}"`;
|
|
98
|
+
}
|