Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
mysql-dynamo
Advanced tools
A solution to use the `simple-dynamo` interface with a MySQL database. So you can create a offline version of a AWS-DynamoDB project.
A solution to use the simple-dynamo interface with a MySQL database. So you can create a offline version of a AWS-DynamoDB project.
Written in coffee-script
INFO: all examples are written in coffee-script
npm install mysql-dynamo
simple-dynamo
Compared to the simple-dynamo
module you only have to make a few smears.
consistent
not existes and will be ignoredoverwriteExistingHash
not exists. It always will NOT overwrite existing hashescombineTableTo
not exists. Within MySQL there is no need to save tableslength
The length of a field has to be defineddefault
The SQL default of a field could be definedconsistent
not existes and will be ignoredoverwriteExistingHash
not exists. It always will NOT overwrite existing hashesOne big difference between the Dynamo and MySQL version is that dynamo can generate undefined attributes on the fly. Th MySQL version will ignore undefined attributes. So all necessary attributes has to be defined within the table definitions
first you have to define the connection and table attributes and get an instance of the simple-dynamo interface.
new MySqlDynamo( connectionSettings, tables )
###connection Settings
String
optional ) Option to prefix all generating tablesMySQL connection settings
String
required ) MySQL hostString
optional: default = root
) MySQL userString
optional: default = secret
) MySQL passwordString
optional: default = database
) Database name###table Definition
An Object of Tables.
The key you are using will be the key to get the table object.
Every object can have the following keys:
String
required )String
required )String
optional: default = S
)hashKey
. Possible values are: S
= String and N
= NumericNumber
optional: default = 5
for hashKeyType N
and 255
for hashKeyType S
)String
optional: default = N
)rangeKey
. Possible values are: S
= String and N
= NumericNumber
optional: default = 5
for rangeKeyType N
and 255
for rangeKeyType S
)Function
optional: default = new UUID
)Array
optional )options.fields
.cb( "my-special-hash" )
Function
optional: default = current Timestamp
)cb( "my-special-range" )
Array of Objects
required )String
required )String
required )string
= String, number
= Numeric and array
= Array/Set of StringsBoolean
optional: default = false
)Number
optional: default = 255
)type
= string
with a length
= +Infinity
the field will generated as type TEXT
otherwise as VARCHAR( length )
. If you define a number
with a length > 11 a type BIGINT( length )
will be used. On a length <= 11 a type INT( length )
will be generated.String
optional: default = `` )Example
# import module
MySQLDynamo = require "mysql-dynamo"
# define connection settings
connectionSettings =
host: "localhost"
user: "root"
password: "root"
database: "myDB"
# define tables
tables =
"Users":
name: "users"
hashKey: "id"
attributes: [
{ key: "name", type: "string", required: true }
{ key: "email", type: "string" }
]
"Todos":
name: "todos"
hashKey: "id"
rangeKey: "_t"
rangeKeyType: "N"
fnCreateHash: ( attributes, cb )=>
cb( attributes.user_id )
return
attributes: [
{ key: "title", type: "string", required: true }
{ key: "done", type: "number" }
]
# create instance
sqldManager = new MySQLDynamo( connectionSettings, tables )
# connect
sqldManager.connect ( err )->
console.log( "mysql-dynamo ready to use" )
The module has to know about the existing SQL tables so you have to read them first.
If you do not run .connect()
the module will throw an error everytime
Manager.connect( fnCallback )
Arguments :
Function
required )null
Example
sqldManager.connect ( err )->
if err
console.error( "connect ERROR", err )
else
console.log( "mysql-dynamo ready to use" )
to create all missing tables just call .createAll()
.
This is not necessary if you know the tables has been created in the past.
Manager.generateAll( fnCallback )
Arguments :
Function
required )null
Example
sqldManager.generateAll ( err )->
if err
console.error( "connect ERROR", err )
else
console.log( "mysql-dynamo ready to use" )
To interact with a table you have to retrieve the table object. It's defined in the table-definitions
Manager.get( 'tableName' )
Arguments :
String
required )Example
tblTodos = sqldManager.get( 'Todos' )
Loop trough all table objects
Manager.each( eachFn )
Arguments :
Function
required )Example
Manager.each ( tableKey, tableObj )=>
console.log( "SQL table name", tableObj.tableName )
return
destroy table in MySQL. This drops a table from MySQL will all the data
Table.destroy( fnCallback )
Arguments :
Function
required )null
. On an error a object with error
and msg
Example
tblTodos.del ( err )->
if err
console.error( "destroy ERROR", err )
else
console.log( "table destroyed" )
Create a new item in a select table. You can also add some attributes not defined in the table-definition, which will be saved, too.
Table.set( data, options, fnCallback )
Arguments :
Object
required )name
out of the table-config in front of every hash.Object
optional )
Array
) An array of fields to receiveFunction
required )null
. On an error a object with error
and msg
Example
data =
title: "My First Todo"
done: 0
aditionalData: "Foo bar"
tblTodos.set data, ( err, todo )->
if err
console.error( "insert ERROR", err )
else
console.log( todo )
Get an existing element by id/hash
Table.get( id[, options], fnCallback )
Arguments :
String|Number|Array
required )[hash,range]
as combined id. Otherwise you will get an error.Object
optional )
Array
) An array of fields to receive. If nothing is defined all fields are returned.Function
required )null
. On an error a object with error
and msg
null
Example
tblTodos.get 'myTodoId', ( err, todo )->
if err
console.error( "get ERROR", err )
else
console.log( todo )
tblRangeTodos.get [ 'myHash', 'myRange' ], ( err, todo )->
if err
console.error( "get ERROR", err )
else
console.log( todo )
Get an many existing elements by id/hash in one request
Table.mget( [ id1, id2, .. ], options, fnCallback )
Arguments :
Array
required )[hash,range]
as combined id. Otherwise you will get an error.Object
optional )
Array
) An array of fields to receive. If nothing is defined all fields are returned.Function
required )null
. On an error a object with error
and msg
Example
tblTodos.mget [ 'myTodoIdA', 'myTodoIdB' ], ( err, todos )->
if err
console.error( "get ERROR", err )
else
console.log( todos )
tblRangeTodos.mget [ [ 'myHash', 1 ], [ 'myHash', 2 ] ], ( err, todos )->
if err
console.error( "get ERROR", err )
else
console.log( todos )
update an existing item. If you try to update a not existing item it will be created.
To remove a attribute you have to set the value to null
Table.set( id, data, options, fnCallback )
Arguments :
String|Number|Array
required )[hash,range]
as combined id. Otherwise you will get an error.Object
required )Object
optional )Array
) An array of fields to receiveObject
) A query object to define a conditional. Only {"==": value}
, {"==": null}
, and {"!=": null}
are allowed. How to build? … have a look at Jed's Predicates Function
required )null
. On an error a object with error
and msg
null
Example
data =
title: "My First Update"
done: 1
tblTodos.set 'myTodoId', data, ( err, todo )->
if err
console.error( "update ERROR", err )
else
# note. the key 'aditionalData' will be gone
console.log( todo )
delete an item by id/hash
Table.del( id, fnCallback )
Arguments :
String|Number|Array
required )[hash,range]
as combined id. Otherwise you will get an error.Function
required )null
. On an error a object with error
and msg
Example
tblTodos.del 'myTodoId', ( err )->
if err
console.error( "delete ERROR", err )
else
console.log( "delete done" )
run a query on a table. The module automatically trys to do a Dynamo.db scan
or Dynamo query
.
Table.find( query, startAt, options, fnCallback )
Arguments :
Object
: default = {}
all )String|Number|Array
optional )startAt
. Usually the last item of a list. If you define startAt
with the last item of the previous find you get the next collection of items without the given startAt
item.[hash,range]
as combined startAt
. Otherwise you will get an error.Object
optional )
Array
) An array of fields to receiveNumber
) Define the max. items to returnBoolean
default = true ) define the direction acs
or desc
for range querys.Function
required )null
. On an error a object with error
and msg
Example
tblTodos.find {}, ( err, items )->
if err
console.error( "delete ERROR", err )
else
console.log( "all existend items", items )
Advanced Examples
# create a query to read all todos from last hour
_query =
id: { "!=": null }
_t: { "<": ( Date.now() - ( 1000 * 60 * 60 ) ) }
tblTodos.find , ( err, items )->
if err
console.error( "delete ERROR", err )
else
console.log( "found items", items )
# read 4 todos from last hour beginning starting with a known id
_query =
id: { "!=": null }
_t: { "<": ( Date.now() - ( 1000 * 60 * 60 ) ) }
_startAt = "myid_todoItem12"
_options = { "limit": 4, "fields": [ "id", "_t", "title" ] }
tblTodos.find _query, _startAt, _options, ( err, items )->
if err
console.error( "delete ERROR", err )
else
console.log( "4 found items", items )
If a field is defined as number you can run simple operations like increase and decrese the value.
To do this you can pass the value of a number as
"key": { "$add": 1 }
to increase the number by 1"key": { "$rem": 1 }
to decrease the number by 1"key": { "$reset": 1 }
to set the number by 1Dynamo has the ability to work with sets. That means you can save a Set of Strings as an Array.
During an update you have the ability to add or remove a single value out of the set. Or you can reset the whole set.
But you can only perform one action per key and you only can use the functionality if defined through the table-definition ( type:"array"
).
Existing values will be ignored.
The following key variants are available:
"key":[ "a", "b", "c" ]'
: Resets the whole value of the key"key":{ "$add": [ "d", "e" ] }
: Add some values to the set"key":{ "$rem": [ "a", "b" ] }
: remove some values"key":{ "$reset": [ "x", "y" ] }
: reset the whole value. Same as "key":[ "x", "y" ]'
"key":{ "$add": "d"}
: Add a single value to the set"key":{ "$rem": "a" }
: remove a single value"key":{ "$reset": "y" }
: reset the whole set to a single value. Same as "key":[ "y" ]'
Examples
# Source "key: [ "a", "b", "c" ]"
data =
key: [ "x", "y", "z" ]
tblSets.set 'mySetsId', data, ( err, setData )->
# Result "key: [ "x", "y", "z" ]"
console.log( setData )
# Source "key: [ "a", "b", "c" ]"
data =
key: { "$add": [ "a", "d", "e" ] }
tblSets.set 'mySetsId', data, ( err, setData )->
# Result "key: [ "a", "b", "c", "d", "e" ]"
console.log( setData )
# Source "key: [ "a", "b", "c" ]"
data =
key: { "$rem": [ "a", "b", "x" ] }
tblSets.set 'mySetsId', data, ( err, setData )->
# Result "key: [ "c" ]"
console.log( setData )
# Source "key: [ "a", "b", "c" ]"
data =
key: { "$reset": [ "x", "y", "z" ] }
tblSets.set 'mySetsId', data, ( err, setData )->
# Result "key: [ "x", "y", "z" ]"
console.log( setData )
#Events
To provide a API to react on different events you can listen to a bunch of events.
##Manager Events
new-table
: Table object initialized and ready to use. This means only the client model is ready. Eventually you have to create the table first.Table
objecttable-generated
: Fired after all a new tables has been generated.
Event Arguments
all-tables-generated
: Fired after all tables are generated.##Table Events
create-status
: fired on table create.already-active
, waiting
, active
get
: fired after a table.get.get-empty
: fired after a table.get with an empty result.mget
: fired after a table.mget.mget-empty
: fired after a table.mget with no results.create
: fired after a item has been created.update
: fired after a item has been updated.delete
: fired after a item has been deleted.16. Sep. 2014
Fixed delete to return the correct data;
5. Sep. 2014
Small fix for escaping the keys; fixes for current dependencies;
07. May 2014
key
as fieldnames15. April 2014
18. Okt. 2013
combineTableTo
fields to tablename if it's defined18. Okt. 2013
14. Okt. 2013
14. Okt. 2013
7. June 2013
7. June 2013
7. June 2013
7. June 2013
tablePrefix
to prefix the sql table names.each()
6. June 2013
6. June 2013
mysql-node
version if an older version is already used within the project6. June 2013
6. June 2013
5. June 2013
mysql-dynamo
is work in progress. Your ideas, suggestions etc. are very welcome.
(The MIT License)
Copyright (c) 2010 TCS <dev (at) tcs.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A solution to use the `simple-dynamo` interface with a MySQL database. So you can create a offline version of a AWS-DynamoDB project.
The npm package mysql-dynamo receives a total of 63 weekly downloads. As such, mysql-dynamo popularity was classified as not popular.
We found that mysql-dynamo 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’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.