Database Seeding
Relevant source files
The following files were used as context for generating this wiki page:Database seeding in the LostData project refers to the process of populating the database with an initial set of data. This is crucial for development and testing, ensuring the application has content to work with from the start. The repository contains two distinct mechanisms for data seeding: a primary Node.js script that populates a MongoDB database for the main web application, and a secondary Python script that populates a local JSON file which acts as a simple database.
This document outlines the architecture and execution flow of both seeding methods, the data structures involved, and the relevant database configurations.
Node.js Seeding (seed.js)
This is the primary method for populating the MongoDB database used by the Express.js application. The seed.js script is a standalone utility designed to be run from the command line to insert a predefined set of "model" data into the database.
Sources: seed.js, server.js:25, APP/models/index.js
Execution Flow
The script connects to the MongoDB database, inserts a hardcoded array of model data using the Model.insertMany Mongoose function, and then exits the process.
The following diagram illustrates the execution flow of the seed.js script.
Sources: seed.js:100-108
Data Structure and Schema
The data for seeding is stored in a JavaScript array named modelsData. Each element in the array is an object representing a single model, containing detailed information. This data structure must conform to the Mongoose schema defined for the "Model" collection.
Sources: seed.js:3-99, APP/models/model.model.js:2-15
Model Schema
The Model schema is defined in APP/models/model.model.js and dictates the structure of the documents in the MongoDB collection.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
id | String | true | A unique identifier for the model. | |
name | String | true | The name of the model. | |
age | Number | false | The age of the model. | |
location | String | false | The location of the model. | |
profile | String | true | A detailed profile description. | |
pricing | String | true | Pricing information for services. | |
dosAndDonts | String | true | A list of allowed and disallowed activities. | |
extraTabs | Array | false | [] | An array for additional information tabs. |
history | Array | false | [] | An audit log of changes made to the document. |
createdAt | Date | true | (auto) | Timestamp of document creation (from {timestamps: true}). |
updatedAt | Date | true | (auto) | Timestamp of last document update (from {timestamps: true}). |
Sources: APP/models/model.model.js:2-15
The relationship between the seed script, the model definition, and the database configuration is shown below.
Sources: seed.js:1, APP/models/index.js:1,6
Seed Data Example
The modelsData array contains objects with extensive string-based data for each model.
// Source: seed.js:3-99
const modelsData = [
{
id: "roxie_sinner_001",
name: "ROXIE SINNER",
profile: "Model Name: Roxie Sinner\nImportant Info: ...",
pricing: "PRICE LIST - LAST UPDATED: 08/12/2024\nClient: Roxie Sinner...",
dosAndDonts: "DOS AND DON'TS - LAST UPDATED: 01/07/2024\nClient: Roxie Sinner...",
extraTabs: []
}
// Add other 33 models here in the same format...
];
Sources: seed.js:3-99
Python Seeding (model_database.py)
The repository also includes a Python script, model_database.py, which manages a simple database using a local models.json file. This script has its own seeding logic that runs when the script is executed directly. It is separate from the main Node.js application and its MongoDB database.
Sources: model_database.py
Execution Flow
When model_database.py is run, it initializes a ModelDatabase class. The script checks if a specific model ('ROXIE SINNER') exists in its data file (models.json). If not, it adds the model's data, effectively seeding the JSON file.
This diagram shows the seeding process within the Python script.
Seeding Logic
The seeding is triggered within the if __name__ == "__main__" block. It ensures that the models.json file is populated with at least one entry if it's empty or doesn't contain the 'ROXIE SINNER' key.
// Source: model_database.py:100-209
if __name__ == "__main__":
db = ModelDatabase()
// Add Roxie Sinner (your example data)
if 'ROXIE SINNER' not in db.models:
db.add_model(
"ROXIE SINNER",
"""PRICE LIST - LAST UPDATED: 08/12/2024...""",
[
"Fully Nude: TRUE",
// ...
],
[
"Femdom Videos: FALSE",
// ...
],
"""🔹 Model Name: Roxie Sinner..."""
)
db.interactive_mode()
Sources: model_database.py:100-209
Summary
The LostData project employs two different strategies for data seeding. The primary, production-oriented approach uses a Node.js script (seed.js) to populate a MongoDB collection based on a strict Mongoose schema. This is essential for the main web application's functionality. A secondary, self-contained Python script (model_database.py) provides a simpler, file-based database solution with its own on-launch seeding mechanism, likely for local development, testing, or as a separate utility. Both methods ensure that the application or tool has initial data to work with.