Once you have your required dependencies, inside of the plugins section of your file:
Npm install -save-dev exports-loader imports-loader
However, any code in your app referencing Promise will get the polyfill version. The approach we are taking won’t pollute the global namespace, whenever a reference to a module is found Webpack will automatically rewrite its reference to be the supplied module.Īs you can see, we are polyfilling Promise, which means if you open up your application in a browser without native Promise support, typing Promise into the console will result in undefined. The solution I am proposing might sound familiar, using imports-loader and exports-loader in combination with the Provide plugin to override native API’s.Īnother solution is to conditionally load polyfills depending on what the browser supports, but that is for another time. I wanted to specifically know the best way to polyfill promises using Bluebird in Webpack 2. As you know, front-end development moves rapidly and 2015 is 100 years in front-end land. For this I will again be using the n(.) method in conjunction with new delete methods for both the ProjectRepository and TaskRepository classes.įor ProjectRepository this looks like this: // project_repository.Surprisingly, whilst determining the best way to polyfill promises in a Webpack build and using Bluebird I came across such mixed results.Ī lot of the posts out there don’t mention Bluebird at all, opting for es6-promise when it has been proven Bluebird has such great performance, it’s even faster than native promises in some browsers.Ī lot of the results I found were from 2015 and a couple in 2016. The last mutational functionality to implement is to provide the ability to delete records from the database. This.db = new sqlite3.Database(dbFilePath, ( err) => = task dao.js const sqlite3 = require( 'sqlite3') After that I will scaffold out a data access class called AppDAO that will establish a connection to the database inside a constructor and assign it to a member field called db. Inside dao.js I will add a imports for sqlite3 and Bluebird's Promise objects. To start I will need to make a main.js file along with a dao.js (or Data Access Object) file in the same directory as the package.json file. Ok, now that I know what I need to create I can now translate that into code. For the rest I will just use a little intuition, some made up test data, and roll with it (a common work characteristic for most developers). It is clear that I will need a projects table as well as a tasks table. With the business rules stated I can take that info and start designing the necessary tables and their fields.
The basic business rules for this application's data access layer are as follows: For this article I am going with the assumption that I am building out the data access layer for a project and task tracking application.
Designing the DatabaseĪs with almost every one of my other articles I will be using a made up application to help describe some of the important aspects of database programming with Node.js and SQLite. I now will create an empty file right next to the package.json file called database.sqlite3 that SQLite will store data in. In addition to sqlite3 I am going to install Bluebird so that I can use the familiar promise functionality in my database programming.
Next I will need to install sqlite3 package via npm like so: $ npm install -save sqlite3 "test": "echo \"Error: no test specified\" & exit 1" "description": "Code for tutorial blog on node and sqlite", Save it as a dependency in the package.json file.ĭescription: Code for tutorial blog on node and sqliteĪbout to write to /node-sqlite/app/package.json: Use `npm install -save` afterwards to install a package and See `npm help json` for definitive documentation on these fields It only covers the most common items, and tries to guess sane defaults. This utility will walk you through creating a package.json file. I will begin by creating a new npm package using npm init inside an empty directory called node-sqlite-tutorial. For those not familiar with SQLite, it is a simple single file relational database that is very popular among smart devices, embedded systems, and even small web application.
In this tutorial I will be demonstrating how to use SQLite in combination with JavaScript inside the Node.js environment with the help of the sqlite3 Node.js driver.