Skip to content

Commit bb6c347

Browse files
committed
Update deploy collection to deploy relationship attributes
1 parent 4e4d122 commit bb6c347

File tree

1 file changed

+70
-68
lines changed

1 file changed

+70
-68
lines changed

templates/cli/lib/commands/deploy.js.twig

Lines changed: 70 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeploymen
88
const {
99
databasesGet,
1010
databasesCreate,
11+
databasesUpdate,
1112
databasesCreateBooleanAttribute,
1213
databasesGetCollection,
1314
databasesCreateCollection,
@@ -20,6 +21,7 @@ const {
2021
databasesCreateUrlAttribute,
2122
databasesCreateIpAttribute,
2223
databasesCreateEnumAttribute,
24+
databasesCreateRelationshipAttribute,
2325
databasesDeleteAttribute,
2426
databasesListAttributes,
2527
databasesListIndexes,
@@ -408,6 +410,18 @@ const createAttribute = async (databaseId, collectionId, attribute) => {
408410
array: attribute.array,
409411
parseOutput: false
410412
})
413+
case 'relationship':
414+
return databasesCreateRelationshipAttribute({
415+
databaseId,
416+
collectionId,
417+
relatedCollectionId: attribute.relatedCollection,
418+
type: attribute.relationType,
419+
twoWay: attribute.twoWay,
420+
key: attribute.key,
421+
twoWayKey: attribute.twoWayKey,
422+
onDelete: attribute.onDelete,
423+
parseOutput: false
424+
})
411425
}
412426
}
413427

@@ -483,7 +497,7 @@ const deployCollection = async ({ all, yes } = {}) => {
483497
}
484498
}
485499

486-
log(`Updating attributes ... `);
500+
log(`Deleting indexes and attributes ... `);
487501

488502
// TODO: Pagination?
489503
const { indexes: remoteIndexes } = await databasesListIndexes({
@@ -529,39 +543,6 @@ const deployCollection = async ({ all, yes } = {}) => {
529543
throw new Error("Attribute deletion did not finish for too long.");
530544
}
531545

532-
await Promise.all(collection.attributes.map(async attribute => {
533-
await createAttribute(databaseId, collection['$id'], attribute);
534-
}));
535-
536-
const attributeKeys = collection.attributes.map(attribute => attribute.key);
537-
const createPoolStatus = await awaitPools.expectAttributes(databaseId, collection['$id'], attributeKeys);
538-
if (!createPoolStatus) {
539-
throw new Error("Attribute creation did not finish for too long.");
540-
}
541-
542-
success(`Created ${collection.attributes.length} attributes`);
543-
544-
log(`Creating indexes ...`)
545-
await Promise.all(collection.indexes.map(async index => {
546-
await databasesCreateIndex({
547-
databaseId,
548-
collectionId: collection['$id'],
549-
key: index.key,
550-
type: index.type,
551-
attributes: index.attributes,
552-
orders: index.orders,
553-
parseOutput: false
554-
});
555-
}));
556-
557-
const indexKeys = collection.indexes.map(attribute => attribute.key);
558-
const indexPoolStatus = await awaitPools.expectIndexes(databaseId, collection['$id'], indexKeys);
559-
if (!indexPoolStatus) {
560-
throw new Error("Index creation did not finish for too long.");
561-
}
562-
563-
success(`Created ${collection.indexes.length} indexes`);
564-
565546
await databasesUpdateCollection({
566547
databaseId,
567548
collectionId: collection['$id'],
@@ -571,8 +552,6 @@ const deployCollection = async ({ all, yes } = {}) => {
571552
enabled: collection.enabled,
572553
parseOutput: false
573554
})
574-
575-
success(`Deployed ${collection.name} ( ${collection['$id']} )`);
576555
} catch (e) {
577556
if (e.code == 404) {
578557
log(`Collection ${collection.name} does not exist in the project. Creating ... `);
@@ -585,45 +564,68 @@ const deployCollection = async ({ all, yes } = {}) => {
585564
parseOutput: false
586565
})
587566

588-
log(`Creating attributes ... `);
589-
await Promise.all(collection.attributes.map(async attribute => {
590-
await createAttribute(databaseId, collection['$id'], attribute);
591-
}));
567+
} else {
568+
throw e;
569+
}
570+
}
592571

593-
const attributeKeys = collection.attributes.map(attribute => attribute.key);
594-
const attributePoolStatus = await awaitPools.expectAttributes(databaseId, collection['$id'], attributeKeys);
595-
if (!attributePoolStatus) {
596-
throw new Error("Attribute creation did not finish for too long.");
597-
}
572+
// Create all non-relationship attributes first
573+
const nonRelationshipAttributes = collection.attributes.filter(attribute => attribute.type !== 'relationship');
574+
await Promise.all(nonRelationshipAttributes.map(attribute => {
575+
return createAttribute(databaseId, collection['$id'], attribute);
576+
}));
598577

599-
success(`Created ${collection.attributes.length} attributes`);
578+
const nonRelationshipAttributeKeys = nonRelationshipAttributes.map(attribute => attribute.key);
579+
const createPoolStatus = await awaitPools.expectAttributes(databaseId, collection['$id'], nonRelationshipAttributeKeys);
580+
if (!createPoolStatus) {
581+
throw new Error("Attribute creation did not finish for too long.");
582+
}
600583

601-
log(`Creating indexes ...`);
602-
await Promise.all(collection.indexes.map(async index => {
603-
await databasesCreateIndex({
604-
databaseId,
605-
collectionId: collection['$id'],
606-
key: index.key,
607-
type: index.type,
608-
attributes: index.attributes,
609-
orders: index.orders,
610-
parseOutput: false
611-
});
612-
}));
584+
success(`Created ${nonRelationshipAttributeKeys.length} non-relationship attributes`);
613585

614-
const indexKeys = collection.indexes.map(attribute => attribute.key);
615-
const indexPoolStatus = await awaitPools.expectIndexes(databaseId, collection['$id'], indexKeys);
616-
if (!indexPoolStatus) {
617-
throw new Error("Index creation did not finish for too long.");
618-
}
586+
log(`Creating indexes ...`)
587+
await Promise.all(collection.indexes.map(async index => {
588+
await databasesCreateIndex({
589+
databaseId,
590+
collectionId: collection['$id'],
591+
key: index.key,
592+
type: index.type,
593+
attributes: index.attributes,
594+
orders: index.orders,
595+
parseOutput: false
596+
});
597+
}));
598+
599+
const indexKeys = collection.indexes.map(attribute => attribute.key);
600+
const indexPoolStatus = await awaitPools.expectIndexes(databaseId, collection['$id'], indexKeys);
601+
if (!indexPoolStatus) {
602+
throw new Error("Index creation did not finish for too long.");
603+
}
619604

620-
success(`Created ${collection.indexes.length} indexes`);
605+
success(`Created ${collection.indexes.length} indexes`);
621606

622-
success(`Deployed ${collection.name} ( ${collection['$id']} )`);
623-
} else {
624-
throw e;
625-
}
607+
success(`Deployed ${collection.name} ( ${collection['$id']} )`);
608+
}
609+
610+
// Create the relationship attributes
611+
for (let collection of collections) {
612+
const relationshipAttributes = collection.attributes.filter(attribute => attribute.type === 'relationship' && attribute.side === 'parent');
613+
614+
if (relationshipAttributes.length === 0) continue;
615+
616+
log(`Deploying relationships for collection ${collection.name} ( ${collection['$id']} )`);
617+
618+
await Promise.all(relationshipAttributes.map(attribute => {
619+
return createAttribute(collection['databaseId'], collection['$id'], attribute);
620+
}));
621+
622+
const nonRelationshipAttributeKeys = relationshipAttributes.map(attribute => attribute.key);
623+
const createPoolStatus = await awaitPools.expectAttributes(collection['databaseId'], collection['$id'], nonRelationshipAttributeKeys);
624+
if (!createPoolStatus) {
625+
throw new Error("Attribute creation did not finish for too long.");
626626
}
627+
628+
success(`Created ${nonRelationshipAttributeKeys.length} relationship attributes`);
627629
}
628630
}
629631

0 commit comments

Comments
 (0)