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

sqlite-orm

v0.2.4

Published

the ORM framework for sqlite

Downloads

52

Readme

sqlite-orm

NPM version Build Status Dependency Status Coverage Status

the ORM framework for sqlite

Install

$ npm install --save sqlite-orm

Usage

The coffeescript sample code

Mapper = require 'sqlite-orm'
path = require 'path'
Migration = Mapper.Migration
ModelBase = Mapper.ModelBase

Migration.createTable 'ParentModel', (t) ->
  t.addColumn 'name', 'TEXT'

Migration.createTable 'ChildModel', (t) ->
  t.addColumn 'name', 'TEXT'
  t.addReference 'parentModelId', 'ParentModel'

class ChildModel
  ModelBase.includeInto this
  constructor: (params) -> @initModel params
  @initAssos: ->
    @belongsTo ParentModel

class ParentModel
  ModelBase.includeInto this
  constructor: (params) -> @initModel params
  @initAssos: ->
    @hasOne ChildModel

mapper = new Mapper path.resolve(__dirname, 'test.db')
mapper.sync()

The corresponding javascript code.

var Mapper = require('sqlite-orm');
var Migration = Mapper.Migration;
var ModelBase = Mapper.ModelBase;
var path = require('path');

Migration.createTable('ParentModel', function(t) {
  t.addColumn('name', 'TEXT');
});

Migration.createTable('ChildModel', function(t) {
  t.addColumn('name', 'TEXT');
  t.addReference('parentModelId', 'ParentModel');
});

function ParentModel(params) {
  this.initModel(params);
}
ModelBase.includeInto(ParentModel);

function ChildModel(params) {
  this.initModel(params);
}
ModelBase.includeInto(ChildModel);

ParentModel.initAssos(function() {
  ParentModel.hasOne(ChildModel);
});
ChildModel.initAssos(function() {
  ChildModel.belongsTo(ParentModel);
});

mapper = new Mapper(path.resolve(__dirname, 'test.db'));
mapper.sync().then(function() {
});

More sample can refer to below sites:

API

Mapper

  • sync function() synchronize the model definition and the database

    • return: Promise
  • close function() close the database

    • return: Promise
  • beginTransaction: function() begin the transaction.

  • endTransaction: function() end the transaction.

  • scopeTransaction: function(callback) make the callback invoke in the transaction, after this callback complete, endTransaction will invoke automatically.

  • @Migration Migration get the Migration class

  • @ModelBase ModelBase get the ModelBase class

  • @INTEGER String the INTEGER data type

  • @REAL String the REAL data type

  • @TEXT String the TEXT data type

  • @BLOB String the BLOB data type

  • DATETIME String the Date date type. If declare DATETIME, then the type of this attribute will be Date.

  • BOOL String the Bool date type.

Migration

  • @createTable: function(tableName, callback) create the database table

    • tableName: String

    • callback: function(tableInfo) create the columns in this callback

      • tableInfo: TableInfo the class to create the columns and index
  • @clear: function() clear the table definition

TableInfo

  • addColumn: function(name, type, opts) add the table column

    • name: String the column name

    • type: String the column data type, such as INTEGER or TEXT

    • opts: Object the column options

  • createIndex: function(indexName, columns) add index for the specific column

    • indexName: String the index name.
    • columns: Array each item of columns is the column name that need index.
  • addReference: function(name, tableName, opts) add foreign key

    • name: the column name that need index

    • tableName: the name of table that the index will point to

    • opts: Object the index options

ModelBase

  • @initAssos: function() declare the association

    all the subclass must implement this interface to declare the association

  • @hasOne: function(ChildModel, opts) declare this Model has one child Model

    • ChildModel: ModelBase or String the child Model class or class name.

    • opts: Object the options used for hasOne association

      • as: String(optional) the property name to refer to the ChildModel instance, the default value is "#{childModel}". e.g. ChildModel is 'ChildModel', then the as value is childModel
  • @hasMany: function(ChildModel, opts) declare this Model has many children.

    • ChildModel: ModelBase or String the child Model class or class name.

    • opts: Object the options used for hasOne association

      • as: String(optional) the property name to refer to the ChildModel instances, the default value is "#{childModels}". e.g. ChildModel is 'ChildModel', then the as value is childModels
  • @hasManyBelongsTo: function(TargetModel, opts) declare this Model and the target Model is N-N connection. The description of this connection can refer to rails association guide

    • TargetModel: ModelBase or String the target Model class or class name.

    • opts: Object the options used for this association.

      • midTableName: String the reference table name.
      • sourceThrough: String the column foreign key name that refer the Model.
      • targetThrough: String the column foreign key name that refer the TargetModel.
      • as: String just like @hasMany or @hasOne
  • @belongsTo: function(ParentModel, opts) declare this Model is member of ParentModel

    • ParentModel: ModelBase or String the parent Model class or class name.

    • opts: Object the options used for hasOne association

      • through: String(optional) the column name that used for foreign key, the default value is "#{ParentModel}#{primaryKey}". e.g. ParentModel name is 'ParentModel', primaryKey is 'id', then the foreign key is parentModelId.

      • as: String(optional) the property name to refer to the ParentModel instance, the default value is "#{ParentModel}". e.g. ParentModel is 'ParentModel', then the as value is parentModel

  • @new: function(obj) create a new model object, not saved into database

    obj: Object the attributes list

  • @create: function(obj) just like @new, but save into database

  • @drop: function() drop the table

  • @destroy: function() destroy this model object and delete the db row.

  • @find: function(where, opts) find the object that match the where statement

  • @findAll: function(where, opts) find all of object match the condition

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using gulp.

License

Copyright (c) 2015 liuxiong. Licensed under the MIT license.