erikolson186 / zangodb Goto Github PK
View Code? Open in Web Editor NEWMongoDB-like interface for HTML5 IndexedDB
Home Page: https://erikolson186.github.io/zangodb/
License: MIT License
MongoDB-like interface for HTML5 IndexedDB
Home Page: https://erikolson186.github.io/zangodb/
License: MIT License
I wanted to create an instance of zangodb and passed the database name and a string array with the collections, but in my code he wants then a version parameter.
I saw in your db.js file on line 55 that you only check if the second parameter is an object and not if its an string array, so you definitly need a version parameter, when you pass only a string array with the collections.
Update: But i noticed that i have to pass a version parameter anyway, because i use Typescript. But dont know if it is intended with the missing type check in your code.
Nice library... keep up the good work
I have a question, while the below is supported
col.find({ x: 4, g: { $lt: 10 } }, { k: 0 })
Will querying on nested objects path be supported too like in mongo
col.find({ 'person.age' : { $lt: 10 } })
Now the error comes with message only like this:
"Error" "Key already exists in the object store."
Is it possible to use message code as error name? Much easier to use switch
or other expression to identify.
I notice that there is no save
method, does that mean the best way to do save
with existing object is to delete
first then insert
?
Now the API is not easy to implement upsert
. I need findOne
first then if the id
is found, in order to update I have to call update
which will filter the collection again, which is kind of slow in large collection.
Hi,
Is it possible to query an array property of a document. Given the following structure of my documents, I want to query for documents where Id15 contains 37330006.
{
"Id15":["37330006", "37330007", "37330008"],
"Name":"John Smith",
"Address":"The street 152",
"Postal":"90210",
"City":"BH",
"Cvr":"1123123234",
"_id":25953
}
BR
Thomas
Under the current type definitions, collections and cursors return documents that are always typed as Object
. This means that when using TypeScript, documents must always be type-asserted to a concrete type before being used:
const db = new zango.Db('mydb');
const people = db.collection('people');
const person = people.find({ name: 'John' });
person.name = 'Mike'; // ERROR: Property 'ok' does not exist on type 'Object'.
interface Person {
name: string;
}
const person2 = people.find({ name: 'John' }) as Person;
person.name = 'Mike'; // OK
This happens because current type definitions are hard-typed to Object
and do not allow generics:
Lines 21 to 31 in 868babf
We could get inspired by mongodb's type definitions at DefinitelyTyped, and generalize Cursor
and Collection
, allowing the library consumer to set the document's interface for a given collection, falling back to Record<string, any>
if the type parameter is not supplied:
mongodb's Collection
type signature - allows a type parameter
mongodb's Collection#findOne
type signature - leverages collection type parameter
Using a similar sytem, we could accomplish the following with TypeScript:
interface Person {
name: string;
}
const db = new zango.Db('mydb');
const people = db.collection<Person>('people');
const person = people.find({ name: 'John' }); // typeof person == Person
person.name = 'Mike'; // OK
I can go ahead and write a quick PR to fix this if you'd like.
I believe because of this line https://github.com/erikolson186/zangodb/blob/master/src/sort.js#L48 when sorting on multiple fields, if the first field comes back from compare with a result of 0
the sort returns without evaluating the second field.
I think the fix is to just change that line to: if (result !== null && result !== 0) { return result; }
.
collection.find().sort({ x: 1 }).hint('x').toArray()
will get this error:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'args')
at removeClause (create_next_fn.js:39:1)
at create_next_fn.js:357:1
at createGetIDBReqFn (create_next_fn.js:358:1)
at createGetIDBCurFn (create_next_fn.js:378:1)
at createNextFn (create_next_fn.js:521:1)
at push../node_modules/zangodb/build/src/create_next_fn.js.module.exports (create_next_fn.js:572:1)
at Cursor._init (cursor.js:321:1)
at Cursor._forEach (cursor.js:62:1)
at Cursor._toArray (cursor.js:115:1)
at Cursor.toArray (cursor.js:142:1)
Hi there
I'm using zangodb in chrome extension development.
The problem is that when I use the config object and version number to upgrade the database version to the new version, the indexes has not changed, but the version number has changed.
These two configurations are like this:
// version 1
let DBStruct = {
user: {"userid":true},
};
// version 2
let DBStruct = {
user: {"uid":true,"userid":false},
};
The uid index not created, and userid is still there.
Using it in a query like {value: {$regex: /abc/}} throws Error: unknown operator '$regex'
let db = new zango.Db('searcher', { people: ['uids'] });
let people = db.collection('people');
let docs = [
{ name: 'Frank', uids: ['1', '2', '3'] }
];
people.insert(docs).then(() => {
console.log('save success');
return people.find({
uids: '2'
}).forEach(doc => console.log('doc:', doc));
}).catch(error => console.error(error));
> save success
The search for an element within an array is not supported?
How to get count directly?
people.find({ name: { $regex: /Todd/ } }).toArray().then(res => console.log(res.length)).catch(err =>console.log(err))
may be add count support?
people.count({ name: { $regex: /Todd/ } }).then(res => console.log(res)).catch(err =>console.log(err))
deepmerge dependency is causing error in file src/create_next_fn.js, line 182, because of this error TehShrike/deepmerge#137
I met the following questions and solved them according to the answers:
Q--Using object-hash in an Angular 8 project causes “Cannot read property 'crypto' of undefined” error
A--https://stackoverflow.com/questions/57393092/using-object-hash-in-an-angular-8-project-causes-cannot-read-property-crypto?r=SearchResults
I have the following code:
db.js
import zango from 'zangodb';
const db = new zango.Db('app', {
translations: ['id'],
services: ['id', 'key'],
console: true
});
export default db;
logger.js
import db from 'environment/db';
class Logger {
static log(message, type) {
db.collection('console').insert({
timestamp: 'test',
message,
type
}, (error) => {
if (error) {
throw error;
}
console.log('success');
});
}
}
export default Logger;
test.js
import Logger from 'environment/logger';
Logger.log('test log', 'info');
I keep getting the following error:
NotFoundError (DOM IDBDatabase Exception 8): Failed to execute 'transaction' on 'IDBDatabase': One of the specified object stores was not found.
My other two collections (services and translations) work properly but for some reason the 3rd one does not run. What am I missing here?
const exclude = loadedItems.map(item => item._id);
The following query doesn't work (gives back zero elements) when the exlude is an empty array. Tested many different ways.
const query = searchValue ? {
parentId: { $eq: selectedFolderId },
_id: { $nin: exclude },
name: { $regex: searchValue, $options: "i" },
} : {
parentId: { $eq: selectedFolderId },
_id: { $nin: exclude },
}
Hi,
You might consider dropping bower support as it is deprecated.
bower/bower#2298 (comment)
First of all, thank you for such an amazing library. It has been of a tremendous help for us at distill.io when we needed to transition from SQLite to tIndexedDB to build our Firefox web extension.
There is a type of find query that we are not able to use ZangoDB. In this query, one needs to compare one field with another. For example price > threshold
. Is there a way to perform such queries?
In general, can one use a function that can perform arbitrary operation in JavaScript and return a boolean value to filter records? Such an API can enable a wide range of queries that may not be possible at this moment.
Thoughts?
Insert should return to ID
The $in operator always fetch one record.
I have very simple database structure as shown below
I am trying to fetch all those records which have name field value 'Bill' or 'Don' I am using $in operator but it always return only one row. Below is the code that I am using
let db = new zango.Db('customer_db', { customers: ['snn'] });
let people = db.collection('customers');
people.find({
name: { $in: ["Don","Bil"]}
}).forEach(doc => console.log('doc:', doc));
and the output of above code is
@erikolson186 can you please help me know to resolve above issue?
I'm looking for an alternative to NeDB which seems abandoned a little for a node-webkit app.
Could I see an example of a join operation? I'm not quite sure how I should do it with ZangoDB.
I'm familiar with EJDB join requests but I prefer a library that would avoid me to compile stuff.
db.find('orders', {$do: {member: {$join:'members'}}});
Thanks!
How can I find out that the search is completed to start another operation after the search?
let db = new zango.Db('searcher', { people: ['uids'] });
let people = db.collection('people');
let docs = [
{ name: "1", gender: "m", uids: ['1', '2', '3'] },
{ name: "2", gender: "m", uids: ['1', '2', '3'] },
{ name: "3", gender: "m", uids: ['1', '2', '3'] },
{ name: "4", gender: "m", uids: ['1', '2', '3'] }
];
people.insert(docs).then(() => {
console.log('save success');
}).catch(error => console.error(error));
people.find({
name: {$in: ["1"]},
gender: "m"
}).toArray((error, doc) => console.log('doc:', doc));
console.log('start another operation');
(index):70 start another operation
(index):64 save success
(index):69 doc: (3) [{…}, {…}, {…}]
For some reason, my attempt couldn't get working. https://jsfiddle.net/3br5uh2g/
Solved
I write the upsert
method like this:
public upsert(doc: Object, collectionName: string) {
if (!doc) return Promise.reject('invalid doc');
doc['updatedAt'] = Date.now();
let id = doc['_id'];
if (id) {
return this.get(id, collectionName)
.toArray((err: Error, docs: Object[]) => {
if (!err && docs && docs.length === 1) {
// found one, update
return this.collection(collectionName).update({ _id: id }, doc);
} else {
// found no record, insert
return this.insert(doc, collectionName);
}
});
} else {
// missing _id, insert directly
return this.insert(doc, collectionName);
}
}
And I call it like this:
public try() {
this.db.upsert({ _id: '12345', name: '123' }, DbService.COL_MAIN)
.then(insertResult => {
this.db.find({}, DbService.COL_MAIN)
.forEach(doc => {
console.log('result:', doc);
});
})
.catch((err: Error) => console.error(err, err.name, err.message));
}
The value of name 123
is not updated in the inner find
and forEach
. That's to say when I call find
the updates are not guaranteed?
Is it possible to have something similar to LIKE operator in SQL? Mongo supports regex.
Either way, would love to know your thoughts on this. This is useful when writing queries that search for keywords in documents.
Hello.
How I can get last added document or last id?
I waited similar - https://github.com/dfahlander/Dexie.js/blob/master/samples/react-redux/src/actions/index.js#L27
It would be great if there is:
collection.on('change', e => ...);
or other events like creation
delete
...
After creating multiple collections for the first time, it will not be possible to append new collections, I would like to be able to append collections at a later stage, but there is no corresponding api at the moment
according to mongodb sort should be applied before limit, regardless of the chain order https://docs.mongodb.com/manual/reference/method/db.collection.find/#combine-cursor-methods
what I'm seeing is the docs passed to the sort stage have already been limited (regardless of chain order), so the final result is wrong when limit < total docs count.
I just hit this and haven't dug in too much yet, but If you want I can investigate more and see if I can fix it.
Implement typescript support for angular2 and ionic2 projects.
Mongodb aggregate
follows the order specified in pipeline. But zangodb does not appear to. I think that I must be missing something. Following is an adapted example from issue #9 :
var db = new zango.Db(Math.random(), { col: ['x', 'g'] });
var col = db.collection('col');
var docs = [{ x: 2, g: 3 }, { x: 2, g: 8 }, { x: 2, g: 8, z: 10 }, { x: 3, z: 3, g: 4 }, { x: 4, k: 8, g: 1 }, { x: 6, g: 9 }, { x: 10, k: 4 }, { x: undefined }, { x: null }, { x: [{ k: 2 }, { k: 8 }] }];
col.insert(docs);
col.aggregate([{$skip : 2}, {$sort: {g: -1} }, ]).toArray().then(docs => {
console.log(docs.map(doc => doc.g))
});
col.aggregate([{$sort: {g: -1} }, {$skip : 2}]).toArray().then(docs => {
console.log(docs.map(doc => doc.g))
});
Output in both cases is:
(8) [undefined, undefined, undefined, undefined, 9, 8, 4, 1]
What is the correct way to query a collection, sort it and then use skip and limit? I am trying to do this to paginate ordered data.
Many thanks for this wonderful library. With its help, using IndexedDB has been a joy!
Why does the following code produce the error in the title?
(async () => {
let res = await window.db.collection('change_queue')
.find({})
.sort({timestamp: -1})
res = await res.toArray()
console.log("RES", res)
})()
typeof timestamp is Date btw. Seems to be working without the sort() part
Hi Again,
Just curious if $pull is supported. I have requirement to pull/remove items from an nested array. Is there a way to do so?
Any chance you'd add this to bower?
https://bower.io/
Hello,
Thank you for a great framework.
I have a question. Have you done any benchmarking on inserts. I'm trying to figure out the optimal size for the docs array, when inserting into a collection using:
collection.insert(docs).then(function () { ... });
Or rather i'm trying to figure out if I need to split the array into chunks.
BR
Thomas
Hi!
Giving this data:
{
"_id": "12451234",
"tracks": [
{"creation": "2017-11-12", "text": "asfdasdfas"},
{"creation": "2017-11-11", "text": "asdfadsfasd"},
]
}
is it possible to index tracks.creation?
How?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.