1.4.0 • Published 2 years ago
@datasco/memorydb v1.4.0
MemoryDB
In-memory typesafe database with Queries, Events, Analytics.
Installation
npm i @datasco/memorydb # pnpm add @datasco/memorydbUsage
You can use MemoryDB for creating in-memory database with any type of data.
Creating instance of database
Primitive database:
const db = new MemoryDB<string>("primitives_database")Typed object-based database:
const db = new MemoryDB<{
name: string;
price: number;
}>("complex_database")Insert row
db.insert({ name: "Bear toy", price: 1000 })Insert multiple rows
db.insert([
{ name: "Cat toy", price: 2000 },
{ name: "Dog toy", price: 3000 },
])Mapping rows
db.map(
row => ({ ...row, price: price + 100 })
)Remove rows
db.remove(
row => row.price > 1000
)Remove all rows
db.clear()Remove duplicates with predicate
// remove rows, where value of "price" is same
db.removeDuplicatesByPredicate(
(duplicateRows) => [ duplicateRows[0] ],
"price"
)Remove duplicates (primitive)
db.removeDuplicates()Remove column
db.removeColumn("price")Splitting into chunks
// split database into chunks of size 5
db.chunks(5)Merge with another database
db.merge(new MemoryDB("another_db"))Listing rows
const { data } = db.list()Listing rows, but paginated
// 1 page, 50 rows per page
const { data } = db.listPaginated(1, 50)Find row
const { data } = db.find(
row => row.name === "Bear toy"
)Search row
const { data } = db.search(
row => row.name.includes("toy")
)Chain multiple operations
db.chain([
_ => _.insert({ name: "Test", price: 7500 }),
_ => _.map(row => {...row, price: price + 500 }),
_ => _.map(row => {...row, name: name + " toy" })
])(Analytics) Use Analytics API
const {analytics} = db(Analytics) Display contents of table
analytics.head()