@rebasepro/server-postgres 0.12.1-canary.gf5f1d39 → 0.13.1-canary.g18cfeb7
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/PostgresBackendDriver.d.ts +49 -2
- package/dist/PostgresBootstrapper.d.ts +25 -1
- package/dist/auth/services.d.ts +21 -0
- package/dist/{src-BbFOPJ1S.js → auth-users-columns-Do9mw5Y5.js} +602 -200
- package/dist/auth-users-columns-Do9mw5Y5.js.map +1 -0
- package/dist/backup/backup-service.d.ts +10 -1
- package/dist/backup/pg-tools.d.ts +47 -0
- package/dist/backup-service-Bww-Lg0s.js +8999 -0
- package/dist/backup-service-Bww-Lg0s.js.map +1 -0
- package/dist/cli-helpers.d.ts +40 -1
- package/dist/connection-BuZ97wsr.js +250 -0
- package/dist/connection-BuZ97wsr.js.map +1 -0
- package/dist/connection.d.ts +42 -0
- package/dist/ensure-collection-policies-DMUdRQdM.js +57 -0
- package/dist/ensure-collection-policies-DMUdRQdM.js.map +1 -0
- package/dist/ensure-collection-tables-DSIxvLLD.js +694 -0
- package/dist/ensure-collection-tables-DSIxvLLD.js.map +1 -0
- package/dist/index.es.js +1701 -10167
- package/dist/index.es.js.map +1 -1
- package/dist/policy-CPkCqVTz.js +105 -0
- package/dist/policy-CPkCqVTz.js.map +1 -0
- package/dist/rls-bootstrap-sql-Bpv3nUZo.js +244 -0
- package/dist/rls-bootstrap-sql-Bpv3nUZo.js.map +1 -0
- package/dist/schema/auth-schema.d.ts +83 -144
- package/dist/schema/auth-users-columns.d.ts +97 -0
- package/dist/schema/ensure-collection-policies.d.ts +60 -0
- package/dist/schema/ensure-collection-tables.d.ts +45 -3
- package/dist/schema/generate-drizzle-schema-logic.d.ts +1 -1
- package/dist/schema/generate-postgres-ddl-logic.d.ts +137 -3
- package/dist/schema/generated-schema-staleness.d.ts +39 -0
- package/dist/schema/introspect-db-constraints.d.ts +57 -0
- package/dist/schema/introspect-db-logic.d.ts +94 -5
- package/dist/schema/introspect-db-queries.d.ts +119 -0
- package/dist/schema/introspect-db-structure.d.ts +263 -0
- package/dist/schema/introspect-db-types.d.ts +11 -0
- package/dist/schema/rls-bootstrap-sql.d.ts +135 -0
- package/dist/security/rls-enforcement.d.ts +53 -2
- package/dist/services/FetchService.d.ts +14 -8
- package/dist/services/RelationService.d.ts +24 -1
- package/dist/services/channel-bus/index.d.ts +1 -7
- package/dist/services/collection-helpers.d.ts +36 -2
- package/dist/services/dataService.d.ts +5 -1
- package/dist/services/realtimeService.d.ts +25 -21
- package/dist/services/row-pipeline.d.ts +1 -1
- package/dist/{src-Zqwaw3P5.js → src-C_wvdMnl.js} +78 -145
- package/dist/src-C_wvdMnl.js.map +1 -0
- package/dist/utils/connection-string.d.ts +29 -0
- package/dist/utils/drizzle-conditions.d.ts +5 -4
- package/dist/utils/pg-error-utils.d.ts +35 -0
- package/dist/websocket-D0TBU3ia.js +530 -0
- package/dist/websocket-D0TBU3ia.js.map +1 -0
- package/package.json +17 -16
- package/src/PostgresAdapter.ts +21 -2
- package/src/PostgresBackendDriver.ts +169 -3
- package/src/PostgresBootstrapper.ts +233 -35
- package/src/auth/ensure-tables.ts +335 -81
- package/src/auth/services.ts +24 -2
- package/src/backup/backup-cli.ts +41 -2
- package/src/backup/backup-service.ts +38 -5
- package/src/backup/pg-tools.ts +96 -3
- package/src/cli-helpers.ts +92 -9
- package/src/cli.ts +219 -56
- package/src/collections/validate-relations.ts +136 -14
- package/src/connection.ts +73 -0
- package/src/data-transformer.ts +22 -6
- package/src/databasePoolManager.ts +5 -2
- package/src/history/ensure-history-table.ts +7 -0
- package/src/schema/auth-schema.ts +30 -19
- package/src/schema/auth-users-columns.ts +131 -0
- package/src/schema/doctor.ts +7 -5
- package/src/schema/ensure-collection-policies.ts +105 -0
- package/src/schema/ensure-collection-tables.test.ts +105 -9
- package/src/schema/ensure-collection-tables.ts +301 -44
- package/src/schema/generate-drizzle-schema-logic.ts +49 -7
- package/src/schema/generate-postgres-ddl-logic.ts +474 -27
- package/src/schema/generated-schema-staleness.ts +169 -0
- package/src/schema/introspect-db-constraints.ts +385 -0
- package/src/schema/introspect-db-inference.ts +18 -8
- package/src/schema/introspect-db-logic.ts +385 -71
- package/src/schema/introspect-db-queries.ts +326 -0
- package/src/schema/introspect-db-structure.ts +670 -0
- package/src/schema/introspect-db-types.ts +56 -0
- package/src/schema/introspect-db.ts +37 -80
- package/src/schema/introspect-runtime.test.ts +56 -8
- package/src/schema/introspect-runtime.ts +31 -9
- package/src/schema/non-sql-collections.test.ts +131 -0
- package/src/schema/rls-bootstrap-sql.ts +288 -0
- package/src/security/anonymous-grants.test.ts +4 -2
- package/src/security/policy-drift.test.ts +11 -3
- package/src/security/rls-enforcement.ts +141 -3
- package/src/services/BranchService.ts +71 -28
- package/src/services/FetchService.ts +100 -107
- package/src/services/PersistService.ts +34 -7
- package/src/services/RelationService.ts +249 -48
- package/src/services/channel-bus/index.ts +0 -9
- package/src/services/channel-history.ts +8 -0
- package/src/services/channel-presence.ts +6 -0
- package/src/services/collection-helpers.ts +69 -3
- package/src/services/dataService.ts +5 -1
- package/src/services/realtimeService.ts +38 -35
- package/src/services/row-pipeline.ts +1 -1
- package/src/utils/connection-string.ts +58 -0
- package/src/utils/drizzle-conditions.ts +31 -6
- package/src/utils/pg-error-utils.ts +65 -0
- package/src/websocket.ts +18 -9
- package/dist/chunk-DSJWtz9O.js +0 -40
- package/dist/ensure-collection-tables-CNTcZGvn.js +0 -304
- package/dist/ensure-collection-tables-CNTcZGvn.js.map +0 -1
- package/dist/schema/auth-bootstrap-sql.d.ts +0 -24
- package/dist/src-BbFOPJ1S.js.map +0 -1
- package/dist/src-Zqwaw3P5.js.map +0 -1
- package/src/schema/auth-bootstrap-sql.ts +0 -47
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CollectionConfig, NumberProperty, Property, ResolvedRelation, RelationProperty, SecurityOperation, SecurityRule, StringProperty, isPostgresCollectionConfig, DateProperty, ArrayProperty, MapProperty, ReferenceProperty, VectorProperty, BinaryProperty, isManyToMany, type ResolvedManyToMany, type ResolvedBelongsTo, type ResolvedForeignKeyOnTarget, hasForeignKeyOnTarget } from "@rebasepro/types";
|
|
2
2
|
import { getPrimaryKeys } from "../services/collection-helpers";
|
|
3
|
-
import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig } from "@rebasepro/common";
|
|
3
|
+
import { getEnumVarName, getTableName, getTableVarName, resolveCollectionRelations, findRelation, securityRuleToConditions, policyToPostgres, getEffectiveSecurityRules, resolveJunctionSpecs, getJunctionSecurityRules, getJunctionCollectionConfig, resolveStringColumnLength, relationalCollections } from "@rebasepro/common";
|
|
4
4
|
import { toSnakeCase, getPolicyNamesForRule } from "@rebasepro/utils";
|
|
5
5
|
import { logger } from "@rebasepro/server";
|
|
6
6
|
// --- Helper Functions ---
|
|
@@ -98,9 +98,13 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
|
|
|
98
98
|
} else if (stringProp.columnType === "uuid") {
|
|
99
99
|
columnDefinition = `uuid("${colName}")`;
|
|
100
100
|
} else if (stringProp.columnType === "char") {
|
|
101
|
-
columnDefinition = `char("${colName}")`;
|
|
101
|
+
columnDefinition = `char("${colName}", { length: ${resolveStringColumnLength(stringProp)} })`;
|
|
102
102
|
} else if (stringProp.columnType === "varchar") {
|
|
103
|
-
|
|
103
|
+
// The length is not optional decoration: `varchar("col")` with
|
|
104
|
+
// no length is an UNBOUNDED varchar in Postgres, which is what
|
|
105
|
+
// this emitted while the DDL generator emitted VARCHAR(255) for
|
|
106
|
+
// the very same property.
|
|
107
|
+
columnDefinition = `varchar("${colName}", { length: ${resolveStringColumnLength(stringProp)} })`;
|
|
104
108
|
} else {
|
|
105
109
|
// `text` is the default, and the only length-unbounded choice.
|
|
106
110
|
// Ask for `varchar` explicitly if you want the length constraint.
|
|
@@ -133,6 +137,25 @@ const getDrizzleColumn = (propName: string, prop: Property, collection: Collecti
|
|
|
133
137
|
let baseType = (numProp.validation?.integer || isId) ? `integer("${colName}")` : `numeric("${colName}")`;
|
|
134
138
|
if (numProp.columnType) {
|
|
135
139
|
if (numProp.columnType === "double precision") baseType = `doublePrecision("${colName}")`;
|
|
140
|
+
// `bigint` and `bigserial` are the only pg-core builders that
|
|
141
|
+
// *require* a config argument: without `mode`, drizzle cannot
|
|
142
|
+
// know whether to hand back a `number` or a `bigint`, and the
|
|
143
|
+
// emitted call does not typecheck.
|
|
144
|
+
//
|
|
145
|
+
// This is why `schema.generated.ts` drifted. Regenerating it
|
|
146
|
+
// produced a file that would not compile, so the bigint lines
|
|
147
|
+
// were hand-patched — and every regeneration after that looked
|
|
148
|
+
// like a large, alarming diff nobody wanted to ship. The file
|
|
149
|
+
// then sat stale for long enough that a security fix to two RLS
|
|
150
|
+
// policies never reached production.
|
|
151
|
+
//
|
|
152
|
+
// `number` rather than `bigint`: these are counters and byte
|
|
153
|
+
// totals that every caller already treats as numbers, and
|
|
154
|
+
// switching the runtime type would be a breaking change to
|
|
155
|
+
// every consumer of the generated schema.
|
|
156
|
+
else if (numProp.columnType === "bigint" || numProp.columnType === "bigserial") {
|
|
157
|
+
baseType = `${numProp.columnType}("${colName}", { mode: "number" })`;
|
|
158
|
+
}
|
|
136
159
|
else baseType = `${numProp.columnType}("${colName}")`;
|
|
137
160
|
}
|
|
138
161
|
|
|
@@ -428,7 +451,13 @@ const computeSharedRelationName = (
|
|
|
428
451
|
};
|
|
429
452
|
|
|
430
453
|
// --- Main Schema Generation Logic ---
|
|
431
|
-
export const generateSchema = async (
|
|
454
|
+
export const generateSchema = async (allCollections: CollectionConfig[], stripPolicies = false): Promise<string> => {
|
|
455
|
+
// A Firestore or MongoDB collection has no table to generate, and generating
|
|
456
|
+
// one for it is not merely wasted output: `db push` would create it, and
|
|
457
|
+
// `rebase doctor` would then report the store the collection actually reads
|
|
458
|
+
// from as drift. Non-SQL collections leave the toolchain here, once, rather
|
|
459
|
+
// than being filtered again in each stage below.
|
|
460
|
+
const collections = relationalCollections(allCollections);
|
|
432
461
|
let schemaContent = "// This file is auto-generated by the Rebase Drizzle generator. Do not edit manually.\n\n";
|
|
433
462
|
|
|
434
463
|
|
|
@@ -547,8 +576,15 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
|
|
|
547
576
|
const tableVarName = getTableVarName(tableName);
|
|
548
577
|
if (isJunction && relation && sourceCollection && isManyToMany(relation)) {
|
|
549
578
|
const targetCollection = relation.target();
|
|
550
|
-
|
|
551
|
-
|
|
579
|
+
// Junctions live in `public`, full stop — `resolveJunctionSpecs`
|
|
580
|
+
// hardcodes that, so it is where `planJunctionTables` CREATEs them
|
|
581
|
+
// and where the derived RLS policies are applied. Inheriting the
|
|
582
|
+
// endpoint's schema here (as this used to) put the junction of any
|
|
583
|
+
// m2m onto `users` in `rebase`, while its policies were still
|
|
584
|
+
// created against `public.<junction>` — RLS enabled on one table,
|
|
585
|
+
// rows written to another. The three generators have to agree, and
|
|
586
|
+
// the other two already did.
|
|
587
|
+
const tableCreator = "pgTable";
|
|
552
588
|
const baseTableName = tableName.includes(".") ? tableName.split(".").pop()! : tableName;
|
|
553
589
|
const {
|
|
554
590
|
sourceColumn,
|
|
@@ -774,8 +810,14 @@ export const generateSchema = async (collections: CollectionConfig[], stripPolic
|
|
|
774
810
|
// name (e.g. "client_id") may differ from the property key
|
|
775
811
|
// (e.g. "clientId") when `columnName` is set.
|
|
776
812
|
const drizzleFieldKey = resolvePropertyKeyForColumn(collection, otherRel.foreignKeyOnTarget);
|
|
813
|
+
// The column the far side points at: its
|
|
814
|
+
// primary key, unless the link names another
|
|
815
|
+
// one with `sourceKey`.
|
|
816
|
+
const referencedKey = otherRel.sourceKey
|
|
817
|
+
? resolvePropertyKeyForColumn(otherCollection, otherRel.sourceKey)
|
|
818
|
+
: getPrimaryKeyName(otherCollection);
|
|
777
819
|
const synthKey = `_synth_${otherTableVar}_${drizzleFieldKey}`;
|
|
778
|
-
tableRelations.push(` "${synthKey}": one(${otherTableVar}, {\n fields: [${tableVarName}.${drizzleFieldKey}],\n references: [${otherTableVar}.${
|
|
820
|
+
tableRelations.push(` "${synthKey}": one(${otherTableVar}, {\n fields: [${tableVarName}.${drizzleFieldKey}],\n references: [${otherTableVar}.${referencedKey}],\n relationName: \"${drizzleRelationName}\"\n })`);
|
|
779
821
|
emittedRelationNames.add(deduplicationKey);
|
|
780
822
|
}
|
|
781
823
|
}
|