7.0.76 • Published 16 days ago

larvitfiles v7.0.76

Weekly downloads
4
License
ISC
Repository
github
Last release
16 days ago

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.
});
7.0.76

16 days ago

7.0.75

1 month ago

7.0.74

1 month ago

7.0.73

1 month ago

7.0.72

2 months ago

7.0.71

2 months ago

7.0.70

2 months ago

7.0.69

3 months ago

7.0.68

3 months ago

7.0.67

3 months ago

7.0.66

4 months ago

7.0.65

4 months ago

7.0.64

4 months ago

7.0.63

5 months ago

7.0.62

5 months ago

7.0.61

5 months ago

7.0.60

6 months ago

7.0.57

6 months ago

7.0.58

6 months ago

7.0.59

6 months ago

7.0.56

7 months ago

7.0.54

7 months ago

7.0.55

7 months ago

7.0.52

8 months ago

7.0.53

8 months ago

7.0.50

8 months ago

7.0.51

8 months ago

7.0.49

9 months ago

7.0.45

11 months ago

7.0.46

11 months ago

7.0.43

12 months ago

7.0.44

12 months ago

7.0.42

12 months ago

7.0.47

11 months ago

7.0.48

10 months ago

7.0.38

1 year ago

7.0.39

1 year ago

7.0.41

1 year ago

7.0.40

1 year ago

7.0.29

1 year ago

7.0.27

1 year ago

7.0.28

1 year ago

7.0.34

1 year ago

7.0.35

1 year ago

7.0.32

1 year ago

7.0.33

1 year ago

7.0.30

1 year ago

7.0.31

1 year ago

7.0.36

1 year ago

7.0.37

1 year ago

7.0.23

1 year ago

7.0.24

1 year ago

7.0.21

1 year ago

7.0.22

1 year ago

7.0.20

1 year ago

7.0.25

1 year ago

7.0.26

1 year ago

7.0.18

2 years ago

7.0.19

2 years ago

7.0.16

2 years ago

7.0.17

2 years ago

7.0.13

2 years ago

7.0.14

2 years ago

7.0.15

2 years ago

7.0.8

2 years ago

7.0.7

2 years ago

7.0.6

2 years ago

7.0.5

2 years ago

7.0.9

2 years ago

7.0.4

2 years ago

7.0.12

2 years ago

7.0.10

2 years ago

7.0.11

2 years ago

7.0.3

2 years ago

7.0.2

2 years ago

7.0.1

2 years ago

7.0.0

2 years ago

6.0.0

5 years ago

5.0.2

5 years ago

5.0.1

5 years ago

5.0.0

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.4.7

5 years ago

3.4.6

5 years ago

3.4.5

6 years ago

3.4.4

6 years ago

3.4.3

6 years ago

3.4.2

6 years ago

3.4.1

6 years ago

3.4.0

6 years ago

3.3.1

6 years ago

3.2.5

6 years ago

3.2.4

6 years ago

3.2.3

6 years ago

3.2.2

6 years ago

3.2.0

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.6

6 years ago

3.0.5

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago