Skip to content
Open
19 changes: 17 additions & 2 deletions libraries/grpc-sdk/src/interfaces/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ export enum PostgresIndexType {
BRIN = 'BRIN',
}

/**
* Portable ascending/descending indexes that exist on MongoDB and every SQL dialect.
* Map to Mongo 1/-1 and SQL BTREE ASC/DESC. Optional uniqueness lives on index options.
*/
export enum CompatibleIndexType {
Ascending = 'Ascending',
Descending = 'Descending',
}

export type IndexType = MongoIndexType | PostgresIndexType | CompatibleIndexType;

export type ModelOptionsIndexTypes = IndexType | readonly IndexType[];

export type Array = any[];

export interface ConduitStringValidation {
Expand Down Expand Up @@ -244,16 +257,18 @@ export interface ConduitSchemaOptions {
}

export interface SchemaFieldIndex {
type?: MongoIndexType | PostgresIndexType;
type?: IndexType;
options?: MongoIndexOptions | PostgresIndexOptions;

[field: string]: any;
}

export interface ModelOptionsIndexes {
fields: string[] | readonly string[];
types?: MongoIndexType[] | PostgresIndexType;
types?: ModelOptionsIndexTypes;
options?: MongoIndexOptions | PostgresIndexOptions;
/** Optional. Generated deterministically when omitted. */
name?: string;

[field: string]: any;
}
Expand Down
7 changes: 4 additions & 3 deletions modules/authorization/src/models/ActorIndex.schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
CompatibleIndexType,
ConduitModel,
ConduitSchemaOptions,
DatabaseProvider,
MongoIndexType,
TYPE,
} from '@conduitplatform/grpc-sdk';
import { ConduitActiveSchema } from '@conduitplatform/module-tools';
Expand All @@ -23,7 +23,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
subjectId: {
Expand All @@ -41,7 +41,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
entityId: {
Expand Down Expand Up @@ -69,6 +69,7 @@ const schemaOptions: ConduitSchemaOptions = {
indexes: [
{
fields: ['subject', 'entity'],
types: [CompatibleIndexType.Ascending, CompatibleIndexType.Ascending],
},
],
conduit: {
Expand Down
23 changes: 19 additions & 4 deletions modules/authorization/src/models/ObjectIndex.schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
CompatibleIndexType,
ConduitModel,
ConduitSchemaOptions,
DatabaseProvider,
MongoIndexType,
TYPE,
} from '@conduitplatform/grpc-sdk';
import { ConduitActiveSchema } from '@conduitplatform/module-tools';
Expand All @@ -23,7 +23,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
subjectId: {
Expand All @@ -47,7 +47,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
entityId: {
Expand Down Expand Up @@ -75,7 +75,7 @@ const schema: ConduitModel = {
type: [TYPE.String],
default: [],
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
createdAt: TYPE.Date,
Expand All @@ -86,12 +86,27 @@ const schemaOptions: ConduitSchemaOptions = {
indexes: [
{
fields: ['subjectType', 'subjectPermission', 'entity'],
types: [
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
],
},
{
fields: ['entity', 'subjectType', 'subjectPermission'],
types: [
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
],
},
{
fields: ['entityType', 'entityId', 'entityPermission'],
types: [
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
],
},
],
conduit: {
Expand Down
8 changes: 4 additions & 4 deletions modules/authorization/src/models/Permission.schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
CompatibleIndexType,
ConduitModel,
DatabaseProvider,
MongoIndexType,
TYPE,
} from '@conduitplatform/grpc-sdk';
import { ConduitActiveSchema } from '@conduitplatform/module-tools';
Expand All @@ -18,7 +18,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
resourceId: {
Expand All @@ -37,7 +37,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
subjectId: {
Expand All @@ -61,7 +61,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
options: {
unique: true,
},
Expand Down
8 changes: 4 additions & 4 deletions modules/authorization/src/models/Relationship.schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
CompatibleIndexType,
ConduitModel,
DatabaseProvider,
MongoIndexType,
TYPE,
} from '@conduitplatform/grpc-sdk';
import { ConduitActiveSchema } from '@conduitplatform/module-tools';
Expand All @@ -17,7 +17,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
resourceId: {
Expand All @@ -36,7 +36,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
},
},
// user:1adasdas
Expand All @@ -61,7 +61,7 @@ const schema: ConduitModel = {
type: TYPE.String,
required: true,
index: {
type: MongoIndexType.Ascending,
type: CompatibleIndexType.Ascending,
options: {
unique: true,
},
Expand Down
9 changes: 8 additions & 1 deletion modules/chat/src/models/ChatRoom.schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CompatibleIndexType,
ConduitModel,
ConduitSchemaOptions,
DatabaseProvider,
Expand Down Expand Up @@ -40,7 +41,13 @@ const schema: ConduitModel = {
};
const modelOptions: ConduitSchemaOptions = {
timestamps: true,
indexes: [{ fields: ['participants'] }, { fields: ['participants', 'deleted'] }],
indexes: [
{ fields: ['participants'], types: [CompatibleIndexType.Ascending] },
{
fields: ['participants', 'deleted'],
types: [CompatibleIndexType.Ascending, CompatibleIndexType.Ascending],
},
],
conduit: {
permissions: {
extendable: true,
Expand Down
17 changes: 14 additions & 3 deletions modules/chat/src/models/Message.schema.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
CompatibleIndexType,
ConduitModel,
ConduitSchemaOptions,
DatabaseProvider,
TYPE,
ConduitSchemaOptions,
} from '@conduitplatform/grpc-sdk';
import { ConduitActiveSchema } from '@conduitplatform/module-tools';
import { ChatRoom } from './ChatRoom.schema.js';
Expand Down Expand Up @@ -53,8 +54,18 @@ const schema: ConduitModel = {
const modelOptions: ConduitSchemaOptions = {
timestamps: true,
indexes: [
{ fields: ['room', 'createdAt'] },
{ fields: ['room', 'deleted', 'createdAt'] },
{
fields: ['room', 'createdAt'],
types: [CompatibleIndexType.Ascending, CompatibleIndexType.Ascending],
},
{
fields: ['room', 'deleted', 'createdAt'],
types: [
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
CompatibleIndexType.Ascending,
],
},
],
conduit: {
permissions: {
Expand Down
29 changes: 15 additions & 14 deletions modules/database/src/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,26 +167,27 @@ export default class DatabaseModule extends ManagedModule<Config> {
const isReplica = this.grpcSdk.isAvailable('database');
await this._activeAdapter.registerSystemSchema(models.DeclaredSchema, isReplica);
await this._activeAdapter.registerSystemSchema(models.MigratedSchemas, isReplica);
let modelPromises = DATABASE_SYSTEM_SCHEMAS.filter(
model =>
model.name !== models.DeclaredSchema.name &&
model.name !== models.MigratedSchemas.name,
).map(model => this._activeAdapter.registerSystemSchema(model, isReplica));
await Promise.all(modelPromises);
for (const model of DATABASE_SYSTEM_SCHEMAS) {
if (
model.name === models.DeclaredSchema.name ||
model.name === models.MigratedSchemas.name
) {
continue;
}
await this._activeAdapter.registerSystemSchema(model, isReplica);
}
await this._activeAdapter.retrieveForeignSchemas();
await this._activeAdapter.recoverSchemasFromDatabase();
await this._activeAdapter.recoverViewsFromDatabase();
if (!isReplica) {
await runMigrations(this._activeAdapter);
}
modelPromises = DATABASE_SYSTEM_SCHEMAS.map(model => {
return this._activeAdapter.registerSystemSchema(model, isReplica).then(() => {
if (this._activeAdapter.getDatabaseType() !== 'MongoDB' && !isReplica) {
return this._activeAdapter.syncSchema(model.name);
}
});
});
await Promise.all(modelPromises);
for (const model of DATABASE_SYSTEM_SCHEMAS) {
await this._activeAdapter.registerSystemSchema(model, isReplica);
if (this._activeAdapter.getDatabaseType() !== 'MongoDB' && !isReplica) {
await this._activeAdapter.syncSchema(model.name);
}
}
this.updateHealth(HealthCheckStatus.SERVING);
}

Expand Down
Loading
Loading