Skip to content

Commit

Permalink
fix: synchronized collection creation
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Aug 29, 2024
1 parent 83d50b4 commit 9ba6f12
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/api/models/configTenant.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ObjectId } from 'mongodb';
import omit from 'lodash/omit';
import { castIdsFactory } from './utils';
import { castIdsFactory, getCreatedCollection } from './utils';

export default async (db) => {
const collection = db.collection('configTenant');
const collection = await getCreatedCollection(db, 'configTenant');

collection.findOneById = async (id) =>
collection.findOne({ $or: [{ _id: new ObjectId(id) }, { _id: id }] });
Expand Down
5 changes: 3 additions & 2 deletions src/api/models/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { ObjectId } from 'mongodb';
import { URI_FIELD_NAME, moveUriToFirstPosition } from '../../common/uris';
import countNotUnique from './countNotUnique';
import countNotUniqueSubresources from './countNotUniqueSubresources';
import { getCreatedCollection } from './utils';

export default (db) => {
const collection = db.collection('dataset');
export default async (db) => {
const collection = await getCreatedCollection(db, 'dataset');
collection.insertBatch = (documents) => {
return Promise.all(
chunk(documents, 100).map((data) => {
Expand Down
4 changes: 2 additions & 2 deletions src/api/models/enrichment.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ObjectId } from 'mongodb';
import omit from 'lodash/omit';
import { castIdsFactory } from './utils';
import { castIdsFactory, getCreatedCollection } from './utils';

export default async (db) => {
const collection = db.collection('enrichment');
const collection = await getCreatedCollection(db, 'enrichment');
await collection.createIndex({ name: 1 }, { unique: true });

collection.findOneById = async (id) =>
Expand Down
5 changes: 2 additions & 3 deletions src/api/models/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { validateField as validateFieldIsomorphic } from '../../common/validateF
import { URI_FIELD_NAME } from '../../common/uris';
import { SCOPE_DOCUMENT, SCOPE_COLLECTION } from '../../common/scope';
import generateUid from '../services/generateUid';
import { castIdsFactory } from './utils';
import { castIdsFactory, getCreatedCollection } from './utils';

export const buildInvalidPropertiesMessage = (name) =>
`Invalid data for field ${name} which need a name, a label, a scope, a valid scheme if specified and a transformers array`;
Expand Down Expand Up @@ -96,8 +96,7 @@ const createSubresourceUriField = (subresource) => ({
});

export default async (db) => {
const collection = db.collection('field');

const collection = await getCreatedCollection(db, 'field');
await collection.createIndex({ name: 1 }, { unique: true });

collection.findAll = async () =>
Expand Down
4 changes: 2 additions & 2 deletions src/api/models/hiddenResource.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ObjectId } from 'mongodb';
import { castIdsFactory } from './utils';
import { castIdsFactory, getCreatedCollection } from './utils';

export default async (db) => {
const collection = db.collection('hiddenResource');
const collection = await getCreatedCollection(db, 'hiddenResource');
await collection.createIndex({ uri: 1 }, { unique: true });

collection.findAll = async () => collection.find({}).toArray();
Expand Down
4 changes: 2 additions & 2 deletions src/api/models/precomputed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectId } from 'mongodb';
import omit from 'lodash/omit';
import { castIdsFactory } from './utils';
import { castIdsFactory, getCreatedCollection } from './utils';

const checkMissingFields = (data) =>
!data.name ||
Expand All @@ -9,7 +9,7 @@ const checkMissingFields = (data) =>
(data.sourceColumns instanceof Array && data.sourceColumns.length === 0);

export default async (db) => {
const collection = db.collection('precomputed');
const collection = await getCreatedCollection(db, 'precomputed');
await collection.createIndex({ name: 1 }, { unique: true });

collection.findOneById = async (id) =>
Expand Down
9 changes: 7 additions & 2 deletions src/api/models/publishedCharacteristic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export default (db) => {
const collection = db.collection('publishedCharacteristic');
import { getCreatedCollection } from './utils';

export default async (db) => {
const collection = await getCreatedCollection(
db,
'publishedCharacteristic',
);

collection.findLastVersion = async () => {
const items = await collection
Expand Down
3 changes: 2 additions & 1 deletion src/api/models/publishedDataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import omit from 'lodash/omit';
import { getFullResourceUri } from '../../common/uris';
import getPublishedDatasetFilter from './getPublishedDatasetFilter';
import { VALIDATED, PROPOSED } from '../../common/propositionStatus';
import { getCreatedCollection } from './utils';

const getMeta = (match, searchableFieldNames) => {
if (!match || !searchableFieldNames || !searchableFieldNames.length) {
Expand Down Expand Up @@ -35,7 +36,7 @@ const getSort = (sortBy, sortDir, match, searchableFieldNames) => {
};

export default async (db) => {
const collection = db.collection('publishedDataset');
const collection = await getCreatedCollection(db, 'publishedDataset');

await collection.createIndex({ uri: 1 }, { unique: true });

Expand Down
5 changes: 3 additions & 2 deletions src/api/models/publishedFacet.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import chunk from 'lodash/chunk';
import { getCreatedCollection } from './utils';

export default (db) => {
const collection = db.collection('publishedFacet');
export default async (db) => {
const collection = await getCreatedCollection(db, 'publishedFacet');

collection.insertBatch = (documents) =>
chunk(documents, 100).map((data) => collection.insertMany(data));
Expand Down
4 changes: 2 additions & 2 deletions src/api/models/subresource.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ObjectId } from 'mongodb';
import omit from 'lodash/omit';
import { castIdsFactory } from './utils';
import { castIdsFactory, getCreatedCollection } from './utils';

export default async (db) => {
const collection = db.collection('subresource');
const collection = await getCreatedCollection(db, 'subresource');

collection.findOneById = async (id) =>
collection.findOne({ _id: new ObjectId(id) });
Expand Down
4 changes: 2 additions & 2 deletions src/api/models/tenant.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ObjectId } from 'mongodb';
import omit from 'lodash/omit';
import { castIdsFactory } from './utils';
import { castIdsFactory, getCreatedCollection } from './utils';

export default async (db) => {
const collection = db.collection('tenant');
const collection = await getCreatedCollection(db, 'tenant');

collection.findOneById = async (id) =>
collection.findOne({ $or: [{ _id: new ObjectId(id) }, { _id: id }] });
Expand Down
10 changes: 10 additions & 0 deletions src/api/models/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ export const castIdsFactory = (collection) => async () => {
Promise.resolve(),
);
};

export const getCreatedCollection = async (db, collectionName) => {
const checkIfExists = await db
.listCollections({ name: collectionName }, { nameOnly: true })
.toArray();
if (checkIfExists.length === 0) {
await db.createCollection(collectionName);
}
return db.collection(collectionName);
};
2 changes: 1 addition & 1 deletion src/api/services/repositoryMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import mongoClient from './mongoClient';
import dataset from '../models/dataset';
import field from '../models/field';
import publishedCharacteristic from '../models/publishedCharacteristic';
Expand All @@ -6,7 +7,6 @@ import publishedFacet from '../models/publishedFacet';
import subresource from '../models/subresource';
import enrichment from '../models/enrichment';
import precomputed from '../models/precomputed';
import mongoClient from './mongoClient';
import tenant from '../models/tenant';
import configTenant from '../models/configTenant';
import hiddenResource from '../models/hiddenResource';
Expand Down

0 comments on commit 9ba6f12

Please sign in to comment.