### Installing sequelize-fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This command installs the `sequelize-fixtures` library using npm, making it available for use in your project's dependencies. ```Bash npm install sequelize-fixtures ``` -------------------------------- ### Loading Fixtures with Custom Logging in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This example shows how to provide a custom logging function to `sequelize-fixtures`, allowing you to control how progress and information messages are displayed during fixture loading. ```JavaScript function myLogging(defaultLog) { console.log('Fixtures: processing ...') } sequelize_fixtures.loadFile('fixtures/*.json', models, { log: myLogging}).then(function(){ doStuffAfterLoad(); }); ``` -------------------------------- ### Loading Fixtures from an Array of Files in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This example illustrates loading fixtures from a specified array of file paths, which can include both specific files and glob patterns, into Sequelize models. ```JavaScript sequelize_fixtures.loadFiles(['fixtures/users.json', 'fixtures/data*.json'], models).then(function(){ doStuffAfterLoad(); }); ``` -------------------------------- ### Example YAML Fixture File Format Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This YAML snippet shows the structure for fixture data files in YAML format. It defines a `fixtures` key containing a list of objects, each with a `model` and `data` section. ```YAML fixtures: - model: Foo data: propA: bar propB: 1 - model: Foo data: propA: baz propB: 3 ``` -------------------------------- ### Example JSON Fixture File Format Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON snippet illustrates the expected structure for fixture data files. It's an array of objects, each containing a `model` name and a `data` object with properties for the model. ```JSON [ { "model": "Foo", "data": { "propA": "bar", "propB": 1 } }, { "model": "Foo", "data": { "propA": "baz", "propB": 3 } } ] ``` -------------------------------- ### Example JavaScript Fixture File Format Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JavaScript snippet demonstrates how to define fixture data directly within a JavaScript file, exporting an array of fixture objects. This allows for dynamic data generation if needed. ```JavaScript module.exports = [ { "model": "Foo", "data": { "propA": "bar", "propB": 1 } }, { "model": "Foo", "data": { "propA": "baz", "propB": 3 } } ]; ``` -------------------------------- ### Loading Fixtures within a Sequelize Transaction in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This example demonstrates how to load fixtures as part of a Sequelize transaction, ensuring atomicity where all fixture insertions succeed or none are committed. ```JavaScript sequelize.transaction(function(tx) { sequelize_fixtures.loadFile('fixtures/*.json', models, { transaction: tx}).then(doStuffAfterLoad); }); ``` -------------------------------- ### Loading Fixtures from a Single JSON File in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This example demonstrates how to load fixtures from a single JSON file (`fixtures/test_data.json`) into Sequelize models. It requires a `models` object mapping model names to Sequelize model instances and returns a Bluebird promise. ```JavaScript const sequelize_fixtures = require('sequelize-fixtures'); //a map of [model name] : model //see offical example on how to load models //https://github.com/sequelize/express-example/blob/master/models/index.js const models = require('./models'); //from file sequelize_fixtures.loadFile('fixtures/test_data.json', models).then(function(){ doStuffAfterLoad(); }); ``` -------------------------------- ### Modifying Fixture Data Before Insertion in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This example uses `modifyFixtureDataFn` to alter fixture data just before it's saved. It ensures that a `createdAt` timestamp is present, setting it to the current date if missing. ```JavaScript sequelize_fixtures.loadFile('fixtures/*.json', models, { modifyFixtureDataFn: function (data) { if(!data.createdAt) { data.createdAt = new Date(); } return data; } }).then(function() { doStuffAfterLoad(); }); ``` -------------------------------- ### Mapping One-to-Many Associations by ID in JSON Fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON fixture example shows how to associate a `Car` with an `Owner` in a one-to-many relationship by referencing the `Owner`'s ID. The `owner` property in the `Car` data directly uses the ID of the pre-defined `Owner`. ```json [ { "model": "Owner", "data": { "id": 11, "name": "John Doe", "city": "Vilnius" } }, { "model": "Car", "data": { "id": 203, "make": "Ford", "owner": 11 } } ] ``` -------------------------------- ### Running Tests for sequelize-fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This command executes the test suite for the `sequelize-fixtures` library, ensuring its functionality is working as expected. ```Bash npm test ``` -------------------------------- ### Loading Fixtures with Detailed Logger Configuration in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This snippet illustrates configuring a detailed logger object with separate functions for debug, info, warn, and error streams, useful for integrating with Winston-compatible loggers. ```JavaScript function errorReporter(message) { console.error('OH NO! ERROR: ' + message); } sequelize_fixtures.loadFile('fixtures/*.json', models, { logger: { debug: console.log, info: console.log, warn: console.log, error: errorReporter } }).then(function(){ doStuffAfterLoad(); }); ``` -------------------------------- ### Configuring Sequelize Fixtures as Grunt Task Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JavaScript snippet demonstrates how to integrate `sequelize-fixtures` into a Grunt build process. It configures a `fixtures` task named `import_test_data` to load data from specified JSON files, supports asynchronous model loading, and allows for custom encoding options, followed by loading the `sequelize-fixtures` Grunt plugin. ```javascript grunt.initConfig({ fixtures: { import_test_data: { src: ['fixtures/data1.json', 'fixtures/models*.json'], // supports async loading models models: async function () { return await loadModels(); }, options: { //specify encoding, optional default utf-8 encoding: 'windows-1257' } } } }); grunt.loadNpmTasks('sequelize-fixtures'); ``` -------------------------------- ### Applying Build and Save Options to Sequelize Fixtures (JSON) Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON fixture demonstrates how to specify `buildOptions` and `saveOptions` for a model when loading data. `buildOptions` are passed to `Model.build()`, affecting instance creation, while `saveOptions` are passed to `instance.save()`, controlling how the instance is persisted. ```json { "model": "Article", "buildOptions": { "raw": true, "isNewRecord": true }, "saveOptions": { "fields": ["title", "body"] }, "data": { "title": "Any title", "slug": "My Invalid Slug" } } ``` -------------------------------- ### Loading Fixtures from Multiple Files using Glob in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This snippet shows how to load multiple fixture files matching a glob pattern (`fixtures/*.json`) into Sequelize models. It's useful for loading all fixtures within a directory. ```JavaScript sequelize_fixtures.loadFile('fixtures/*.json', models).then(function(){ doStuffAfterLoad(); }); ``` -------------------------------- ### Loading Fixtures Directly from an Array in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This snippet demonstrates loading fixtures directly from a JavaScript array of objects, where each object specifies the model name and the data for the record. ```JavaScript var fixtures = [ { model: 'Foo', data: { propA: 'bar', propB: 1 } }, { model: 'Foo', data: { propA: 'baz', propB: 3 } } ]; sequelize_fixtures.loadFixtures(fixtures, models).then(function(){ doStuffAfterLoad(); }); ``` -------------------------------- ### Loading Fixtures with Custom Encoding in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This snippet demonstrates how to specify a custom file encoding (e.g., 'windows-1257') when loading fixture files, overriding the default 'utf8' encoding. ```JavaScript sequelize_fixtures.loadFile('fixtures/*.json', models, { encoding: 'windows-1257'}).then(function(){ doStuffAfterLoad(); }); ``` -------------------------------- ### Detecting Duplicates by Specific Fields in JSON Fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON fixture illustrates how to use the `keys` property to define specific fields for duplicate detection. When loading multiple `Person` entries, only the first entry with a unique `email` will be loaded, preventing duplicates based on that field. ```json { "model": "Person", "keys": ["email"], "data": { "name": "John", "email": "example@example.com" } }, { "model": "Person", "keys": ["email"], "data": { "name": "Patrick", "email": "example@example.com" } } ``` -------------------------------- ### Mapping One-to-Many Associations by Property-Value Map in JSON Fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON fixture demonstrates associating a `Car` with an `Owner` using a property-value map for the `owner` field. Instead of an ID, an object with a `name` property is provided, which Sequelize will use to find the corresponding `Owner`. ```json [ { "model": "Owner", "data": { "name": "John Doe", "city": "Vilnius" } }, { "model": "Car", "data": { "make": "Ford", "owner": { "name": "John Doe" } } } ] ``` -------------------------------- ### Mapping Many-to-Many Associations by Property-Value Map Array in JSON Fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON fixture illustrates mapping a many-to-many relationship using an array of property-value maps. The `people` property in the `Project` data contains objects with `name` properties, which Sequelize will use to find and associate the corresponding `Person` entities. ```json [ { "model":"Person", "data":{ "name": "Jack", "role": "Developer" } }, { "model":"Person", "data":{ "name": "John", "role": "Analyst" } }, { "model":"Project", "data": { "name": "The Great Project", "people": [ { "name": "Jack" }, { "name": "John" } ] } } ] ``` -------------------------------- ### Sequelize Fixture Configuration to Ignore Setters Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON snippet illustrates how to configure a `sequelize-fixtures` data import to bypass model setters. By setting `"ignoreSet": true`, the library will directly assign the provided data values without invoking any `set` functions defined on the model, preventing potential errors from complex setter logic. ```json { "model": "User", "ignoreSet": true, "saveOptions": { "fields": ["title", "body"] }, "data": { "title": "Any title", "slug": "My Invalid Slug" } } ``` -------------------------------- ### Transforming Fixture Data Before Loading in JavaScript Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This snippet shows how to use `transformFixtureDataFn` to modify fixture data before it's inserted into the database. Here, it adjusts `createdAt` values if they are negative, treating them as relative times. ```JavaScript sequelize_fixtures.loadFile('fixtures/*.json', models, { transformFixtureDataFn: function (data) { if(data.createdAt && data.createdAt < 0) { data.createdAt = new Date((new Date()).getTime() + parseFloat(data.createdAt) * 1000 * 60); } return data; } }).then(function() { doStuffAfterLoad(); }); ``` -------------------------------- ### Mapping Many-to-Many Associations by ID Array in JSON Fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON fixture demonstrates mapping a many-to-many relationship by providing an array of IDs for the associated entities. The `people` property in the `Project` data contains an array of `Person` IDs, linking the project to multiple people. ```json [ { "model":"Person", "data":{ "id":122, "name": "Jack", "role": "Developer" } }, { "model":"Person", "data":{ "id": 123, "name": "John", "role": "Analyst" } }, { "model":"Project", "data": { "id": 20, "name": "The Great Project", "people": [122, 123] } } ] ``` -------------------------------- ### Defining One-to-Many Association in Sequelize Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JavaScript snippet demonstrates how to define a one-to-many relationship between `Car` and `Owner` models using Sequelize's `belongsTo` and `hasMany` methods. `Car` belongs to an `Owner`, and an `Owner` can have many `Car`s. ```javascript Car.belongsTo(Owner); Owner.hasMany(Car); ``` -------------------------------- ### Mapping Many-to-Many with Custom Through Model Attributes in JSON Fixtures Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JSON fixture demonstrates how to populate a many-to-many relationship that uses a custom through model with additional attributes. The `_through` property within the associated entity allows setting values for fields on the join table, such as `character`. ```json [ { "model": "Movie", "data": { "name": "Terminator" } }, { "model": "Actor", "data": { "name": "Arnie", "movies": [ { "name": "Terminator", "_through": { "character": "T-80" } } ] } } ] ``` -------------------------------- ### Defining Sequelize Model with Setter Causing Error Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JavaScript snippet defines a Sequelize `User` model with a custom `set` function for the `email` field. It demonstrates a scenario where accessing `this.previous('email')` within the setter can cause an error when `sequelize-fixtures` attempts to run values through it, highlighting the default behavior of the library. ```javascript const User = sequelize.define('User', email: { type: DataTypes.STRING, unique: true, validate: { isEmail: true, }, set: function set(val) { if (this.previous('email')) { // <--- this line will raise an error // check some thing } this.setDataValue('email', val); } } }); ``` -------------------------------- ### Defining Many-to-Many Association in Sequelize Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JavaScript snippet illustrates how to define a many-to-many relationship between `Project` and `Person` models in Sequelize. It uses `belongsToMany` for both models, specifying `peopleprojects` as the `through` table for the join. ```javascript Project.belongsToMany(Person, {through: 'peopleprojects'}); Person.belongsToMany(Project, {through: 'peopleprojects'}); ``` -------------------------------- ### Defining Custom Through Model for Many-to-Many in Sequelize Source: https://github.com/domasx2/sequelize-fixtures/blob/master/README.md This JavaScript snippet defines a custom through model `ActorsMovies` for a many-to-many relationship between `Movie` and `Actor`. It includes an additional `character` attribute on the join table, allowing for extra data specific to the association. ```javascript ActorsMovies = sequelize.define("ActorsMovies", { character: {type: DataTypes.STRING} }); Movie.belongsToMany(Actor, {through: ActorsMovies}); Actor.belongsToMany(Movie, {through: ActorsMovies}); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.