1.20.4 • Published 4 days ago

@onehat/data v1.20.4

Weekly downloads
237
License
MIT
Repository
github
Last release
4 days ago

Overview

@onehat/data A robust ORM for Javascript. Can CRUD, search, sort, filter, paginate your data. Integrates with many front- and back-end storage mediums.

  • Repositories. A Repository stores many Entities in a storage medium. Corresponds to a database table. Repositories can sort, search/filter, and add/edit/delete their constituent Entities.
  • Storage Mediums. Repositories are specialized to store their data in a single type of storage medium. Available types of Repositories include: Memory, Ajax, Rest, LocalStorage (Browser), SessionStorage (Browser), IndexedDB (Browser), AsyncStorage (React Native/Expo), SecureStore (React Native/Expo). One special type of Repository—LocalFromRemote—combines two different Repository types (one local and one remote) into a single Repository, thereby allowing autosyncing between the local and remote repositories, enabling true offline-first capability.
  • Entities. An Entity is a single record of data, organized into properties. Corresponds to a database row. Entity data can be accessed directly (entity.username), via specific properties and their formatted values (entity.properties.username.displayValue), or by obtaining a JS object of the whole Entity (entity.getDisplayValues(), or entity.getSubmitValues()).
  • Properties. A Property is a single unit of data. Corresponds to a database field. Properties are differentiated into different Property types (e.g. Integer, String, Boolean, etc), and thereby allow for easy formatting of "display" or "submit" values. For example, a date might be set to display as "Wed, Feb 5, 2020" but submit as "2020-02-05".
  • Schemas. A Schema defines the configuration of a Repository. Corresponds roughly to the database table schema. The Schema defines the name and type of Repository, the Properties that exist, and which are "id" and "display" Properties.

Install

npm i @onehat/data

Usage

Comprehensive unit tests can be found in ./cypress/integration. These are an excellent source of code examples. Comprehensive API documentation can be found in ./docs.

1. Define a Schema

For every type of Entity you will use (e.g. Users or Animals or Invoices), define a Schema. A Schema determines the various Properties that each Entity will have, as well as the medium where the Entities will be stored.

const Users = {
	name: 'Users',
	model: {
		idProperty: 'id',
		displayProperty: 'username',
		properties: [
			{ name: 'id', type: 'int', },
			{ name: 'username', type: 'string', }, // explicitly set property type
			{ name: 'password', }, // type: 'string' is assumed, if not explicitly set
			{ name: 'first_name', },
			{ name: 'last_name', },
			{ name: 'email', allowNull: false, }, // make it a required field
			{ name: 'last_login', type: 'datetime', defaultValue: 'now', }, // give it a default value.
		],
		sorters: [
			{
				name: 'last_name',
				direction: 'ASC',
			},
			{
				name: 'first_name',
				direction: 'ASC',
			},
		],
	},
	repository: 'memory', // Repository type. Can be string name or config object
};
export default Users;

Every Property must have a unique name. All other attributes are optional. Common Property attributes include:

  • name - The name of the Property
  • type - The type of the Property (e.g. 'string', 'bool', 'int', etc)
  • allowNull - Is this Property required to have a value?
  • defaultValue - Default value for this Property if none is supplied
  • isSortable - Whether this Property type is sortable

Other Property attributes exist and can be found in the API.

2. Create a Repository

The easiest way to create one or more Repositories is to use the global oneHatData singleton object. Each schema will have a bound repository of the same name (e.g. "Users", or "Groups").

import oneHatData from '@onehat/data';
import Groups from './Groups';
import Users from './Users';

oneHatData
	.createSchemas([
		Groups,
		Users,
	])
	.createBoundRepositories()
	.then(() => {
		setIsReady(true);
		
		const UsersRepository = oneHatData.getRepository('Users');

		// Do something with your data

	});

3. Add / Edit / Delete an Entity

Once you have a Repository initialized, you can start adding data to it. Data is manipulated asynchronously, so you may optionally wait for it to complete.

const UsersRepository = oneHatData.getRepository('Users');

// 1. Add an Entity
const userEntity = await UsersRepository.add({
	username: 'ajones',
	password: '12345',
	first_name: 'Alice',
	last_name: 'Jones',
	email: 'alice@example.com',
});


// 2. Edit an Entity
// Use assignment to change the value of a particular Property.
userEntity.password = 'mypass';

// Or you can be more verbose about it
userEntity.getProperty('password').setValue('mypass');


// 3. Delete an Entity
userEntity.delete();

// Or delete it from the Repository
await UsersRepository.delete(userEntity);

4. Filter and Sort the Entities in a Repository

There are lots of filtering and sorting methods available on Repositories.

// Add a single filter
UsersRepository.filter('first_name', 'Alice');
const foundEntities = UsersRepository.entities;

// Or search by an id or function
const myEntity = UsersRepository.getById(1);
const results = UsersRepository.getBy((entity) => {
	return entity.id > 2;
});

// Sort the entities by a particular Property
UsersRepository.sort('last_name', 'DESC');
const sortedEntities = UsersRepository.entities;

5. Listen for events and respond to them

Repositories, Entities, and Properties emit many different kinds of events.

// The 'change' event, emitted from an Entity, is relayed through the Repository and becomes 'entity_change'
UsersRepository.on('entity_change', (entity) => {
	console.log('changed entity');
});
userEntity.first_name = 'Joe';
// prints 'changed entity' to console


// The 'changeData' event is fired from the Repository after multiple Entities are loaded at once
UsersRepository.on('changeData', (entities) => {
	console.log('entities changed');
});
UsersRepository.load([
	{ email: 'alice@example.com' },
	{ email: 'bob@example.com' },
	{ email: 'charlie@example.com' },
]);
// prints 'entities changed' to console
1.20.4

4 days ago

1.20.3

15 days ago

1.20.1

2 months ago

1.20.2

2 months ago

1.20.0

2 months ago

1.19.41

3 months ago

1.19.40

3 months ago

1.19.39

3 months ago

1.19.38

3 months ago

1.19.36

3 months ago

1.19.37

3 months ago

1.19.35

3 months ago

1.19.30

4 months ago

1.19.31

4 months ago

1.19.32

4 months ago

1.19.33

4 months ago

1.19.28

4 months ago

1.19.29

4 months ago

1.19.27

4 months ago

1.19.26

5 months ago

1.19.25

5 months ago

1.19.24

5 months ago

1.18.9

9 months ago

1.18.8

9 months ago

1.18.7

9 months ago

1.19.0

8 months ago

1.19.4

7 months ago

1.19.3

8 months ago

1.19.2

8 months ago

1.19.1

8 months ago

1.19.8

7 months ago

1.19.7

7 months ago

1.19.6

7 months ago

1.19.5

7 months ago

1.19.9

6 months ago

1.19.12

6 months ago

1.19.13

6 months ago

1.19.10

6 months ago

1.19.11

6 months ago

1.19.16

6 months ago

1.19.17

6 months ago

1.19.14

6 months ago

1.19.15

6 months ago

1.19.18

5 months ago

1.19.19

5 months ago

1.19.20

5 months ago

1.19.21

5 months ago

1.19.22

5 months ago

1.18.12

8 months ago

1.18.11

8 months ago

1.18.10

8 months ago

1.18.13

8 months ago

1.18.1

10 months ago

1.18.0

10 months ago

1.18.4

10 months ago

1.18.3

10 months ago

1.18.2

10 months ago

1.16.10

11 months ago

1.16.9

12 months ago

1.17.2

11 months ago

1.17.0

11 months ago

1.17.5

10 months ago

1.17.3

11 months ago

1.14.1

1 year ago

1.15.0

1 year ago

1.15.4

1 year ago

1.15.3

1 year ago

1.15.2

1 year ago

1.15.1

1 year ago

1.15.7

1 year ago

1.15.6

1 year ago

1.15.5

1 year ago

1.16.3

1 year ago

1.16.2

1 year ago

1.16.1

1 year ago

1.16.0

1 year ago

1.16.7

1 year ago

1.16.6

1 year ago

1.16.8

1 year ago

1.14.0

1 year ago

1.12.0

1 year ago

1.13.2

1 year ago

1.13.1

1 year ago

1.13.0

1 year ago

1.13.6

1 year ago

1.13.5

1 year ago

1.13.4

1 year ago

1.13.3

1 year ago

1.10.5

1 year ago

1.10.4

1 year ago

1.10.3

1 year ago

1.10.2

1 year ago

1.11.4

1 year ago

1.11.3

1 year ago

1.11.2

1 year ago

1.11.1

1 year ago

1.11.6

1 year ago

1.11.5

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.10.1

1 year ago

1.10.0

1 year ago

1.8.26

1 year ago

1.8.27

1 year ago

1.8.28

1 year ago

1.8.29

1 year ago

1.8.30

1 year ago

1.8.31

1 year ago

1.8.32

1 year ago

1.8.33

1 year ago

1.8.34

1 year ago

1.8.35

1 year ago

1.9.3

1 year ago

1.9.2

1 year ago

1.11.0

1 year ago

1.8.20

1 year ago

1.8.21

1 year ago

1.8.22

1 year ago

1.8.23

1 year ago

1.8.24

1 year ago

1.8.25

1 year ago

1.8.19

1 year ago

1.8.13

2 years ago

1.8.14

2 years ago

1.8.15

2 years ago

1.8.16

2 years ago

1.8.17

2 years ago

1.8.18

2 years ago

1.8.9

2 years ago

1.8.10

2 years ago

1.8.8

2 years ago

1.8.11

2 years ago

1.8.7

2 years ago

1.8.12

2 years ago

1.7.16

2 years ago

1.8.2

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.8.6

2 years ago

1.8.5

2 years ago

1.8.4

2 years ago

1.8.3

2 years ago

1.7.15

2 years ago

1.6.4

2 years ago

1.6.3

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.7.10

2 years ago

1.7.11

2 years ago

1.7.12

2 years ago

1.7.14

2 years ago

1.7.9

2 years ago

1.7.8

2 years ago

1.7.7

2 years ago

1.7.6

2 years ago

1.7.5

2 years ago

1.7.4

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.6.9

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.4.11

2 years ago

1.6.5

2 years ago

1.4.10

2 years ago

1.4.13

2 years ago

1.4.12

2 years ago

1.4.14

2 years ago

1.6.11

2 years ago

1.6.10

2 years ago

1.6.13

2 years ago

1.6.12

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.4.9

2 years ago

1.4.8

2 years ago

1.4.7

2 years ago

1.4.6

2 years ago

1.4.5

2 years ago

1.4.4

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.3

2 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.9

3 years ago

1.1.8

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.10

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.13.6

3 years ago

0.13.5

3 years ago

0.13.4

3 years ago

0.13.3

3 years ago

0.13.2

3 years ago

0.13.0

3 years ago

0.13.1

3 years ago

0.12.5

3 years ago

0.12.4

4 years ago

0.12.3

4 years ago

0.12.2

4 years ago

0.12.1

4 years ago

0.12.0

4 years ago

0.11.9

4 years ago

0.11.8

4 years ago

0.11.7

4 years ago

0.11.5

4 years ago

0.11.6

4 years ago

0.11.2

4 years ago

0.11.3

4 years ago

0.11.4

4 years ago

0.11.1

4 years ago

0.11.0

4 years ago

0.10.3

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.13

4 years ago

0.8.12

4 years ago

0.8.11

4 years ago

0.8.10

4 years ago

0.8.9

4 years ago

0.8.5

4 years ago

0.8.7

4 years ago

0.8.6

4 years ago

0.8.4

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago