Skip to content

Instantly share code, notes, and snippets.

View tonyspiro's full-sized avatar
🚀
Building and shipping

Tony Spiro tonyspiro

🚀
Building and shipping
View GitHub Profile
@tonyspiro
tonyspiro / connect-to-bucket.js
Created August 24, 2022 14:23
Connect to your Cosmic Bucket
const bucket = api.bucket({
slug: "YOUR_BUCKET_SLUG",
read_key: "YOUR_BUCKET_READ_KEY",
});
@tonyspiro
tonyspiro / import-npm-module.js
Created August 24, 2022 14:21
Import the Cosmic NPM module
const Cosmic = require("cosmicjs");
const api = Cosmic();
@tonyspiro
tonyspiro / chain-method-objects-find.js
Last active August 24, 2022 14:26
bucket.objects.find example
// bucket.objects.find
const data = await bucket.objects
.find({
type: "products", // Object Type slug
})
.props("title,slug,metadata") // response properties
.limit(10); // number of Objects to be returned
// bucket.objects.insertOne
const data = await bucket.objects
@tonyspiro
tonyspiro / advanced-query-ne.js
Last active June 7, 2022 06:31
Get Objects from Cosmic by metadata value (not equal to)
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
@tonyspiro
tonyspiro / advanced-query-lte.js
Last active June 7, 2022 06:30
Get Objects from Cosmic by number metadata (less than or equal to)
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
@tonyspiro
tonyspiro / advanced-query-metadata.js
Last active June 7, 2022 06:30
Get Objects from Cosmic by metadata value
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'docs-ecommerce-app-production', // YOUR_BUCKET_SLUG
read_key: '49TG2OEZMVML9fNvVObbFOEsjkFaCYpbVIP1kb8kYCY6RMWqBf' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'products',
@tonyspiro
tonyspiro / basic-query-single.js
Last active June 7, 2022 06:32
Get Objects from Cosmic by slug
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'simple-react-blog', // YOUR_BUCKET_SLUG
read_key: 'bucket-read-key' // YOUR_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
slug: 'a-wonderful-blog-post-about-earth'
@tonyspiro
tonyspiro / basic-query.js
Last active June 7, 2022 06:32
Get Objects from Cosmic by type
// index.js
const api = require('cosmicjs')() // empty init
const bucket = api.bucket({
slug: 'simple-react-blog', // YOUR_BUCKET_SLUG
read_key: 'bucket-read-key' // YOUR_BUCKET_READ_KEY
})
const app = async () => {
const data = await bucket.getObjects({
query: {
type: 'posts'
{
"created_at": 1632927978821,
"data": {
"_id": "61547f5e16bbc1ecc42e2bf6",
"order": 0,
"slug": "new-blog-post",
"title": "New Blog Post UPDATE",
"content": "<p>This is my blog post content...</p>",
"metafields": [
{
{
"created_at": 1632927654815,
"data": {
"slug": "new-blog-post",
"title": "New Blog Post UPDATE",
"metadata": {
"headline": "Amazing Beach!",
"image": {
"url": "https://cdn.cosmicjs.com/cc633600-2135-11ec-81a5-2b4858c629b3-quino-al-mBQIfKlvowM-unsplash.jpg",
"imgix_url": "https://imgix.cosmicjs.com/cc633600-2135-11ec-81a5-2b4858c629b3-quino-al-mBQIfKlvowM-unsplash.jpg"