<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shannen Ye on Medium]]></title>
        <description><![CDATA[Stories by Shannen Ye on Medium]]></description>
        <link>https://medium.com/@shannen.ye?source=rss-1e66d8b341c7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*vXqFyQhqHHKff7sxA_kywg.jpeg</url>
            <title>Stories by Shannen Ye on Medium</title>
            <link>https://medium.com/@shannen.ye?source=rss-1e66d8b341c7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 02:29:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shannen.ye/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Setting Up A Database and Seed File]]></title>
            <link>https://medium.com/@shannen.ye/setting-up-a-database-and-seed-file-7e73fe2a9fe6?source=rss-1e66d8b341c7------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e73fe2a9fe6</guid>
            <category><![CDATA[datatables]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[seeds]]></category>
            <category><![CDATA[post-ico]]></category>
            <dc:creator><![CDATA[Shannen Ye]]></dc:creator>
            <pubDate>Mon, 29 Jan 2018 03:24:53 GMT</pubDate>
            <atom:updated>2018-01-29T03:24:53.594Z</atom:updated>
            <content:encoded><![CDATA[<p><em>What is a database, seed file, and why is any of this important?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*RB4cF1mnegIG4QN0eQ6gGw.jpeg" /><figcaption>Campuses and Students data table in Postico</figcaption></figure><p>A database is a collection of tables that contain, you guessed it, data! This can be associated with other tables and create a modular structure for storing large amounts of information. A seed file will contain dummy data that populates your database so that you can test if your models and associations are working the way you want them to. This file can contain whatever information you want so have fun with it! Give your models funky names but don’t go crazy in case you decide to keep this file to display your skills to employers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cwaxhb-F_8mpXweYnNETQQ.png" /><figcaption>Populated students table with association to campuses table</figcaption></figure><p>Now that you’ve seen what a database looks like, let’s get to coding! You’ll need to have your <em>sequelize</em> models already defined for this part. You can create something simple or follow along to the one below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fi8hS_lzvTlUeuYnzLJT1w.png" /><figcaption>Campus (left) and Student (right) models</figcaption></figure><p>You’ll need to make sure you’re importing and exporting files correctly or else the information will never reach the database. Your models and associations should be imported, or required, in the <em>index.js</em> file of the same folder and then exported together. The outer <em>index.js</em> can be where the postgres database is set up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2A9CHCn0EBeOuNSgUJ95Fg.jpeg" /><figcaption>importing models into index.js in ‘models’ folder and creating a postgres database in the index.js of ‘db’ folder</figcaption></figure><p>We’re almost there! Now that we have our associations, imports and postgres set up, we can start making our seed file! This file can be created anywhere in your directory, for convenience I created mine in the top layer. If you look in the above picture, you can see the file is literally named s<em>eed.js.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GKtYNEulXkj0Eyodj6I1KQ.png" /><figcaption>seed.js snippet</figcaption></figure><p>The above picture contains an array of objects for students and campuses in which each element is a model with populated information. If your models have a <em>allowNull: false</em>, or <em>validators</em>, you’ll need to make sure the inputs in the seed file are correct or else you’ll get an error. Next up is setting up a function so that you can populate your database with <strong>both</strong> <em>studentsForPostico</em> and <em>campusesForPostico.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/1*UsrSAsQQhTmWNMbmSQTsPw.png" /><figcaption>Campuses model is called campuses in the data table because we defined it in our models as such. Same for Students.</figcaption></figure><p>This should be at the bottom of your seed file. Once the database is created you’ll have two tables (Campuses and Students), each will be populated with the data you provided. Don’t forget to add a seed script in your package.json!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*ihibU_83OJUY-JdesJQ2xQ.jpeg" /><figcaption>package.json seed script</figcaption></figure><p>This will allow you to populate your database from your CLI (command line interface) by typing “npm run seed” when we want to add, drop, or edit tables. We’re down to the <strong>final steps</strong>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_mR5yeY_dR7iLdEt-3GWog.png" /></figure><p>Add this function to wherever your server is <em>listening</em>. You want your database to be populated <strong>before </strong>your server starts so that the information can be loaded on the site. We have it set to <em>force: false </em>because when the server starts, we don’t want the database cleared. If we did, we’d have to change it to <em>force: true</em>, save the file, run the server, then exit server in your CLI, then change it back to <em>force: false,</em> save, and run the server again!</p><p>It looks like we have all of our code set up. Now it’s time to go to your terminal and manually create a database that’ll hold all of our information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*CGZxdLGotkBlRcaomadsAw.png" /><figcaption>CLI (top) with successfully created database in Postico (bottom)</figcaption></figure><p>The moment we’ve all been waiting for has arrived! Let’s run the below:</p><pre>npm run seed</pre><p>Check out your database and it should look similar to the first two images. Congratulations, you’re now a pro at creating a database, creating a seed file, creating a seed script, and syncing the database!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e73fe2a9fe6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>