Giter Club home page Giter Club logo

camlsql-js's Introduction

camlsql-js

Build Status codecov

CSOM library for getting SharePoint List items using SQL syntax

Camlsql will let you fetch SharePoint List Items using SQL syntax. It relies on an already existing SP ClientContext and is therefore very lightweight.

camlsql.prepare("SELECT * FROM Pages WHERE Title LIKE ? ORDER BY Modified DESC LIMIT 10", [ "%hello%" ])
.exec(function(err, rows) {
 if (err) console.error(err);
 console.table(rows);
});

Looking for the Documentation?


Why should I use this?

Maybe you shouldn't.

This is a very specific library to do one specific thing - get list items from a list - while the REST API can be used for creating, updating and deleting as well.

But let me try to sell camlsql to you anyway.

  • camlsql is lightweight - only 8.3 KB min+gz
  • Generate CAML queries using SQL-like syntax! SQL! Too easy!
  • No dependencies other than the existing SharePoint CSOM libraries
  • SQL syntax (yes, I mention it again) and no need to manually build XML strings

Not convinced? I figured as much... Read on...

Why choose CSOM over REST?

Other than my above attempts to convice you there are few "actual" reasons when you may need to use CamlQueries over the SharePoint REST API.

  • Nested joins is not supported by the REST API
    • If you have list A with a lookup field to list B,
      and list B has a lookup field to C - then you can not query list A and join list C via list B.
    • This is possible using CAML and by using JOIN in the camlsql library
  • Calculated fields can not be queried using the REST API

Still not convinced? Well, then perhaps this is not the library you're looking for.

Thanks for reading this far!

Install

npm i docsify-cli -g
npm install

# To run docs
docsify serve docs

# To build
gulp

camlsql-js's People

Contributors

dlid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

camlsql-js's Issues

Double groups in parsed query

vkbeautify.xml(camlsql.prepare("SELECT * FROM Movies WHERE (Title = ? AND Title LIKE ?) AND (Fun = ? OR Scary < ?)",["summer", 'did', 10, 6,0,6]).getXml());

This query creates the following parsed object.

The first two "groups" are the same. There should only be one of those.

[{
	"type": "group",
	"value": "Title = ? AND Title LIKE ?",
	"operator": "and",
	"items": [{
		"type": "statement",
		"operator": "and",
		"field": "Title",
		"macro": "@param0",
		"comparison": "eq"
	}, {
		"type": "statement",
		"operator": "and",
		"field": "Title",
		"macro": "@param1",
		"comparison": "like"
	}]
}, {
	"type": "group",
	"value": "(Title = ? AND Title LIKE ?)",
	"operator": "and",
	"items": [{
		"type": "statement",
		"operator": "and",
		"field": "(Title",
		"macro": "@param2",
		"comparison": "eq"
	}, {
		"type": "statement",
		"operator": "and",
		"field": "Title",
		"macro": "@param3",
		"comparison": "like"
	}]
}, {
	"type": "group",
	"value": "Fun = ? OR Scary < ?",
	"operator": "and",
	"items": [{
		"type": "statement",
		"operator": "and",
		"field": "Fun",
		"macro": "@param4",
		"comparison": "eq"
	}, {
		"type": "statement",
		"operator": "or",
		"field": "Scary",
		"macro": "@param5",
		"comparison": "lt"
	}]
}]

Complex queries

Complex groups will break

SELECT * FROM [Decisions] WHERE [Title] LIKE ? AND (([Motivation] IS NULL AND [IsBoss] = ?) or (P = ? AND P2 < ?))

Normalize allowed List names

Make sure you can use proper list names where needed.
Maybe require [ ] when it contains spaces? That's a good plan!

Let's not allow spaces in a joined list alias..

SELECT * FROM ListName -> "ListName"
SELECT * FROM [List Name] -> "List Name"
SELECT * FROM List Name -> Not ok

SELECT * FROM [List Name]
JOIN FavoriteBook ON [List Name].Favourite_book 
 -> ok

SELECT * FROM [List Name]
JOIN FavoriteBook ON List Name.Favourite_book 
 -> error

SELECT * FROM [List Name]
JOIN FavoriteBook ON [List Name].Favourite_book 
 -> ok

Encoded column names in result

When using encoded field names in the query it would make sense to be able to access properties using the non-encoded name in the result.

SELECT [Title], [Båtfärg] FROM ListOfBoats`

<script>
 camlsql.prepare(...).exec(function(err, rows) {

   // I want to be able to use 
   var color = rows[0]["Båtfärg"];

   // Instead of 
   var color = rows[0].B_x00e5_tf_x00e4_rg
 });
</script>

Or I will have to know the encoded filename anyway and then what's the point of being able to use the non-encoded filename in the SQL-query?

WHERE: Comparison pattern overhaul

When I write WHERE Preamble=? (with no spaces around =) then the field name is cut.

  • When using WHERE Preamble=? (with no spaces around =) then the field name is cut to <FieldRef Name="Preambl" />
  • When using Preamble<? the comparison is not recognized at all
  • When using Preamble>? the comparison becomes EQUAL and not GT
  • PreambleLIKE seems to work. But it probably should not... Same thing with Preamblein? or walkin?. Field name will be "Preamble" with an IN clause

Expanding to other CRUD operations

Greetings again! How useful do you think a library like this would be if it could expand to support the other CRUD list operations in sharepoint? I love how caml sql supports the select statement, but I think it would be a real quantum leap if it could also support inserts, updates, and deletes.

How popular/beneficial do you think that capability would be? From what I've seen, there's a few other SP JS libraries that allow you to do similar things, but their syntax is broken into something resembling LINQ methods. (I'm not asking you to develop this capability, I'm really just asking your opinion, and I would take care of the dev part myself :)

Space madness

SELECT * FROM [Decisions] WHERE [Title] LIKE ? AND (([Motivation] IS NULL OR [IsBoss] = ?) or P = ?) works fine

But SELECT * FROM [Decisions] WHERE [Title] LIKE ? AND ( ([Motivation] IS NULL OR [IsBoss] = ?) or P = ?)
does not (See the SPACE character in the Motivation parenthesises.

Named parameters and NULL / NOT NULL

camlsql.prepare("SELECT * FROM Listan WHERE a = @meh AND y is null", {"@meh" : "test"});

You get the error:

[camlsql] You can not mix named macros and ?

week start returns wrong day

getDateFromTextualRepresentation("week start", new Date(1980, 03, 04));

supposed to be 31st of april 00:00:00 but returns
Sun Mar 30 1980 00:00:00 GMT+0100 (Central Europe Standard Time)
same with week end
var d = camlsql.testonly.getDateFromTextualRepresentation("week end", new Date(1980, 03, 04));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.