fsdb (File System Database)
Simple NodeJS File System Database.
Install
npm install amb-fsdb
Getting Started
const fsdb = require("amb-fsdb");
const db = await fsdb();
await db("players").create({
name: "ambratolm",
level: 99,
inventory: ["sword", "shield", "potion"]
});
const players = await db("players").read();
console.log(players);
A database JSON file named db.json will be created in the current directory with the following content:
{
"players": [
{
"name": "ambratolm",
"level": 99,
"inventory": ["sword", "shield", "potion"],
"id": "c8256c53-44ba-4ef4-bc89-e6e9a400fc0c",
"createdAt": "2020-09-28T02:01:39.064Z"
}
]
}
The Id and createdAt fields are created automatically.
Initialize
const db = await fsdb();
const db = await fsdb("my-database-file");
const db = await fsdb("my-database-file", "../some/directory");
CREATE
await db(collectionName).create(item, options);
Creates a new item in a collection.
If the specified collection doesn’t exist it will be created automatically.
The created item is returned.
| Parameter |
Type |
Default |
Description |
| collectionName |
string |
|
Targeted collection |
| item |
object |
|
Item to create |
| options |
object |
{} |
Additional options |
| @returns |
object |
|
The created item |
| @throws |
Error |
|
If a unique field value is already used |
| @throws |
Error |
|
If a value to encrypt is not a string |
CREATE Examples
const createdPlayer = await db("players").create({
name: "ambratolm",
level: 99,
inventory: ["sword", "shield", "potion"]
});
const createdPlayer = await db("players").create(
{
name: "ambratolm",
level: 99,
inventory: ["sword", "shield", "potion"],
password: "this_is_a_secret"
},
{
unique: ["name"],
encrypt: ["password"],
omit: ["password", "level"],
nocase: true
}
);
CREATE Queries
No Queries.
CREATE Options
| Propperty |
Type |
Default |
Description |
| unique |
string[] |
[] |
Fields to make unique |
| encrypt |
string[] |
[] |
Fields to encrypt |
| pick |
string[] |
[] |
Fields to pick in returned item |
| omit |
string[] |
[] |
Fields to omit in returned item |
| nocase |
boolean |
false |
Ignore case in search |
READ
await db(collectionName).read(query, options);
Reads an existing item in a collection.
If the specified collection doesn’t exist it will be created automatically.
The read item is returned.
| Parameter |
Type |
Default |
Description |
| collectionName |
string |
|
Targeted collection |
| query |
object |
|
Query object or function |
| options |
object |
{} |
Additional options |
| @returns |
object |
|
The read item |
| @throws |
Error |
|
If an encrypted field not matched |
READ Examples
const players = await db("players").read();
const somePlayers = await db("players").read({ name: "ambratolm" });
const someOtherPlayers = await db("players").read(player => player.level >= 90);
const player = await db("players").read(
{ name: "AmBrAtOlM" },
{
nocase: true,
one: true
}
);
READ Queries
…
READ Options
…
UPDATE
await db(collectionName).update(query, changes, options);
Updates an existing item in a collection.
If the specified collection doesn’t exist it will be created automatically.
The updated item is returned.
| Parameter |
Type |
Default |
Description |
| collectionName |
string |
|
Targeted collection |
| query |
object |
|
Query object or function |
| changes |
object |
|
Changes to apply |
| options |
object |
{} |
Additional options |
| @returns |
object |
|
The updated item |
| @throws |
Error |
|
If Items matching query not found |
| @throws |
Error |
|
If a unique field value is already used |
| @throws |
Error |
|
If a value to encrypt is not a string |
UPDATE Examples
const updatedPlayer = await db("players").update(
{ name: "ambratolm" },
{ name: "new name", level: 0 }
);
const updatedPlayer = await db("players").update(
{ name: "ambratolm" },
{ name: "new name", level: 0 },
{
}
);
UPDATE Queries
…
UPDATE Options
…
DELETE
await db(collectionName).delete(query, options);
Deletes an existing item in a collection.
If the specified collection doesn’t exist it will be created automatically.
The deleted item is returned.
| Parameter |
Type |
Default |
Description |
| collectionName |
string |
|
Targeted collection |
| query |
object |
|
Query object or function |
| options |
object |
{} |
Additional options |
| @returns |
object |
|
The deleted item |
| @throws |
Error |
|
If Items matching query not found |
DELETE Examples
const deletedPlayer = await db("players").delete(
{ name: "ambratolm" }
);
const deletedPlayer = await db("players").delete(
{ name: "ambratolm" },
{
}
);
DELETE Queries
…
DELETE Options
…
Other
await db.drop();
await db("players").drop();
await db("players").clear();
评论