Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit 08d5ff9

Browse files
committed
Update README.md
1 parent da8a25c commit 08d5ff9

File tree

4 files changed

+342
-5
lines changed

4 files changed

+342
-5
lines changed

README.md

Lines changed: 334 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
JayData is a unified data access library for JavaScript to CRUD data from different sources like WebSQL/SQLite,
1414
IndexedDB, MongoDb, OData, HTML5 localStorage.
15-
The library can be integrated with KendoUI, Knockout.js, Handlebars.js or Sencha Touch 2 and can be used on
15+
The library can be integrated with React, Angular2, Durandal, KendoUI, Knockout.js, Handlebars.js or Sencha Touch 2 and can be used on
1616
Node.js as well. Check out the latest [JayData examples](https://github.com/jaystack/odata-v4-client-examples)
1717

1818
JayData not only provides JavaScript Language Query (JSLQ) syntax to access local (in-browser and mobile)
@@ -55,6 +55,338 @@ $ npm install
5555
$ gulp
5656
```
5757

58+
## Use JayData as a standalone global library
59+
60+
```html
61+
<script type="text/javascript" src="jaydata.min.js"></script>
62+
```
63+
64+
Providers are lazy loaded from the same location as the core JayData script under the ```jaydataproviders``` folder.
65+
If you want to use a JayData module, include it manually in a ```<script>``` tag.
66+
67+
## Use Jaydata with System.js
68+
69+
Using JayData with System.js needs a little bit of a setup and you have to map all providers you want to use in your application.
70+
71+
```javascript
72+
var map = {
73+
'jaydata/core': 'lib/jaydata/jaydata.min',
74+
'jaydata/odata': 'lib/jaydata/jaydataproviders/oDataProvider.min'
75+
};
76+
77+
var meta = {
78+
'jaydata/odata': {
79+
format: 'cjs',
80+
deps: ['jaydata/core']
81+
}
82+
};
83+
84+
var config = {
85+
map: map,
86+
meta: meta
87+
};
88+
89+
System.config(config);
90+
```
91+
92+
With this setup you can now import the ```jaydata/odata``` module in your application code. See a full example in Angular2 [here](https://github.com/jaystack/odata-v4-client-examples/tree/master/angular2-product-editor).
93+
94+
## Use JayData with Require.js
95+
96+
If you want to use Require.js to import JayData into your application, you need to set the path configuration correctly:
97+
98+
```javascript
99+
requirejs.config({
100+
paths: {
101+
'jaydata/core': '../lib/jaydata/jaydata.min',
102+
'jaydata/odata': '../lib/jaydata/jaydataproviders/oDataProvider.min',
103+
}
104+
});
105+
```
106+
107+
See a working example using Require.js and Durandal [here](https://github.com/jaystack/odata-v4-client-examples/tree/master/durandal-product-editor).
108+
109+
## JayData basics in 7 simple steps
110+
111+
In most scenarios, the 7 simple steps of JayData basics are enough to handle data of your application. For more details, visit [http://jaydata.org](http://jaydata.org).
112+
113+
### Step 1 - Define your data model
114+
115+
Simple model that works online and offline as well. Define your data model:
116+
117+
```javascript
118+
var Todo = $data.Entity.extend("Todo", {
119+
Id: { type: "int", key: true, computed: true },
120+
Task: { type: String, required: true, maxLength: 200 },
121+
DueDate: { type: Date },
122+
Completed: { type: Boolean }
123+
});
124+
125+
var TodoDatabase = $data.EntityContext.extend("TodoDatabase", {
126+
Todos: { type: $data.EntitySet, elementType: "Todo" }
127+
});
128+
```
129+
130+
### Step 2 - Initialize the data storage
131+
132+
#### OData
133+
134+
You can Initialize your context to handle an OData endpoint just by passing the OData service URL as a single string parameter.
135+
136+
```javascript
137+
var todoDB = new TodoDatabase("http://mysite.com/my.svc");
138+
```
139+
140+
#### Local database
141+
142+
If you want to use a local database, pass the name of your database as a string.
143+
JayData automatically detects what type of local database solution is available on the client and creates a context to an IndexedDB, WebSQL, LocalStorage or InMemory database.
144+
145+
```javascript
146+
var todoDB = new TodoDatabase("MyTodoDatase");
147+
```
148+
149+
#### WebSQL
150+
151+
You can even specify, what type of database you want to use by providing a storage provider configuration object.
152+
153+
```javascript
154+
var todoDB = new TodoDatabase({
155+
provider: 'webSql', databaseName: 'MyTodoDatabase'
156+
});
157+
```
158+
159+
### Step 3 - Create data
160+
161+
You can create new data by adding new entities to an entity set.
162+
163+
#### Simple
164+
165+
You can add a single entity to an entity set...
166+
167+
```javascript
168+
todoDB.onReady(function(){
169+
var task = todoDB.Todos.add({ Task: 'Step0: Get this list', Completed: true });
170+
todoDB.saveChanges(function(){
171+
alert(task.Id);
172+
});
173+
});
174+
```
175+
176+
#### Batch
177+
178+
...or you can create multiple new entities by using the ```addMany``` function. In the handler of the ```saveChanges``` function you will get how many entities were saved.
179+
180+
```javascript
181+
todoDB.onReady(function(){
182+
var tasks = todoDB.Todos.addMany([
183+
{ Task: 'Step1: Define your data model'},
184+
{ Task: 'Step2: Initialize the data storage'},
185+
{ Task: 'Step3: Create data' }
186+
]);
187+
todoDB.saveChanges(function(count){
188+
alert("Created " + count + " new task");
189+
tasks.forEach(function(todo){ alert(todo.Id); });
190+
});
191+
});
192+
```
193+
194+
### Step 4 - Read data
195+
196+
#### All items
197+
198+
To retrieve all database items as an array, use the ```toArray``` function.
199+
200+
```javascript
201+
todoDB.onReady(function(){
202+
todoDB.Todos.toArray(function(todos){
203+
yourTemplate.render(todos);
204+
});
205+
});
206+
```
207+
208+
#### Filter #1
209+
210+
You can filter your data just like the native ```filter``` function of JavaScript. If you want to handle your result in a loop use the ```forEach``` function.
211+
212+
```javascript
213+
todoDB.onReady(function(){
214+
todoDB.Todos
215+
.filter(function(todo){
216+
return todo.Completed == true || todo.Task.startsWith("Step2");
217+
})
218+
.forEach(function(todo){
219+
yourTemplate.render(todo);
220+
});
221+
});
222+
```
223+
224+
Using ```forEach``` is equivalent to this:
225+
226+
```javascript
227+
todoDB.onReady(function(){
228+
todoDB.Todos
229+
.filter(function(todo){
230+
return todo.Completed == true || todo.Task.startsWith("Step2");
231+
})
232+
.toArray(function(todos){
233+
todos.forEach(function(todo){
234+
yourTemplate.render(todo);
235+
});
236+
});
237+
});
238+
```
239+
240+
#### Filter #2
241+
242+
You can pass a query parameters object as the second argument of the ```filter``` function and access these query parameters on ```this``` in the query function.
243+
244+
```javascript
245+
todoDB.onReady(function(){
246+
todoDB.Todos
247+
.filter(function(todo){
248+
return todo.Completed == true || todo.Task.startsWith(this.stepName);
249+
}, {
250+
stepName: 'Step2'
251+
})
252+
.forEach(function(todo){
253+
yourTemplate.render(todo);
254+
});
255+
});
256+
```
257+
258+
#### Filter #3
259+
260+
Instead of a JavaScript function you can use a query string in the ```filter``` function. This is specially useful, when you want to create your query dynamically.
261+
262+
```javascript
263+
todoDB.onReady(function(){
264+
var stepName = 'Step2';
265+
todoDB.Todos
266+
.filter("it.Completed || it.task.startsWith('" + stepName + "')")
267+
.forEach(function(todo){
268+
yourTemplate.render(todo);
269+
});
270+
});
271+
```
272+
273+
#### Mapping
274+
275+
In some scenarios you want to just retrieve some fields of your entity and map these fields as different names.
276+
277+
```javascript
278+
todoDB.onReady(function(){
279+
todoDB.Todos
280+
.map(function(todo){
281+
return {
282+
_task: todo.Task,
283+
_completed: todo.Completed
284+
}
285+
})
286+
.toArray(function(todos){
287+
yourTemplate.render(todos);
288+
});
289+
});
290+
```
291+
292+
#### Paging
293+
294+
As you store more and more entities in your database, it's practical to retrieve only a subset of your data by using paging functions.
295+
296+
```javascript
297+
todoDB.onReady(function(){
298+
todoDB.Todos
299+
.skip(2)
300+
.take(3)
301+
.toArray(function(todo){
302+
yourTemplate.render(todo);
303+
});
304+
});
305+
```
306+
307+
#### Ordering
308+
309+
If you want to sort your result by a selected field, use ```orderBy``` or ```orderByDescending```. As you can still use a string instead of a function in the query function, you can dynamically construct your ordering query.
310+
311+
```javascript
312+
todoDB.onReady(function(){
313+
todoDB.Todos
314+
.orderBy("it.Task")
315+
.toArray(function(todo){
316+
yourTemplate.render(todo);
317+
});
318+
});
319+
```
320+
321+
If you want more dynamic control over order direction, use the ```order``` function. If you need descending ordering on the field, use the ```-``` sign before the field name.
322+
323+
```javascript
324+
todoDB.onReady(function(){
325+
todoDB.Todos
326+
.order("-it.Task")
327+
.toArray(function(todo){
328+
yourTemplate.render(todo);
329+
});
330+
});
331+
```
332+
333+
### Step 5 - Update data
334+
335+
To update an entity, attach it to the context and JayData will track the changes on the entity. On calling the ```saveChanges``` function, your attached and updated entities will be saved.
336+
337+
```javascript
338+
todoDB.onReady(function(){
339+
todoDB.Todos.single("it.Id == 1", function(todo){
340+
todoDB.attach(todo);
341+
todo.Completed = true;
342+
todoDB.saveChanges(function(count){
343+
alert("Updated " + count + " task");
344+
});
345+
});
346+
});
347+
```
348+
349+
### Step 6 - Delete data
350+
351+
#### Simple
352+
353+
Just like updating data, but instead of ```attach``` you will use ```remove```.
354+
355+
```javascript
356+
todoDB.onReady(function(){
357+
todoDB.Todos.single("it.Id == 3", function(todo){
358+
todoDB.Todos.remove(todo);
359+
todoDB.saveChanges(function(count){
360+
alert("Removed " + count + " task");
361+
});
362+
});
363+
});
364+
```
365+
366+
#### Batch
367+
368+
Use the ```removeAll``` function if you want to truncate all data in a single entity set.
369+
370+
```javascript
371+
todoDB.onReady(function(){
372+
todoDB.Todos.removeAll(function(){
373+
alert("Removed all tasks");
374+
});
375+
});
376+
```
377+
378+
### Step 7 - Generate some UI with jQuery
379+
380+
```javascript
381+
todoDB.onReady(function(){
382+
todoDB.Todos
383+
.forEach(function(todo) {
384+
$('#todos')
385+
.append('<li><b>' + todo.Task + '</b>' + (todo.Completed ? ' - completed' : '') + '</li>');
386+
});
387+
});
388+
```
389+
58390
## Related projects
391+
59392
[JaySvcUtil](https://github.com/jaystack/jaysvcutil) - Code generator tool that builds JayData data model classes from $metadata service of OData endpoints.
60-
[Dynamic Metadata](https://github.com/jaystack/jaydata-dynamic-metadata)

dist/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jaydata",
3-
"version": "1.5.6",
3+
"version": "1.5.8",
44
"description": "Cross-platform HTML5 data-management, JavaScript Language Query (JSLQ) support for OData, SQLite, WebSQL, IndexedDB, YQL and Facebook (packaged for Node.JS)",
55
"keywords": [
66
"HTML5 data management",

gulpfile.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ gulp.task('lint', function(){
6060
.pipe(eslint.failAfterError())
6161
});
6262

63-
gulp.task('nodejs', function() {
63+
gulp.task('readme', function(){
64+
gulp.src('README.md')
65+
.pipe(gulp.dest('./dist'));
66+
});
67+
68+
gulp.task('nodejs', ['readme'], function() {
6469
return gulp.src(['src/**/*.js'])
6570
.pipe(babel({
6671
compact: false

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jaydata",
3-
"version": "1.5.6",
3+
"version": "1.5.8",
44
"description": "Cross-platform HTML5 data-management, JavaScript Language Query (JSLQ) support for OData, SQLite, WebSQL, IndexedDB, YQL and Facebook (packaged for Node.JS)",
55
"keywords": [
66
"HTML5 data management",

0 commit comments

Comments
 (0)