npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

larvitfiles

v7.0.99

Published

Storage of files with an API and database to use in web environments

Downloads

723

Readme

Build Status

larvitfiles

Installation

npm i larvitfiles;

Usage

Load library

const FileLib = require('larvitfiles');
const db = require('larvitdb');
const fs = require('fs');

db.setup(conf); // Only needed once per script. See https://github.com/larvit/larvitdb for details

const fileLib = new FileLib({
	db: db,
	storagePath: '/tmp/larvitfiles',

	// All below settings are optional, and their default is whats shown here
	log: new (new (require('larvitutils'))).Log(),
	prefix: '/dbfiles/'
});

await fileLib.ready(); // Not needed to run actions, but no action will start until this is returning true

Add file from disk

fs.readFile('/some/file.txt', function (err, data) {
	let	file;

	if (err) throw err;

	const file = await fileLib.save({
		slug: 'slug/foo/bar.txt',
		data: data,
		metadata: {metadata1: 'metavalue1', metadata2: ['multiple', 'values']}, // optional, will erase previous metadata if left blank
		//uuid: uuid() - optional
	});

	console.log('file saved with uuid: ' + file.uuid);
	console.log('metadata: ' + JSON.stringify(file.metadata));
	console.log('slug: ' + file.slug);
});

Update file on disk

By default .save() will not accept a duplicate slug without also supplying a matching uuid.

If the below script is ran when a file with the slug "slug/foo/bar.txt" already exists in the database, this will throw an error.

const file = await fileLib.save({
	slug: 'slug/foo/bar.txt',
	data: Buffer.from('någe')
});

To overwrite the existing file, on the same uuid, use option "updateMatchingSlug":

const file = await fileLib.save({
	slug: 'slug/foo/bar.txt',
	data: Buffer.from('någe'),
	updateMatchingSlug: true // Defaults to false
});

Get file from storage

const file = await fileLib.get({slug: 'slug/foo/bar.txt'});

// or

const file = await fileLib.get({uuid: 'uuid of file'});

console.log('file saved with uuid: ' + file.uuid);
console.log('metadata: ' + JSON.stringify(file.metadata));
console.log('slug: ' + file.slug);
// file data in file.data

Remove a file from storage

fileLib.rm(await fileLib.uuidFromSlug('slog/foo/bar.txt'));
console.log('File is now removed from storage');

List files in storage

List all files

const files = await fileLib.list();
console.log(result); // Array of objects with uuid, slugs and metadata, but NOT file data as values.

Filter list based on metadata

// This will only return files with metadata
// 1) "foo" = "bar" (and possibly other values as well)
// and
// 2) "zoo" = anything
const options = {
	filter: {
		metadata: {
			foo: 'bar',
			zoo: true
		},
		operator: 'and' // or 'or'. 'and' is default
	}
};

const files	= await fileLib.list(options);
console.log(files); // Array of objects with uuid, slugs and metadata, but NOT file data as values.

And if several values should exist on a single metadata do this:

// This will only return files with metadata
// 1) "foo" = "bar" (and possibly other values as well)
// and
// 2) "foo" = "baz" (and possibly other values as well)
const options = {
	filter: {
		metadata: {
			foo: ['bar', 'baz']
		}
	}
};

const files	= await fileLib.list(options);
console.log(files); // Array of objects with uuid, slugs and metadata, but NOT file data as values.
});