How to write Node modules with Rust

TLDR: there’s a sample repo here if you’re lazy to read this post. The sample repo include GitHub Actions sample for CI as well. The Rust bit It’s very simple. You write your function in Rust #[js_function(3)] fn say_hello(ctx: CallContext) -> Result<JsString> { let name = ctx.get::<JsString>(0)?.into_utf8()?; let name = name.as_str()?; let s = ctx.env.create_string_from_std(format!("Hello, {}!", name))?; Ok(s) } And then you expose it to Node.js runtime with #[module_exports] fn init(mut exports: JsObject) -> Result<()> { exports.

Sharding and IDs

So I was going through this post from Instagram Engineering blog while researching for some sharding solutions. The solution is quite elegant and I decided to port this to MySQL. Turns out, it’s harder than I thought since we’re using a pretty dated MySQL version at work. There is no sequence, just AUTO_INCREMENTAL. In order to use the code snippet for PL/PGSQL, I would have to find a way to mimic nextval function.

The Birth & Death of JavaScript

Old but gold. This science fiction / comedy / absurdist / completely serious talk traces the history of JavaScript, and programming in general, from 1995 until 2035. It’s not pro- or anti-JavaScript; the language’s flaws are discussed frankly, but its ultimate impact on the industry is tremendously positive.

Explicit over clever

I always prefer explicit over clever, hacky hack. Explicit make the code looks clearer, more maintainable and leaning toward a more predictable behavior (aka junior developers will be less likely to mess it up). Take an example of this code where I have a folder called providers. This here below is the content of the index.js file which basically read all the files in that folder (except index.js), require them and then module.

Some of the most useful tips I learn when working with NodeJS

modules management with npm Nodejs comes with an amazing package manager called npm. Start a project with npm init which will then create a configuration file named package.json, keeping track of all the modules your project is using. You don’t have to manually manage this file yourself. If you want to add a package to package.json you can add --save parameter when installing it. npm install koa-static --save Also, you should ignore npm_modules folder when git push because whoever clone the repo can do npm install by themselves.

Things software developers wish they had known in their 20s

The era in which a common career trajectory is to become a lifer at some software company and work one’s way up to higher and higher internal positions has been over for a long time (This era had already been over in the 1990s when I was a young programmer). You don’t owe the company you work for anything beyond what you put your name on when you signed the employee agreement.

REST APIs made easy with StrongLoop

StrongLoop allows you to quickly create REST APIs using their graphic interface and CLI. SLC also supports debugging, profiling, tracing, deploying as well as monitoring features. Creating REST APIs with slc is as easy as creating datasource and model. StrongLoop will do the rest for you. I was very tempted to use StrongLoop for a recent project but I had to deal with a legacy database that use a very old odbc driver which force me to use node.

Fuck callbacks! Let's use generators

Let’s write a simple function hello that return a string when called. var hello = function () { return 'Hello ' + name; } Now convert it into a generator. Call hello() this time will return you an Object instead: the generator. var hello = function *() { return 'Hello ' + name; } Let’s consider the following snippet. var hello = function *() { yield 'Stopped here!'; return 'Hello ' + name; } var generator = hello('Clark Kent'); console.

bluebird - a promise library with unmatched performance

The documentation of bluebird is so much better than Q - the library which I’m currently using. bluebird with nodejs is even better: it can convert an existing promise-unaware API to promise-returning API which is so awesome. Thid feature works with most popular libraries (which use error as first arg (as they all should)). I’m sold!! Usage function getConnection(urlString) { return new Promise(function(resolve) { //Without new Promise, this throwing will throw an actual exception var params = parse(urlString); resolve(getAdapater(params).

The next bullet on my resume: AngularJS

I started a HTML5 hybrid mobile app project recently. Before I start coding the frontend, I had to decide which client side JavaScript framework I will be using. The choice narrows down between AngularJS (Ionic framework) and Ember. Of those two, AngularJS is obviously the more popular choice. There’re many frameworks built on AngularJS; thousands of questions on StackOverflow; lots of open-source projects base on AngularJS on GitHub. There are many great frameworks out there but few has gained so much developer mindshare like AngularJS.