Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
dynamic-record
Advanced tools
Readme
Dynamic Record is a database abstraction package for node.js that is inspired by Ruby on Rails' ActiveRecord for node.js. We aim to support most major database systems including MongoDB, MySQL, PostgreSQL and many more. For the moment there is only support for MongoDB
Note: Dynamic Record is currently in Beta and should not be considered production ready.
npm install dynamic-record
npx dynamic-record init
.npx dynamic-record init
by providing flags and arguments that can be found using npx dynamic-record init --help
.database_host
needs to be set in the format of [schema]://[username]:[password]@[host]:[port]/[database]
(eg. mongodb://username:password@localhost:27017/my_blog
) when running DynamicRecord. You can pass a -e
flag to npx dynamic-record init
to automatically write this setting to a .env
file which will be read by DynamicRecord.Although the API is more or less as how we wanted it to be, changes can still occur. Documentation of the API is available at https://dynamic-record.js.org/
A database initialized with the included dynamic-record
tool is enough to get started with a user that has readWrite
access.
Next we look at Dynamic Record in a bit more detail. Dynamic Record is split into three main parts:
Before diving into the individual parts, we need to explain a few concepts.
First is that for each table (or collection if you are using MongoDB) in your database, you will create an instance of DynamicRecord
and it will be responsible for handling all read write operations to the database. Dynamic Record uses a single pooled connection to the database for the moment.
Next is the idea of models and collections. If you have used an MVC framework such as Backbone before, you will be familiar with this concept. We are borrowing quite directly from the concept in Backbone where models are wrapped data objects and collections are an array-like object that contains multiple entries of models. This corresponds to DynamicRecord.Model
and DynamicCollection
respectively. These instances handle write operations to the database.
Finally for each table in the database they will have their own schema defined with JSON Schema. They will be saved in a table named _schema
and will mainly be used for validation and in the case of NoSQL database, schema definition. They are mainly accessed by DynamicSchema
instances.
DynamicSchema
Before creating any entries in the database with Dynamic Record, you will first need to define the tables that are going to be used.
In your app, you can create an instance of DynamicSchema
as follow.
const DynamicSchema = require("dynamic-record").DynamicSchema;
const mySchema = new DynamicSchema();
This will create an empty instance of DynamicSchema
but it will do nothing else. To create a table in the database with a set schema, call createTable()
on the instance and provide to it a parameter in the form of a JSON Schema object.
const monarchSchema = require("./monarch.schema.json");
mySchema.createTable(monarchSchema).then(() => {
// Schema created in database
});
DynamicRecord
This is the main entry point to the ORM. You can initialize an instance of DynamicRecord
with the following snippet.
const DynamicRecord = require("dynamic-record");
const myTable = new DynamicRecord({tableSlug: "myTableSlug"});
With the created instance you can fetch a particular entry from the database by providing a query object to match with findBy()
.
myTable.findBy({name: "Henry V"}).then((entry) => {
console.log(entry.data);
});
DynamicRecord.Model
DynamicRecord.Model
is a wrapper for the objects returned by any query function under DynamicRecord
, it is also used to create new models to be inserted into the database. To create a new instance of DynamicRecord.Model
use the following snippet.
const DynamicRecord = require("dynamic-record");
const myTable = new DynamicRecord({tableSlug: "myTableSlug"});
const newKing = new myTable.Model({
name: "Henry VI",
house: "York",
coronation: "6 November 1429"
});
At this point although the instance is created and populated with data, it is not saved into the database yet. You can still modify it further by manipulating the data
property of the instance.
newKing.data.house = "Lancaster";
When you are ready to save the model into the database, simply call save()
on the instance.
newKing.save().then(() => {
// Model saved
});
DynamicCollection
DynamicCollection
is usually returned by query functions of a DynamicRecord
instance that returns more than one entry such as where()
. You can also create your own DynamicCollection
instance for easy manipulation of a series of models.
const DynamicCollection = require("dynamic-record").DynamicCollection;
const myCollection = new DynamicCollection();
DynamicCollection
extends the native Javascript Array object and thus inherits all of the array functions including push()
, forEach()
and map()
. The difference is that you should only store DynamicRecord.Model
instances in a DynamicCollection
.
DynamicCollection
has a property called data
that will return all the models' data objects in a native Javascript array.
After modifying the models on the DynamicCollection
instance and you want to save all the changes into the database, instead of calling save()
on each of the models individually, you can call saveAll()
on the DynamicCollection
instance.
myCollection.saveAll().then(() => {
// All models in collection saved to database
});
Clone the repo locally
Run npm install
To build the library, run npm run build
To run the test:
node tools/cli.js init
to set up the environment as documented above, or you can write the following to a .env
file manually (changing value where appropriate)database_host=mongodb://localhost:27017
database_name=dynamic_record
database_username=test_user
database_password=test_user
Run npm test
To build the documentation, run npm run docs
(test must be setup)
FAQs
A bare minimum Javascript implementation of the Active Record pattern
We found that dynamic-record demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).