memory-adapter.spec.js 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145
  1. import {expect} from 'chai';
  2. import {Schema} from '../../schema.js';
  3. import {format} from '@e22m4u/js-format';
  4. import {MemoryAdapter} from './memory-adapter.js';
  5. import {DataType} from '../../definition/index.js';
  6. import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK} from '../../definition/index.js';
  7. describe('MemoryAdapter', function () {
  8. describe('_getTableOrCreate', function () {
  9. it('returns an existing table or creates a new', function () {
  10. const S = new Schema();
  11. S.defineModel({name: 'model'});
  12. const A = S.getService(MemoryAdapter);
  13. const table = A._getTableOrCreate('model');
  14. expect(table).to.be.instanceof(Map);
  15. const sameTable = A._getTableOrCreate('model');
  16. expect(table).to.be.eq(sameTable);
  17. });
  18. it('uses a model name to find a table, even a table name is specified', function () {
  19. const S = new Schema();
  20. S.defineModel({
  21. name: 'myModel',
  22. tableName: 'myTable',
  23. });
  24. const A = S.getService(MemoryAdapter);
  25. const table = A._getTableOrCreate('myModel');
  26. expect(table).to.be.instanceof(Map);
  27. const sameTable = A._getTableOrCreate('myModel');
  28. expect(table).to.be.eq(sameTable);
  29. });
  30. it('stores a table by specified table name', function () {
  31. const S = new Schema();
  32. S.defineModel({
  33. name: 'myModel',
  34. tableName: 'myTable',
  35. });
  36. const A = S.getService(MemoryAdapter);
  37. const table = A._getTableOrCreate('myModel');
  38. expect(table).to.be.instanceof(Map);
  39. const sameTable = A._tables.get('myTable');
  40. expect(table).to.be.eq(sameTable);
  41. });
  42. });
  43. describe('_genNextIdValue', function () {
  44. it('returns an unique number identifier', function () {
  45. const S = new Schema();
  46. S.defineModel({name: 'model'});
  47. const A = S.getService(MemoryAdapter);
  48. const id1 = A._genNextIdValue('model', DEF_PK);
  49. const id2 = A._genNextIdValue('model', DEF_PK);
  50. const id3 = A._genNextIdValue('model', DEF_PK);
  51. expect(id1).to.be.eq(1);
  52. expect(id2).to.be.eq(2);
  53. expect(id3).to.be.eq(3);
  54. });
  55. });
  56. describe('create', function () {
  57. it('skips existing values when generating a new identifier for a default primary key', async function () {
  58. const schema = new Schema();
  59. schema.defineDatasource({
  60. name: 'memory',
  61. adapter: 'memory',
  62. });
  63. schema.defineModel({
  64. name: 'model',
  65. datasource: 'memory',
  66. });
  67. const adapter = new MemoryAdapter(schema.container, {});
  68. const result1 = await adapter.create('model', {});
  69. const result2 = await adapter.create('model', {});
  70. const result3 = await adapter.create('model', {[DEF_PK]: 3});
  71. const result4 = await adapter.create('model', {});
  72. expect(result1).to.be.eql({[DEF_PK]: 1});
  73. expect(result2).to.be.eql({[DEF_PK]: 2});
  74. expect(result3).to.be.eql({[DEF_PK]: 3});
  75. expect(result4).to.be.eql({[DEF_PK]: 4});
  76. });
  77. it('skips existing values when generating a new identifier for a specified primary key', async function () {
  78. const schema = new Schema();
  79. schema.defineDatasource({
  80. name: 'memory',
  81. adapter: 'memory',
  82. });
  83. schema.defineModel({
  84. name: 'model',
  85. datasource: 'memory',
  86. properties: {
  87. myId: {
  88. type: DataType.NUMBER,
  89. primaryKey: true,
  90. },
  91. },
  92. });
  93. const adapter = new MemoryAdapter(schema.container, {});
  94. const result1 = await adapter.create('model', {});
  95. const result2 = await adapter.create('model', {});
  96. const result3 = await adapter.create('model', {myId: 3});
  97. const result4 = await adapter.create('model', {});
  98. expect(result1).to.be.eql({myId: 1});
  99. expect(result2).to.be.eql({myId: 2});
  100. expect(result3).to.be.eql({myId: 3});
  101. expect(result4).to.be.eql({myId: 4});
  102. });
  103. it('generates a new identifier when a value of a primary key has not provided', async function () {
  104. const schema = new Schema();
  105. schema.defineDatasource({
  106. name: 'memory',
  107. adapter: 'memory',
  108. });
  109. schema.defineModel({
  110. name: 'model',
  111. datasource: 'memory',
  112. properties: {
  113. foo: DataType.STRING,
  114. bar: DataType.NUMBER,
  115. },
  116. });
  117. const adapter = new MemoryAdapter(schema.container, {});
  118. const input = {foo: 'string', bar: 10};
  119. const created = await adapter.create('model', input);
  120. const idValue = created[DEF_PK];
  121. expect(created).to.be.eql({...input, [DEF_PK]: idValue});
  122. const table = adapter._getTableOrCreate('model');
  123. const tableData = table.get(idValue);
  124. expect(tableData).to.be.eql({...input, [DEF_PK]: idValue});
  125. });
  126. it('generates a new identifier when a value of a primary key is undefined', async function () {
  127. const schema = new Schema();
  128. schema.defineDatasource({
  129. name: 'memory',
  130. adapter: 'memory',
  131. });
  132. schema.defineModel({
  133. name: 'model',
  134. datasource: 'memory',
  135. properties: {
  136. foo: DataType.STRING,
  137. bar: DataType.NUMBER,
  138. },
  139. });
  140. const adapter = new MemoryAdapter(schema.container, {});
  141. const input = {
  142. [DEF_PK]: undefined,
  143. foo: 'string',
  144. bar: 10,
  145. };
  146. const created = await adapter.create('model', input);
  147. const idValue = created[DEF_PK];
  148. expect(created).to.be.eql({...input, [DEF_PK]: idValue});
  149. const table = adapter._getTableOrCreate('model');
  150. const tableData = table.get(idValue);
  151. expect(tableData).to.be.eql({...input, [DEF_PK]: idValue});
  152. });
  153. it('generates a new identifier when a value of a primary key is null', async function () {
  154. const schema = new Schema();
  155. schema.defineDatasource({
  156. name: 'memory',
  157. adapter: 'memory',
  158. });
  159. schema.defineModel({
  160. name: 'model',
  161. datasource: 'memory',
  162. properties: {
  163. foo: DataType.STRING,
  164. bar: DataType.NUMBER,
  165. },
  166. });
  167. const adapter = new MemoryAdapter(schema.container, {});
  168. const input = {
  169. [DEF_PK]: null,
  170. foo: 'string',
  171. bar: 10,
  172. };
  173. const created = await adapter.create('model', input);
  174. const idValue = created[DEF_PK];
  175. expect(created).to.be.eql({...input, [DEF_PK]: idValue});
  176. const table = adapter._getTableOrCreate('model');
  177. const tableData = table.get(idValue);
  178. expect(tableData).to.be.eql({...input, [DEF_PK]: idValue});
  179. });
  180. it('generates a new identifier for a primary key of a "number" type', async function () {
  181. const schema = new Schema();
  182. schema.defineDatasource({
  183. name: 'memory',
  184. adapter: 'memory',
  185. });
  186. schema.defineModel({
  187. name: 'model',
  188. datasource: 'memory',
  189. properties: {
  190. myId: {
  191. type: DataType.NUMBER,
  192. primaryKey: true,
  193. },
  194. },
  195. });
  196. const adapter = new MemoryAdapter(schema.container, {});
  197. const result1 = await adapter.create('model', {});
  198. const result2 = await adapter.create('model', {});
  199. const result3 = await adapter.create('model', {});
  200. expect(result1).to.be.eql({myId: 1});
  201. expect(result2).to.be.eql({myId: 2});
  202. expect(result3).to.be.eql({myId: 3});
  203. });
  204. it('generates a new identifier for a primary key of an "any" type', async function () {
  205. const schema = new Schema();
  206. schema.defineDatasource({
  207. name: 'memory',
  208. adapter: 'memory',
  209. });
  210. schema.defineModel({
  211. name: 'model',
  212. datasource: 'memory',
  213. properties: {
  214. myId: {
  215. type: DataType.ANY,
  216. primaryKey: true,
  217. },
  218. },
  219. });
  220. const adapter = new MemoryAdapter(schema.container, {});
  221. const result1 = await adapter.create('model', {});
  222. const result2 = await adapter.create('model', {});
  223. const result3 = await adapter.create('model', {});
  224. expect(result1).to.be.eql({myId: 1});
  225. expect(result2).to.be.eql({myId: 2});
  226. expect(result3).to.be.eql({myId: 3});
  227. });
  228. it('throws an error when generating a new value for a primary key of a "string" type', async function () {
  229. const schema = new Schema();
  230. schema.defineDatasource({
  231. name: 'memory',
  232. adapter: 'memory',
  233. });
  234. schema.defineModel({
  235. name: 'model',
  236. datasource: 'memory',
  237. properties: {
  238. myId: {
  239. type: DataType.STRING,
  240. primaryKey: true,
  241. },
  242. },
  243. });
  244. const adapter = new MemoryAdapter(schema.container, {});
  245. const promise = adapter.create('model', {foo: 'string', bar: 10});
  246. await expect(promise).to.be.rejectedWith(
  247. 'The memory adapter able to generate only Number identifiers, ' +
  248. 'but the primary key "myId" of the model "model" is defined as String. ' +
  249. 'Do provide your own value for the "myId" property, or change the type ' +
  250. 'in the primary key definition to a Number that will be ' +
  251. 'generated automatically.',
  252. );
  253. });
  254. it('throws an error when generating a new value for a primary key of a "boolean" type', async function () {
  255. const schema = new Schema();
  256. schema.defineDatasource({
  257. name: 'memory',
  258. adapter: 'memory',
  259. });
  260. schema.defineModel({
  261. name: 'model',
  262. datasource: 'memory',
  263. properties: {
  264. myId: {
  265. type: DataType.BOOLEAN,
  266. primaryKey: true,
  267. },
  268. },
  269. });
  270. const adapter = new MemoryAdapter(schema.container, {});
  271. const promise = adapter.create('model', {foo: 'string', bar: 10});
  272. await expect(promise).to.be.rejectedWith(
  273. 'The memory adapter able to generate only Number identifiers, ' +
  274. 'but the primary key "myId" of the model "model" is defined as Boolean. ' +
  275. 'Do provide your own value for the "myId" property, or change the type ' +
  276. 'in the primary key definition to a Number that will be ' +
  277. 'generated automatically.',
  278. );
  279. });
  280. it('throws an error when generating a new value for a primary key of an "array" type', async function () {
  281. const schema = new Schema();
  282. schema.defineDatasource({
  283. name: 'memory',
  284. adapter: 'memory',
  285. });
  286. schema.defineModel({
  287. name: 'model',
  288. datasource: 'memory',
  289. properties: {
  290. myId: {
  291. type: DataType.ARRAY,
  292. itemType: DataType.NUMBER,
  293. primaryKey: true,
  294. },
  295. },
  296. });
  297. const adapter = new MemoryAdapter(schema.container, {});
  298. const promise = adapter.create('model', {});
  299. await expect(promise).to.be.rejectedWith(
  300. 'The memory adapter able to generate only Number identifiers, ' +
  301. 'but the primary key "myId" of the model "model" is defined as Array. ' +
  302. 'Do provide your own value for the "myId" property, or change the type ' +
  303. 'in the primary key definition to a Number that will be ' +
  304. 'generated automatically.',
  305. );
  306. });
  307. it('throws an error when generating a new value for a primary key of an "object" type', async function () {
  308. const schema = new Schema();
  309. schema.defineDatasource({
  310. name: 'memory',
  311. adapter: 'memory',
  312. });
  313. schema.defineModel({
  314. name: 'model',
  315. datasource: 'memory',
  316. properties: {
  317. myId: {
  318. type: DataType.OBJECT,
  319. primaryKey: true,
  320. },
  321. },
  322. });
  323. const adapter = new MemoryAdapter(schema.container, {});
  324. const promise = adapter.create('model', {});
  325. await expect(promise).to.be.rejectedWith(
  326. 'The memory adapter able to generate only Number identifiers, ' +
  327. 'but the primary key "myId" of the model "model" is defined as Object. ' +
  328. 'Do provide your own value for the "myId" property, or change the type ' +
  329. 'in the primary key definition to a Number that will be ' +
  330. 'generated automatically.',
  331. );
  332. });
  333. it('allows to specify an identifier value for a new item', async function () {
  334. const schema = new Schema();
  335. schema.defineDatasource({
  336. name: 'memory',
  337. adapter: 'memory',
  338. });
  339. schema.defineModel({
  340. name: 'model',
  341. datasource: 'memory',
  342. properties: {
  343. foo: DataType.STRING,
  344. bar: DataType.NUMBER,
  345. },
  346. });
  347. const adapter = new MemoryAdapter(schema.container, {});
  348. const idValue = 5;
  349. const input = {foo: 'string', bar: 10};
  350. const created = await adapter.create('model', {
  351. [DEF_PK]: idValue,
  352. ...input,
  353. });
  354. expect(created).to.be.eql({[DEF_PK]: idValue, ...input});
  355. const table = adapter._getTableOrCreate('model');
  356. const tableData = table.get(idValue);
  357. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...input});
  358. });
  359. it('throws an error if a given identifier value already exists', async function () {
  360. const schema = new Schema();
  361. schema.defineDatasource({
  362. name: 'memory',
  363. adapter: 'memory',
  364. });
  365. schema.defineModel({
  366. name: 'model',
  367. datasource: 'memory',
  368. properties: {
  369. foo: DataType.STRING,
  370. },
  371. });
  372. const adapter = new MemoryAdapter(schema.container, {});
  373. const created = await adapter.create('model', {foo: 'string'});
  374. const promise = adapter.create('model', created);
  375. await expect(promise).to.be.rejectedWith(
  376. format(
  377. 'The value 1 of the primary key %v already exists in the model "model".',
  378. DEF_PK,
  379. ),
  380. );
  381. });
  382. it('sets default values if they are not provided for a new item', async function () {
  383. const schema = new Schema();
  384. schema.defineDatasource({
  385. name: 'memory',
  386. adapter: 'memory',
  387. });
  388. schema.defineModel({
  389. name: 'model',
  390. datasource: 'memory',
  391. properties: {
  392. foo: {
  393. type: DataType.NUMBER,
  394. default: 10,
  395. },
  396. bar: {
  397. type: DataType.STRING,
  398. default: 'string',
  399. },
  400. },
  401. });
  402. const adapter = new MemoryAdapter(schema.container, {});
  403. const created = await adapter.create('model', {});
  404. const idValue = created[DEF_PK];
  405. const defaults = {foo: 10, bar: 'string'};
  406. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  407. const table = adapter._getTableOrCreate('model');
  408. const tableData = table.get(idValue);
  409. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  410. });
  411. it('sets default values for properties provided with an undefined value', async function () {
  412. const schema = new Schema();
  413. schema.defineDatasource({
  414. name: 'memory',
  415. adapter: 'memory',
  416. });
  417. schema.defineModel({
  418. name: 'model',
  419. datasource: 'memory',
  420. properties: {
  421. foo: {
  422. type: DataType.NUMBER,
  423. default: 1,
  424. },
  425. bar: {
  426. type: DataType.NUMBER,
  427. default: 2,
  428. },
  429. },
  430. });
  431. const adapter = new MemoryAdapter(schema.container, {});
  432. const created = await adapter.create('model', {foo: undefined});
  433. const idValue = created[DEF_PK];
  434. const defaults = {foo: 1, bar: 2};
  435. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  436. const table = adapter._getTableOrCreate('model');
  437. const tableData = table.get(idValue);
  438. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  439. });
  440. it('sets default values for properties provided with a null value', async function () {
  441. const schema = new Schema();
  442. schema.defineDatasource({
  443. name: 'memory',
  444. adapter: 'memory',
  445. });
  446. schema.defineModel({
  447. name: 'model',
  448. datasource: 'memory',
  449. properties: {
  450. foo: {
  451. type: DataType.NUMBER,
  452. default: 1,
  453. },
  454. bar: {
  455. type: DataType.NUMBER,
  456. default: 2,
  457. },
  458. },
  459. });
  460. const adapter = new MemoryAdapter(schema.container, {});
  461. const created = await adapter.create('model', {foo: null});
  462. const idValue = created[DEF_PK];
  463. const defaults = {foo: 1, bar: 2};
  464. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  465. const table = adapter._getTableOrCreate('model');
  466. const tableData = table.get(idValue);
  467. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  468. });
  469. it('uses a specified column name for a primary key', async function () {
  470. const schema = new Schema();
  471. schema.defineDatasource({
  472. name: 'memory',
  473. adapter: 'memory',
  474. });
  475. schema.defineModel({
  476. name: 'model',
  477. datasource: 'memory',
  478. properties: {
  479. foo: {
  480. type: DataType.NUMBER,
  481. primaryKey: true,
  482. columnName: 'bar',
  483. },
  484. },
  485. });
  486. const adapter = new MemoryAdapter(schema.container, {});
  487. const created = await adapter.create('model', {});
  488. expect(created).to.be.eql({foo: created.foo});
  489. const table = adapter._getTableOrCreate('model');
  490. const tableData = table.get(created.foo);
  491. expect(tableData).to.be.eql({bar: created.foo});
  492. });
  493. it('uses a specified column name for a regular property', async function () {
  494. const schema = new Schema();
  495. schema.defineDatasource({
  496. name: 'memory',
  497. adapter: 'memory',
  498. });
  499. schema.defineModel({
  500. name: 'model',
  501. datasource: 'memory',
  502. properties: {
  503. foo: {
  504. type: DataType.NUMBER,
  505. columnName: 'bar',
  506. },
  507. },
  508. });
  509. const adapter = new MemoryAdapter(schema.container, {});
  510. const created = await adapter.create('model', {foo: 10});
  511. const idValue = created[DEF_PK];
  512. expect(created).to.be.eql({[DEF_PK]: idValue, foo: 10});
  513. const table = adapter._getTableOrCreate('model');
  514. const tableData = table.get(idValue);
  515. expect(tableData).to.be.eql({[DEF_PK]: idValue, bar: 10});
  516. });
  517. it('uses a specified column name for a regular property with a default value', async function () {
  518. const schema = new Schema();
  519. schema.defineDatasource({
  520. name: 'memory',
  521. adapter: 'memory',
  522. });
  523. schema.defineModel({
  524. name: 'model',
  525. datasource: 'memory',
  526. properties: {
  527. foo: {
  528. type: DataType.NUMBER,
  529. columnName: 'bar',
  530. default: 10,
  531. },
  532. },
  533. });
  534. const adapter = new MemoryAdapter(schema.container, {});
  535. const created = await adapter.create('model', {});
  536. const idValue = created[DEF_PK];
  537. expect(created).to.be.eql({[DEF_PK]: idValue, foo: 10});
  538. const table = adapter._getTableOrCreate('model');
  539. const tableData = table.get(idValue);
  540. expect(tableData).to.be.eql({[DEF_PK]: idValue, bar: 10});
  541. });
  542. it('uses a short form of a fields clause to filter a return value', async function () {
  543. const schema = new Schema();
  544. schema.defineDatasource({
  545. name: 'memory',
  546. adapter: 'memory',
  547. });
  548. schema.defineModel({
  549. name: 'model',
  550. datasource: 'memory',
  551. properties: {
  552. foo: DataType.STRING,
  553. bar: DataType.NUMBER,
  554. },
  555. });
  556. const adapter = new MemoryAdapter(schema.container, {});
  557. const input = {foo: 'string', bar: 10};
  558. const filter = {fields: 'foo'};
  559. const result = await adapter.create('model', input, filter);
  560. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: input.foo});
  561. });
  562. it('uses a full form of a fields clause to filter a return value', async function () {
  563. const schema = new Schema();
  564. schema.defineDatasource({
  565. name: 'memory',
  566. adapter: 'memory',
  567. });
  568. schema.defineModel({
  569. name: 'model',
  570. datasource: 'memory',
  571. properties: {
  572. foo: DataType.STRING,
  573. bar: DataType.NUMBER,
  574. baz: DataType.BOOLEAN,
  575. },
  576. });
  577. const adapter = new MemoryAdapter(schema.container, {});
  578. const input = {foo: 'string', bar: 10, baz: true};
  579. const filter = {fields: ['foo', 'bar']};
  580. const result = await adapter.create('model', input, filter);
  581. expect(result).to.be.eql({
  582. [DEF_PK]: result[DEF_PK],
  583. foo: input.foo,
  584. bar: input.bar,
  585. });
  586. });
  587. it('a fields clause uses property names instead of column names', async function () {
  588. const schema = new Schema();
  589. schema.defineDatasource({
  590. name: 'memory',
  591. adapter: 'memory',
  592. });
  593. schema.defineModel({
  594. name: 'model',
  595. datasource: 'memory',
  596. properties: {
  597. foo: {
  598. type: DataType.STRING,
  599. columnName: 'fooCol',
  600. },
  601. bar: {
  602. type: DataType.NUMBER,
  603. columnName: 'barCol',
  604. },
  605. baz: {
  606. type: DataType.BOOLEAN,
  607. columnName: 'bazCol',
  608. },
  609. },
  610. });
  611. const adapter = new MemoryAdapter(schema.container, {});
  612. const input = {foo: 'string', bar: 10, baz: true};
  613. const filter = {fields: ['foo', 'bar']};
  614. const result = await adapter.create('model', input, filter);
  615. expect(result).to.be.eql({
  616. [DEF_PK]: result[DEF_PK],
  617. foo: input.foo,
  618. bar: input.bar,
  619. });
  620. });
  621. });
  622. describe('replaceById', function () {
  623. it('removes properties when replacing an item by a given identifier', async function () {
  624. const schema = new Schema();
  625. schema.defineDatasource({
  626. name: 'memory',
  627. adapter: 'memory',
  628. });
  629. schema.defineModel({
  630. name: 'model',
  631. datasource: 'memory',
  632. properties: {
  633. foo: DataType.NUMBER,
  634. bar: DataType.NUMBER,
  635. },
  636. });
  637. const adapter = new MemoryAdapter(schema.container, {});
  638. const input = {foo: 1, bar: 2};
  639. const created = await adapter.create('model', input);
  640. const idValue = created[DEF_PK];
  641. expect(created).to.be.eql({[DEF_PK]: idValue, ...input});
  642. const replaced = await adapter.replaceById('model', idValue, {foo: 2});
  643. expect(replaced).to.be.eql({[DEF_PK]: idValue, foo: 2});
  644. const table = adapter._getTableOrCreate('model');
  645. const tableData = table.get(idValue);
  646. expect(tableData).to.be.eql({[DEF_PK]: idValue, foo: 2});
  647. });
  648. it('ignores identifier value in a given data in case of a default primary key', async function () {
  649. const schema = new Schema();
  650. schema.defineDatasource({
  651. name: 'memory',
  652. adapter: 'memory',
  653. });
  654. schema.defineModel({
  655. name: 'model',
  656. datasource: 'memory',
  657. });
  658. const adapter = new MemoryAdapter(schema.container, {});
  659. const createdModelData = await adapter.create('model', {[DEF_PK]: 10});
  660. expect(createdModelData).to.be.eql({[DEF_PK]: 10});
  661. const table = adapter._getTableOrCreate('model');
  662. const createdTableData = table.get(10);
  663. expect(createdTableData).to.be.eql({[DEF_PK]: 10});
  664. const replacedModelData = await adapter.replaceById('model', 10, {
  665. [DEF_PK]: 20,
  666. });
  667. expect(replacedModelData).to.be.eql({[DEF_PK]: 10});
  668. const replacedTableData = table.get(10);
  669. expect(replacedTableData).to.be.eql({[DEF_PK]: 10});
  670. });
  671. it('ignores identifier value in a given data in case of a specified primary key', async function () {
  672. const schema = new Schema();
  673. schema.defineDatasource({
  674. name: 'memory',
  675. adapter: 'memory',
  676. });
  677. schema.defineModel({
  678. name: 'model',
  679. datasource: 'memory',
  680. properties: {
  681. myId: {
  682. type: DataType.NUMBER,
  683. primaryKey: true,
  684. },
  685. },
  686. });
  687. const adapter = new MemoryAdapter(schema.container, {});
  688. const createdModelData = await adapter.create('model', {myId: 10});
  689. expect(createdModelData).to.be.eql({myId: 10});
  690. const table = adapter._getTableOrCreate('model');
  691. const createdTableData = table.get(10);
  692. expect(createdTableData).to.be.eql({myId: 10});
  693. const replacedModelData = await adapter.replaceById('model', 10, {
  694. myId: 20,
  695. });
  696. expect(replacedModelData).to.be.eql({myId: 10});
  697. const replacedTableData = table.get(10);
  698. expect(replacedTableData).to.be.eql({myId: 10});
  699. });
  700. it('sets a default values for removed properties when replacing an item', async function () {
  701. const schema = new Schema();
  702. schema.defineDatasource({
  703. name: 'memory',
  704. adapter: 'memory',
  705. });
  706. schema.defineModel({
  707. name: 'model',
  708. datasource: 'memory',
  709. properties: {
  710. foo: {
  711. type: DataType.NUMBER,
  712. default: 1,
  713. },
  714. bar: {
  715. type: DataType.NUMBER,
  716. default: 2,
  717. },
  718. },
  719. });
  720. const adapter = new MemoryAdapter(schema.container, {});
  721. const created = await adapter.create('model', {});
  722. const idValue = created[DEF_PK];
  723. const defaults = {foo: 1, bar: 2};
  724. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  725. const replacing = {foo: 2};
  726. const replaced = await adapter.replaceById('model', idValue, replacing);
  727. expect(replaced).to.be.eql({
  728. [DEF_PK]: idValue,
  729. ...defaults,
  730. ...replacing,
  731. });
  732. const table = adapter._getTableOrCreate('model');
  733. const tableData = table.get(idValue);
  734. expect(tableData).to.be.eql({
  735. [DEF_PK]: idValue,
  736. ...defaults,
  737. ...replacing,
  738. });
  739. });
  740. it('sets a default values for replaced properties with an undefined value', async function () {
  741. const schema = new Schema();
  742. schema.defineDatasource({
  743. name: 'memory',
  744. adapter: 'memory',
  745. });
  746. schema.defineModel({
  747. name: 'model',
  748. datasource: 'memory',
  749. properties: {
  750. foo: {
  751. type: DataType.NUMBER,
  752. default: 1,
  753. },
  754. bar: {
  755. type: DataType.NUMBER,
  756. default: 2,
  757. },
  758. },
  759. });
  760. const adapter = new MemoryAdapter(schema.container, {});
  761. const created = await adapter.create('model', {});
  762. const idValue = created[DEF_PK];
  763. const defaults = {foo: 1, bar: 2};
  764. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  765. const replaced = await adapter.replaceById('model', idValue, {
  766. foo: undefined,
  767. });
  768. expect(replaced).to.be.eql({[DEF_PK]: idValue, ...defaults});
  769. const table = adapter._getTableOrCreate('model');
  770. const tableData = table.get(idValue);
  771. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  772. });
  773. it('sets a default values for replaced properties with a null value', async function () {
  774. const schema = new Schema();
  775. schema.defineDatasource({
  776. name: 'memory',
  777. adapter: 'memory',
  778. });
  779. schema.defineModel({
  780. name: 'model',
  781. datasource: 'memory',
  782. properties: {
  783. foo: {
  784. type: DataType.NUMBER,
  785. default: 1,
  786. },
  787. bar: {
  788. type: DataType.NUMBER,
  789. default: 2,
  790. },
  791. },
  792. });
  793. const adapter = new MemoryAdapter(schema.container, {});
  794. const created = await adapter.create('model', {});
  795. const idValue = created[DEF_PK];
  796. const defaults = {foo: 1, bar: 2};
  797. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  798. const replaced = await adapter.replaceById('model', idValue, {
  799. foo: null,
  800. });
  801. expect(replaced).to.be.eql({[DEF_PK]: idValue, ...defaults});
  802. const table = adapter._getTableOrCreate('model');
  803. const tableData = table.get(idValue);
  804. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  805. });
  806. it('throws an error if a given identifier does not exist', async function () {
  807. const schema = new Schema();
  808. schema.defineDatasource({
  809. name: 'memory',
  810. adapter: 'memory',
  811. });
  812. schema.defineModel({
  813. name: 'model',
  814. datasource: 'memory',
  815. properties: {
  816. foo: DataType.NUMBER,
  817. bar: DataType.NUMBER,
  818. },
  819. });
  820. const adapter = new MemoryAdapter(schema.container, {});
  821. const promise = adapter.replaceById('model', 1, {foo: 2});
  822. await expect(promise).to.be.rejectedWith(
  823. format(
  824. 'The value 1 of the primary key %v does not exist in the model "model".',
  825. DEF_PK,
  826. ),
  827. );
  828. });
  829. it('uses a specified column name for a primary key', async function () {
  830. const schema = new Schema();
  831. schema.defineDatasource({
  832. name: 'memory',
  833. adapter: 'memory',
  834. });
  835. schema.defineModel({
  836. name: 'model',
  837. datasource: 'memory',
  838. properties: {
  839. foo: {
  840. type: DataType.NUMBER,
  841. primaryKey: true,
  842. columnName: 'qux',
  843. },
  844. bar: DataType.NUMBER,
  845. baz: DataType.NUMBER,
  846. },
  847. });
  848. const adapter = new MemoryAdapter(schema.container, {});
  849. const input = {bar: 1, baz: 2};
  850. const createdModelData = await adapter.create('model', input);
  851. expect(createdModelData).to.be.eql({
  852. foo: createdModelData.foo,
  853. ...input,
  854. });
  855. const table = adapter._getTableOrCreate('model');
  856. const createdTableData = table.get(createdModelData.foo);
  857. expect(createdTableData).to.be.eql({
  858. qux: createdModelData.foo,
  859. ...input,
  860. });
  861. const replacing = {bar: 2};
  862. const replacedModelData = await adapter.replaceById(
  863. 'model',
  864. createdModelData.foo,
  865. replacing,
  866. );
  867. expect(replacedModelData).to.be.eql({
  868. foo: createdModelData.foo,
  869. ...replacing,
  870. });
  871. const replacedTableData = table.get(createdModelData.foo);
  872. expect(replacedTableData).to.be.eql({
  873. qux: createdModelData.foo,
  874. ...replacing,
  875. });
  876. });
  877. it('uses a specified column name for a regular property', async function () {
  878. const schema = new Schema();
  879. schema.defineDatasource({
  880. name: 'memory',
  881. adapter: 'memory',
  882. });
  883. schema.defineModel({
  884. name: 'model',
  885. datasource: 'memory',
  886. properties: {
  887. foo: {
  888. type: DataType.NUMBER,
  889. columnName: 'baz',
  890. },
  891. bar: DataType.NUMBER,
  892. },
  893. });
  894. const adapter = new MemoryAdapter(schema.container, {});
  895. const input = {foo: 1, bar: 2};
  896. const createdModelData = await adapter.create('model', input);
  897. const idValue = createdModelData[DEF_PK];
  898. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  899. const table = adapter._getTableOrCreate('model');
  900. const createdTableData = table.get(idValue);
  901. expect(createdTableData).to.be.eql({
  902. [DEF_PK]: idValue,
  903. baz: input.foo,
  904. bar: input.bar,
  905. });
  906. const replacing = {foo: 2};
  907. const replacedModelData = await adapter.replaceById(
  908. 'model',
  909. idValue,
  910. replacing,
  911. );
  912. expect(replacedModelData).to.be.eql({[DEF_PK]: idValue, ...replacing});
  913. const replacedTableData = table.get(idValue);
  914. expect(replacedTableData).to.be.eql({
  915. [DEF_PK]: idValue,
  916. baz: replacing.foo,
  917. });
  918. });
  919. it('uses a specified column name for a regular property with a default value', async function () {
  920. const schema = new Schema();
  921. schema.defineDatasource({
  922. name: 'memory',
  923. adapter: 'memory',
  924. });
  925. schema.defineModel({
  926. name: 'model',
  927. datasource: 'memory',
  928. properties: {
  929. foo: {
  930. type: DataType.NUMBER,
  931. columnName: 'baz',
  932. default: 1,
  933. },
  934. bar: {
  935. type: DataType.NUMBER,
  936. default: 2,
  937. },
  938. },
  939. });
  940. const adapter = new MemoryAdapter(schema.container, {});
  941. const createdModelData = await adapter.create('model', {});
  942. const idValue = createdModelData[DEF_PK];
  943. const defaults = {foo: 1, bar: 2};
  944. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  945. const table = adapter._getTableOrCreate('model');
  946. const createdTableData = table.get(idValue);
  947. expect(createdTableData).to.be.eql({
  948. [DEF_PK]: idValue,
  949. baz: defaults.foo,
  950. bar: defaults.bar,
  951. });
  952. const replacing = {foo: 2};
  953. const replacedModelData = await adapter.replaceById(
  954. 'model',
  955. idValue,
  956. replacing,
  957. );
  958. expect(replacedModelData).to.be.eql({
  959. [DEF_PK]: idValue,
  960. foo: replacing.foo,
  961. bar: defaults.bar,
  962. });
  963. const replacedTableData = table.get(idValue);
  964. expect(replacedTableData).to.be.eql({
  965. [DEF_PK]: idValue,
  966. baz: replacing.foo,
  967. bar: defaults.bar,
  968. });
  969. });
  970. it('allows to specify a short form of a fields clause to filter a return value', async function () {
  971. const schema = new Schema();
  972. schema.defineDatasource({
  973. name: 'memory',
  974. adapter: 'memory',
  975. });
  976. schema.defineModel({
  977. name: 'model',
  978. datasource: 'memory',
  979. properties: {
  980. foo: DataType.STRING,
  981. bar: DataType.NUMBER,
  982. },
  983. });
  984. const adapter = new MemoryAdapter(schema.container, {});
  985. const input = {foo: 'string', bar: 10};
  986. const createdModelData = await adapter.create('model', input);
  987. const idValue = createdModelData[DEF_PK];
  988. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  989. const table = adapter._getTableOrCreate('model');
  990. const createdTableData = table.get(idValue);
  991. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  992. const replacedModelData = await adapter.replaceById(
  993. 'model',
  994. idValue,
  995. input,
  996. {fields: 'foo'},
  997. );
  998. expect(replacedModelData).to.be.eql({[DEF_PK]: idValue, foo: input.foo});
  999. const replacedTableData = table.get(idValue);
  1000. expect(replacedTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  1001. });
  1002. it('allows to specify a full form of a fields clause to filter a return value', async function () {
  1003. const schema = new Schema();
  1004. schema.defineDatasource({
  1005. name: 'memory',
  1006. adapter: 'memory',
  1007. });
  1008. schema.defineModel({
  1009. name: 'model',
  1010. datasource: 'memory',
  1011. properties: {
  1012. foo: DataType.STRING,
  1013. bar: DataType.NUMBER,
  1014. baz: DataType.BOOLEAN,
  1015. },
  1016. });
  1017. const adapter = new MemoryAdapter(schema.container, {});
  1018. const input = {foo: 'string', bar: 10, baz: true};
  1019. const createdModelData = await adapter.create('model', input);
  1020. const idValue = createdModelData[DEF_PK];
  1021. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  1022. const table = adapter._getTableOrCreate('model');
  1023. const createdTableData = table.get(idValue);
  1024. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  1025. const replacedModelData = await adapter.replaceById(
  1026. 'model',
  1027. idValue,
  1028. input,
  1029. {fields: ['foo', 'bar']},
  1030. );
  1031. expect(replacedModelData).to.be.eql({
  1032. [DEF_PK]: idValue,
  1033. foo: input.foo,
  1034. bar: input.bar,
  1035. });
  1036. const replacedTableData = table.get(idValue);
  1037. expect(replacedTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  1038. });
  1039. it('a fields clause uses property names instead of column names', async function () {
  1040. const schema = new Schema();
  1041. schema.defineDatasource({
  1042. name: 'memory',
  1043. adapter: 'memory',
  1044. });
  1045. schema.defineModel({
  1046. name: 'model',
  1047. datasource: 'memory',
  1048. properties: {
  1049. foo: {
  1050. type: DataType.STRING,
  1051. columnName: 'fooCol',
  1052. },
  1053. bar: {
  1054. type: DataType.NUMBER,
  1055. columnName: 'barCol',
  1056. },
  1057. baz: {
  1058. type: DataType.BOOLEAN,
  1059. columnName: 'bazCol',
  1060. },
  1061. },
  1062. });
  1063. const adapter = new MemoryAdapter(schema.container, {});
  1064. const input = {foo: 'string', bar: 10, baz: true};
  1065. const createdModelData = await adapter.create('model', input);
  1066. const idValue = createdModelData[DEF_PK];
  1067. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  1068. const table = adapter._getTableOrCreate('model');
  1069. const createdTableData = table.get(idValue);
  1070. expect(createdTableData).to.be.eql({
  1071. [DEF_PK]: idValue,
  1072. fooCol: input.foo,
  1073. barCol: input.bar,
  1074. bazCol: input.baz,
  1075. });
  1076. const replacedModelData = await adapter.replaceById(
  1077. 'model',
  1078. idValue,
  1079. input,
  1080. {fields: ['foo', 'bar']},
  1081. );
  1082. expect(replacedModelData).to.be.eql({
  1083. [DEF_PK]: idValue,
  1084. foo: input.foo,
  1085. bar: input.bar,
  1086. });
  1087. const replacedTableData = table.get(idValue);
  1088. expect(replacedTableData).to.be.eql({
  1089. [DEF_PK]: idValue,
  1090. fooCol: input.foo,
  1091. barCol: input.bar,
  1092. bazCol: input.baz,
  1093. });
  1094. });
  1095. });
  1096. describe('replaceOrCreate', function () {
  1097. it('generates a new identifier when a value of a primary key has not provided', async function () {
  1098. const schema = new Schema();
  1099. schema.defineDatasource({
  1100. name: 'memory',
  1101. adapter: 'memory',
  1102. });
  1103. schema.defineModel({
  1104. name: 'model',
  1105. datasource: 'memory',
  1106. properties: {
  1107. foo: DataType.STRING,
  1108. bar: DataType.NUMBER,
  1109. },
  1110. });
  1111. const adapter = new MemoryAdapter(schema.container, {});
  1112. const input = {foo: 'string', bar: 10};
  1113. const created = await adapter.replaceOrCreate('model', input);
  1114. const idValue = created[DEF_PK];
  1115. expect(created).to.be.eql({...input, [DEF_PK]: idValue});
  1116. const table = adapter._getTableOrCreate('model');
  1117. const tableData = table.get(idValue);
  1118. expect(tableData).to.be.eql({...input, [DEF_PK]: idValue});
  1119. });
  1120. it('generates a new identifier when a value of a primary key is undefined', async function () {
  1121. const schema = new Schema();
  1122. schema.defineDatasource({
  1123. name: 'memory',
  1124. adapter: 'memory',
  1125. });
  1126. schema.defineModel({
  1127. name: 'model',
  1128. datasource: 'memory',
  1129. properties: {
  1130. foo: DataType.STRING,
  1131. bar: DataType.NUMBER,
  1132. },
  1133. });
  1134. const adapter = new MemoryAdapter(schema.container, {});
  1135. const input = {
  1136. [DEF_PK]: undefined,
  1137. foo: 'string',
  1138. bar: 10,
  1139. };
  1140. const created = await adapter.replaceOrCreate('model', input);
  1141. const idValue = created[DEF_PK];
  1142. expect(created).to.be.eql({...input, [DEF_PK]: idValue});
  1143. const table = adapter._getTableOrCreate('model');
  1144. const tableData = table.get(idValue);
  1145. expect(tableData).to.be.eql({...input, [DEF_PK]: idValue});
  1146. });
  1147. it('generates a new identifier when a value of a primary key is null', async function () {
  1148. const schema = new Schema();
  1149. schema.defineDatasource({
  1150. name: 'memory',
  1151. adapter: 'memory',
  1152. });
  1153. schema.defineModel({
  1154. name: 'model',
  1155. datasource: 'memory',
  1156. properties: {
  1157. foo: DataType.STRING,
  1158. bar: DataType.NUMBER,
  1159. },
  1160. });
  1161. const adapter = new MemoryAdapter(schema.container, {});
  1162. const input = {
  1163. [DEF_PK]: null,
  1164. foo: 'string',
  1165. bar: 10,
  1166. };
  1167. const created = await adapter.replaceOrCreate('model', input);
  1168. const idValue = created[DEF_PK];
  1169. expect(created).to.be.eql({...input, [DEF_PK]: idValue});
  1170. const table = adapter._getTableOrCreate('model');
  1171. const tableData = table.get(idValue);
  1172. expect(tableData).to.be.eql({...input, [DEF_PK]: idValue});
  1173. });
  1174. it('generates a new identifier for a primary key of a "number" type', async function () {
  1175. const schema = new Schema();
  1176. schema.defineDatasource({
  1177. name: 'memory',
  1178. adapter: 'memory',
  1179. });
  1180. schema.defineModel({
  1181. name: 'model',
  1182. datasource: 'memory',
  1183. properties: {
  1184. myId: {
  1185. type: DataType.NUMBER,
  1186. primaryKey: true,
  1187. },
  1188. },
  1189. });
  1190. const adapter = new MemoryAdapter(schema.container, {});
  1191. const result1 = await adapter.replaceOrCreate('model', {});
  1192. const result2 = await adapter.replaceOrCreate('model', {});
  1193. const result3 = await adapter.replaceOrCreate('model', {});
  1194. expect(result1).to.be.eql({myId: 1});
  1195. expect(result2).to.be.eql({myId: 2});
  1196. expect(result3).to.be.eql({myId: 3});
  1197. });
  1198. it('generates a new identifier for a primary key of an "any" type', async function () {
  1199. const schema = new Schema();
  1200. schema.defineDatasource({
  1201. name: 'memory',
  1202. adapter: 'memory',
  1203. });
  1204. schema.defineModel({
  1205. name: 'model',
  1206. datasource: 'memory',
  1207. properties: {
  1208. myId: {
  1209. type: DataType.ANY,
  1210. primaryKey: true,
  1211. },
  1212. },
  1213. });
  1214. const adapter = new MemoryAdapter(schema.container, {});
  1215. const result1 = await adapter.replaceOrCreate('model', {});
  1216. const result2 = await adapter.replaceOrCreate('model', {});
  1217. const result3 = await adapter.replaceOrCreate('model', {});
  1218. expect(result1).to.be.eql({myId: 1});
  1219. expect(result2).to.be.eql({myId: 2});
  1220. expect(result3).to.be.eql({myId: 3});
  1221. });
  1222. it('throws an error when generating a new value for a primary key of a "string" type', async function () {
  1223. const schema = new Schema();
  1224. schema.defineDatasource({
  1225. name: 'memory',
  1226. adapter: 'memory',
  1227. });
  1228. schema.defineModel({
  1229. name: 'model',
  1230. datasource: 'memory',
  1231. properties: {
  1232. myId: {
  1233. type: DataType.STRING,
  1234. primaryKey: true,
  1235. },
  1236. },
  1237. });
  1238. const adapter = new MemoryAdapter(schema.container, {});
  1239. const promise = adapter.replaceOrCreate('model', {
  1240. foo: 'string',
  1241. bar: 10,
  1242. });
  1243. await expect(promise).to.be.rejectedWith(
  1244. 'The memory adapter able to generate only Number identifiers, ' +
  1245. 'but the primary key "myId" of the model "model" is defined as String. ' +
  1246. 'Do provide your own value for the "myId" property, or change the type ' +
  1247. 'in the primary key definition to a Number that will be ' +
  1248. 'generated automatically.',
  1249. );
  1250. });
  1251. it('throws an error when generating a new value for a primary key of a "boolean" type', async function () {
  1252. const schema = new Schema();
  1253. schema.defineDatasource({
  1254. name: 'memory',
  1255. adapter: 'memory',
  1256. });
  1257. schema.defineModel({
  1258. name: 'model',
  1259. datasource: 'memory',
  1260. properties: {
  1261. myId: {
  1262. type: DataType.BOOLEAN,
  1263. primaryKey: true,
  1264. },
  1265. },
  1266. });
  1267. const adapter = new MemoryAdapter(schema.container, {});
  1268. const promise = adapter.replaceOrCreate('model', {
  1269. foo: 'string',
  1270. bar: 10,
  1271. });
  1272. await expect(promise).to.be.rejectedWith(
  1273. 'The memory adapter able to generate only Number identifiers, ' +
  1274. 'but the primary key "myId" of the model "model" is defined as Boolean. ' +
  1275. 'Do provide your own value for the "myId" property, or change the type ' +
  1276. 'in the primary key definition to a Number that will be ' +
  1277. 'generated automatically.',
  1278. );
  1279. });
  1280. it('throws an error when generating a new value for a primary key of an "array" type', async function () {
  1281. const schema = new Schema();
  1282. schema.defineDatasource({
  1283. name: 'memory',
  1284. adapter: 'memory',
  1285. });
  1286. schema.defineModel({
  1287. name: 'model',
  1288. datasource: 'memory',
  1289. properties: {
  1290. myId: {
  1291. type: DataType.ARRAY,
  1292. itemType: DataType.NUMBER,
  1293. primaryKey: true,
  1294. },
  1295. },
  1296. });
  1297. const adapter = new MemoryAdapter(schema.container, {});
  1298. const promise = adapter.replaceOrCreate('model', {});
  1299. await expect(promise).to.be.rejectedWith(
  1300. 'The memory adapter able to generate only Number identifiers, ' +
  1301. 'but the primary key "myId" of the model "model" is defined as Array. ' +
  1302. 'Do provide your own value for the "myId" property, or change the type ' +
  1303. 'in the primary key definition to a Number that will be ' +
  1304. 'generated automatically.',
  1305. );
  1306. });
  1307. it('throws an error when generating a new value for a primary key of an "object" type', async function () {
  1308. const schema = new Schema();
  1309. schema.defineDatasource({
  1310. name: 'memory',
  1311. adapter: 'memory',
  1312. });
  1313. schema.defineModel({
  1314. name: 'model',
  1315. datasource: 'memory',
  1316. properties: {
  1317. myId: {
  1318. type: DataType.OBJECT,
  1319. primaryKey: true,
  1320. },
  1321. },
  1322. });
  1323. const adapter = new MemoryAdapter(schema.container, {});
  1324. const promise = adapter.replaceOrCreate('model', {});
  1325. await expect(promise).to.be.rejectedWith(
  1326. 'The memory adapter able to generate only Number identifiers, ' +
  1327. 'but the primary key "myId" of the model "model" is defined as Object. ' +
  1328. 'Do provide your own value for the "myId" property, or change the type ' +
  1329. 'in the primary key definition to a Number that will be ' +
  1330. 'generated automatically.',
  1331. );
  1332. });
  1333. it('allows to specify an identifier value for a new item', async function () {
  1334. const schema = new Schema();
  1335. schema.defineDatasource({
  1336. name: 'memory',
  1337. adapter: 'memory',
  1338. });
  1339. schema.defineModel({
  1340. name: 'model',
  1341. datasource: 'memory',
  1342. properties: {
  1343. foo: DataType.STRING,
  1344. bar: DataType.NUMBER,
  1345. },
  1346. });
  1347. const adapter = new MemoryAdapter(schema.container, {});
  1348. const idValue = 5;
  1349. const input = {foo: 'string', bar: 10};
  1350. const created = await adapter.replaceOrCreate('model', {
  1351. [DEF_PK]: idValue,
  1352. ...input,
  1353. });
  1354. expect(created).to.be.eql({[DEF_PK]: idValue, ...input});
  1355. const table = adapter._getTableOrCreate('model');
  1356. const tableData = table.get(idValue);
  1357. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...input});
  1358. });
  1359. it('sets default values if they are not provided for a new item', async function () {
  1360. const schema = new Schema();
  1361. schema.defineDatasource({
  1362. name: 'memory',
  1363. adapter: 'memory',
  1364. });
  1365. schema.defineModel({
  1366. name: 'model',
  1367. datasource: 'memory',
  1368. properties: {
  1369. foo: {
  1370. type: DataType.NUMBER,
  1371. default: 10,
  1372. },
  1373. bar: {
  1374. type: DataType.STRING,
  1375. default: 'string',
  1376. },
  1377. },
  1378. });
  1379. const adapter = new MemoryAdapter(schema.container, {});
  1380. const created = await adapter.replaceOrCreate('model', {});
  1381. const idValue = created[DEF_PK];
  1382. const defaults = {foo: 10, bar: 'string'};
  1383. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1384. const table = adapter._getTableOrCreate('model');
  1385. const tableData = table.get(idValue);
  1386. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1387. });
  1388. it('sets default values for properties provided with an undefined value', async function () {
  1389. const schema = new Schema();
  1390. schema.defineDatasource({
  1391. name: 'memory',
  1392. adapter: 'memory',
  1393. });
  1394. schema.defineModel({
  1395. name: 'model',
  1396. datasource: 'memory',
  1397. properties: {
  1398. foo: {
  1399. type: DataType.NUMBER,
  1400. default: 1,
  1401. },
  1402. bar: {
  1403. type: DataType.NUMBER,
  1404. default: 2,
  1405. },
  1406. },
  1407. });
  1408. const adapter = new MemoryAdapter(schema.container, {});
  1409. const created = await adapter.replaceOrCreate('model', {foo: undefined});
  1410. const idValue = created[DEF_PK];
  1411. const defaults = {foo: 1, bar: 2};
  1412. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1413. const table = adapter._getTableOrCreate('model');
  1414. const tableData = table.get(idValue);
  1415. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1416. });
  1417. it('sets default values for properties provided with a null value', async function () {
  1418. const schema = new Schema();
  1419. schema.defineDatasource({
  1420. name: 'memory',
  1421. adapter: 'memory',
  1422. });
  1423. schema.defineModel({
  1424. name: 'model',
  1425. datasource: 'memory',
  1426. properties: {
  1427. foo: {
  1428. type: DataType.NUMBER,
  1429. default: 1,
  1430. },
  1431. bar: {
  1432. type: DataType.NUMBER,
  1433. default: 2,
  1434. },
  1435. },
  1436. });
  1437. const adapter = new MemoryAdapter(schema.container, {});
  1438. const created = await adapter.replaceOrCreate('model', {foo: null});
  1439. const idValue = created[DEF_PK];
  1440. const defaults = {foo: 1, bar: 2};
  1441. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1442. const table = adapter._getTableOrCreate('model');
  1443. const tableData = table.get(idValue);
  1444. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1445. });
  1446. it('uses a specified column name for a primary key', async function () {
  1447. const schema = new Schema();
  1448. schema.defineDatasource({
  1449. name: 'memory',
  1450. adapter: 'memory',
  1451. });
  1452. schema.defineModel({
  1453. name: 'model',
  1454. datasource: 'memory',
  1455. properties: {
  1456. foo: {
  1457. type: DataType.NUMBER,
  1458. primaryKey: true,
  1459. columnName: 'bar',
  1460. },
  1461. },
  1462. });
  1463. const adapter = new MemoryAdapter(schema.container, {});
  1464. const created = await adapter.replaceOrCreate('model', {});
  1465. expect(created).to.be.eql({foo: created.foo});
  1466. const table = adapter._getTableOrCreate('model');
  1467. const tableData = table.get(created.foo);
  1468. expect(tableData).to.be.eql({bar: created.foo});
  1469. });
  1470. it('uses a specified column name for a regular property', async function () {
  1471. const schema = new Schema();
  1472. schema.defineDatasource({
  1473. name: 'memory',
  1474. adapter: 'memory',
  1475. });
  1476. schema.defineModel({
  1477. name: 'model',
  1478. datasource: 'memory',
  1479. properties: {
  1480. foo: {
  1481. type: DataType.NUMBER,
  1482. columnName: 'bar',
  1483. },
  1484. },
  1485. });
  1486. const adapter = new MemoryAdapter(schema.container, {});
  1487. const created = await adapter.replaceOrCreate('model', {foo: 10});
  1488. const idValue = created[DEF_PK];
  1489. expect(created).to.be.eql({[DEF_PK]: idValue, foo: 10});
  1490. const table = adapter._getTableOrCreate('model');
  1491. const tableData = table.get(idValue);
  1492. expect(tableData).to.be.eql({[DEF_PK]: idValue, bar: 10});
  1493. });
  1494. it('uses a specified column name for a regular property with a default value', async function () {
  1495. const schema = new Schema();
  1496. schema.defineDatasource({
  1497. name: 'memory',
  1498. adapter: 'memory',
  1499. });
  1500. schema.defineModel({
  1501. name: 'model',
  1502. datasource: 'memory',
  1503. properties: {
  1504. foo: {
  1505. type: DataType.NUMBER,
  1506. columnName: 'bar',
  1507. default: 10,
  1508. },
  1509. },
  1510. });
  1511. const adapter = new MemoryAdapter(schema.container, {});
  1512. const created = await adapter.replaceOrCreate('model', {});
  1513. const idValue = created[DEF_PK];
  1514. expect(created).to.be.eql({[DEF_PK]: idValue, foo: 10});
  1515. const table = adapter._getTableOrCreate('model');
  1516. const tableData = table.get(idValue);
  1517. expect(tableData).to.be.eql({[DEF_PK]: idValue, bar: 10});
  1518. });
  1519. it('uses a short form of a fields clause to filter a return value', async function () {
  1520. const schema = new Schema();
  1521. schema.defineDatasource({
  1522. name: 'memory',
  1523. adapter: 'memory',
  1524. });
  1525. schema.defineModel({
  1526. name: 'model',
  1527. datasource: 'memory',
  1528. properties: {
  1529. foo: DataType.STRING,
  1530. bar: DataType.NUMBER,
  1531. },
  1532. });
  1533. const adapter = new MemoryAdapter(schema.container, {});
  1534. const input = {foo: 'string', bar: 10};
  1535. const filter = {fields: 'foo'};
  1536. const result = await adapter.replaceOrCreate('model', input, filter);
  1537. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: input.foo});
  1538. });
  1539. it('uses a full form of a fields clause to filter a return value', async function () {
  1540. const schema = new Schema();
  1541. schema.defineDatasource({
  1542. name: 'memory',
  1543. adapter: 'memory',
  1544. });
  1545. schema.defineModel({
  1546. name: 'model',
  1547. datasource: 'memory',
  1548. properties: {
  1549. foo: DataType.STRING,
  1550. bar: DataType.NUMBER,
  1551. baz: DataType.BOOLEAN,
  1552. },
  1553. });
  1554. const adapter = new MemoryAdapter(schema.container, {});
  1555. const input = {foo: 'string', bar: 10, baz: true};
  1556. const filter = {fields: ['foo', 'bar']};
  1557. const result = await adapter.replaceOrCreate('model', input, filter);
  1558. expect(result).to.be.eql({
  1559. [DEF_PK]: result[DEF_PK],
  1560. foo: input.foo,
  1561. bar: input.bar,
  1562. });
  1563. });
  1564. it('a fields clause uses property names instead of column names', async function () {
  1565. const schema = new Schema();
  1566. schema.defineDatasource({
  1567. name: 'memory',
  1568. adapter: 'memory',
  1569. });
  1570. schema.defineModel({
  1571. name: 'model',
  1572. datasource: 'memory',
  1573. properties: {
  1574. foo: {
  1575. type: DataType.STRING,
  1576. columnName: 'fooCol',
  1577. },
  1578. bar: {
  1579. type: DataType.NUMBER,
  1580. columnName: 'barCol',
  1581. },
  1582. baz: {
  1583. type: DataType.BOOLEAN,
  1584. columnName: 'bazCol',
  1585. },
  1586. },
  1587. });
  1588. const adapter = new MemoryAdapter(schema.container, {});
  1589. const input = {foo: 'string', bar: 10, baz: true};
  1590. const filter = {fields: ['foo', 'bar']};
  1591. const result = await adapter.replaceOrCreate('model', input, filter);
  1592. expect(result).to.be.eql({
  1593. [DEF_PK]: result[DEF_PK],
  1594. foo: input.foo,
  1595. bar: input.bar,
  1596. });
  1597. });
  1598. it('removes properties when replacing an item by a given identifier', async function () {
  1599. const schema = new Schema();
  1600. schema.defineDatasource({
  1601. name: 'memory',
  1602. adapter: 'memory',
  1603. });
  1604. schema.defineModel({
  1605. name: 'model',
  1606. datasource: 'memory',
  1607. properties: {
  1608. foo: DataType.NUMBER,
  1609. bar: DataType.NUMBER,
  1610. },
  1611. });
  1612. const adapter = new MemoryAdapter(schema.container, {});
  1613. const input = {foo: 1, bar: 2};
  1614. const created = await adapter.create('model', input);
  1615. const idValue = created[DEF_PK];
  1616. expect(created).to.be.eql({[DEF_PK]: idValue, ...input});
  1617. const replacement = {[DEF_PK]: idValue, foo: 2};
  1618. const replaced = await adapter.replaceOrCreate('model', replacement);
  1619. expect(replaced).to.be.eql(replacement);
  1620. const table = adapter._getTableOrCreate('model');
  1621. const tableData = table.get(idValue);
  1622. expect(tableData).to.be.eql(replacement);
  1623. });
  1624. it('sets a default values for removed properties when replacing an item', async function () {
  1625. const schema = new Schema();
  1626. schema.defineDatasource({
  1627. name: 'memory',
  1628. adapter: 'memory',
  1629. });
  1630. schema.defineModel({
  1631. name: 'model',
  1632. datasource: 'memory',
  1633. properties: {
  1634. foo: {
  1635. type: DataType.NUMBER,
  1636. default: 1,
  1637. },
  1638. bar: {
  1639. type: DataType.NUMBER,
  1640. default: 2,
  1641. },
  1642. },
  1643. });
  1644. const adapter = new MemoryAdapter(schema.container, {});
  1645. const created = await adapter.create('model', {});
  1646. const idValue = created[DEF_PK];
  1647. const defaults = {foo: 1, bar: 2};
  1648. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1649. const replacement = {[DEF_PK]: idValue, foo: 2};
  1650. const replaced = await adapter.replaceOrCreate('model', replacement);
  1651. expect(replaced).to.be.eql({...defaults, ...replacement});
  1652. const table = adapter._getTableOrCreate('model');
  1653. const tableData = table.get(idValue);
  1654. expect(tableData).to.be.eql({...defaults, ...replacement});
  1655. });
  1656. it('sets a default values for replaced properties with an undefined value', async function () {
  1657. const schema = new Schema();
  1658. schema.defineDatasource({
  1659. name: 'memory',
  1660. adapter: 'memory',
  1661. });
  1662. schema.defineModel({
  1663. name: 'model',
  1664. datasource: 'memory',
  1665. properties: {
  1666. foo: {
  1667. type: DataType.NUMBER,
  1668. default: 1,
  1669. },
  1670. bar: {
  1671. type: DataType.NUMBER,
  1672. default: 2,
  1673. },
  1674. },
  1675. });
  1676. const adapter = new MemoryAdapter(schema.container, {});
  1677. const created = await adapter.create('model', {});
  1678. const idValue = created[DEF_PK];
  1679. const defaults = {foo: 1, bar: 2};
  1680. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1681. const replaced = await adapter.replaceOrCreate('model', {
  1682. [DEF_PK]: idValue,
  1683. foo: undefined,
  1684. });
  1685. expect(replaced).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1686. const table = adapter._getTableOrCreate('model');
  1687. const tableData = table.get(idValue);
  1688. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1689. });
  1690. it('sets a default values for replaced properties with a null value', async function () {
  1691. const schema = new Schema();
  1692. schema.defineDatasource({
  1693. name: 'memory',
  1694. adapter: 'memory',
  1695. });
  1696. schema.defineModel({
  1697. name: 'model',
  1698. datasource: 'memory',
  1699. properties: {
  1700. foo: {
  1701. type: DataType.NUMBER,
  1702. default: 1,
  1703. },
  1704. bar: {
  1705. type: DataType.NUMBER,
  1706. default: 2,
  1707. },
  1708. },
  1709. });
  1710. const adapter = new MemoryAdapter(schema.container, {});
  1711. const created = await adapter.create('model', {});
  1712. const idValue = created[DEF_PK];
  1713. const defaults = {foo: 1, bar: 2};
  1714. expect(created).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1715. const replaced = await adapter.replaceOrCreate('model', {
  1716. [DEF_PK]: idValue,
  1717. foo: null,
  1718. });
  1719. expect(replaced).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1720. const table = adapter._getTableOrCreate('model');
  1721. const tableData = table.get(idValue);
  1722. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  1723. });
  1724. });
  1725. describe('patch', function () {
  1726. it('updates only provided properties for all items and returns their number', async function () {
  1727. const schema = new Schema();
  1728. schema.defineDatasource({
  1729. name: 'memory',
  1730. adapter: 'memory',
  1731. });
  1732. schema.defineModel({
  1733. name: 'model',
  1734. datasource: 'memory',
  1735. properties: {
  1736. foo: DataType.STRING,
  1737. bar: DataType.STRING,
  1738. },
  1739. });
  1740. const adapter = new MemoryAdapter(schema.container, {});
  1741. const input1 = {foo: 'a1', bar: 'a2'};
  1742. const input2 = {foo: 'b1', bar: 'b2'};
  1743. const input3 = {foo: 'c1', bar: 'c2'};
  1744. const created1 = await adapter.create('model', input1);
  1745. const created2 = await adapter.create('model', input2);
  1746. const created3 = await adapter.create('model', input3);
  1747. const id1 = created1[DEF_PK];
  1748. const id2 = created2[DEF_PK];
  1749. const id3 = created3[DEF_PK];
  1750. const table = adapter._getTableOrCreate('model');
  1751. const createdItems = Array.from(table.values());
  1752. expect(createdItems).to.be.eql([
  1753. {[DEF_PK]: id1, ...input1},
  1754. {[DEF_PK]: id2, ...input2},
  1755. {[DEF_PK]: id3, ...input3},
  1756. ]);
  1757. const result = await adapter.patch('model', {foo: 'd1'});
  1758. expect(result).to.be.eq(3);
  1759. const patchedItems = Array.from(table.values());
  1760. expect(patchedItems).to.be.eql([
  1761. {[DEF_PK]: id1, foo: 'd1', bar: 'a2'},
  1762. {[DEF_PK]: id2, foo: 'd1', bar: 'b2'},
  1763. {[DEF_PK]: id3, foo: 'd1', bar: 'c2'},
  1764. ]);
  1765. });
  1766. it('does not throw an error if a partial data does not have required property', async function () {
  1767. const schema = new Schema();
  1768. schema.defineDatasource({
  1769. name: 'memory',
  1770. adapter: 'memory',
  1771. });
  1772. schema.defineModel({
  1773. name: 'model',
  1774. datasource: 'memory',
  1775. properties: {
  1776. foo: DataType.STRING,
  1777. bar: {
  1778. type: DataType.STRING,
  1779. required: true,
  1780. },
  1781. },
  1782. });
  1783. const adapter = new MemoryAdapter(schema.container, {});
  1784. const input1 = {foo: 'a1', bar: 'a2'};
  1785. const input2 = {foo: 'b1', bar: 'b2'};
  1786. const input3 = {foo: 'c1', bar: 'c2'};
  1787. const created1 = await adapter.create('model', input1);
  1788. const created2 = await adapter.create('model', input2);
  1789. const created3 = await adapter.create('model', input3);
  1790. const id1 = created1[DEF_PK];
  1791. const id2 = created2[DEF_PK];
  1792. const id3 = created3[DEF_PK];
  1793. const table = adapter._getTableOrCreate('model');
  1794. const createdItems = Array.from(table.values());
  1795. expect(createdItems).to.be.eql([
  1796. {[DEF_PK]: id1, ...input1},
  1797. {[DEF_PK]: id2, ...input2},
  1798. {[DEF_PK]: id3, ...input3},
  1799. ]);
  1800. const result = await adapter.patch('model', {foo: 'd1'});
  1801. expect(result).to.be.eq(3);
  1802. const patchedItems = Array.from(table.values());
  1803. expect(patchedItems).to.be.eql([
  1804. {[DEF_PK]: id1, foo: 'd1', bar: 'a2'},
  1805. {[DEF_PK]: id2, foo: 'd1', bar: 'b2'},
  1806. {[DEF_PK]: id3, foo: 'd1', bar: 'c2'},
  1807. ]);
  1808. });
  1809. it('ignores identifier value in a given data in case of a default primary key', async function () {
  1810. const schema = new Schema();
  1811. schema.defineDatasource({
  1812. name: 'memory',
  1813. adapter: 'memory',
  1814. });
  1815. schema.defineModel({
  1816. name: 'model',
  1817. datasource: 'memory',
  1818. properties: {
  1819. foo: DataType.STRING,
  1820. bar: DataType.STRING,
  1821. },
  1822. });
  1823. const adapter = new MemoryAdapter(schema.container, {});
  1824. const input1 = {foo: 'a1', bar: 'a2'};
  1825. const input2 = {foo: 'b1', bar: 'b2'};
  1826. const input3 = {foo: 'c1', bar: 'c2'};
  1827. const created1 = await adapter.create('model', input1);
  1828. const created2 = await adapter.create('model', input2);
  1829. const created3 = await adapter.create('model', input3);
  1830. const id1 = created1[DEF_PK];
  1831. const id2 = created2[DEF_PK];
  1832. const id3 = created3[DEF_PK];
  1833. const table = adapter._getTableOrCreate('model');
  1834. const createdItems = Array.from(table.values());
  1835. expect(createdItems).to.be.eql([
  1836. {[DEF_PK]: id1, ...input1},
  1837. {[DEF_PK]: id2, ...input2},
  1838. {[DEF_PK]: id3, ...input3},
  1839. ]);
  1840. const result = await adapter.patch('model', {[DEF_PK]: 100, foo: 'd1'});
  1841. expect(result).to.be.eq(3);
  1842. const patchedItems = Array.from(table.values());
  1843. expect(patchedItems).to.be.eql([
  1844. {[DEF_PK]: id1, foo: 'd1', bar: 'a2'},
  1845. {[DEF_PK]: id2, foo: 'd1', bar: 'b2'},
  1846. {[DEF_PK]: id3, foo: 'd1', bar: 'c2'},
  1847. ]);
  1848. });
  1849. it('ignores identifier value in a given data in case of a specified primary key', async function () {
  1850. const schema = new Schema();
  1851. schema.defineDatasource({
  1852. name: 'memory',
  1853. adapter: 'memory',
  1854. });
  1855. schema.defineModel({
  1856. name: 'model',
  1857. datasource: 'memory',
  1858. properties: {
  1859. myId: {
  1860. type: DataType.NUMBER,
  1861. primaryKey: true,
  1862. },
  1863. foo: DataType.STRING,
  1864. bar: DataType.STRING,
  1865. },
  1866. });
  1867. const adapter = new MemoryAdapter(schema.container, {});
  1868. const input1 = {foo: 'a1', bar: 'a2'};
  1869. const input2 = {foo: 'b1', bar: 'b2'};
  1870. const input3 = {foo: 'c1', bar: 'c2'};
  1871. const created1 = await adapter.create('model', input1);
  1872. const created2 = await adapter.create('model', input2);
  1873. const created3 = await adapter.create('model', input3);
  1874. const id1 = created1.myId;
  1875. const id2 = created2.myId;
  1876. const id3 = created3.myId;
  1877. const table = adapter._getTableOrCreate('model');
  1878. const createdItems = Array.from(table.values());
  1879. expect(createdItems).to.be.eql([
  1880. {myId: id1, ...input1},
  1881. {myId: id2, ...input2},
  1882. {myId: id3, ...input3},
  1883. ]);
  1884. const result = await adapter.patch('model', {myId: 100, foo: 'd1'});
  1885. expect(result).to.be.eq(3);
  1886. const patchedItems = Array.from(table.values());
  1887. expect(patchedItems).to.be.eql([
  1888. {myId: id1, foo: 'd1', bar: 'a2'},
  1889. {myId: id2, foo: 'd1', bar: 'b2'},
  1890. {myId: id3, foo: 'd1', bar: 'c2'},
  1891. ]);
  1892. });
  1893. it('sets a default values for patched properties with an undefined value', async function () {
  1894. const schema = new Schema();
  1895. schema.defineDatasource({
  1896. name: 'memory',
  1897. adapter: 'memory',
  1898. });
  1899. schema.defineModel({
  1900. name: 'model',
  1901. datasource: 'memory',
  1902. properties: {
  1903. foo: {
  1904. type: DataType.STRING,
  1905. default: 'fooVal',
  1906. },
  1907. bar: {
  1908. type: DataType.STRING,
  1909. default: 'barVal',
  1910. },
  1911. },
  1912. });
  1913. const adapter = new MemoryAdapter(schema.container, {});
  1914. const input1 = {foo: 'a1', bar: 'a2'};
  1915. const input2 = {foo: 'b1', bar: 'b2'};
  1916. const input3 = {foo: 'c1', bar: 'c2'};
  1917. const created1 = await adapter.create('model', input1);
  1918. const created2 = await adapter.create('model', input2);
  1919. const created3 = await adapter.create('model', input3);
  1920. const id1 = created1[DEF_PK];
  1921. const id2 = created2[DEF_PK];
  1922. const id3 = created3[DEF_PK];
  1923. const table = adapter._getTableOrCreate('model');
  1924. const createdItems = Array.from(table.values());
  1925. expect(createdItems).to.be.eql([
  1926. {[DEF_PK]: id1, ...input1},
  1927. {[DEF_PK]: id2, ...input2},
  1928. {[DEF_PK]: id3, ...input3},
  1929. ]);
  1930. const result = await adapter.patch('model', {foo: undefined});
  1931. expect(result).to.be.eq(3);
  1932. const patchedItems = Array.from(table.values());
  1933. expect(patchedItems).to.be.eql([
  1934. {[DEF_PK]: id1, foo: 'fooVal', bar: 'a2'},
  1935. {[DEF_PK]: id2, foo: 'fooVal', bar: 'b2'},
  1936. {[DEF_PK]: id3, foo: 'fooVal', bar: 'c2'},
  1937. ]);
  1938. });
  1939. it('sets a default values for patched properties with a null value', async function () {
  1940. const schema = new Schema();
  1941. schema.defineDatasource({
  1942. name: 'memory',
  1943. adapter: 'memory',
  1944. });
  1945. schema.defineModel({
  1946. name: 'model',
  1947. datasource: 'memory',
  1948. properties: {
  1949. foo: {
  1950. type: DataType.STRING,
  1951. default: 'fooVal',
  1952. },
  1953. bar: {
  1954. type: DataType.STRING,
  1955. default: 'barVal',
  1956. },
  1957. },
  1958. });
  1959. const adapter = new MemoryAdapter(schema.container, {});
  1960. const input1 = {foo: 'a1', bar: 'a2'};
  1961. const input2 = {foo: 'b1', bar: 'b2'};
  1962. const input3 = {foo: 'c1', bar: 'c2'};
  1963. const created1 = await adapter.create('model', input1);
  1964. const created2 = await adapter.create('model', input2);
  1965. const created3 = await adapter.create('model', input3);
  1966. const id1 = created1[DEF_PK];
  1967. const id2 = created2[DEF_PK];
  1968. const id3 = created3[DEF_PK];
  1969. const table = adapter._getTableOrCreate('model');
  1970. const createdItems = Array.from(table.values());
  1971. expect(createdItems).to.be.eql([
  1972. {[DEF_PK]: id1, ...input1},
  1973. {[DEF_PK]: id2, ...input2},
  1974. {[DEF_PK]: id3, ...input3},
  1975. ]);
  1976. const result = await adapter.patch('model', {foo: null});
  1977. expect(result).to.be.eq(3);
  1978. const patchedItems = Array.from(table.values());
  1979. expect(patchedItems).to.be.eql([
  1980. {[DEF_PK]: id1, foo: 'fooVal', bar: 'a2'},
  1981. {[DEF_PK]: id2, foo: 'fooVal', bar: 'b2'},
  1982. {[DEF_PK]: id3, foo: 'fooVal', bar: 'c2'},
  1983. ]);
  1984. });
  1985. it('uses a specified column name for a regular property', async function () {
  1986. const schema = new Schema();
  1987. schema.defineDatasource({
  1988. name: 'memory',
  1989. adapter: 'memory',
  1990. });
  1991. schema.defineModel({
  1992. name: 'model',
  1993. datasource: 'memory',
  1994. properties: {
  1995. foo: {
  1996. type: DataType.STRING,
  1997. columnName: 'fooCol',
  1998. },
  1999. bar: {
  2000. type: DataType.STRING,
  2001. columnName: 'barCol',
  2002. },
  2003. },
  2004. });
  2005. const adapter = new MemoryAdapter(schema.container, {});
  2006. const input1 = {foo: 'a1', bar: 'a2'};
  2007. const input2 = {foo: 'b1', bar: 'b2'};
  2008. const input3 = {foo: 'c1', bar: 'c2'};
  2009. const created1 = await adapter.create('model', input1);
  2010. const created2 = await adapter.create('model', input2);
  2011. const created3 = await adapter.create('model', input3);
  2012. const id1 = created1[DEF_PK];
  2013. const id2 = created2[DEF_PK];
  2014. const id3 = created3[DEF_PK];
  2015. const table = adapter._getTableOrCreate('model');
  2016. const createdItems = Array.from(table.values());
  2017. expect(createdItems).to.be.eql([
  2018. {[DEF_PK]: id1, fooCol: 'a1', barCol: 'a2'},
  2019. {[DEF_PK]: id2, fooCol: 'b1', barCol: 'b2'},
  2020. {[DEF_PK]: id3, fooCol: 'c1', barCol: 'c2'},
  2021. ]);
  2022. const result = await adapter.patch('model', {foo: 'd1'});
  2023. expect(result).to.be.eq(3);
  2024. const patchedItems = Array.from(table.values());
  2025. expect(patchedItems).to.be.eql([
  2026. {[DEF_PK]: id1, fooCol: 'd1', barCol: 'a2'},
  2027. {[DEF_PK]: id2, fooCol: 'd1', barCol: 'b2'},
  2028. {[DEF_PK]: id3, fooCol: 'd1', barCol: 'c2'},
  2029. ]);
  2030. });
  2031. it('uses a specified column name for a regular property with a default value', async function () {
  2032. const schema = new Schema();
  2033. schema.defineDatasource({
  2034. name: 'memory',
  2035. adapter: 'memory',
  2036. });
  2037. schema.defineModel({
  2038. name: 'model',
  2039. datasource: 'memory',
  2040. properties: {
  2041. foo: {
  2042. type: DataType.STRING,
  2043. columnName: 'fooCol',
  2044. default: 'fooVal',
  2045. },
  2046. bar: {
  2047. type: DataType.STRING,
  2048. columnName: 'barCol',
  2049. default: 'barVal',
  2050. },
  2051. },
  2052. });
  2053. const adapter = new MemoryAdapter(schema.container, {});
  2054. const input1 = {foo: 'a1', bar: 'a2'};
  2055. const input2 = {foo: 'b1', bar: 'b2'};
  2056. const input3 = {foo: 'c1', bar: 'c2'};
  2057. const created1 = await adapter.create('model', input1);
  2058. const created2 = await adapter.create('model', input2);
  2059. const created3 = await adapter.create('model', input3);
  2060. const id1 = created1[DEF_PK];
  2061. const id2 = created2[DEF_PK];
  2062. const id3 = created3[DEF_PK];
  2063. const table = adapter._getTableOrCreate('model');
  2064. const createdItems = Array.from(table.values());
  2065. expect(createdItems).to.be.eql([
  2066. {[DEF_PK]: id1, fooCol: 'a1', barCol: 'a2'},
  2067. {[DEF_PK]: id2, fooCol: 'b1', barCol: 'b2'},
  2068. {[DEF_PK]: id3, fooCol: 'c1', barCol: 'c2'},
  2069. ]);
  2070. const result = await adapter.patch('model', {foo: undefined});
  2071. expect(result).to.be.eq(3);
  2072. const patchedItems = Array.from(table.values());
  2073. expect(patchedItems).to.be.eql([
  2074. {[DEF_PK]: id1, fooCol: 'fooVal', barCol: 'a2'},
  2075. {[DEF_PK]: id2, fooCol: 'fooVal', barCol: 'b2'},
  2076. {[DEF_PK]: id3, fooCol: 'fooVal', barCol: 'c2'},
  2077. ]);
  2078. });
  2079. it('returns zero if nothing matched by the "where" clause', async function () {
  2080. const schema = new Schema();
  2081. schema.defineDatasource({
  2082. name: 'memory',
  2083. adapter: 'memory',
  2084. });
  2085. schema.defineModel({
  2086. name: 'model',
  2087. datasource: 'memory',
  2088. properties: {
  2089. foo: DataType.STRING,
  2090. bar: DataType.STRING,
  2091. },
  2092. });
  2093. const adapter = new MemoryAdapter(schema.container, {});
  2094. const input1 = {foo: 'a1', bar: 'a2'};
  2095. const input2 = {foo: 'b1', bar: 'b2'};
  2096. const input3 = {foo: 'c1', bar: 'c2'};
  2097. const created1 = await adapter.create('model', input1);
  2098. const created2 = await adapter.create('model', input2);
  2099. const created3 = await adapter.create('model', input3);
  2100. const id1 = created1[DEF_PK];
  2101. const id2 = created2[DEF_PK];
  2102. const id3 = created3[DEF_PK];
  2103. const table = adapter._getTableOrCreate('model');
  2104. const createdItems = Array.from(table.values());
  2105. expect(createdItems).to.be.eql([
  2106. {[DEF_PK]: id1, ...input1},
  2107. {[DEF_PK]: id2, ...input2},
  2108. {[DEF_PK]: id3, ...input3},
  2109. ]);
  2110. const result = await adapter.patch('model', {foo: 'test'}, {baz: 'd3'});
  2111. expect(result).to.be.eq(0);
  2112. const patchedItems = Array.from(table.values());
  2113. expect(patchedItems).to.be.eql([
  2114. {[DEF_PK]: id1, foo: 'a1', bar: 'a2'},
  2115. {[DEF_PK]: id2, foo: 'b1', bar: 'b2'},
  2116. {[DEF_PK]: id3, foo: 'c1', bar: 'c2'},
  2117. ]);
  2118. });
  2119. it('uses the "where" clause to patch specific items', async function () {
  2120. const schema = new Schema();
  2121. schema.defineDatasource({
  2122. name: 'memory',
  2123. adapter: 'memory',
  2124. });
  2125. schema.defineModel({
  2126. name: 'model',
  2127. datasource: 'memory',
  2128. properties: {
  2129. foo: DataType.STRING,
  2130. bar: DataType.STRING,
  2131. },
  2132. });
  2133. const adapter = new MemoryAdapter(schema.container, {});
  2134. const input1 = {foo: 'a', bar: '1'};
  2135. const input2 = {foo: 'b', bar: '2'};
  2136. const input3 = {foo: 'c', bar: '2'};
  2137. const created1 = await adapter.create('model', input1);
  2138. const created2 = await adapter.create('model', input2);
  2139. const created3 = await adapter.create('model', input3);
  2140. const id1 = created1[DEF_PK];
  2141. const id2 = created2[DEF_PK];
  2142. const id3 = created3[DEF_PK];
  2143. const table = adapter._getTableOrCreate('model');
  2144. const createdItems = Array.from(table.values());
  2145. expect(createdItems).to.be.eql([
  2146. {[DEF_PK]: id1, ...input1},
  2147. {[DEF_PK]: id2, ...input2},
  2148. {[DEF_PK]: id3, ...input3},
  2149. ]);
  2150. const result = await adapter.patch('model', {foo: 'd'}, {bar: '2'});
  2151. expect(result).to.be.eq(2);
  2152. const patchedItems = Array.from(table.values());
  2153. expect(patchedItems).to.be.eql([
  2154. {[DEF_PK]: id1, foo: 'a', bar: '1'},
  2155. {[DEF_PK]: id2, foo: 'd', bar: '2'},
  2156. {[DEF_PK]: id3, foo: 'd', bar: '2'},
  2157. ]);
  2158. });
  2159. it('the "where" clause uses property names instead of column names', async function () {
  2160. const schema = new Schema();
  2161. schema.defineDatasource({
  2162. name: 'memory',
  2163. adapter: 'memory',
  2164. });
  2165. schema.defineModel({
  2166. name: 'model',
  2167. datasource: 'memory',
  2168. properties: {
  2169. foo: {
  2170. type: DataType.STRING,
  2171. columnName: 'fooCol',
  2172. },
  2173. bar: {
  2174. type: DataType.STRING,
  2175. columnName: 'barCol',
  2176. },
  2177. },
  2178. });
  2179. const adapter = new MemoryAdapter(schema.container, {});
  2180. const input1 = {foo: 'a', bar: '1'};
  2181. const input2 = {foo: 'b', bar: '2'};
  2182. const input3 = {foo: 'c', bar: '2'};
  2183. const created1 = await adapter.create('model', input1);
  2184. const created2 = await adapter.create('model', input2);
  2185. const created3 = await adapter.create('model', input3);
  2186. const id1 = created1[DEF_PK];
  2187. const id2 = created2[DEF_PK];
  2188. const id3 = created3[DEF_PK];
  2189. const table = adapter._getTableOrCreate('model');
  2190. const createdItems = Array.from(table.values());
  2191. expect(createdItems).to.be.eql([
  2192. {[DEF_PK]: id1, fooCol: 'a', barCol: '1'},
  2193. {[DEF_PK]: id2, fooCol: 'b', barCol: '2'},
  2194. {[DEF_PK]: id3, fooCol: 'c', barCol: '2'},
  2195. ]);
  2196. const result = await adapter.patch('model', {foo: 'd'}, {bar: '2'});
  2197. expect(result).to.be.eq(2);
  2198. const patchedItems = Array.from(table.values());
  2199. expect(patchedItems).to.be.eql([
  2200. {[DEF_PK]: id1, fooCol: 'a', barCol: '1'},
  2201. {[DEF_PK]: id2, fooCol: 'd', barCol: '2'},
  2202. {[DEF_PK]: id3, fooCol: 'd', barCol: '2'},
  2203. ]);
  2204. });
  2205. it('the "where" clause uses a persisted data instead of default values in case of undefined', async function () {
  2206. const schema = new Schema();
  2207. schema.defineDatasource({
  2208. name: 'memory',
  2209. adapter: 'memory',
  2210. });
  2211. schema.defineModel({
  2212. name: 'model',
  2213. datasource: 'memory',
  2214. properties: {
  2215. foo: DataType.STRING,
  2216. bar: {
  2217. type: DataType.STRING,
  2218. default: 'barVal',
  2219. },
  2220. },
  2221. });
  2222. const adapter = new MemoryAdapter(schema.container, {});
  2223. const input1 = {[DEF_PK]: 1, foo: 'a', bar: undefined};
  2224. const input2 = {[DEF_PK]: 2, foo: 'b', bar: undefined};
  2225. const input3 = {[DEF_PK]: 3, foo: 'c', bar: 10};
  2226. const input4 = {[DEF_PK]: 4, foo: 'd', bar: null};
  2227. const table = adapter._getTableOrCreate('model');
  2228. table.set(input1[DEF_PK], input1);
  2229. table.set(input2[DEF_PK], input2);
  2230. table.set(input3[DEF_PK], input3);
  2231. table.set(input4[DEF_PK], input4);
  2232. const result = await adapter.patch('model', {foo: 'e'}, {bar: undefined});
  2233. expect(result).to.be.eq(2);
  2234. const patchedItems = Array.from(table.values());
  2235. expect(patchedItems).to.be.eql([
  2236. {[DEF_PK]: 1, foo: 'e', bar: undefined},
  2237. {[DEF_PK]: 2, foo: 'e', bar: undefined},
  2238. {[DEF_PK]: 3, foo: 'c', bar: 10},
  2239. {[DEF_PK]: 4, foo: 'd', bar: null},
  2240. ]);
  2241. });
  2242. it('the "where" clause uses a persisted data instead of default values in case of null', async function () {
  2243. const schema = new Schema();
  2244. schema.defineDatasource({
  2245. name: 'memory',
  2246. adapter: 'memory',
  2247. });
  2248. schema.defineModel({
  2249. name: 'model',
  2250. datasource: 'memory',
  2251. properties: {
  2252. foo: DataType.STRING,
  2253. bar: {
  2254. type: DataType.STRING,
  2255. default: 'barVal',
  2256. },
  2257. },
  2258. });
  2259. const adapter = new MemoryAdapter(schema.container, {});
  2260. const input1 = {[DEF_PK]: 1, foo: 'a', bar: undefined};
  2261. const input2 = {[DEF_PK]: 2, foo: 'b', bar: undefined};
  2262. const input3 = {[DEF_PK]: 3, foo: 'c', bar: 10};
  2263. const input4 = {[DEF_PK]: 4, foo: 'd', bar: null};
  2264. const table = adapter._getTableOrCreate('model');
  2265. table.set(input1[DEF_PK], input1);
  2266. table.set(input2[DEF_PK], input2);
  2267. table.set(input3[DEF_PK], input3);
  2268. table.set(input4[DEF_PK], input4);
  2269. const result = await adapter.patch('model', {foo: 'e'}, {bar: null});
  2270. expect(result).to.be.eq(1);
  2271. const patchedItems = Array.from(table.values());
  2272. expect(patchedItems).to.be.eql([
  2273. {[DEF_PK]: 1, foo: 'a', bar: undefined},
  2274. {[DEF_PK]: 2, foo: 'b', bar: undefined},
  2275. {[DEF_PK]: 3, foo: 'c', bar: 10},
  2276. {[DEF_PK]: 4, foo: 'e', bar: null},
  2277. ]);
  2278. });
  2279. });
  2280. describe('patchById', function () {
  2281. it('updates only provided properties by a given identifier', async function () {
  2282. const schema = new Schema();
  2283. schema.defineDatasource({
  2284. name: 'memory',
  2285. adapter: 'memory',
  2286. });
  2287. schema.defineModel({
  2288. name: 'model',
  2289. datasource: 'memory',
  2290. properties: {
  2291. foo: DataType.NUMBER,
  2292. bar: DataType.NUMBER,
  2293. },
  2294. });
  2295. const adapter = new MemoryAdapter(schema.container, {});
  2296. const input = {foo: 1, bar: 2};
  2297. const createdModelData = await adapter.create('model', input);
  2298. const idValue = createdModelData[DEF_PK];
  2299. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  2300. const table = adapter._getTableOrCreate('model');
  2301. const createdTableData = table.get(idValue);
  2302. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  2303. const patch = {foo: 20};
  2304. const patchedModelData = await adapter.patchById('model', idValue, patch);
  2305. expect(patchedModelData).to.be.eql({
  2306. [DEF_PK]: idValue,
  2307. foo: patch.foo,
  2308. bar: input.bar,
  2309. });
  2310. const patchedTableData = table.get(idValue);
  2311. expect(patchedTableData).to.be.eql({
  2312. [DEF_PK]: idValue,
  2313. foo: patch.foo,
  2314. bar: input.bar,
  2315. });
  2316. });
  2317. it('does not throw an error if a partial data does not have required property', async function () {
  2318. const schema = new Schema();
  2319. schema.defineDatasource({
  2320. name: 'memory',
  2321. adapter: 'memory',
  2322. });
  2323. schema.defineModel({
  2324. name: 'model',
  2325. datasource: 'memory',
  2326. properties: {
  2327. foo: {
  2328. type: DataType.NUMBER,
  2329. required: true,
  2330. },
  2331. bar: DataType.NUMBER,
  2332. },
  2333. });
  2334. const adapter = new MemoryAdapter(schema.container, {});
  2335. const input = {foo: 1, bar: 2};
  2336. const createdModelData = await adapter.create('model', input);
  2337. const idValue = createdModelData[DEF_PK];
  2338. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  2339. const table = adapter._getTableOrCreate('model');
  2340. const createdTableData = table.get(idValue);
  2341. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  2342. const patch = {bar: 20};
  2343. const patchedModelData = await adapter.patchById('model', idValue, patch);
  2344. expect(patchedModelData).to.be.eql({
  2345. [DEF_PK]: idValue,
  2346. foo: input.foo,
  2347. bar: patch.bar,
  2348. });
  2349. const patchedTableData = table.get(idValue);
  2350. expect(patchedTableData).to.be.eql({
  2351. [DEF_PK]: idValue,
  2352. foo: input.foo,
  2353. bar: patch.bar,
  2354. });
  2355. });
  2356. it('ignores identifier value in a given data in case of a default primary key', async function () {
  2357. const schema = new Schema();
  2358. schema.defineDatasource({
  2359. name: 'memory',
  2360. adapter: 'memory',
  2361. });
  2362. schema.defineModel({
  2363. name: 'model',
  2364. datasource: 'memory',
  2365. });
  2366. const adapter = new MemoryAdapter(schema.container, {});
  2367. const idValue = 10;
  2368. const createdModelData = await adapter.create('model', {
  2369. [DEF_PK]: idValue,
  2370. });
  2371. expect(createdModelData).to.be.eql({[DEF_PK]: idValue});
  2372. const table = adapter._getTableOrCreate('model');
  2373. const createdTableData = table.get(idValue);
  2374. expect(createdTableData).to.be.eql({[DEF_PK]: idValue});
  2375. const patchedModelData = await adapter.patchById('model', idValue, {
  2376. [DEF_PK]: 20,
  2377. });
  2378. expect(patchedModelData).to.be.eql({[DEF_PK]: idValue});
  2379. const patchedTableData = table.get(idValue);
  2380. expect(patchedTableData).to.be.eql({[DEF_PK]: idValue});
  2381. });
  2382. it('ignores identifier value in a given data in case of a specified primary key', async function () {
  2383. const schema = new Schema();
  2384. schema.defineDatasource({
  2385. name: 'memory',
  2386. adapter: 'memory',
  2387. });
  2388. schema.defineModel({
  2389. name: 'model',
  2390. datasource: 'memory',
  2391. properties: {
  2392. myId: {
  2393. type: DataType.NUMBER,
  2394. primaryKey: true,
  2395. },
  2396. },
  2397. });
  2398. const adapter = new MemoryAdapter(schema.container, {});
  2399. const idValue = 10;
  2400. const createdModelData = await adapter.create('model', {myId: idValue});
  2401. expect(createdModelData).to.be.eql({myId: idValue});
  2402. const table = adapter._getTableOrCreate('model');
  2403. const createdTableData = table.get(idValue);
  2404. expect(createdTableData).to.be.eql({myId: idValue});
  2405. const patchedModelData = await adapter.patchById('model', idValue, {
  2406. myId: 20,
  2407. });
  2408. expect(patchedModelData).to.be.eql({myId: idValue});
  2409. const patchedTableData = table.get(idValue);
  2410. expect(patchedTableData).to.be.eql({myId: idValue});
  2411. });
  2412. it('sets a default values for patched properties with an undefined value', async function () {
  2413. const schema = new Schema();
  2414. schema.defineDatasource({
  2415. name: 'memory',
  2416. adapter: 'memory',
  2417. });
  2418. schema.defineModel({
  2419. name: 'model',
  2420. datasource: 'memory',
  2421. properties: {
  2422. foo: {
  2423. type: DataType.NUMBER,
  2424. default: 1,
  2425. },
  2426. bar: {
  2427. type: DataType.NUMBER,
  2428. default: 2,
  2429. },
  2430. },
  2431. });
  2432. const adapter = new MemoryAdapter(schema.container, {});
  2433. const createdModelData = await adapter.create('model', {});
  2434. const idValue = createdModelData[DEF_PK];
  2435. const defaults = {foo: 1, bar: 2};
  2436. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2437. const table = adapter._getTableOrCreate('model');
  2438. const createdTableData = table.get(idValue);
  2439. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2440. const patchedModelData = await adapter.patchById('model', idValue, {
  2441. foo: undefined,
  2442. });
  2443. expect(patchedModelData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2444. const patchedTableData = table.get(idValue);
  2445. expect(patchedTableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2446. });
  2447. it('sets a default values for patched properties with a null value', async function () {
  2448. const schema = new Schema();
  2449. schema.defineDatasource({
  2450. name: 'memory',
  2451. adapter: 'memory',
  2452. });
  2453. schema.defineModel({
  2454. name: 'model',
  2455. datasource: 'memory',
  2456. properties: {
  2457. foo: {
  2458. type: DataType.NUMBER,
  2459. default: 1,
  2460. },
  2461. bar: {
  2462. type: DataType.NUMBER,
  2463. default: 2,
  2464. },
  2465. },
  2466. });
  2467. const adapter = new MemoryAdapter(schema.container, {});
  2468. const createdModelData = await adapter.create('model', {});
  2469. const idValue = createdModelData[DEF_PK];
  2470. const defaults = {foo: 1, bar: 2};
  2471. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2472. const table = adapter._getTableOrCreate('model');
  2473. const createdTableData = table.get(idValue);
  2474. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2475. const patchedModelData = await adapter.patchById('model', idValue, {
  2476. foo: null,
  2477. });
  2478. expect(patchedModelData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2479. const patchedTableData = table.get(idValue);
  2480. expect(patchedTableData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2481. });
  2482. it('throws an error if a given identifier does not exist', async function () {
  2483. const schema = new Schema();
  2484. schema.defineDatasource({
  2485. name: 'memory',
  2486. adapter: 'memory',
  2487. });
  2488. schema.defineModel({
  2489. name: 'model',
  2490. datasource: 'memory',
  2491. properties: {
  2492. foo: DataType.NUMBER,
  2493. bar: DataType.NUMBER,
  2494. },
  2495. });
  2496. const adapter = new MemoryAdapter(schema.container, {});
  2497. const promise = adapter.patchById('model', 1, {foo: 2});
  2498. await expect(promise).to.be.rejectedWith(
  2499. format(
  2500. 'The value 1 of the primary key %v does not exist in the model "model".',
  2501. DEF_PK,
  2502. ),
  2503. );
  2504. });
  2505. it('uses a specified column name for a primary key', async function () {
  2506. const schema = new Schema();
  2507. schema.defineDatasource({
  2508. name: 'memory',
  2509. adapter: 'memory',
  2510. });
  2511. schema.defineModel({
  2512. name: 'model',
  2513. datasource: 'memory',
  2514. properties: {
  2515. foo: {
  2516. type: DataType.NUMBER,
  2517. primaryKey: true,
  2518. columnName: 'qux',
  2519. },
  2520. bar: DataType.NUMBER,
  2521. baz: DataType.NUMBER,
  2522. },
  2523. });
  2524. const adapter = new MemoryAdapter(schema.container, {});
  2525. const input = {bar: 1, baz: 2};
  2526. const createdModelData = await adapter.create('model', input);
  2527. expect(createdModelData).to.be.eql({foo: createdModelData.foo, ...input});
  2528. const table = adapter._getTableOrCreate('model');
  2529. const createdTableData = table.get(createdModelData.foo);
  2530. expect(createdTableData).to.be.eql({qux: createdModelData.foo, ...input});
  2531. const patching = {bar: 2};
  2532. const patchedModelData = await adapter.patchById(
  2533. 'model',
  2534. createdModelData.foo,
  2535. patching,
  2536. );
  2537. expect(patchedModelData).to.be.eql({
  2538. foo: createdModelData.foo,
  2539. bar: patching.bar,
  2540. baz: input.baz,
  2541. });
  2542. const patchedTableData = table.get(createdModelData.foo);
  2543. expect(patchedTableData).to.be.eql({
  2544. qux: createdModelData.foo,
  2545. bar: patching.bar,
  2546. baz: input.baz,
  2547. });
  2548. });
  2549. it('uses a specified column name for a regular property', async function () {
  2550. const schema = new Schema();
  2551. schema.defineDatasource({
  2552. name: 'memory',
  2553. adapter: 'memory',
  2554. });
  2555. schema.defineModel({
  2556. name: 'model',
  2557. datasource: 'memory',
  2558. properties: {
  2559. foo: {
  2560. type: DataType.NUMBER,
  2561. columnName: 'baz',
  2562. },
  2563. bar: DataType.NUMBER,
  2564. },
  2565. });
  2566. const adapter = new MemoryAdapter(schema.container, {});
  2567. const input = {foo: 1, bar: 2};
  2568. const createdModelData = await adapter.create('model', input);
  2569. const idValue = createdModelData[DEF_PK];
  2570. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  2571. const table = adapter._getTableOrCreate('model');
  2572. const createdTableData = table.get(idValue);
  2573. expect(createdTableData).to.be.eql({
  2574. [DEF_PK]: idValue,
  2575. baz: input.foo,
  2576. bar: input.bar,
  2577. });
  2578. const patching = {foo: 2};
  2579. const patchedModelData = await adapter.patchById(
  2580. 'model',
  2581. idValue,
  2582. patching,
  2583. );
  2584. expect(patchedModelData).to.be.eql({
  2585. [DEF_PK]: idValue,
  2586. foo: patching.foo,
  2587. bar: input.bar,
  2588. });
  2589. const patchedTableData = table.get(idValue);
  2590. expect(patchedTableData).to.be.eql({
  2591. [DEF_PK]: idValue,
  2592. baz: patching.foo,
  2593. bar: input.bar,
  2594. });
  2595. });
  2596. it('uses a specified column name for a regular property with a default value', async function () {
  2597. const schema = new Schema();
  2598. schema.defineDatasource({
  2599. name: 'memory',
  2600. adapter: 'memory',
  2601. });
  2602. schema.defineModel({
  2603. name: 'model',
  2604. datasource: 'memory',
  2605. properties: {
  2606. foo: {
  2607. type: DataType.NUMBER,
  2608. columnName: 'baz',
  2609. default: 1,
  2610. },
  2611. bar: {
  2612. type: DataType.NUMBER,
  2613. default: 2,
  2614. },
  2615. },
  2616. });
  2617. const adapter = new MemoryAdapter(schema.container, {});
  2618. const createdModelData = await adapter.create('model', {});
  2619. const idValue = createdModelData[DEF_PK];
  2620. const defaults = {foo: 1, bar: 2};
  2621. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...defaults});
  2622. const table = adapter._getTableOrCreate('model');
  2623. const createdTableData = table.get(idValue);
  2624. expect(createdTableData).to.be.eql({
  2625. [DEF_PK]: idValue,
  2626. baz: defaults.foo,
  2627. bar: defaults.bar,
  2628. });
  2629. const patching = {foo: 2};
  2630. const patchedModelData = await adapter.patchById(
  2631. 'model',
  2632. idValue,
  2633. patching,
  2634. );
  2635. expect(patchedModelData).to.be.eql({
  2636. [DEF_PK]: idValue,
  2637. foo: patching.foo,
  2638. bar: defaults.bar,
  2639. });
  2640. const patchedTableData = table.get(idValue);
  2641. expect(patchedTableData).to.be.eql({
  2642. [DEF_PK]: idValue,
  2643. baz: patching.foo,
  2644. bar: defaults.bar,
  2645. });
  2646. });
  2647. it('allows to specify a short form of a fields clause to filter a return value', async function () {
  2648. const schema = new Schema();
  2649. schema.defineDatasource({
  2650. name: 'memory',
  2651. adapter: 'memory',
  2652. });
  2653. schema.defineModel({
  2654. name: 'model',
  2655. datasource: 'memory',
  2656. properties: {
  2657. foo: DataType.STRING,
  2658. bar: DataType.NUMBER,
  2659. },
  2660. });
  2661. const adapter = new MemoryAdapter(schema.container, {});
  2662. const input = {foo: 'string', bar: 10};
  2663. const createdModelData = await adapter.create('model', input);
  2664. const idValue = createdModelData[DEF_PK];
  2665. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  2666. const table = adapter._getTableOrCreate('model');
  2667. const createdTableData = table.get(idValue);
  2668. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  2669. const patchedModelData = await adapter.patchById(
  2670. 'model',
  2671. idValue,
  2672. input,
  2673. {fields: 'foo'},
  2674. );
  2675. expect(patchedModelData).to.be.eql({
  2676. [DEF_PK]: idValue,
  2677. foo: input.foo,
  2678. });
  2679. const patchedTableData = table.get(idValue);
  2680. expect(patchedTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  2681. });
  2682. it('allows to specify a full form of a fields clause to filter a return value', async function () {
  2683. const schema = new Schema();
  2684. schema.defineDatasource({
  2685. name: 'memory',
  2686. adapter: 'memory',
  2687. });
  2688. schema.defineModel({
  2689. name: 'model',
  2690. datasource: 'memory',
  2691. properties: {
  2692. foo: DataType.STRING,
  2693. bar: DataType.NUMBER,
  2694. baz: DataType.BOOLEAN,
  2695. },
  2696. });
  2697. const adapter = new MemoryAdapter(schema.container, {});
  2698. const input = {foo: 'string', bar: 10, baz: true};
  2699. const createdModelData = await adapter.create('model', input);
  2700. const idValue = createdModelData[DEF_PK];
  2701. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  2702. const table = adapter._getTableOrCreate('model');
  2703. const createdTableData = table.get(idValue);
  2704. expect(createdTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  2705. const patchedModelData = await adapter.patchById(
  2706. 'model',
  2707. idValue,
  2708. input,
  2709. {fields: ['foo', 'bar']},
  2710. );
  2711. expect(patchedModelData).to.be.eql({
  2712. [DEF_PK]: idValue,
  2713. foo: input.foo,
  2714. bar: input.bar,
  2715. });
  2716. const patchedTableData = table.get(idValue);
  2717. expect(patchedTableData).to.be.eql({[DEF_PK]: idValue, ...input});
  2718. });
  2719. it('a fields clause uses property names instead of column names', async function () {
  2720. const schema = new Schema();
  2721. schema.defineDatasource({
  2722. name: 'memory',
  2723. adapter: 'memory',
  2724. });
  2725. schema.defineModel({
  2726. name: 'model',
  2727. datasource: 'memory',
  2728. properties: {
  2729. foo: {
  2730. type: DataType.STRING,
  2731. columnName: 'fooCol',
  2732. },
  2733. bar: {
  2734. type: DataType.NUMBER,
  2735. columnName: 'barCol',
  2736. },
  2737. baz: {
  2738. type: DataType.BOOLEAN,
  2739. columnName: 'bazCol',
  2740. },
  2741. },
  2742. });
  2743. const adapter = new MemoryAdapter(schema.container, {});
  2744. const input = {foo: 'string', bar: 10, baz: true};
  2745. const createdModelData = await adapter.create('model', input);
  2746. const idValue = createdModelData[DEF_PK];
  2747. expect(createdModelData).to.be.eql({[DEF_PK]: idValue, ...input});
  2748. const table = adapter._getTableOrCreate('model');
  2749. const createdTableData = table.get(idValue);
  2750. expect(createdTableData).to.be.eql({
  2751. [DEF_PK]: idValue,
  2752. fooCol: input.foo,
  2753. barCol: input.bar,
  2754. bazCol: input.baz,
  2755. });
  2756. const patchedModelData = await adapter.patchById(
  2757. 'model',
  2758. idValue,
  2759. input,
  2760. {fields: ['foo', 'bar']},
  2761. );
  2762. expect(patchedModelData).to.be.eql({
  2763. [DEF_PK]: idValue,
  2764. foo: input.foo,
  2765. bar: input.bar,
  2766. });
  2767. const patchedTableData = table.get(idValue);
  2768. expect(patchedTableData).to.be.eql({
  2769. [DEF_PK]: idValue,
  2770. fooCol: input.foo,
  2771. barCol: input.bar,
  2772. bazCol: input.baz,
  2773. });
  2774. });
  2775. });
  2776. describe('find', function () {
  2777. it('returns an empty array if a table does not have an items', async function () {
  2778. const schema = new Schema();
  2779. schema.defineDatasource({
  2780. name: 'memory',
  2781. adapter: 'memory',
  2782. });
  2783. schema.defineModel({
  2784. name: 'model',
  2785. datasource: 'memory',
  2786. });
  2787. const adapter = new MemoryAdapter(schema.container, {});
  2788. const result = await adapter.find('model');
  2789. expect(result).to.be.instanceof(Array);
  2790. expect(result).to.be.empty;
  2791. });
  2792. it('returns an array of table items', async function () {
  2793. const schema = new Schema();
  2794. schema.defineDatasource({
  2795. name: 'memory',
  2796. adapter: 'memory',
  2797. });
  2798. schema.defineModel({
  2799. name: 'model',
  2800. datasource: 'memory',
  2801. });
  2802. const adapter = new MemoryAdapter(schema.container, {});
  2803. await adapter.create('model', {});
  2804. await adapter.create('model', {});
  2805. await adapter.create('model', {});
  2806. const result = await adapter.find('model');
  2807. expect(result).to.be.instanceof(Array);
  2808. expect(result).to.have.lengthOf(3);
  2809. expect(result[0]).to.be.eql({[DEF_PK]: 1});
  2810. expect(result[1]).to.be.eql({[DEF_PK]: 2});
  2811. expect(result[2]).to.be.eql({[DEF_PK]: 3});
  2812. });
  2813. it('uses default values for non-existent properties', async function () {
  2814. const schema = new Schema();
  2815. schema.defineDatasource({
  2816. name: 'memory',
  2817. adapter: 'memory',
  2818. });
  2819. schema.defineModel({
  2820. name: 'model',
  2821. datasource: 'memory',
  2822. properties: {
  2823. foo: {
  2824. type: DataType.STRING,
  2825. default: 'string',
  2826. },
  2827. },
  2828. });
  2829. const adapter = new MemoryAdapter(schema.container, {});
  2830. const table = adapter._getTableOrCreate('model');
  2831. table.set(1, {[DEF_PK]: 1});
  2832. table.set(2, {[DEF_PK]: 2});
  2833. table.set(3, {[DEF_PK]: 3});
  2834. const result = await adapter.find('model');
  2835. expect(result).to.be.instanceof(Array);
  2836. expect(result).to.have.lengthOf(3);
  2837. expect(result[0]).to.be.eql({[DEF_PK]: 1, foo: 'string'});
  2838. expect(result[1]).to.be.eql({[DEF_PK]: 2, foo: 'string'});
  2839. expect(result[2]).to.be.eql({[DEF_PK]: 3, foo: 'string'});
  2840. const tableItems = Array.from(table.values());
  2841. expect(tableItems).to.have.lengthOf(3);
  2842. expect(tableItems[0]).to.be.eql({[DEF_PK]: 1});
  2843. expect(tableItems[1]).to.be.eql({[DEF_PK]: 2});
  2844. expect(tableItems[2]).to.be.eql({[DEF_PK]: 3});
  2845. });
  2846. it('uses default values for properties of an undefined', async function () {
  2847. const schema = new Schema();
  2848. schema.defineDatasource({
  2849. name: 'memory',
  2850. adapter: 'memory',
  2851. });
  2852. schema.defineModel({
  2853. name: 'model',
  2854. datasource: 'memory',
  2855. properties: {
  2856. foo: {
  2857. type: DataType.STRING,
  2858. default: 'string',
  2859. },
  2860. },
  2861. });
  2862. const adapter = new MemoryAdapter(schema.container, {});
  2863. const table = adapter._getTableOrCreate('model');
  2864. table.set(1, {[DEF_PK]: 1, foo: undefined});
  2865. table.set(2, {[DEF_PK]: 2, foo: undefined});
  2866. table.set(3, {[DEF_PK]: 3, foo: undefined});
  2867. const result = await adapter.find('model');
  2868. expect(result).to.be.instanceof(Array);
  2869. expect(result).to.have.lengthOf(3);
  2870. expect(result[0]).to.be.eql({[DEF_PK]: 1, foo: 'string'});
  2871. expect(result[1]).to.be.eql({[DEF_PK]: 2, foo: 'string'});
  2872. expect(result[2]).to.be.eql({[DEF_PK]: 3, foo: 'string'});
  2873. const tableItems = Array.from(table.values());
  2874. expect(tableItems).to.have.lengthOf(3);
  2875. expect(tableItems[0]).to.be.eql({[DEF_PK]: 1, foo: undefined});
  2876. expect(tableItems[1]).to.be.eql({[DEF_PK]: 2, foo: undefined});
  2877. expect(tableItems[2]).to.be.eql({[DEF_PK]: 3, foo: undefined});
  2878. });
  2879. it('uses default values for properties of a null', async function () {
  2880. const schema = new Schema();
  2881. schema.defineDatasource({
  2882. name: 'memory',
  2883. adapter: 'memory',
  2884. });
  2885. schema.defineModel({
  2886. name: 'model',
  2887. datasource: 'memory',
  2888. properties: {
  2889. foo: {
  2890. type: DataType.STRING,
  2891. default: 'string',
  2892. },
  2893. },
  2894. });
  2895. const adapter = new MemoryAdapter(schema.container, {});
  2896. const table = adapter._getTableOrCreate('model');
  2897. table.set(1, {[DEF_PK]: 1, foo: null});
  2898. table.set(2, {[DEF_PK]: 2, foo: null});
  2899. table.set(3, {[DEF_PK]: 3, foo: null});
  2900. const result = await adapter.find('model');
  2901. expect(result).to.be.instanceof(Array);
  2902. expect(result).to.have.lengthOf(3);
  2903. expect(result[0]).to.be.eql({[DEF_PK]: 1, foo: 'string'});
  2904. expect(result[1]).to.be.eql({[DEF_PK]: 2, foo: 'string'});
  2905. expect(result[2]).to.be.eql({[DEF_PK]: 3, foo: 'string'});
  2906. const tableItems = Array.from(table.values());
  2907. expect(tableItems).to.have.lengthOf(3);
  2908. expect(tableItems[0]).to.be.eql({[DEF_PK]: 1, foo: null});
  2909. expect(tableItems[1]).to.be.eql({[DEF_PK]: 2, foo: null});
  2910. expect(tableItems[2]).to.be.eql({[DEF_PK]: 3, foo: null});
  2911. });
  2912. it('allows to specify a short form of a fields clause to filter a return value', async function () {
  2913. const schema = new Schema();
  2914. schema.defineDatasource({
  2915. name: 'memory',
  2916. adapter: 'memory',
  2917. });
  2918. schema.defineModel({
  2919. name: 'model',
  2920. datasource: 'memory',
  2921. properties: {
  2922. foo: DataType.STRING,
  2923. bar: DataType.NUMBER,
  2924. },
  2925. });
  2926. const adapter = new MemoryAdapter(schema.container, {});
  2927. const input = {foo: 'string1', bar: 10};
  2928. await adapter.create('model', input);
  2929. await adapter.create('model', input);
  2930. await adapter.create('model', input);
  2931. const result = await adapter.find('model', {fields: 'foo'});
  2932. expect(result).to.be.instanceof(Array);
  2933. expect(result).to.have.lengthOf(3);
  2934. expect(result[0]).to.be.eql({[DEF_PK]: 1, foo: input.foo});
  2935. expect(result[1]).to.be.eql({[DEF_PK]: 2, foo: input.foo});
  2936. expect(result[2]).to.be.eql({[DEF_PK]: 3, foo: input.foo});
  2937. const table = adapter._getTableOrCreate('model');
  2938. const tableItems = Array.from(table.values());
  2939. expect(tableItems).to.have.lengthOf(3);
  2940. expect(tableItems[0]).to.be.eql({[DEF_PK]: 1, ...input});
  2941. expect(tableItems[1]).to.be.eql({[DEF_PK]: 2, ...input});
  2942. expect(tableItems[2]).to.be.eql({[DEF_PK]: 3, ...input});
  2943. });
  2944. it('allows to specify a full form of a fields clause to filter a return value', async function () {
  2945. const schema = new Schema();
  2946. schema.defineDatasource({
  2947. name: 'memory',
  2948. adapter: 'memory',
  2949. });
  2950. schema.defineModel({
  2951. name: 'model',
  2952. datasource: 'memory',
  2953. properties: {
  2954. foo: DataType.STRING,
  2955. bar: DataType.NUMBER,
  2956. baz: DataType.BOOLEAN,
  2957. },
  2958. });
  2959. const adapter = new MemoryAdapter(schema.container, {});
  2960. const input = {foo: 'string1', bar: 10, baz: true};
  2961. await adapter.create('model', input);
  2962. await adapter.create('model', input);
  2963. await adapter.create('model', input);
  2964. const result = await adapter.find('model', {fields: ['foo', 'bar']});
  2965. expect(result).to.be.instanceof(Array);
  2966. expect(result).to.have.lengthOf(3);
  2967. expect(result[0]).to.be.eql({
  2968. [DEF_PK]: 1,
  2969. foo: input.foo,
  2970. bar: input.bar,
  2971. });
  2972. expect(result[1]).to.be.eql({
  2973. [DEF_PK]: 2,
  2974. foo: input.foo,
  2975. bar: input.bar,
  2976. });
  2977. expect(result[2]).to.be.eql({
  2978. [DEF_PK]: 3,
  2979. foo: input.foo,
  2980. bar: input.bar,
  2981. });
  2982. const table = adapter._getTableOrCreate('model');
  2983. const tableItems = Array.from(table.values());
  2984. expect(tableItems).to.have.lengthOf(3);
  2985. expect(tableItems[0]).to.be.eql({[DEF_PK]: 1, ...input});
  2986. expect(tableItems[1]).to.be.eql({[DEF_PK]: 2, ...input});
  2987. expect(tableItems[2]).to.be.eql({[DEF_PK]: 3, ...input});
  2988. });
  2989. it('a fields clause uses property names instead of column names', async function () {
  2990. const schema = new Schema();
  2991. schema.defineDatasource({
  2992. name: 'memory',
  2993. adapter: 'memory',
  2994. });
  2995. schema.defineModel({
  2996. name: 'model',
  2997. datasource: 'memory',
  2998. properties: {
  2999. foo: {
  3000. type: DataType.STRING,
  3001. columnName: 'fooCol',
  3002. },
  3003. bar: {
  3004. type: DataType.NUMBER,
  3005. columnName: 'barCol',
  3006. },
  3007. baz: {
  3008. type: DataType.BOOLEAN,
  3009. columnName: 'bazCol',
  3010. },
  3011. },
  3012. });
  3013. const adapter = new MemoryAdapter(schema.container, {});
  3014. const input = {foo: 'string', bar: 10, baz: true};
  3015. await adapter.create('model', input);
  3016. await adapter.create('model', input);
  3017. await adapter.create('model', input);
  3018. const result = await adapter.find('model', {fields: ['foo', 'bar']});
  3019. expect(result).to.be.instanceof(Array);
  3020. expect(result).to.have.lengthOf(3);
  3021. expect(result[0]).to.be.eql({
  3022. [DEF_PK]: 1,
  3023. foo: input.foo,
  3024. bar: input.bar,
  3025. });
  3026. expect(result[1]).to.be.eql({
  3027. [DEF_PK]: 2,
  3028. foo: input.foo,
  3029. bar: input.bar,
  3030. });
  3031. expect(result[2]).to.be.eql({
  3032. [DEF_PK]: 3,
  3033. foo: input.foo,
  3034. bar: input.bar,
  3035. });
  3036. const table = adapter._getTableOrCreate('model');
  3037. const tableItems = Array.from(table.values());
  3038. const tableInput = {fooCol: 'string', barCol: 10, bazCol: true};
  3039. expect(tableItems).to.have.lengthOf(3);
  3040. expect(tableItems[0]).to.be.eql({[DEF_PK]: 1, ...tableInput});
  3041. expect(tableItems[1]).to.be.eql({[DEF_PK]: 2, ...tableInput});
  3042. expect(tableItems[2]).to.be.eql({[DEF_PK]: 3, ...tableInput});
  3043. });
  3044. it('allows to specify a short form of an order clause to sort a return value', async function () {
  3045. const schema = new Schema();
  3046. schema.defineDatasource({
  3047. name: 'memory',
  3048. adapter: 'memory',
  3049. });
  3050. schema.defineModel({
  3051. name: 'model',
  3052. datasource: 'memory',
  3053. properties: {
  3054. foo: DataType.NUMBER,
  3055. },
  3056. });
  3057. const adapter = new MemoryAdapter(schema.container, {});
  3058. await adapter.create('model', {foo: 20});
  3059. await adapter.create('model', {foo: 10});
  3060. await adapter.create('model', {foo: 15});
  3061. const result1 = await adapter.find('model', {order: 'foo'});
  3062. const result2 = await adapter.find('model', {order: 'foo ASC'});
  3063. const result3 = await adapter.find('model', {order: 'foo DESC'});
  3064. expect(result1).to.have.lengthOf(3);
  3065. expect(result1[0]).to.be.eql({[DEF_PK]: 2, foo: 10});
  3066. expect(result1[1]).to.be.eql({[DEF_PK]: 3, foo: 15});
  3067. expect(result1[2]).to.be.eql({[DEF_PK]: 1, foo: 20});
  3068. expect(result2).to.have.lengthOf(3);
  3069. expect(result2[0]).to.be.eql({[DEF_PK]: 2, foo: 10});
  3070. expect(result2[1]).to.be.eql({[DEF_PK]: 3, foo: 15});
  3071. expect(result2[2]).to.be.eql({[DEF_PK]: 1, foo: 20});
  3072. expect(result3).to.have.lengthOf(3);
  3073. expect(result3[0]).to.be.eql({[DEF_PK]: 1, foo: 20});
  3074. expect(result3[1]).to.be.eql({[DEF_PK]: 3, foo: 15});
  3075. expect(result3[2]).to.be.eql({[DEF_PK]: 2, foo: 10});
  3076. });
  3077. it('allows to specify a full form of an order clause to sort a return value', async function () {
  3078. const schema = new Schema();
  3079. schema.defineDatasource({
  3080. name: 'memory',
  3081. adapter: 'memory',
  3082. });
  3083. schema.defineModel({
  3084. name: 'model',
  3085. datasource: 'memory',
  3086. properties: {
  3087. foo: DataType.NUMBER,
  3088. bar: DataType.NUMBER,
  3089. },
  3090. });
  3091. const adapter = new MemoryAdapter(schema.container, {});
  3092. await adapter.create('model', {foo: 2, bar: 20});
  3093. await adapter.create('model', {foo: 2, bar: 10});
  3094. await adapter.create('model', {foo: 1, bar: 15});
  3095. const filter1 = {order: ['foo', 'bar']};
  3096. const filter2 = {order: ['foo ASC', 'bar ASC']};
  3097. const filter3 = {order: ['foo DESC', 'bar DESC']};
  3098. const filter4 = {order: ['foo', 'bar DESC']};
  3099. const result1 = await adapter.find('model', filter1);
  3100. const result2 = await adapter.find('model', filter2);
  3101. const result3 = await adapter.find('model', filter3);
  3102. const result4 = await adapter.find('model', filter4);
  3103. expect(result1).to.have.lengthOf(3);
  3104. expect(result1[0]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3105. expect(result1[1]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3106. expect(result1[2]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3107. expect(result2).to.have.lengthOf(3);
  3108. expect(result2[0]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3109. expect(result2[1]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3110. expect(result2[2]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3111. expect(result3).to.have.lengthOf(3);
  3112. expect(result3[0]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3113. expect(result3[1]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3114. expect(result3[2]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3115. expect(result4).to.have.lengthOf(3);
  3116. expect(result4[0]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3117. expect(result4[1]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3118. expect(result4[2]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3119. });
  3120. it('an order clause uses property names instead of column names', async function () {
  3121. const schema = new Schema();
  3122. schema.defineDatasource({
  3123. name: 'memory',
  3124. adapter: 'memory',
  3125. });
  3126. schema.defineModel({
  3127. name: 'model',
  3128. datasource: 'memory',
  3129. properties: {
  3130. foo: {
  3131. type: DataType.NUMBER,
  3132. columnName: 'fooCol',
  3133. },
  3134. bar: {
  3135. type: DataType.NUMBER,
  3136. columnName: 'barCol',
  3137. },
  3138. },
  3139. });
  3140. const adapter = new MemoryAdapter(schema.container, {});
  3141. const table = adapter._getTableOrCreate('model');
  3142. const tableInput1 = {fooCol: 2, barCol: 20};
  3143. const tableInput2 = {fooCol: 2, barCol: 10};
  3144. const tableInput3 = {fooCol: 1, barCol: 15};
  3145. table.set(1, {[DEF_PK]: 1, ...tableInput1});
  3146. table.set(2, {[DEF_PK]: 2, ...tableInput2});
  3147. table.set(3, {[DEF_PK]: 3, ...tableInput3});
  3148. const filter1 = {order: ['foo', 'bar']};
  3149. const filter2 = {order: ['foo ASC', 'bar ASC']};
  3150. const filter3 = {order: ['foo DESC', 'bar DESC']};
  3151. const filter4 = {order: ['foo', 'bar DESC']};
  3152. const result1 = await adapter.find('model', filter1);
  3153. const result2 = await adapter.find('model', filter2);
  3154. const result3 = await adapter.find('model', filter3);
  3155. const result4 = await adapter.find('model', filter4);
  3156. expect(result1).to.have.lengthOf(3);
  3157. expect(result1[0]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3158. expect(result1[1]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3159. expect(result1[2]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3160. expect(result2).to.have.lengthOf(3);
  3161. expect(result2[0]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3162. expect(result2[1]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3163. expect(result2[2]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3164. expect(result3).to.have.lengthOf(3);
  3165. expect(result3[0]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3166. expect(result3[1]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3167. expect(result3[2]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3168. expect(result4).to.have.lengthOf(3);
  3169. expect(result4[0]).to.be.eql({[DEF_PK]: 3, foo: 1, bar: 15});
  3170. expect(result4[1]).to.be.eql({[DEF_PK]: 1, foo: 2, bar: 20});
  3171. expect(result4[2]).to.be.eql({[DEF_PK]: 2, foo: 2, bar: 10});
  3172. });
  3173. it('allows to specify the "where" clause to filter a return value', async function () {
  3174. const schema = new Schema();
  3175. schema.defineDatasource({
  3176. name: 'memory',
  3177. adapter: 'memory',
  3178. });
  3179. schema.defineModel({
  3180. name: 'model',
  3181. datasource: 'memory',
  3182. properties: {
  3183. foo: DataType.NUMBER,
  3184. bar: DataType.BOOLEAN,
  3185. baz: DataType.STRING,
  3186. },
  3187. });
  3188. const adapter = new MemoryAdapter(schema.container, {});
  3189. const input1 = {foo: 20, bar: true, baz: 'abc'};
  3190. const input2 = {foo: 10, bar: true, baz: 'abc'};
  3191. const input3 = {foo: 15, bar: false, baz: 'abe'};
  3192. await adapter.create('model', input1);
  3193. await adapter.create('model', input2);
  3194. await adapter.create('model', input3);
  3195. const filter1 = {where: {foo: 10}};
  3196. const filter2 = {where: {foo: {gte: 15}, baz: {like: 'bc'}}};
  3197. const filter3 = {where: {bar: true}};
  3198. const result1 = await adapter.find('model', filter1);
  3199. const result2 = await adapter.find('model', filter2);
  3200. const result3 = await adapter.find('model', filter3);
  3201. expect(result1).to.have.lengthOf(1);
  3202. expect(result1[0]).to.be.eql({[DEF_PK]: 2, ...input2});
  3203. expect(result2).to.have.lengthOf(1);
  3204. expect(result2[0]).to.be.eql({[DEF_PK]: 1, ...input1});
  3205. expect(result3).to.have.lengthOf(2);
  3206. expect(result3[0]).to.be.eql({[DEF_PK]: 1, ...input1});
  3207. expect(result3[1]).to.be.eql({[DEF_PK]: 2, ...input2});
  3208. });
  3209. it('the "where" clause uses property names instead of column names', async function () {
  3210. const schema = new Schema();
  3211. schema.defineDatasource({
  3212. name: 'memory',
  3213. adapter: 'memory',
  3214. });
  3215. schema.defineModel({
  3216. name: 'model',
  3217. datasource: 'memory',
  3218. properties: {
  3219. foo: {
  3220. type: DataType.STRING,
  3221. columnName: 'fooCol',
  3222. },
  3223. bar: {
  3224. type: DataType.NUMBER,
  3225. columnName: 'barCol',
  3226. },
  3227. baz: {
  3228. type: DataType.BOOLEAN,
  3229. columnName: 'bazCol',
  3230. },
  3231. },
  3232. });
  3233. const adapter = new MemoryAdapter(schema.container, {});
  3234. const table = adapter._getTableOrCreate('model');
  3235. const tableInput1 = {fooCol: 20, barCol: true, bazCol: 'abc'};
  3236. const tableInput2 = {fooCol: 10, barCol: true, bazCol: 'abc'};
  3237. const tableInput3 = {fooCol: 15, barCol: false, bazCol: 'abe'};
  3238. table.set(1, {[DEF_PK]: 1, ...tableInput1});
  3239. table.set(2, {[DEF_PK]: 2, ...tableInput2});
  3240. table.set(3, {[DEF_PK]: 3, ...tableInput3});
  3241. const input1 = {
  3242. foo: tableInput1.fooCol,
  3243. bar: tableInput1.barCol,
  3244. baz: tableInput1.bazCol,
  3245. };
  3246. const input2 = {
  3247. foo: tableInput2.fooCol,
  3248. bar: tableInput2.barCol,
  3249. baz: tableInput2.bazCol,
  3250. };
  3251. const filter1 = {where: {foo: 10}};
  3252. const filter2 = {where: {foo: {gte: 15}, baz: {like: 'bc'}}};
  3253. const filter3 = {where: {bar: true}};
  3254. const result1 = await adapter.find('model', filter1);
  3255. const result2 = await adapter.find('model', filter2);
  3256. const result3 = await adapter.find('model', filter3);
  3257. expect(result1).to.have.lengthOf(1);
  3258. expect(result1[0]).to.be.eql({[DEF_PK]: 2, ...input2});
  3259. expect(result2).to.have.lengthOf(1);
  3260. expect(result2[0]).to.be.eql({[DEF_PK]: 1, ...input1});
  3261. expect(result3).to.have.lengthOf(2);
  3262. expect(result3[0]).to.be.eql({[DEF_PK]: 1, ...input1});
  3263. expect(result3[1]).to.be.eql({[DEF_PK]: 2, ...input2});
  3264. });
  3265. it('the "where" clause uses a persisted data instead of default values', async function () {
  3266. const schema = new Schema();
  3267. schema.defineDatasource({
  3268. name: 'memory',
  3269. adapter: 'memory',
  3270. });
  3271. schema.defineModel({
  3272. name: 'model',
  3273. datasource: 'memory',
  3274. properties: {
  3275. foo: {
  3276. type: DataType.STRING,
  3277. default: 'hello',
  3278. },
  3279. },
  3280. });
  3281. const adapter = new MemoryAdapter(schema.container, {});
  3282. const table = adapter._getTableOrCreate('model');
  3283. table.set(1, {[DEF_PK]: 1, foo: undefined});
  3284. table.set(2, {[DEF_PK]: 2, foo: undefined});
  3285. table.set(3, {[DEF_PK]: 3, foo: 10});
  3286. const result1 = await adapter.find('model', {where: {foo: undefined}});
  3287. const result2 = await adapter.find('model', {where: {foo: 10}});
  3288. const result3 = await adapter.find('model', {where: {foo: 'hello'}});
  3289. expect(table.size).to.be.eq(3);
  3290. expect(result1).to.have.lengthOf(2);
  3291. expect(result1[0]).to.be.eql({[DEF_PK]: 1, foo: 'hello'});
  3292. expect(result1[1]).to.be.eql({[DEF_PK]: 2, foo: 'hello'});
  3293. expect(result2).to.have.lengthOf(1);
  3294. expect(result2[0]).to.be.eql({[DEF_PK]: 3, foo: 10});
  3295. expect(result3).to.be.empty;
  3296. });
  3297. it('allows to specify a limit clause to filter a return value', async function () {
  3298. const schema = new Schema();
  3299. schema.defineDatasource({
  3300. name: 'memory',
  3301. adapter: 'memory',
  3302. });
  3303. schema.defineModel({
  3304. name: 'model',
  3305. datasource: 'memory',
  3306. properties: {
  3307. foo: DataType.NUMBER,
  3308. },
  3309. });
  3310. const adapter = new MemoryAdapter(schema.container, {});
  3311. await adapter.create('model', {});
  3312. await adapter.create('model', {});
  3313. await adapter.create('model', {});
  3314. const result1 = await adapter.find('model', {limit: 0});
  3315. const result2 = await adapter.find('model', {limit: 1});
  3316. const result3 = await adapter.find('model', {limit: 2});
  3317. expect(result1).to.have.lengthOf(3);
  3318. expect(result1[0]).to.be.eql({[DEF_PK]: 1});
  3319. expect(result1[1]).to.be.eql({[DEF_PK]: 2});
  3320. expect(result1[2]).to.be.eql({[DEF_PK]: 3});
  3321. expect(result2).to.have.lengthOf(1);
  3322. expect(result2[0]).to.be.eql({[DEF_PK]: 1});
  3323. expect(result3).to.have.lengthOf(2);
  3324. expect(result3[0]).to.be.eql({[DEF_PK]: 1});
  3325. expect(result3[1]).to.be.eql({[DEF_PK]: 2});
  3326. });
  3327. it('allows to specify a skip clause to filter a return value', async function () {
  3328. const schema = new Schema();
  3329. schema.defineDatasource({
  3330. name: 'memory',
  3331. adapter: 'memory',
  3332. });
  3333. schema.defineModel({
  3334. name: 'model',
  3335. datasource: 'memory',
  3336. properties: {
  3337. foo: DataType.NUMBER,
  3338. },
  3339. });
  3340. const adapter = new MemoryAdapter(schema.container, {});
  3341. await adapter.create('model', {});
  3342. await adapter.create('model', {});
  3343. await adapter.create('model', {});
  3344. const result1 = await adapter.find('model', {skip: 0});
  3345. const result2 = await adapter.find('model', {skip: 1});
  3346. const result3 = await adapter.find('model', {skip: 2});
  3347. expect(result1).to.have.lengthOf(3);
  3348. expect(result1[0]).to.be.eql({[DEF_PK]: 1});
  3349. expect(result1[1]).to.be.eql({[DEF_PK]: 2});
  3350. expect(result1[2]).to.be.eql({[DEF_PK]: 3});
  3351. expect(result2).to.have.lengthOf(2);
  3352. expect(result1[1]).to.be.eql({[DEF_PK]: 2});
  3353. expect(result1[2]).to.be.eql({[DEF_PK]: 3});
  3354. expect(result3).to.have.lengthOf(1);
  3355. expect(result1[2]).to.be.eql({[DEF_PK]: 3});
  3356. });
  3357. });
  3358. describe('findById', function () {
  3359. it('throws an error if a given identifier does not exist', async function () {
  3360. const schema = new Schema();
  3361. schema.defineDatasource({
  3362. name: 'memory',
  3363. adapter: 'memory',
  3364. });
  3365. schema.defineModel({
  3366. name: 'model',
  3367. datasource: 'memory',
  3368. });
  3369. const adapter = new MemoryAdapter(schema.container, {});
  3370. const promise = adapter.findById('model', 1);
  3371. await expect(promise).to.be.rejectedWith(
  3372. format(
  3373. 'The value 1 of the primary key %v does not exist in the model "model".',
  3374. DEF_PK,
  3375. ),
  3376. );
  3377. });
  3378. it('uses default values for non-existent properties', async function () {
  3379. const schema = new Schema();
  3380. schema.defineDatasource({
  3381. name: 'memory',
  3382. adapter: 'memory',
  3383. });
  3384. schema.defineModel({
  3385. name: 'model',
  3386. datasource: 'memory',
  3387. properties: {
  3388. foo: {
  3389. type: DataType.STRING,
  3390. default: 'string',
  3391. },
  3392. },
  3393. });
  3394. const adapter = new MemoryAdapter(schema.container, {});
  3395. const table = adapter._getTableOrCreate('model');
  3396. const idValue = 1;
  3397. table.set(idValue, {[DEF_PK]: idValue});
  3398. const result = await adapter.findById('model', idValue);
  3399. expect(result).to.be.eql({[DEF_PK]: idValue, foo: 'string'});
  3400. const tableData = table.get(idValue);
  3401. expect(tableData).to.be.eql({[DEF_PK]: idValue});
  3402. });
  3403. it('uses default values for properties of an undefined', async function () {
  3404. const schema = new Schema();
  3405. schema.defineDatasource({
  3406. name: 'memory',
  3407. adapter: 'memory',
  3408. });
  3409. schema.defineModel({
  3410. name: 'model',
  3411. datasource: 'memory',
  3412. properties: {
  3413. foo: {
  3414. type: DataType.STRING,
  3415. default: 'string',
  3416. },
  3417. },
  3418. });
  3419. const adapter = new MemoryAdapter(schema.container, {});
  3420. const table = adapter._getTableOrCreate('model');
  3421. const idValue = 1;
  3422. const input = {foo: undefined};
  3423. table.set(idValue, {[DEF_PK]: idValue, ...input});
  3424. const result = await adapter.findById('model', idValue);
  3425. expect(result).to.be.eql({[DEF_PK]: idValue, foo: 'string'});
  3426. const tableData = table.get(idValue);
  3427. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...input});
  3428. });
  3429. it('uses default values for properties of a null', async function () {
  3430. const schema = new Schema();
  3431. schema.defineDatasource({
  3432. name: 'memory',
  3433. adapter: 'memory',
  3434. });
  3435. schema.defineModel({
  3436. name: 'model',
  3437. datasource: 'memory',
  3438. properties: {
  3439. foo: {
  3440. type: DataType.STRING,
  3441. default: 'string',
  3442. },
  3443. },
  3444. });
  3445. const adapter = new MemoryAdapter(schema.container, {});
  3446. const table = adapter._getTableOrCreate('model');
  3447. const idValue = 1;
  3448. const input = {foo: null};
  3449. table.set(idValue, {[DEF_PK]: idValue, ...input});
  3450. const result = await adapter.findById('model', idValue);
  3451. expect(result).to.be.eql({[DEF_PK]: idValue, foo: 'string'});
  3452. const tableData = table.get(idValue);
  3453. expect(tableData).to.be.eql({[DEF_PK]: idValue, ...input});
  3454. });
  3455. it('uses a specified column name for a primary key', async function () {
  3456. const schema = new Schema();
  3457. schema.defineDatasource({
  3458. name: 'memory',
  3459. adapter: 'memory',
  3460. });
  3461. schema.defineModel({
  3462. name: 'model',
  3463. datasource: 'memory',
  3464. properties: {
  3465. foo: {
  3466. type: DataType.NUMBER,
  3467. primaryKey: true,
  3468. columnName: 'qux',
  3469. },
  3470. bar: DataType.NUMBER,
  3471. baz: DataType.NUMBER,
  3472. },
  3473. });
  3474. const adapter = new MemoryAdapter(schema.container, {});
  3475. const input = {bar: 1, baz: 2};
  3476. const table = adapter._getTableOrCreate('model');
  3477. const idValue = 1;
  3478. table.set(idValue, {qux: idValue, ...input});
  3479. const result = await adapter.findById('model', idValue);
  3480. expect(result).to.be.eql({foo: idValue, ...input});
  3481. });
  3482. it('uses a specified column name for a regular property', async function () {
  3483. const schema = new Schema();
  3484. schema.defineDatasource({
  3485. name: 'memory',
  3486. adapter: 'memory',
  3487. });
  3488. schema.defineModel({
  3489. name: 'model',
  3490. datasource: 'memory',
  3491. properties: {
  3492. foo: {
  3493. type: DataType.NUMBER,
  3494. columnName: 'baz',
  3495. },
  3496. bar: DataType.NUMBER,
  3497. },
  3498. });
  3499. const adapter = new MemoryAdapter(schema.container, {});
  3500. const table = adapter._getTableOrCreate('model');
  3501. const idValue = 1;
  3502. const input = {foo: 1, bar: 2};
  3503. table.set(idValue, {
  3504. [DEF_PK]: idValue,
  3505. baz: input.foo,
  3506. bar: input.bar,
  3507. });
  3508. const result = await adapter.findById('model', idValue);
  3509. expect(result).to.be.eql({[DEF_PK]: idValue, ...input});
  3510. });
  3511. it('uses a specified column name for a regular property with a default value', async function () {
  3512. const schema = new Schema();
  3513. schema.defineDatasource({
  3514. name: 'memory',
  3515. adapter: 'memory',
  3516. });
  3517. schema.defineModel({
  3518. name: 'model',
  3519. datasource: 'memory',
  3520. properties: {
  3521. foo: {
  3522. type: DataType.NUMBER,
  3523. columnName: 'baz',
  3524. default: 1,
  3525. },
  3526. bar: {
  3527. type: DataType.NUMBER,
  3528. default: 2,
  3529. },
  3530. },
  3531. });
  3532. const adapter = new MemoryAdapter(schema.container, {});
  3533. const table = adapter._getTableOrCreate('model');
  3534. const idValue = 1;
  3535. table.set(idValue, {[DEF_PK]: idValue});
  3536. const defaults = {foo: 1, bar: 2};
  3537. const result = await adapter.findById('model', idValue);
  3538. expect(result).to.be.eql({[DEF_PK]: idValue, ...defaults});
  3539. });
  3540. it('allows to specify a short form of a fields clause to filter a return value', async function () {
  3541. const schema = new Schema();
  3542. schema.defineDatasource({
  3543. name: 'memory',
  3544. adapter: 'memory',
  3545. });
  3546. schema.defineModel({
  3547. name: 'model',
  3548. datasource: 'memory',
  3549. properties: {
  3550. foo: DataType.STRING,
  3551. bar: DataType.NUMBER,
  3552. },
  3553. });
  3554. const adapter = new MemoryAdapter(schema.container, {});
  3555. const table = adapter._getTableOrCreate('model');
  3556. const input = {foo: 'string', bar: 10};
  3557. const idValue = 1;
  3558. table.set(idValue, {[DEF_PK]: idValue, ...input});
  3559. const result = await adapter.findById('model', idValue, {fields: 'foo'});
  3560. expect(result).to.be.eql({[DEF_PK]: idValue, foo: input.foo});
  3561. });
  3562. it('allows to specify a full form of a fields clause to filter a return value', async function () {
  3563. const schema = new Schema();
  3564. schema.defineDatasource({
  3565. name: 'memory',
  3566. adapter: 'memory',
  3567. });
  3568. schema.defineModel({
  3569. name: 'model',
  3570. datasource: 'memory',
  3571. properties: {
  3572. foo: DataType.STRING,
  3573. bar: DataType.NUMBER,
  3574. baz: DataType.BOOLEAN,
  3575. },
  3576. });
  3577. const adapter = new MemoryAdapter(schema.container, {});
  3578. const table = adapter._getTableOrCreate('model');
  3579. const input = {foo: 'string', bar: 10, baz: true};
  3580. const idValue = 1;
  3581. table.set(idValue, {[DEF_PK]: idValue, ...input});
  3582. const filter = {fields: ['foo', 'bar']};
  3583. const result = await adapter.findById('model', idValue, filter);
  3584. expect(result).to.be.eql({
  3585. [DEF_PK]: idValue,
  3586. foo: input.foo,
  3587. bar: input.bar,
  3588. });
  3589. });
  3590. it('a fields clause uses property names instead of column names', async function () {
  3591. const schema = new Schema();
  3592. schema.defineDatasource({
  3593. name: 'memory',
  3594. adapter: 'memory',
  3595. });
  3596. schema.defineModel({
  3597. name: 'model',
  3598. datasource: 'memory',
  3599. properties: {
  3600. foo: {
  3601. type: DataType.STRING,
  3602. columnName: 'fooCol',
  3603. },
  3604. bar: {
  3605. type: DataType.NUMBER,
  3606. columnName: 'barCol',
  3607. },
  3608. baz: {
  3609. type: DataType.BOOLEAN,
  3610. columnName: 'bazCol',
  3611. },
  3612. },
  3613. });
  3614. const adapter = new MemoryAdapter(schema.container, {});
  3615. const table = adapter._getTableOrCreate('model');
  3616. const tableInput = {fooCol: 'string', barCol: 10, bazCol: true};
  3617. const idValue = 1;
  3618. table.set(idValue, {
  3619. [DEF_PK]: idValue,
  3620. ...tableInput,
  3621. });
  3622. const filter = {fields: ['foo', 'bar']};
  3623. const result = await adapter.findById('model', idValue, filter);
  3624. expect(result).to.be.eql({
  3625. [DEF_PK]: idValue,
  3626. foo: tableInput.fooCol,
  3627. bar: tableInput.barCol,
  3628. });
  3629. });
  3630. });
  3631. describe('delete', function () {
  3632. it('removes all table items and returns their number', async function () {
  3633. const schema = new Schema();
  3634. schema.defineDatasource({
  3635. name: 'memory',
  3636. adapter: 'memory',
  3637. });
  3638. schema.defineModel({
  3639. name: 'model',
  3640. datasource: 'memory',
  3641. });
  3642. const adapter = new MemoryAdapter(schema.container, {});
  3643. const table = adapter._getTableOrCreate('model');
  3644. table.set(1, {[DEF_PK]: 1});
  3645. table.set(2, {[DEF_PK]: 2});
  3646. table.set(3, {[DEF_PK]: 3});
  3647. const result = await adapter.delete('model');
  3648. expect(result).to.be.eq(3);
  3649. const tableSize = Array.from(table.values()).length;
  3650. expect(tableSize).to.be.eq(0);
  3651. });
  3652. it('returns zero if nothing to remove', async function () {
  3653. const schema = new Schema();
  3654. schema.defineDatasource({
  3655. name: 'memory',
  3656. adapter: 'memory',
  3657. });
  3658. schema.defineModel({
  3659. name: 'model',
  3660. datasource: 'memory',
  3661. });
  3662. const adapter = new MemoryAdapter(schema.container, {});
  3663. const result = await adapter.delete('model');
  3664. expect(result).to.be.eq(0);
  3665. });
  3666. it('uses a given where clause to remove specific items', async function () {
  3667. const schema = new Schema();
  3668. schema.defineDatasource({
  3669. name: 'memory',
  3670. adapter: 'memory',
  3671. });
  3672. schema.defineModel({
  3673. name: 'model',
  3674. datasource: 'memory',
  3675. properties: {
  3676. foo: DataType.NUMBER,
  3677. },
  3678. });
  3679. const adapter = new MemoryAdapter(schema.container, {});
  3680. await adapter.create('model', {foo: 20});
  3681. await adapter.create('model', {foo: 10});
  3682. await adapter.create('model', {foo: 15});
  3683. const result = await adapter.delete('model', {foo: {gte: 15}});
  3684. expect(result).to.be.eq(2);
  3685. const table = adapter._getTableOrCreate('model');
  3686. const tableSize = Array.from(table.values()).length;
  3687. expect(tableSize).to.be.eq(1);
  3688. expect(table.get(2)).to.be.eql({[DEF_PK]: 2, foo: 10});
  3689. });
  3690. it('the "where" clause uses property names instead of column names', async function () {
  3691. const schema = new Schema();
  3692. schema.defineDatasource({
  3693. name: 'memory',
  3694. adapter: 'memory',
  3695. });
  3696. schema.defineModel({
  3697. name: 'model',
  3698. datasource: 'memory',
  3699. properties: {
  3700. foo: {
  3701. type: DataType.NUMBER,
  3702. columnName: 'fooCol',
  3703. },
  3704. },
  3705. });
  3706. const adapter = new MemoryAdapter(schema.container, {});
  3707. const table = adapter._getTableOrCreate('model');
  3708. table.set(1, {[DEF_PK]: 1, fooCol: 20});
  3709. table.set(2, {[DEF_PK]: 2, fooCol: 10});
  3710. table.set(3, {[DEF_PK]: 3, fooCol: 15});
  3711. const result = await adapter.delete('model', {foo: {gte: 15}});
  3712. expect(result).to.be.eq(2);
  3713. expect(table.size).to.be.eq(1);
  3714. expect(table.get(2)).to.be.eql({[DEF_PK]: 2, fooCol: 10});
  3715. });
  3716. it('the "where" clause uses a persisted data instead of default values', async function () {
  3717. const schema = new Schema();
  3718. schema.defineDatasource({
  3719. name: 'memory',
  3720. adapter: 'memory',
  3721. });
  3722. schema.defineModel({
  3723. name: 'model',
  3724. datasource: 'memory',
  3725. properties: {
  3726. foo: {
  3727. type: DataType.STRING,
  3728. default: 'hello',
  3729. },
  3730. },
  3731. });
  3732. const adapter = new MemoryAdapter(schema.container, {});
  3733. const table = adapter._getTableOrCreate('model');
  3734. const input1 = {[DEF_PK]: 1, foo: undefined};
  3735. const input2 = {[DEF_PK]: 2, foo: undefined};
  3736. const input3 = {[DEF_PK]: 3, foo: 10};
  3737. const input4 = {[DEF_PK]: 4, foo: true};
  3738. table.set(1, input1);
  3739. table.set(2, input2);
  3740. table.set(3, input3);
  3741. table.set(4, input4);
  3742. const result1 = await adapter.delete('model', {foo: undefined});
  3743. expect(result1).to.be.eq(2);
  3744. expect(table.size).to.be.eq(2);
  3745. expect(table.get(3)).to.be.eql(input3);
  3746. expect(table.get(4)).to.be.eql(input4);
  3747. const result2 = await adapter.delete('model', {foo: 'hello'});
  3748. expect(result2).to.be.eq(0);
  3749. expect(table.size).to.be.eq(2);
  3750. expect(table.get(3)).to.be.eql(input3);
  3751. expect(table.get(4)).to.be.eql(input4);
  3752. const result3 = await adapter.delete('model', {foo: 10});
  3753. expect(result3).to.be.eq(1);
  3754. expect(table.size).to.be.eq(1);
  3755. expect(table.get(4)).to.be.eql(input4);
  3756. });
  3757. });
  3758. describe('deleteById', function () {
  3759. it('returns false if a given identifier is not exist', async function () {
  3760. const schema = new Schema();
  3761. schema.defineDatasource({
  3762. name: 'memory',
  3763. adapter: 'memory',
  3764. });
  3765. schema.defineModel({
  3766. name: 'model',
  3767. datasource: 'memory',
  3768. });
  3769. const adapter = new MemoryAdapter(schema.container, {});
  3770. const result = await adapter.deleteById('model', 1);
  3771. expect(result).to.be.false;
  3772. });
  3773. it('returns true if an item has removed by a given identifier', async function () {
  3774. const schema = new Schema();
  3775. schema.defineDatasource({
  3776. name: 'memory',
  3777. adapter: 'memory',
  3778. });
  3779. schema.defineModel({
  3780. name: 'model',
  3781. datasource: 'memory',
  3782. });
  3783. const adapter = new MemoryAdapter(schema.container, {});
  3784. const created = await adapter.create('model', {});
  3785. const idValue = created[DEF_PK];
  3786. const result = await adapter.deleteById('model', idValue);
  3787. expect(result).to.be.true;
  3788. });
  3789. it('uses a specified column name for a primary key', async function () {
  3790. const schema = new Schema();
  3791. schema.defineDatasource({
  3792. name: 'memory',
  3793. adapter: 'memory',
  3794. });
  3795. schema.defineModel({
  3796. name: 'model',
  3797. datasource: 'memory',
  3798. properties: {
  3799. foo: {
  3800. type: DataType.NUMBER,
  3801. primaryKey: true,
  3802. columnName: 'qux',
  3803. },
  3804. bar: DataType.NUMBER,
  3805. },
  3806. });
  3807. const adapter = new MemoryAdapter(schema.container, {});
  3808. const table = adapter._getTableOrCreate('model');
  3809. const idValue = 1;
  3810. table.set(idValue, {qux: idValue, bar: 10});
  3811. const result = await adapter.deleteById('model', idValue);
  3812. expect(result).to.be.true;
  3813. const tableSize = Array.from(table.values()).length;
  3814. expect(tableSize).to.be.eq(0);
  3815. });
  3816. });
  3817. describe('exists', function () {
  3818. it('returns false if a given identifier is not exist', async function () {
  3819. const schema = new Schema();
  3820. schema.defineDatasource({
  3821. name: 'memory',
  3822. adapter: 'memory',
  3823. });
  3824. schema.defineModel({
  3825. name: 'model',
  3826. datasource: 'memory',
  3827. });
  3828. const adapter = new MemoryAdapter(schema.container, {});
  3829. const result = await adapter.exists('model', 1);
  3830. expect(result).to.be.false;
  3831. });
  3832. it('returns true if a given identifier is exist', async function () {
  3833. const schema = new Schema();
  3834. schema.defineDatasource({
  3835. name: 'memory',
  3836. adapter: 'memory',
  3837. });
  3838. schema.defineModel({
  3839. name: 'model',
  3840. datasource: 'memory',
  3841. });
  3842. const adapter = new MemoryAdapter(schema.container, {});
  3843. await adapter.create('model', {});
  3844. const result = await adapter.exists('model', 1);
  3845. expect(result).to.be.true;
  3846. });
  3847. it('uses a specified column name for a primary key', async function () {
  3848. const schema = new Schema();
  3849. schema.defineDatasource({
  3850. name: 'memory',
  3851. adapter: 'memory',
  3852. });
  3853. schema.defineModel({
  3854. name: 'model',
  3855. datasource: 'memory',
  3856. properties: {
  3857. foo: {
  3858. type: DataType.NUMBER,
  3859. primaryKey: true,
  3860. columnName: 'qux',
  3861. },
  3862. },
  3863. });
  3864. const adapter = new MemoryAdapter(schema.container, {});
  3865. const table = adapter._getTableOrCreate('model');
  3866. const idValue = 1;
  3867. table.set(idValue, {qux: idValue});
  3868. const result = await adapter.exists('model', idValue);
  3869. expect(result).to.be.true;
  3870. });
  3871. });
  3872. describe('count', function () {
  3873. it('returns zero if nothing to count', async function () {
  3874. const schema = new Schema();
  3875. schema.defineDatasource({
  3876. name: 'memory',
  3877. adapter: 'memory',
  3878. });
  3879. schema.defineModel({
  3880. name: 'model',
  3881. datasource: 'memory',
  3882. });
  3883. const adapter = new MemoryAdapter(schema.container, {});
  3884. const result = await adapter.count('model');
  3885. expect(result).to.be.eq(0);
  3886. });
  3887. it('returns zero if a given where clause does not met', async function () {
  3888. const schema = new Schema();
  3889. schema.defineDatasource({
  3890. name: 'memory',
  3891. adapter: 'memory',
  3892. });
  3893. schema.defineModel({
  3894. name: 'model',
  3895. datasource: 'memory',
  3896. properties: {
  3897. foo: DataType.NUMBER,
  3898. },
  3899. });
  3900. const adapter = new MemoryAdapter(schema.container, {});
  3901. await adapter.create('model', {foo: 20});
  3902. await adapter.create('model', {foo: 10});
  3903. await adapter.create('model', {foo: 15});
  3904. const result = await adapter.count('model', {foo: {gte: 150}});
  3905. expect(result).to.be.eq(0);
  3906. const table = adapter._getTableOrCreate('model');
  3907. const tableSize = Array.from(table.values()).length;
  3908. expect(tableSize).to.be.eq(3);
  3909. expect(table.get(1)).to.be.eql({[DEF_PK]: 1, foo: 20});
  3910. expect(table.get(2)).to.be.eql({[DEF_PK]: 2, foo: 10});
  3911. expect(table.get(3)).to.be.eql({[DEF_PK]: 3, foo: 15});
  3912. });
  3913. it('returns a number of table items', async function () {
  3914. const schema = new Schema();
  3915. schema.defineDatasource({
  3916. name: 'memory',
  3917. adapter: 'memory',
  3918. });
  3919. schema.defineModel({
  3920. name: 'model',
  3921. datasource: 'memory',
  3922. });
  3923. const adapter = new MemoryAdapter(schema.container, {});
  3924. await adapter.create('model', {});
  3925. await adapter.create('model', {});
  3926. await adapter.create('model', {});
  3927. const result = await adapter.count('model');
  3928. expect(result).to.be.eq(3);
  3929. });
  3930. it('uses a given where clause to count specific items', async function () {
  3931. const schema = new Schema();
  3932. schema.defineDatasource({
  3933. name: 'memory',
  3934. adapter: 'memory',
  3935. });
  3936. schema.defineModel({
  3937. name: 'model',
  3938. datasource: 'memory',
  3939. properties: {
  3940. foo: DataType.NUMBER,
  3941. },
  3942. });
  3943. const adapter = new MemoryAdapter(schema.container, {});
  3944. await adapter.create('model', {foo: 20});
  3945. await adapter.create('model', {foo: 10});
  3946. await adapter.create('model', {foo: 15});
  3947. const result = await adapter.count('model', {foo: {gte: 15}});
  3948. expect(result).to.be.eq(2);
  3949. const table = adapter._getTableOrCreate('model');
  3950. const tableSize = Array.from(table.values()).length;
  3951. expect(tableSize).to.be.eq(3);
  3952. expect(table.get(1)).to.be.eql({[DEF_PK]: 1, foo: 20});
  3953. expect(table.get(2)).to.be.eql({[DEF_PK]: 2, foo: 10});
  3954. expect(table.get(3)).to.be.eql({[DEF_PK]: 3, foo: 15});
  3955. });
  3956. it('the "where" clause uses property names instead of column names', async function () {
  3957. const schema = new Schema();
  3958. schema.defineDatasource({
  3959. name: 'memory',
  3960. adapter: 'memory',
  3961. });
  3962. schema.defineModel({
  3963. name: 'model',
  3964. datasource: 'memory',
  3965. properties: {
  3966. foo: {
  3967. type: DataType.NUMBER,
  3968. columnName: 'fooCol',
  3969. },
  3970. },
  3971. });
  3972. const adapter = new MemoryAdapter(schema.container, {});
  3973. const table = adapter._getTableOrCreate('model');
  3974. table.set(1, {[DEF_PK]: 1, fooCol: 20});
  3975. table.set(2, {[DEF_PK]: 2, fooCol: 10});
  3976. table.set(3, {[DEF_PK]: 3, fooCol: 15});
  3977. const result = await adapter.count('model', {foo: {gte: 15}});
  3978. expect(result).to.be.eq(2);
  3979. const tableSize = Array.from(table.values()).length;
  3980. expect(tableSize).to.be.eq(3);
  3981. expect(table.get(1)).to.be.eql({[DEF_PK]: 1, fooCol: 20});
  3982. expect(table.get(2)).to.be.eql({[DEF_PK]: 2, fooCol: 10});
  3983. expect(table.get(3)).to.be.eql({[DEF_PK]: 3, fooCol: 15});
  3984. });
  3985. it('the "where" clause uses a persisted data instead of default values', async function () {
  3986. const schema = new Schema();
  3987. schema.defineDatasource({
  3988. name: 'memory',
  3989. adapter: 'memory',
  3990. });
  3991. schema.defineModel({
  3992. name: 'model',
  3993. datasource: 'memory',
  3994. properties: {
  3995. foo: {
  3996. type: DataType.STRING,
  3997. default: 'hello',
  3998. },
  3999. },
  4000. });
  4001. const adapter = new MemoryAdapter(schema.container, {});
  4002. const table = adapter._getTableOrCreate('model');
  4003. table.set(1, {[DEF_PK]: 1, foo: undefined});
  4004. table.set(2, {[DEF_PK]: 2, foo: undefined});
  4005. table.set(3, {[DEF_PK]: 3, foo: 10});
  4006. const result1 = await adapter.count('model', {foo: undefined});
  4007. const result2 = await adapter.count('model', {foo: 10});
  4008. const result3 = await adapter.count('model', {foo: 'hello'});
  4009. expect(result1).to.be.eq(2);
  4010. expect(result2).to.be.eq(1);
  4011. expect(result3).to.be.eq(0);
  4012. });
  4013. });
  4014. });