0.1.34 • Published 8 years ago

storkSQL v0.1.34

Weekly downloads
1
License
ISC
Repository
-
Last release
8 years ago

Getting Started

https://github.com/alexcstark/storkSQL

You'll need two things to get started: the library and a DB client.

npm install storkSQL

npm install pg

Stork uses knex which supports pg, mySQL, and SQlite.

Configure the database

db.js

import Stork from 'storkSQL';
// Put your information below
const DB_CONFIG_OBJ = {
  host: '',
  password: '',
  database: '',
  port: 3241,
  user: '',
  ssl: true
};
export default new Stork({
  connection: DB_CONFIG_OBJ,
  client:'pg'
});

The following methods on the db object exist to help you manage your database. See the bottom of the page for an example.

  dropTableIfExists(tableName)
  hasTable(tableName)
  createTable(tableName, schema)
  endConnection()

Set up your schema and models

User.js

import db from './path/to/db.js';
export const UserSchema = function (user) {
  user.increments('id').primary();
  user.string('email', 100).unique();
  user.string('password', 100);
  user.string('homeLatitude', 100);
  user.string('homeLongitude', 100);
  user.string('homeAddress', 100);
  user.timestamps();
};
export const User = db.model('users', UserSchema);

This will give you access to the following queries:

findAll()
findById(id)
find(obj)
findOne(obj)
findOrCreate(obj)
create(obj)
save(obj)
updateOrCreate(obj)
update(criteriaObj, updateObj)
remove(obj)

Each query will return a promise that must be resolved, like so:

User.find({id: req.params.userid})
  .then((user) => res.json(user));
};

The library also support salting, hashing, and comparing passwords for users or other secure data. Secure fields (as seen below) are salted and hashed by the ORM.

models/User.js

export const UserSchema = function (user) {
  user.increments('id').primary();
  user.timestamp('created_at').defaultTo(db.knex.fn.now());
  // carvis auth -- might be factored out.
  user.string('email', 255).unique();
  user.string('password', 100);
  user.text('alexaUserId', 'longtext').unique();
  // data returned from lyft specific functions
  user.string('firstName', 100);
  user.string('lastName', 100);
  user.string('lyftEmail', 100);
  // the phone number used for lyft authentication
  user.string('lyftPhoneNumber', 100);
  // data lyft uses for request-ride calls
  user.text('lyftPaymentInfo', 'longtext');
  user.text('lyftToken', 'longtext');
  user.text('uberToken', 'longtext');
  // data used for uber authentication
  user.string('uberEmail', 255);
  user.string('uberPassword', 255);
};

export const User = db.model('users', UserSchema, {
  secureFields: {
    password: process.env.USER_ENCRYPT,
    fields: ['lyftToken', 'lyftPaymentInfo', 'uberPassword', 'uberToken', 'password', 'alexaUserId']
  }
});

It is recommended to create files to help manage your DB like this:

import db from '../db';
import {RideSchema} from '../Ride';
import {UserSchema} from '../User';

const resetDb = async function() {
  await db.dropTableIfExists('users');
  console.log('dropping users table');
  await db.dropTableIfExists('rides');
  console.log('dropping rides table');

  if (!(await db.hasTable('users'))) {
    await db.createTable('users', UserSchema);
    console.log('created new users table');
  }

  if (!(await db.hasTable('rides'))) {
    await db.createTable('rides', RideSchema);
    console.log('created new rides table');
  }

  await db.endConnection(); /* eslint-ignore */
  console.log('connection destroyed');
};

resetDb();

Remember to transpile as async/await isn't supported everywhere, yet.

knex

All of the code is written on top of knex. Want access to a method that hasn't been written?

http://knexjs.org/

After you've set up your database connection, use db.knex for access to all of knex's functionality!

To-Do

  • Relationships and joins
0.1.34

8 years ago

0.1.33

8 years ago

0.1.32

8 years ago

0.1.31

8 years ago

0.1.30

8 years ago

0.1.29

8 years ago

0.1.28

8 years ago

0.1.27

8 years ago

0.1.26

8 years ago

0.1.25

8 years ago

0.1.24

8 years ago

0.1.23

8 years ago

0.1.22

8 years ago

0.1.21

8 years ago

0.1.20

8 years ago

0.1.19

8 years ago

0.1.18

8 years ago

0.1.17

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago