npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sqlite3-entities

v1.2.1

Published

SQLite 3 Async Entities in JavaScript / Node.js

Downloads

4

Readme

SQLite 3 Async Entities in JavaScript / Node.js

NPM #Installing ##Node.js

  npm install sqlite3-entities --save

##Source

  git clone https://github.com/TimothyMeadows/sqlite3-entities

#Dependancies node-sqlite3

Build Status Dependencies #Tables I felt it was important that tables could be defined in it's simplest form in a standard object notation. While complex mapping / indexing might be needed at a more advanced level. It didn't have to be "in your face" at all times. Types are inferred by there value. However, those values are not actually treated as defaults. This may change in the future. Similar to other ORM / Entity style frameworks. I wanted advanced "mapping" to be handeled in it's own object allowing for table objects, and, matching map objects when needed.

  var entities = require('sqlite3-entities');
  var context = new entities.database("test.db");
  context.table("test_table", {
    id: 0,
    uid: ""
    active: false,
    created: 0
  });

Numbers, and, Boolean are treated as INTEGER. String, and, object are treated as TEXT, array is treated as a BLOB. Default for unknown types is BLOB. Should you need. You can override a inferred type for a column using a custom mapping.

  var entities = require('sqlite3-entities');
  var context = new entities.database("test.db", { cached: true });
  context.table("test_table", {
    id: 0,
    uid: ""
    active: false,
    created: 0
  }, {
    active: { type: "TEXT" }
  });

The primary key is automatically determined using the first property in the object. You can override this by supplying the property you would like to use as the final parameter in the table method. Type is ignored when using this method. Any property supplied as the primary key will be converted to an auto incrementing INTEGER. If you do not want an auto incrementing primary key you can override this by setting the increment mapping to false.

  var entities = require('sqlite3-entities');
  var context = new entities.database("test.db", { cached: true });
  context.table("test_table", {
    id: 0,
    uid: ""
    active: false,
    created: 0
  }, {
    active: { type: "TEXT" },
    uid: { increment: false }
  }, "uid");

Finally, Foreign keys, and, unqiue constraints can be declared using the mapping object. Current direct entity mapping via the table model is not supported. But it will be a future upgrade when time permits.

  var entities = require('sqlite3-entities');
  var context = new entities.database("test.db", { cached: true });
  context.table("test_table", {
    id: 0,
    uid: "",
    active: false,
    created: 0
  }, {
    uid: { unique: true },
    id2: {foreign:{table:"test_table2", column: "id"}}
  });

  context.table("test_table2", {
    id: 0
  });

#Ready & Error In the unfortunite event an exception occurs. You can use the error event to listen for what occured. More importantly, you will need to listen for the ready event before you can access entities you would like to use. This is due to the pure async nature of the library. Additionally, you can use the migrated, and, created properties to determine if the data was just created, or, migrated and may be in need of seeding.

  var entities = require('sqlite3-entities');
  var context = new entities.database("test.db", { cached: true, migration: entities.migration.alter });
  context.on("ready", function () {
    if (context.migrated) console.log("database was migrated!");
    if (context.created) console.log("database was created!");

    console.log("database is ready!");
    context.test_table.remove((t) => t.uid == "test123", function (deleted) {
        if (deleted) console.log("row removed!");
        context.test_table.add({
            uid: "test123",
            active: true,
            created: 1001
        }, function() {
            console.log("row added!");
            context.test_table.where((t) => t.created == 1001 && t.active, function(row) {
                console.log(row.toList());
            })
        });
    });
  });

  context.on("error", function(err) {
    console.log(err);
  });

#Migration

Automatic migration support exists for halt, drop & create, and, alter. Note that if you use alter, and a physical migration is detected it will trigger the migration event. You can also choose to use "manual" migration by specifying migration.manual or not specifying the migration option at all. Because SQLite3 does not support dropping, or, renaming of columns after they are created. Support for these methods also do not exist in sqlite3-entities.

WARNING: THERE IS NO DATA MIGRATION SUPPORT FOR DROP & CREATE. ALL DATA WILL BE LOST.

  var entities = require('sqlite3-entities');
  var context = new entities.database("test.db", { cached: true, migration: entities.migration.manual });

  context.table("test_table", {
    id: 0,
    uid: "",
    active: false,
    created: 0
  }, {
    uid: { unique: true },
    id2: {foreign:{table:"test_table2", column: "id"}}
  });

  context.table("test_table2", {
    id: 0,
    //uid: "" // uncomment to test manual migration after first execution
  });

  context.once("ready", function () {
    if (context.migrated) console.log("database was migrated!");
    if (context.created) console.log("database was created!");

    console.log("database is ready!");
    console.log(context);

    context.test_table.remove((t) => t.uid == "test123", function (deleted) {
        if (deleted) console.log("row removed!");
        context.test_table.add({
            uid: "test123",
            active: true,
            created: 1001
        }, function() {
            console.log("row added!");
            context.test_table.where((t) => t.created == 1001 && t.active, function(row) {
                console.log(row.toList());
            })
        });
    });
  });

  context.once("migration", function(migration, differences) {
    console.log(differences);

    for (var i = 0; i <= differences.length - 1; i++) {
        switch (differences[i]) {
            case "test_table2":
                migration.prepare("ALTER TABLE test_table2 ADD uid TEXT;");
                migration.run(function() {
                    migration.accept();
                })
                break;
            default:
                migration.reject();
                break;
        }
    }
  });

  context.on("error", function(err) {
    console.log(err);
  });

#Execution Chains (psudeo Linq) All table mappings support async based execution chains. However, due to the async nature if you directly chain statements at the table mapping level they will not contain results from the previous execution. The exception to this being select() which lets you control which columns which are selected in future statements in the same chain.

Chained execution passed the first table mapping execution (I.E. the results returned from async) are synchronis, they can be chained, and, do include results from the previous exection.

  var entities = require('sqlite3-entities');
  var context = new entities.database("test.db", { cached: true, migration: entities.migration.alter });

  context.table("test_table", {
    id: 0,
    uid: "",
    array: [],
    object: {},
    active: false,
    created: 0
  });

  context.once("ready", function () {
    if (context.migrated) console.log("database was migrated!");
    if (context.created) console.log("database was created!");

    context.test_table.select(["id", "uid", "array", "object", "active"]).where((t) => t.active, function(rows) {
        console.log(rows.where((t) => t.created == 0).first((t) => t.uid == "test123"));
    });
  });

  context.on("error", function(err) {
    console.log(err);
  });