
Research
/Security News
Weaponizing Discord for Command and Control Across npm, PyPI, and RubyGems.org
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
mesa provides easy access to postgres databases
every call to a chainable configuration method (table
, connection
, attributes
, where
, ...)
returns a new mesa object and doesn't change the state of the mesa object it is called on.
npm install mesa
pg = require 'pg'
mesa = require 'mesa'
getConnection = (cb) -> pg.create 'tcp://foo@localhost/bar', cb
# the user object will be used in all following examples
user = mesa
.table('user')
.connection(getConnection)
.attributes(['name', 'email'])
.primaryKey('my_id') # optional, defaults to 'id'
connection()
either takes a connection object or a function, which is supposed to take a
callback and call it with a connection object.
providing a connection object explictely is useful for transactions.
attributes()
sets the properties to pick from data in create
and update
.
it must be called before using create
or update
user.insert {
name: 'foo'
}, (err, userId) -> # ...
user.insertMany [
{name: 'foo'}
{name: 'bar'}
], (err, userIds) -> # ...
user.where(id: 3).delete (err) -> # ...
where
can take any valid criterion
user.where(id: 3).where(name: 'foo').update {name: 'bar'}, (err) -> # ...
multiple calls to where
are anded together.
user.where(id: 3).first (err, user) -> # ...
where
can take any valid criterion
user.where(id: 3).exists (err, exists) -> # ...
user.where(id: 3).find (err, users) -> # ...
user
.select('user.*, count(project.id) AS project_count')
.where(id: 3)
.where('name = ?', 'foo')
.join('JOIN project ON user.id = project.user_id')
.group('user.id')
.order('created DESC, name ASC')
.limit(10)
.offset(20)
.find (err, users) ->
mesa uses mohair for where
, select
, join
, group
, order
, limit
and order
.
look here for further documentation.
use hasOne
if the foreign key is in the other table (address
in this example)
user.hasOne 'address', address,
primaryKey: 'id' # optional with default: "id"
foreignKey: 'user_id' # optional with default: "#{user.getTable()}_id"
the second argument can be a function which must return a model. this can be used to resolve models which are not yet created when the association is defined. it's also a way to do self associations.
use belongsTo
if the foreign key is in the table of the model that belongsTo
is called on (project
in this example)
project.belongsTo 'user', user,
primaryKey: 'id' # optional with default: "id"
foreignKey: 'user_id' # optional with default: "#{user.getTable()}_id"
use hasMany
if the foreign key is in the other table (user
in this example) and
there are multiple associated records
user.hasMany 'projects', project,
primaryKey: 'id' # optional with default: "id"
foreignKey: 'user_id' # optional with default: "#{user.getTable()}_id"
use hasAndBelongsToMany
if the association uses a join table
user.hasAndBelongsToMany 'projects', project,
joinTable: 'user_project' # required
primaryKey: 'id' # optional with default: "id"
foreignKey: 'user_id' # optional with default: "#{user.getTable()}_id"
otherPrimaryKey: 'id' # optional with default: "id"
otherForeignKey: 'project_id' # optional with default: "#{project.getTable()}_id"
associations are only fetched if you include
them
user.includes(address: true)
includes can be nested (arbitrarily deep)
user.includes(shipping_address: {street: true, town: true}, billing_address: true, friends: {billing_address: true})
user = mesa.table('user')
user.activeAdmins = -> @where(visible: true, role: 'admin')
user.activeAdmins().find (err, activeAdmins) -> # ...
when inserting a user, also insert his address. do both in the same transaction:
address = mesa
.table('address')
.attributes(['name', 'street', 'user_id']
user = mesa.table('user').attributes(['email', 'password'])
user.insert = (data, cb) ->
@getConnection (err, connection) =>
return cb err if err?
connection.query 'BEGIN;', (err) =>
return cb err if err?
# do the original insert but on the transactional connection
mesa.insert.call @connection(connection), data, (err, userId) =>
return cb err if err?
address = data
address.user_id = userId
# insert the address portion of data on the transactional connection
address.connection(connection).insert data, (err) ->
return cb err if err?
connection.query 'COMMIT;', [], (err) ->
return cb err if err?
cb null, userId
user.insert {
password: 'bar'
email: 'foo@example.com'
name: 'foo'
address: 'foostreet'
}, (err, userId) -> # ...
FAQs
simple elegant sql for nodejs
The npm package mesa receives a total of 49 weekly downloads. As such, mesa popularity was classified as not popular.
We found that mesa demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.
Security News
Socket now integrates with Bun 1.3’s Security Scanner API to block risky packages at install time and enforce your organization’s policies in local dev and CI.
Research
The Socket Threat Research Team is tracking weekly intrusions into the npm registry that follow a repeatable adversarial playbook used by North Korean state-sponsored actors.