Skip to content

Instantly share code, notes, and snippets.

@tonyspiro
Last active February 14, 2018 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyspiro/0954ee17ab2632fc0a6f66d7461784b9 to your computer and use it in GitHub Desktop.
Save tonyspiro/0954ee17ab2632fc0a6f66d7461784b9 to your computer and use it in GitHub Desktop.
/*
Cosmic JS Bucket lifecycle methods
Go to https://cosmicjs.com to create an account and get your Auth token at
Your Account Settings > Authentication
*/
const run = async () => {
const api = require('cosmicjs')({
token: 'your-auth-token' // access token found in Your Account Settings > Authentication
})
// Add Bucket
const add_bucket = await api.addBucket({
title: 'A Bucket\'s Life',
slug: 'a-buckets-life' // Make sure this is unique!
}).then(data => {
return data.bucket
}).catch(err => {
console.log(err)
})
if (!add_bucket)
return console.log('error adding bucket')
console.log('add_bucket', add_bucket)
// Connect to Bucket
const bucket = api.bucket({
slug: add_bucket.slug
})
// Add Object Type
const people = await bucket.addObjectType({
title: 'People',
slug: 'people',
singular: 'Person',
metafields: [
{
type: 'text',
title: 'Role',
value: ''
},
{
type: 'text',
title: 'Age',
value: ''
}
]
}).then(data => {
return data
}).catch(err => {
console.log(err)
})
console.log('people', people)
// Add John
const john = await bucket.addObject({
title: 'John Bucket',
type_slug: 'people',
metafields: [
{
type: 'text',
title: 'Role',
value: 'Husband / Astronaut'
},
{
type: 'text',
title: 'Age',
value: '38'
}
]
}).then(data => {
return data.object
}).catch(err => {
console.log(err)
})
console.log('john', john)
// Add Jane
const jane = await bucket.addObject({
title: 'Jane Bucket',
type_slug: 'people',
metafields: [
{
type: 'text',
title: 'Role',
value: 'Wife / Engineer'
},
{
type: 'text',
title: 'Age',
value: '36'
}
]
}).then(data => {
return data.object
}).catch(err => {
console.log(err)
})
console.log('jane', jane)
// Add Child1
const child1 = await bucket.addObject({
title: 'Jack Bucket',
type_slug: 'people',
metafields: [
{
type: 'text',
title: 'Role',
value: 'Son / Student'
},
{
type: 'text',
title: 'Age',
value: '12'
},
{
type: 'objects',
object_type: 'people',
title: 'Parents',
value: jane._id + ',' + john._id
}
]
}).then(data => {
return data.object
}).catch(err => {
console.log(err)
})
console.log('child1', child1)
// Add Child2
const child2 = await bucket.addObject({
title: 'Kate Bucket',
type_slug: 'people',
metafields: [
{
type: 'text',
title: 'Role',
value: 'Daughter / Student'
},
{
type: 'objects',
object_type: 'people',
title: 'Parents',
value: jane._id + ',' + john._id
},
{
type: 'text',
title: 'Age',
value: '9'
}
]
}).then(data => {
return data.object
}).catch(err => {
console.log(err)
})
console.log('child2', child2)
// Delete Bucket (uncomment to delete Bucket)
// const delete_bucket = await api.deleteBucket({
// id: bucket._id
// }).then(data => {
// return data
// }).catch(err => {
// console.log(err)
// })
// console.log('delete_bucket', delete_bucket)
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment