7.0.108 • Published 4 months ago

larvitfiles v7.0.108

Weekly downloads
4
License
ISC
Repository
github
Last release
4 months 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.98

8 months ago

7.0.99

8 months ago

7.0.97

8 months ago

7.0.104

5 months ago

7.0.105

5 months ago

7.0.106

5 months ago

7.0.107

4 months ago

7.0.108

4 months ago

7.0.100

7 months ago

7.0.101

6 months ago

7.0.102

6 months ago

7.0.103

6 months ago

7.0.96

9 months ago

7.0.95

9 months ago

7.0.89

11 months ago

7.0.92

10 months ago

7.0.93

9 months ago

7.0.90

10 months ago

7.0.91

10 months ago

7.0.94

9 months ago

7.0.78

1 year ago

7.0.79

1 year ago

7.0.77

1 year ago

7.0.81

1 year ago

7.0.82

1 year ago

7.0.80

1 year ago

7.0.87

11 months ago

7.0.88

11 months ago

7.0.85

12 months ago

7.0.86

12 months ago

7.0.83

1 year ago

7.0.84

1 year ago

7.0.76

1 year ago

7.0.75

1 year ago

7.0.74

1 year ago

7.0.73

1 year ago

7.0.72

1 year ago

7.0.71

1 year ago

7.0.70

1 year ago

7.0.69

1 year ago

7.0.68

1 year ago

7.0.67

1 year ago

7.0.66

1 year ago

7.0.65

1 year ago

7.0.64

1 year ago

7.0.63

2 years ago

7.0.62

2 years ago

7.0.61

2 years ago

7.0.60

2 years ago

7.0.57

2 years ago

7.0.58

2 years ago

7.0.59

2 years ago

7.0.56

2 years ago

7.0.54

2 years ago

7.0.55

2 years ago

7.0.52

2 years ago

7.0.53

2 years ago

7.0.50

2 years ago

7.0.51

2 years ago

7.0.49

2 years ago

7.0.45

2 years ago

7.0.46

2 years ago

7.0.43

2 years ago

7.0.44

2 years ago

7.0.42

2 years ago

7.0.47

2 years ago

7.0.48

2 years ago

7.0.38

2 years ago

7.0.39

2 years ago

7.0.41

2 years ago

7.0.40

2 years ago

7.0.29

2 years ago

7.0.27

3 years ago

7.0.28

2 years ago

7.0.34

2 years ago

7.0.35

2 years ago

7.0.32

2 years ago

7.0.33

2 years ago

7.0.30

2 years ago

7.0.31

2 years ago

7.0.36

2 years ago

7.0.37

2 years ago

7.0.23

3 years ago

7.0.24

3 years ago

7.0.21

3 years ago

7.0.22

3 years ago

7.0.20

3 years ago

7.0.25

3 years ago

7.0.26

3 years ago

7.0.18

3 years ago

7.0.19

3 years ago

7.0.16

3 years ago

7.0.17

3 years ago

7.0.13

3 years ago

7.0.14

3 years ago

7.0.15

3 years ago

7.0.8

3 years ago

7.0.7

3 years ago

7.0.6

3 years ago

7.0.5

3 years ago

7.0.9

3 years ago

7.0.4

3 years ago

7.0.12

3 years ago

7.0.10

3 years ago

7.0.11

3 years ago

7.0.3

3 years ago

7.0.2

3 years ago

7.0.1

3 years ago

7.0.0

3 years ago

6.0.0

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.1.1

6 years ago

4.1.0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.4.7

6 years ago

3.4.6

6 years ago

3.4.5

7 years ago

3.4.4

7 years ago

3.4.3

7 years ago

3.4.2

7 years ago

3.4.1

7 years ago

3.4.0

7 years ago

3.3.1

7 years ago

3.2.5

7 years ago

3.2.4

7 years ago

3.2.3

7 years ago

3.2.2

7 years ago

3.2.0

7 years ago

3.1.1

7 years ago

3.1.0

8 years ago

3.0.6

8 years ago

3.0.5

8 years ago

3.0.4

8 years ago

3.0.3

8 years ago

3.0.2

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.2.0

8 years ago

2.1.0

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago