Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uuidv5 v1 #3683

Merged
merged 4 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/gatsby-source-lever/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"deep-map": "^1.5.0",
"deep-map-keys": "^1.2.0",
"json-stringify-safe": "^5.0.1",
"lodash": "^4.17.4",
"uuid": "^3.1.0"
"lodash": "^4.17.4"
},
"deprecated": false,
"description": "Gatsby source plugin for building websites using the Lever.co Recruitment Software as a data source.",
Expand Down

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion packages/gatsby-source-lever/src/__tests__/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ describe(`Process Lever data`, () => {
expect(entities).toMatchSnapshot()
})
it(`creates Gatsby IDs for each entity`, () => {
entities = normalize.createGatsbyIds(entities)
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
entities = normalize.createGatsbyIds(createNodeId, entities)
expect(entities).toMatchSnapshot()
})
it(`creates nodes for each entry`, () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-lever/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const normalize = require(`./normalize`)
const typePrefix = `lever__`

exports.sourceNodes = async (
{ boundActionCreators, getNode, store, cache },
{ boundActionCreators, getNode, store, cache, createNodeId },
{ site, verboseOutput }
) => {
const { createNode } = boundActionCreators
Expand All @@ -27,7 +27,7 @@ exports.sourceNodes = async (
entities = normalize.standardizeDates(entities)

// creates Gatsby IDs for each entity
entities = normalize.createGatsbyIds(entities)
entities = normalize.createGatsbyIds(createNodeId, entities)

// creates nodes for each entry
normalize.createNodesFromEntities({ entities, createNode })
Expand Down
6 changes: 2 additions & 4 deletions packages/gatsby-source-lever/src/normalize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const crypto = require(`crypto`)
const deepMapKeys = require(`deep-map-keys`)
const uuidv5 = require(`uuid/v5`)
const stringify = require(`json-stringify-safe`)

const conflictFieldPrefix = `lever_`
Expand Down Expand Up @@ -129,10 +128,9 @@ exports.standardizeDates = entities =>
return e
})

const seedConstant = `c2012db8-fafc-5a03-915f-e6016ff32086`
exports.createGatsbyIds = entities =>
exports.createGatsbyIds = (createNodeId, entities) =>
entities.map(e => {
e.id = uuidv5(e.lever_id.toString(), uuidv5(`lever`, seedConstant))
e.id = createNodeId(e.lever_id.toString())
return e
})

Expand Down
3 changes: 1 addition & 2 deletions packages/gatsby-source-wordpress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"gatsby-source-filesystem": "^1.5.14",
"json-stringify-safe": "^5.0.1",
"lodash": "^4.17.4",
"qs": "^6.4.0",
"uuid": "^3.1.0"
"qs": "^6.4.0"
},
"deprecated": false,
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/gatsby-source-wordpress/src/__tests__/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ describe(`Process WordPress data`, () => {
entities = normalize.excludeUnknownEntities(entities)
})
it(`creates Gatsby IDs for each entity`, () => {
entities = normalize.createGatsbyIds(entities)
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
entities = normalize.createGatsbyIds(createNodeId, entities)
expect(entities).toMatchSnapshot()
})
it(`Creates map of types`, () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-source-wordpress/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let _auth
let _perPage

exports.sourceNodes = async (
{ boundActionCreators, getNode, store, cache },
{ boundActionCreators, getNode, store, cache, createNodeId },
{
baseUrl,
protocol,
Expand Down Expand Up @@ -71,7 +71,7 @@ exports.sourceNodes = async (
entities = normalize.excludeUnknownEntities(entities)

// Creates Gatsby IDs for each entity
entities = normalize.createGatsbyIds(entities)
entities = normalize.createGatsbyIds(createNodeId, entities)

// Creates links between authors and user entities
entities = normalize.mapAuthorsToUsers(entities)
Expand Down
15 changes: 2 additions & 13 deletions packages/gatsby-source-wordpress/src/normalize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const crypto = require(`crypto`)
const deepMapKeys = require(`deep-map-keys`)
const _ = require(`lodash`)
const uuidv5 = require(`uuid/v5`)
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)

const colorized = require(`./output-color`)
Expand Down Expand Up @@ -141,19 +140,9 @@ exports.liftRenderedField = entities =>
exports.excludeUnknownEntities = entities =>
entities.filter(e => e.wordpress_id) // Excluding entities without ID

const seedConstant = `b2012db8-fafc-5a03-915f-e6016ff32086`
const typeNamespaces = {}
exports.createGatsbyIds = entities =>
exports.createGatsbyIds = (createNodeId, entities) =>
entities.map(e => {
let namespace
if (typeNamespaces[e.__type]) {
namespace = typeNamespaces[e.__type]
} else {
typeNamespaces[e.__type] = uuidv5(e.__type, seedConstant)
namespace = typeNamespaces[e.__type]
}

e.id = uuidv5(e.wordpress_id.toString(), namespace)
e.id = createNodeId(`${e.__type}-${e.wordpress_id.toString()}`)
return e
})

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"style-loader": "^0.13.0",
"type-of": "^2.0.1",
"url-loader": "^0.5.7",
"uuid": "^3.1.0",
"v8-compile-cache": "^1.1.0",
"webpack": "^1.13.3",
"webpack-configurator": "^0.3.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const mapSeries = require(`async/mapSeries`)
const reporter = require(`gatsby-cli/lib/reporter`)
const cache = require(`./cache`)
const apiList = require(`./api-node-docs`)
const createNodeId = require(`./create-node-id`)

// Bind action creators per plugin so we can auto-add
// metadata to actions they create.
Expand Down Expand Up @@ -61,6 +62,8 @@ const runAPI = (plugin, api, args) => {
pathPrefix = store.getState().config.pathPrefix
}

const namespacedCreateNodeId = id => createNodeId(id, plugin.name)

const gatsbyNode = require(`${plugin.resolve}/gatsby-node`)
if (gatsbyNode[api]) {
const apiCallArgs = [
Expand All @@ -76,6 +79,7 @@ const runAPI = (plugin, api, args) => {
reporter,
getNodeAndSavePathDependency,
cache,
createNodeId: namespacedCreateNodeId,
},
plugin.pluginOptions,
]
Expand Down
9 changes: 9 additions & 0 deletions packages/gatsby/src/utils/create-node-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const uuidv5 = require(`uuid/v5`)

const seedConstant = `638f7a53-c567-4eca-8fc1-b23efb1cfb2b`

function createNodeId(id, namespace) {
return uuidv5(id, uuidv5(namespace, seedConstant))
}

module.exports = createNodeId