Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/_example/scripts/db-seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async function createReviewRecords(connection: Connection, storeRecords: any[]):
for (let i = 0; i < 30; i += 1) {
reviewsRecords.push({
title: faker.word.adjective(),
rating: faker.datatype.number({ min: 1, max: 5 }),
message: faker.lorem.paragraphs(1),
storeId: faker.helpers.randomize(storeRecords.map(({ id }) => id)),
});
Expand Down
25 changes: 24 additions & 1 deletion packages/_example/src/datasources/mongoose/mongodb.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mongoose from 'mongoose';
import mongoose, { Schema } from 'mongoose';
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example will be partially removed after the review.


const connectionString = 'mongodb://root:password@localhost:27017';
const connection = mongoose.createConnection(connectionString);
Expand All @@ -13,10 +13,33 @@ connection.model(
message: {
type: String,
},
rating: {
type: Number,
},
storeId: {
type: Number,
required: true,
},
testArrayIds: {
type: [Number],
},
ownerIds: {
type: [mongoose.Schema.Types.ObjectId],
ref: 'ownerMongo',
},
oldOwnerIds: {
type: [mongoose.Schema.Types.ObjectId],
ref: 'ownerMongo',
},
}),
);

connection.model(
'ownerMongo',
new Schema({
name: {
type: String,
},
}),
);

Expand Down
1 change: 1 addition & 0 deletions packages/_example/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export type Schema = {
plain: {
title: string;
message: string;
rating: number;
storeId: number;
_id: string;
};
Expand Down
3 changes: 2 additions & 1 deletion packages/datasource-mongoose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"dependencies": {
"@forestadmin/datasource-toolkit": "1.0.0-beta.16",
"mongoose": "^6.3.2"
"mongoose": "^6.3.2",
"luxon": "^2.3.0"
},
"peerDependencies": {},
"scripts": {
Expand Down
230 changes: 218 additions & 12 deletions packages/datasource-mongoose/src/collection.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,245 @@
/* eslint-disable max-classes-per-file, no-underscore-dangle */
import {
AggregateResult,
Aggregation,
BaseCollection,
Caller,
ConditionTreeLeaf,
DataSource,
Filter,
ManyToManySchema,
PaginatedFilter,
Projection,
RecordData,
} from '@forestadmin/datasource-toolkit';
import { Model } from 'mongoose';
import { Model, PipelineStage, Schema, model as mongooseModel } from 'mongoose';

import PipelineGenerator from './utils/pipeline-generator';
import SchemaFieldsGenerator from './utils/schema-fields-generator';

export default class MongooseCollection extends BaseCollection {
private readonly model: Model<RecordData>;
public readonly model: Model<RecordData>;

constructor(dataSource: DataSource, model: Model<RecordData>) {
super(model.modelName, dataSource);
this.model = model;
this.addFields(SchemaFieldsGenerator.buildFieldsSchema(model));
}

async create(): Promise<RecordData[]> {
throw new Error('not implemented');
async create(caller: Caller, data: RecordData[]): Promise<RecordData[]> {
this.parseJSONToNestedFieldsInPlace(data);
const records = await this.model.insertMany(data);

const ids = records.map(record => record._id);
const conditionTree = new ConditionTreeLeaf('_id', 'In', ids);

return this.list(caller, new Filter({ conditionTree }), new Projection());
}

async list(
caller: Caller,
filter: PaginatedFilter,
projection: Projection,
): Promise<RecordData[]> {
return this.model.aggregate(PipelineGenerator.find(this, this.model, filter, projection));
}

async list(): Promise<RecordData[]> {
throw new Error('not implemented');
async update(caller: Caller, filter: Filter, patch: RecordData): Promise<void> {
const ids = await this.list(caller, filter, new Projection('_id'));
await this.model.updateMany({ _id: ids.map(record => record._id) }, patch);
}

async update(): Promise<void> {
throw new Error('not implemented');
async delete(caller: Caller, filter: Filter): Promise<void> {
const ids = await this.list(caller, filter, new Projection('_id'));
await this.model.deleteMany({ _id: ids.map(record => record._id) });
}

async delete(): Promise<void> {
throw new Error('not implemented');
async aggregate(
caller: Caller,
filter: Filter,
aggregation: Aggregation,
limit?: number,
): Promise<AggregateResult[]> {
let pipeline = PipelineGenerator.find(this, this.model, filter, aggregation.projection);
pipeline = PipelineGenerator.group(aggregation, pipeline);
if (limit) pipeline.push({ $limit: limit });

return MongooseCollection.formatRecords(await this.model.aggregate(pipeline));
}

protected static formatRecords(records: RecordData[]): AggregateResult[] {
const results: AggregateResult[] = [];

records.forEach(record => {
const group = Object.entries(record?._id || {}).reduce((computed, [field, value]) => {
computed[field] = value;

return computed;
}, {});

results.push({ value: record.value, group });
});

return results;
}

private parseJSONToNestedFieldsInPlace(data: RecordData[]) {
data.forEach(currentData => {
Object.entries(this.schema.fields).forEach(([fieldName, schema]) => {
if (schema.type === 'Column' && typeof schema.columnType === 'object') {
if (typeof currentData[fieldName] === 'string') {
currentData[fieldName] = JSON.parse(<string>currentData[fieldName]);
}
}
});
});
}
}

export class ManyToManyMongooseCollection extends MongooseCollection {
private readonly originCollection: MongooseCollection;
private readonly foreignCollection: MongooseCollection;
private readonly originFieldNameOfIds: string;

constructor(
originCollection: MongooseCollection,
foreignCollection: MongooseCollection,
originFieldNameOfIds: string,
manyToManyRelation: string,
) {
const model = ManyToManyMongooseCollection.buildModel(
originCollection,
foreignCollection,
manyToManyRelation,
);

super(originCollection.dataSource, model);
this.originCollection = originCollection;
this.foreignCollection = foreignCollection;
this.originFieldNameOfIds = originFieldNameOfIds;
}

override async create(caller: Caller, data: RecordData[]): Promise<RecordData[]> {
const records = await this.createManyToMany(data);
const ids = records.map(record => record._id);
const conditionTree = new ConditionTreeLeaf('_id', 'In', ids);

return this.list(caller, new Filter({ conditionTree }), new Projection());
}

async aggregate(): Promise<AggregateResult[]> {
throw new Error('not implemented');
override async list(
caller: Caller,
filter: PaginatedFilter,
projection: Projection,
): Promise<RecordData[]> {
const { model } = this.originCollection;

return model.aggregate(this.buildListPipeline(model, filter, projection));
}

override async update(caller: Caller, filter: Filter, patch: RecordData): Promise<void> {
return this.updateManyToMany(caller, filter, patch);
}

override async delete(caller: Caller, filter: Filter): Promise<void> {
return this.updateManyToMany(caller, filter);
}

override async aggregate(
caller: Caller,
filter: Filter,
aggregation: Aggregation,
limit?: number,
): Promise<AggregateResult[]> {
const { model } = this.originCollection;
let pipeline = this.buildListPipeline(model, filter, aggregation.projection);
pipeline = PipelineGenerator.group(aggregation, pipeline);
if (limit) pipeline.push({ $limit: limit });

return MongooseCollection.formatRecords(await model.aggregate(pipeline));
}

private async createManyToMany(data: RecordData[]): Promise<RecordData[]> {
return Promise.all(
data.map(item => {
return this.originCollection.model.updateOne(
{ _id: item[`${this.originCollection.name}_id`] },
{
$addToSet: { [this.originFieldNameOfIds]: item[`${this.foreignCollection.name}_id`] },
},
);
}),
);
}

private async updateManyToMany(
caller: Caller,
filter: Filter,
patch?: RecordData,
): Promise<void> {
const records = await this.list(
caller,
filter,
new Projection(`${this.originCollection.name}_id`, `${this.foreignCollection.name}_id`),
);

for (const record of records) {
// improve by grouping by origin id
// eslint-disable-next-line no-await-in-loop
await this.originCollection.model.updateOne(
{ _id: record[`${this.originCollection.name}_id`] },
{
$pull: { [this.originFieldNameOfIds]: record[`${this.foreignCollection.name}_id`] },
},
);

if (patch) {
// eslint-disable-next-line no-await-in-loop
await this.originCollection.model.updateOne(
{ _id: patch[`${this.originCollection.name}_id`] },
{
$addToSet: { [this.originFieldNameOfIds]: patch[`${this.foreignCollection.name}_id`] },
},
);
}
}
}

private buildListPipeline(
model: Model<RecordData>,
filter: Filter,
projection: Projection,
): PipelineStage[] {
const allFilter = new PaginatedFilter({});
const allProjection = new Projection();
let pipeline = PipelineGenerator.find(this.originCollection, model, allFilter, allProjection);
pipeline = PipelineGenerator.emulateManyToManyCollection(
model,
this.originFieldNameOfIds,
this.originCollection.name,
this.foreignCollection.name,
pipeline,
);

return PipelineGenerator.find(this, model, filter, projection, pipeline);
}

private static buildModel(
originCollection: MongooseCollection,
foreignCollection: MongooseCollection,
manyToManyRelation: string,
): Model<RecordData> {
const { foreignKey, originKey, throughCollection } = foreignCollection.schema.fields[
manyToManyRelation
] as ManyToManySchema;
const schema = new Schema(
{
[foreignKey]: { type: Schema.Types.ObjectId, ref: originCollection.name },
[originKey]: { type: Schema.Types.ObjectId, ref: foreignCollection.name },
},
{ _id: false },
);

return mongooseModel(throughCollection, schema, null, { overwriteModels: true });
}
}
35 changes: 35 additions & 0 deletions packages/datasource-mongoose/src/utils/field-name-generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export default class FieldNameGenerator {
static generateManyToOne(fieldName: string, modelName: string): string {
return `${fieldName}__${modelName.split('_').pop()}__manyToOne`;
}

static generateOneToMany(foreignCollectionName: string, foreignKey: string): string {
return `${foreignCollectionName}__${foreignKey}__oneToMany`;
}

static generateThroughFieldName(
modelName: string,
foreignCollectionName: string,
fieldName: string,
): string {
return `${modelName}__${foreignCollectionName}__${fieldName}`;
}

static getOriginFieldNameOfIds(name: string) {
return name.split('_').pop();
}

static generateManyToManyName(
foreignCollectionName: string,
originKey: string,
throughCollection: string,
): string {
return `${foreignCollectionName}__${originKey}__${this.getOriginFieldNameOfIds(
throughCollection,
)}`;
}

static generateKey(name: string): string {
return `${name}_id`;
}
}
Loading