Gridsome Source Plugin to load data directly from MySQL Database, with support for cloudinary
npm install gridsome-source-mysql-cloudinaryAlternate version with image cdn support using cloudinary and jsonbin.io
Gridsome Source Plugin to load data directly from MySQL Database
* If you don't succeed through a CMS, bypass it and load the data directly from the MySQL Database
* Specify names of columns containing image urls to download and optimized them with Gridsome. Supports single and comma delimited image urls.
* Build up your mysql queries as you need to get the data you require.
* Supports sub queries and references between content types
* id field from mysql is renamed to mysqlId
* Content type references via xxx_id(single id) and xxx_ids(comma delimited ids)
* Cloudinary images also have placeholders which will be generated and saved to jsonstore for future builds.
View the changelog for any possible changes from previous versions.
2.7.0 BREAKING CHANGE: Switched from jsonstore to jsonbin
2.6.0 Added optimsed for cloudinary g-image
npm install gridsome-source-mysql-cloudinary --save
> Make sure your mysql database is accessible everywhere you are planning to build your site from.
Create a FREE account on cloudinary and then enable auto uploading by doing the following:
1. Navigate to settings > upload
2. Then under Auto upload mapping in the Folder field add the name of the directory where all your images are. They may be in subdirectories as well. eg. media
3. Add the the full url that includes the folder name in the URL prefix field eg. https://example.no/media/
4. In the config (see below) update the name, folder and match fields.
5. Update all your queries where you query for images to include the srcset and other required fields. (See example Usage below)
In main.js import and override Gridsome's image component with CloudinaryImage. As a drop-in-replacement we will call it g-image so your code does not need to be updated.
``
// main.js
import CloudinaryImage from 'gridsome-source-mysql-cloudinary/CloudinaryImage'
export default function(Vue) {
/ Drop-in-replacement for Gridsome's image /
Vue.component('g-image', CloudinaryImage)
}
`
Create a free account which should be enough transactions if you aren't building too often with too many images.
Update the config as specified below with you jsonbin key, binId (which you create before hand), and optional collectionId to group all your image bins.
Within plugins in the gridsome-config.js file, add the connection settings and queries for the data you need.
`javascript:title=gridsome-config.js
// gridsome-config.js
module.exports = {
plugins:
{
use: 'gridsome-source-mysql-cloudinary',
options: {
connection: {
host: 'localhost', // required
port: 3306,
user: 'root', // required
password: 'secret', // required
database: 'my_db', // required
connectionLimit : 10
},
debug: true, // Default false on production
ignoreImages: false, // Do not process any images
jsonbin: { // Create a free account on [jsonbin.io
key: process.env.JSONBIN_KEY, // Secret key from jsonbin.io
binId: process.env.JSONBIN_BIN, // The id of the bin to save data too
collectionId: process.env.JSONBIN_COLLECTION, // (optional) The id of the collection in which to save additional bins
},
cloudinary: {
name: 'example',
folder: 'media',
uri: 'c_scale,e_vectorize,w_50', // Scaling for svg placeholder
sizes: ['480', '800'],
match: /https?:\/\/(www\.)?example\.no\/media\// // Url to match for images and swap out for the cloudinary url
},
regex: /()_\d(.(jpg|png|svg|jpeg))/i, // Default false
queries: [ // required
{
name: 'Author',
route: '/authors/:path',
path: 'fullname',
sql: SELECT id, fullname, avatar, url FROM author,SELECT id, title, image, gallery, author as author_id, excerpt, body, created FROM post WHERE published = ?
images: ['avatar'] // Default []
},
{
name: 'Post',
path: 'title',
sql: ,`
args: [1],
images: ['image', ['gallery']] //Gallery contains comma delimited string of image url.
}
]
}
}
]
}
Relationship ids should be in the format of xxx_id where xxx is the name of another query.
On the above example two content types will be created Post and Author with author_id being a relation:
``
query {
allPost {
edges {
node {
title
path
image {
src
dataUri
srcset
size {
width
height
}
}
gallery {
index
image {
src
dataUri
srcset
size {
width
height
}
}
}
excerpt
author {
fullname
url
image {
src
dataUri
srcset
size {
width
height
}
}
}
}
}
}
}
regex: Specify false to not use or a regex expression that has 2 capture groups. This can be used to remove duplicate files for example the value /()_\d(.(jpg|png|svg|jpeg))/i renames all files that end with _\d eg. _1, _2; since we assume them to be duplicate files. We DO NOT change the source url since it might be the original file (not ending with _\d) isn't used.
Field | Type | Info
---|---|---
name | string | Name of the resulting content type
route? | string | Specify a dynamic route structure eg. /blog/:pathfield
path | function(slugify, row, parentRow?): string | Return the path for the given row
path | { prefix?: string, field: string, suffix?: string } | should exist on each row and will be slugified?
path | string | Name of a field on each row to slugify and use as path
sql | string | A SQL Query with optional placeholders which will be replaced by args in ordernull
args? | array
args? | function(parentRow?): array
json | array
images? | array
subs | array
The following is an example of how you can generated the fields for using as a one-to-many relationship in graphql and also joining image urls.
`SELECT
queries: [
{
name: 'Product',
path: 'slug',
images: ['image', ['gallery']] // Default []
sql:
pc.product_id as id,
cats.category_ids,
pc.sku,
pc.name,
pc.price,
pc.slug,
CONCAT('https://example.com/media/', pc.image) as image,
media.images as 'gallery',
FROM product_catalog pc
INNER JOIN (
SELECT product_id, GROUP_CONCAT(CONCAT('https://example.com/media/catalog/product',value)) AS 'images'
FROM product_media
GROUP BY product_id
) media
ON media.product_id = pc.product_id
INNER JOIN (
SELECT product_id, GROUP_CONCAT(category_id) AS 'category_ids'
FROM product_category
GROUP BY product_id
) cats
ON cats.product_id = pc.product_id
WHERE pc.status = 1SELECT
},
{
name: 'Category',
route: '/category/:path',
path: 'path',
images: ['image'],
sql:
category_id AS 'id',
name,
CONCAT('https://example.com/media/', image) as image,
parent_id AS 'category_id',
position,
level,
product_count AS 'count'
FROM category_catelog
WHERE active = 1`
}
]
In the above example cats.category_ids will result in an array of Category content types if you have specified a query for Category
Images in this database were relative, so in order for them to be downloaded they need to be concatenated with the site origin.
In the Category query, we change the parent_id to output as category_id since we want it to be linked to another Category automatically.
This drop-in version will filter through the appropriate sizes available, eg. sizes: ['480', '800', '1200'], on each image, such that even though larger images exist it will only use the image that is >= width, width being speicified on the tag.
Example: here the image with the generated size of 800 will be used, and not 1200`.