storkSQL
v0.1.34
Published
A SQL ORM with Mongoose-like syntax. Built on top of knex.
Downloads
5
Readme
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