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

Support for non-integer IDs #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 20 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = Barrels;

/**
* Barrels module
* @param {string} sourceFolder defaults to <project root>/test/fixtures
* @param {string} [sourceFolder] defaults to <project root>/test/fixtures
*/
function Barrels(sourceFolder) {
if (!(this instanceof Barrels))
Expand Down Expand Up @@ -149,22 +149,35 @@ Barrels.prototype.populate = function(collections, done, autoAssociations) {
var itemIndex = fixtureObjects.indexOf(item);

for (var alias in that.associations[modelName]) {
if (that.associations[modelName][alias].required) {
var association = that.associations[modelName][alias];
if (association.required)
{
// With required associations present, the associated fixtures
// must be already loaded, so we can map the ids
var collectionName = that.associations[modelName][alias].collection; // many-to-many
var associatedModelName = that.associations[modelName][alias].model; // one-to-many
var collectionName = association.collection; // many-to-many
var associatedModelName = association.model; // one-to-many

// NOTE that [item[alias] - 1] and [item[alias][i] - 1]
// only works for auto-increment integers (or string numbers)
// the purpose it to keep auto-incremented integers
// equal to the first created batch, so the database can be
// recreated without higher numbers being created
// (which would breaking associations).

if ((_.isArray(item[alias]))&&(collectionName)) {
if ((_.isArray(item[alias])) && (collectionName)) {
if (!that.idMap[collectionName])
return nextItem(new Error('Please provide a loading order acceptable for required associations'));
for (var i = 0; i < item[alias].length; i++) {
item[alias][i] = that.idMap[collectionName][item[alias][i] - 1];
if (_.isFinite(item[alias][i] - 1)) {
item[alias][i] = that.idMap[collectionName][item[alias][i] - 1];
}
}
} else if (associatedModelName) {
if (!that.idMap[associatedModelName])
return nextItem(new Error('Please provide a loading order acceptable for required associations'));
item[alias] = that.idMap[associatedModelName][item[alias] - 1];
if (_.isFinite(item[alias - 1])) {
item[alias] = that.idMap[associatedModelName][item[alias] - 1];
}
}
} else if (autoAssociations) {
// The order is not important, so we can strip
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
},
"main": "index.js",
"scripts": {
"test": "NODE_ENV=test mocha -R spec"
"test": "mocha test/index.test.js -R spec",
"test:string": "mocha test/string.test.js -R spec",
"test:object": "mocha test/object.test.js -R spec"
},
"repository": {
"type": "git",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions test/fixtures/object-id/Categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = [
{
id: '507f1f77bcf86cd799439011',
name: 'Clothes'
},
{
id: '507f191e810c19729de860ea',
name: "Shoes"
},
{
id: '56799fa7fdafa4f9402ebb06',
"name": 'Accessories'
}
]
12 changes: 12 additions & 0 deletions test/fixtures/object-id/customers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": "56799fa7fdafa4f9402ebb07",
"name": "Walter White",
"email": "[email protected]"
},
{
"id": "56799fa7fdafa4f9402ebb08",
"name": "John Appleseed",
"email": "[email protected]"
}
]
17 changes: 17 additions & 0 deletions test/fixtures/object-id/models/Categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Categories
*/

module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
required: true
},
products: {
collection: 'products',
via: 'category'
}
}
};
30 changes: 30 additions & 0 deletions test/fixtures/object-id/models/Products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Products
*/

module.exports = {
attributes: {
customId: {
type: 'string',
primaryKey: true
},
name: 'string',
category: {
model: 'categories'
},
tags: {
collection: 'tags',
via: 'products',
dominant: true
},
seller: {
model: 'sellers',
required: true
},
regions: {
collection: 'regions',
via: 'products',
required: true
}
}
};
20 changes: 20 additions & 0 deletions test/fixtures/object-id/models/Regions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Region
*/

module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
required: true
},
products: {
collection: 'products',
via: 'regions'
},
name: {
type: 'string'
}
}
};
17 changes: 17 additions & 0 deletions test/fixtures/object-id/models/Sellers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Sellers
*/

module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
required: true
},
name: {
type: 'string',
required: true
}
}
};
17 changes: 17 additions & 0 deletions test/fixtures/object-id/models/Tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Tags
*/

module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
required: true
},
products: {
collection: 'products',
via: 'tags'
}
}
};
46 changes: 46 additions & 0 deletions test/fixtures/object-id/products.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"customId": "56799fa7fdafa4f9402ebb17",
"title": "Leather Jacket",
"category": "507f1f77bcf86cd799439011",
"tags": [
"56799fa7fdafa4f9402ebb13",
"56799fa7fdafa4f9402ebb14",
"56799fa7fdafa4f9402ebb15"
],
"seller": "56799fa7fdafa4f9402ebb12",
"regions": [
"56799fa7fdafa4f9402ebb09",
"56799fa7fdafa4f9402ebb10"
]
},
{
"customId": "56799fa7fdafa4f9402ebb18",
"title": "Driving Shoes",
"category": "507f191e810c19729de860ea",
"tags": [
"56799fa7fdafa4f9402ebb14",
"56799fa7fdafa4f9402ebb15"
],
"seller": "56799fa7fdafa4f9402ebb12",
"regions": [
"56799fa7fdafa4f9402ebb10",
"56799fa7fdafa4f9402ebb11"
]
},
{
"customId": "56799fa7fdafa4f9402ebb19",
"title": "Aviator Sunglasses",
"category": "56799fa7fdafa4f9402ebb06",
"tags": [
"56799fa7fdafa4f9402ebb15",
"56799fa7fdafa4f9402ebb16"
],
"seller": "56799fa7fdafa4f9402ebb12",
"regions": [
"56799fa7fdafa4f9402ebb09",
"56799fa7fdafa4f9402ebb10",
"56799fa7fdafa4f9402ebb11"
]
}
]
14 changes: 14 additions & 0 deletions test/fixtures/object-id/regions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"id": "56799fa7fdafa4f9402ebb09",
"name": "midwest"
},
{
"id": "56799fa7fdafa4f9402ebb10",
"name": "southwest"
},
{
"id": "56799fa7fdafa4f9402ebb11",
"name": "pacific"
}
]
6 changes: 6 additions & 0 deletions test/fixtures/object-id/sellers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"id": "56799fa7fdafa4f9402ebb12",
"name": "Clark Kent"
}
]
18 changes: 18 additions & 0 deletions test/fixtures/object-id/tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"id": "56799fa7fdafa4f9402ebb13",
"name": "black"
},
{
"id": "56799fa7fdafa4f9402ebb14",
"name": "casual"
},
{
"id": "56799fa7fdafa4f9402ebb15",
"name": "leather"
},
{
"id": "56799fa7fdafa4f9402ebb16",
"name": "summer"
}
]
14 changes: 14 additions & 0 deletions test/fixtures/string-id/Categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = [
{
id: '1',
name: 'Clothes'
},
{
id: '2',
name: "Shoes"
},
{
id: '3',
"name": 'Accessories'
}
]
12 changes: 12 additions & 0 deletions test/fixtures/string-id/customers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": "1",
"name": "Walter White",
"email": "[email protected]"
},
{
"id": "2",
"name": "John Appleseed",
"email": "[email protected]"
}
]
17 changes: 17 additions & 0 deletions test/fixtures/string-id/models/Categories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Categories
*/

module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
required: true
},
products: {
collection: 'products',
via: 'category'
}
}
};
30 changes: 30 additions & 0 deletions test/fixtures/string-id/models/Products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Products
*/

module.exports = {
attributes: {
customId: {
type: 'string',
primaryKey: true
},
name: 'string',
category: {
model: 'categories'
},
tags: {
collection: 'tags',
via: 'products',
dominant: true
},
seller: {
model: 'sellers',
required: true
},
regions: {
collection: 'regions',
via: 'products',
required: true
}
}
};
20 changes: 20 additions & 0 deletions test/fixtures/string-id/models/Regions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Region
*/

module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
required: true
},
products: {
collection: 'products',
via: 'regions'
},
name: {
type: 'string'
}
}
};
17 changes: 17 additions & 0 deletions test/fixtures/string-id/models/Sellers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Sellers
*/

module.exports = {
attributes: {
id: {
type: 'string',
primaryKey: true,
required: true
},
name: {
type: 'string',
required: true
}
}
};
Loading